summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/1.5.1/vtest/java-api/annotations/property/src/test/java/org/apache/tuscany/sca/vtest/javaapi/annotations/property/PropertyAnnotationTestCase.java
blob: d5fbc568f7009eb23d21a52df807b60ae68bdd76 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.tuscany.sca.vtest.javaapi.annotations.property;

import static org.junit.Assert.fail;
import junit.framework.Assert;

import org.apache.tuscany.sca.vtest.utilities.ServiceFinder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

/**
 * This test class tests the Property annotation described in section 1.2.3
 * including 1.8.5 and 1.8.13
 */
public class PropertyAnnotationTestCase {

    protected static String compositeName = "property.composite";
    protected static AService aService = null;
    protected static CService cService1 = null;
    protected static CService cService2 = null;
    protected static CService cService3 = null;
    protected static CService cService4 = null;
    protected static CService cService5 = null;
    protected static CService cService6 = null;
    protected static AnotherAService anotherAService = null;

    @BeforeClass
    public static void init() throws Exception {
        try {
            System.out.println("Setting up");
            ServiceFinder.init(compositeName);
            aService = ServiceFinder.getService(AService.class, "AComponent");
            cService1 = ServiceFinder.getService(CService.class, "CComponent1");
            cService2 = ServiceFinder.getService(CService.class, "CComponent2");
            cService3 = ServiceFinder.getService(CService.class, "CComponent3");
            cService4 = ServiceFinder.getService(CService.class, "CComponent4");
            cService5 = ServiceFinder.getService(CService.class, "CComponent5");
            cService6 = ServiceFinder.getService(CService.class, "CComponent6");
            anotherAService = ServiceFinder.getService(AnotherAService.class, "AnotherAComponent");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @AfterClass
    public static void destroy() throws Exception {

        System.out.println("Cleaning up");
        ServiceFinder.cleanup();

    }

    /**
     * Lines 1343 to 1348:<br>
     * The "@Property" annotation type is used to annotate a Java class field or
     * a setter method that is used to inject an SCA property value. The type of
     * the property injected, which can be a simple Java type or a complex Java
     * type, is defined by the type of the Java class field or the type of the
     * setter method input argument.<br>
     * The "@Property" annotation may be used on protected or public fields and
     * on setter methods or on a constructor method.<br>
     * <p>
     * p1 - simple Java type injected via field<br>
     * p2 - simple Java type injected via field<br>
     * p3 - simple Java type injected via setter<br>
     * p4 - simple Java type injected via setter and required=true<br>
     * p5 - simple Java type injected via constructor parameter<br>
     * p6 - simple Java type injected via constructor parameter<br>
     * p7 - complex Java type injected via field and required=true<br>
     * p8 - complex Java type injected via field<br>
     * p9 - complex Java type injected via setter<br>
     * p10 - complex Java type injected via setter<br>
     * p11 - complex Java type injected via constructor parameter<br>
     * p12 - complex Java type injected via constructor parameter<br>
     */
    @Test
    public void atProperty1() throws Exception {
        Assert.assertEquals("p1", aService.getP1());
        Assert.assertEquals("p2", aService.getP2());
        Assert.assertEquals("p3", aService.getP3());
        Assert.assertEquals("p4", aService.getP4());
        Assert.assertEquals("p5", aService.getP5());
        Assert.assertEquals("p6", aService.getP6());
        Assert.assertEquals("p7.aString", aService.getP7AString());
        Assert.assertEquals(7, aService.getP7BInt());
        Assert.assertEquals("p8.aString", aService.getP8AString());
        Assert.assertEquals(8, aService.getP8BInt());
        Assert.assertEquals("p9.aString", aService.getP9AString());
        Assert.assertEquals(9, aService.getP9BInt());
        Assert.assertEquals("p10.aString", aService.getP10AString());
        Assert.assertEquals(10, aService.getP10BInt());
        Assert.assertEquals("p11.aString", aService.getP11AString());
        Assert.assertEquals(11, aService.getP11BInt());
        Assert.assertEquals("p12.aString", aService.getP12AString());
        Assert.assertEquals(12, aService.getP12BInt());
    }

    /**
     * Lines 1349 to 1352:<br>
     * Properties may also be injected via public setter methods even when the
     * "@Property" annotation is not present. However, the
     * 
     * @Property annotation must be used in order to inject a property onto a
     *           non-public field. In the case where there is no "@Property"
     *           annotation, the name of the property is the same as the name of
     *           the field or setter.<br>
     *           <p>
     *           p13 is an un-annotated public field which should be injected
     *           via field<br>
     */
    @Test
    public void atProperty2() throws Exception {
        Assert.assertEquals("p13", anotherAService.getP13());
    }

    /**
     * Line 1353:<br>
     * Where there is both a setter method and a field for a property, the
     * setter method is used.<br>
     * <p>
     * p14 is an un-annotated public field, it should be injected via public
     * setter<br>
     */
    @Test
    public void atProperty3() throws Exception {
        Assert.assertEquals("p14", anotherAService.getP14());
        Assert.assertTrue(anotherAService.getP14SetterIsCalled());
    }

    /**
     * Lines 1355 to 1357:<br>
     * The "@Property" annotation has the following attributes:<br>
     * <li>name (optional) � the name of the property, defaults to the name of
     * the field of the Java class</li>
     * <li>required (optional) � specifies whether injection is required,
     * defaults to false</li>
     * <p>
     * p15 - injected via field with different name "pFifteen"<br>
     * p16 - injected via setter with different name "pSixteen"<br>
     * p17 - injected via field but not defined in composite<br>
     * p18 - injected via setter but not defined in composite<br>
     * 
     * @TODO - Need to test required=true but not defined in composite (The
     *       specification does not describe the proper behaviour in this
     *       situation.)
     */
    @Test
    public void atProperty4() throws Exception {
        Assert.assertEquals("p15", aService.getP15());
        Assert.assertEquals("p16", aService.getP16());
        Assert.assertNull(aService.getP17());
        Assert.assertNull(aService.getP18());
    }

    /**
     * Lines 1369 to 1370:<br>
     * If the property is defined as an array or as a java.util.Collection, then
     * the implied component type has a property with a many attribute set to
     * true.<br>
     * <p>
     * p19 - a List and injected via field with no element<br>
     * p20 - a List and injected via setter<br>
     * p21 - an array and injected via field<br>
     */
    @Test
    public void atProperty5() throws Exception {
        Assert.assertEquals(0, aService.getP19Size());
        Assert.assertEquals(1, aService.getP20Size());
        Assert.assertEquals("p20", aService.getP20(0));
        Assert.assertEquals(3, aService.getP21Size());
        Assert.assertEquals(2, aService.getP21(0));
        Assert.assertEquals(1, aService.getP21(1));
        Assert.assertEquals(21, aService.getP21(2));
    }

    /**
     * Lines 1141 to 1162:<br>
     * 1.8.5. "@Constructor"<br>
     * ...<br>
     * The "@Constructor" annotation is used to mark a particular constructor to
     * use when instantiating a Java component implementation.<br>
     * The "@Constructor" annotation has the following attribute:<br>
     * <li>value (optional) � identifies the property/reference names that
     * correspond to each of the constructor arguments. The position in the
     * array determines which of the arguments are being named.</li>
     * <p>
     * cService1 - "@Constructor" without value and constructor arguments<br>
     * cService2 - "@Constructor" without value but with constructor arguments<br>
     * cService3 - "@Constructor" with values and constructor arguments<br>
     * cService4 - "@Constructor" with values and constructor arguments where
     * value, property and parameter names are same<br>
     * cService5 - "@Constructor" with switched values and constructor arguments<br>
     * cService6 - "@Constructor" with wrong values<br>
     */
    @Test
    public void atProperty6() throws Exception {
        Assert.assertNull(cService1.getB1Name());
        Assert.assertNull(cService1.getP2());
        Assert.assertEquals(0, cService1.getP3());
        Assert.assertNull(cService1.getP4());
        Assert.assertEquals("NoArgument", cService1.getConstructor());

        Assert.assertEquals("BService", cService2.getB1Name());
        Assert.assertEquals("p2", cService2.getP2());
        Assert.assertEquals(3, cService2.getP3());
        Assert.assertEquals("p4", cService2.getP4());
        Assert.assertEquals("AllArguments", cService2.getConstructor());

        Assert.assertEquals("BService", cService3.getB1Name());
        Assert.assertEquals("p2", cService3.getP2());
        Assert.assertEquals(3, cService3.getP3());
        Assert.assertEquals("p4", cService3.getP4());
        Assert.assertEquals("AllArguments", cService3.getConstructor());

        Assert.assertEquals("BService", cService4.getB1Name());
        Assert.assertEquals("p2", cService4.getP2());
        Assert.assertEquals(3, cService4.getP3());
        Assert.assertEquals("p4", cService4.getP4());
        Assert.assertEquals("AllArguments", cService4.getConstructor());

        Assert.assertEquals("BService", cService5.getB1Name());
        Assert.assertEquals("p4", cService5.getP2());
        Assert.assertEquals(3, cService5.getP3());
        Assert.assertEquals("p2", cService5.getP4());
        Assert.assertEquals("SwitchedValues", cService5.getConstructor());

        try {
            System.out.println(cService6.getB1Name());
            fail("Should have failed to call this service");
        } catch (Throwable t) {
        }

    }

    /**
     * Lines 1349 to 1352:<br>
     * 1.8.13. "@Property"<br>
     * ...<br>
     * Properties may also be injected via public setter methods even when the
     * "@Property" annotation is not present. However, the "@Property"
     * annotation must be used in order to inject a property onto a non-public
     * field. In the case where there is no "@Property" annotation, the name of
     * the property is the same as the name of the field or setter.
     * <p>
     * p22 is unannotated protected field which should not be injected p23 is
     * un-annotated protected which should not be injected via protected setter<br>
     */
    @Test
    @Ignore("JIRA-2289 - p23 failed")
    public void atProperty7() throws Exception {
        Assert.assertNull(anotherAService.getP22());
        Assert.assertNull(anotherAService.getP23());
        Assert.assertFalse(anotherAService.getP23SetterIsCalled());
    }

    /**
     * Lines 1349 to 1352:<br>
     * 1.8.13. "@Property"<br>
     * ...<br>
     * Properties may also be injected via public setter methods even when the
     * "@Property" annotation is not present. However, the "@Property"
     * annotation must be used in order to inject a property onto a non-public
     * field. In the case where there is no "@Property" annotation, the name of
     * the property is the same as the name of the field or setter.
     * <p>
     * p24 is un-annotated protected field which should be injected via public
     * setter<br>
     * p25 is un-annotated private field which should be injected via public
     * setter<br>
     */
    @Test
    public void atProperty8() throws Exception {
        Assert.assertEquals("p24", anotherAService.getP24());
        Assert.assertTrue(anotherAService.getP24SetterIsCalled());
        Assert.assertEquals("p25", anotherAService.getP25());
        Assert.assertTrue(anotherAService.getP25SetterIsCalled());
    }

}