summaryrefslogtreecommitdiffstats
path: root/tck/org/atinject/tck/auto/accessories/SpareTire.java
blob: 998574f09b3e68f232ce71779161677ff9d5b197 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) 2009 The JSR-330 Expert Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.atinject.tck.auto.accessories;

import org.atinject.tck.Tester;
import org.atinject.tck.auto.FuelTank;
import org.atinject.tck.auto.Tire;

import javax.inject.Inject;

public class SpareTire extends Tire {

    FuelTank constructorInjection = NEVER_INJECTED;
    @Inject FuelTank fieldInjection = NEVER_INJECTED;
    FuelTank methodInjection = NEVER_INJECTED;
    @Inject static FuelTank staticFieldInjection = NEVER_INJECTED;
    static FuelTank staticMethodInjection = NEVER_INJECTED;

    @Inject public SpareTire(FuelTank forSupertype, FuelTank forSubtype) {
        super(forSupertype);
        this.constructorInjection = forSubtype;
    }

    @Inject void subtypeMethodInjection(FuelTank methodInjection) {
        if (!hasSpareTireBeenFieldInjected()) {
            moreProblems.add("Methods injected before fields");
        }
        this.methodInjection = methodInjection;
    }

    @Inject static void subtypeStaticMethodInjection(FuelTank methodInjection) {
        if (!hasBeenStaticFieldInjected()) {
            moreProblems.add("Static methods injected before static fields");
        }
        staticMethodInjection = methodInjection;
    }

    @Inject private void injectPrivateMethod() {
        if (subPrivateMethodInjected) {
            moreProblems.add("Overridden private method injected twice");
        }
        subPrivateMethodInjected = true;
    }

    @Inject void injectPackagePrivateMethod() {
        if (subPackagePrivateMethodInjected) {
            moreProblems.add("Overridden package private method injected twice");
        }
        subPackagePrivateMethodInjected = true;
    }

    @Inject protected void injectProtectedMethod() {
        if (subProtectedMethodInjected) {
            moreProblems.add("Overridden protected method injected twice");
        }
        subProtectedMethodInjected = true;
    }

    @Inject public void injectPublicMethod() {
        if (subPublicMethodInjected) {
            moreProblems.add("Overridden public method injected twice");
        }
        subPublicMethodInjected = true;
    }

    private void injectPrivateMethodForOverride() {
        superPrivateMethodForOverrideInjected = true;
    }

    void injectPackagePrivateMethodForOverride() {
        superPackagePrivateMethodForOverrideInjected = true;
    }

    protected void injectProtectedMethodForOverride() {
        protectedMethodForOverrideInjected = true;
    }

    public void injectPublicMethodForOverride() {
        publicMethodForOverrideInjected = true;
    }

    protected boolean hasSpareTireBeenFieldInjected() {
        return fieldInjection != NEVER_INJECTED;
    }

    protected boolean hasSpareTireBeenMethodInjected() {
        return methodInjection != NEVER_INJECTED;
    }

    public static boolean hasBeenStaticFieldInjected() {
        return staticFieldInjection != NEVER_INJECTED;
    }

    public static boolean hasBeenStaticMethodInjected() {
        return staticMethodInjection != NEVER_INJECTED;
    }

    /**
     * This class is used to check inheritance. By existing in a separate package
     * from Tire, it can make sure the injector does the right thing with overrides
     * of package-private methods.
     */
    public void check(Tester tester) {
        tester.addProblems(moreProblems);

        tester.test(hasSpareTireBeenFieldInjected(), "Subtype fields not injected");
        tester.test(hasSpareTireBeenMethodInjected(), "Subtype methods not injected");
        tester.test(hasTireBeenFieldInjected(), "Supertype fields not injected");
        tester.test(hasTireBeenMethodInjected(), "Supertype methods not injected");
        tester.test(SpareTire.hasBeenStaticFieldInjected(), "Subtype static fields not injected");
        tester.test(SpareTire.hasBeenStaticMethodInjected(), "Subtype static methods not injected");
        tester.test(Tire.hasBeenStaticFieldInjected(), "Supertype static fields not injected");
        tester.test(Tire.hasBeenStaticMethodInjected(), "Supertype static methods not injected");

        tester.test(superPrivateMethodInjected, "Supertype private method not injected");
        tester.test(superPackagePrivateMethodInjected, "Supertype private method not injected");
        tester.test(!superProtectedMethodInjected, "Overridden protected method injected!");
        tester.test(!superPublicMethodInjected, "Overridden public method injected!");
        tester.test(subPrivateMethodInjected, "Subtype private method not injected");
        tester.test(subPackagePrivateMethodInjected, "Subtype private method not injected");
        tester.test(subProtectedMethodInjected, "Subtype protected method not injected");
        tester.test(subPublicMethodInjected, "Subtype public method not injected");

        tester.test(subPrivateMethodForOverrideInjected,
                "Private method not injected when a subtype has a similar method that lacks @Inject");
        tester.test(subPackagePrivateMethodForOverrideInjected,
                "Package private method not injected when a subtype has a similar method that lacks @Inject");
        tester.test(!superPrivateMethodForOverrideInjected,
                "Private method injected when a supertype has a similar method annotated @Inject");
        tester.test(!superPackagePrivateMethodForOverrideInjected,
                "Package private method injected when a supertype has a similar method annotated @Inject");
        tester.test(!protectedMethodForOverrideInjected,
                "Protected method injected, even though its override lacks @Inject");
        tester.test(!publicMethodForOverrideInjected,
                "Public method injected, even though its override lacks @Inject");
    }
}