summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeutisticExtensibleConstructorTestCase.java
blob: 07ab6f28c8a70cea682cff7e46e920b27df89e05 (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
/*
 * 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.implementation.java.introspect.impl;

import static org.junit.Assert.assertEquals;

import java.lang.reflect.Constructor;

import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory;
import org.apache.tuscany.sca.assembly.Property;
import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory;
import org.apache.tuscany.sca.implementation.java.IntrospectionException;
import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl;
import org.apache.tuscany.sca.implementation.java.JavaElementImpl;
import org.apache.tuscany.sca.implementation.java.JavaImplementation;
import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory;
import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory;
import org.junit.Test;

/**
 * Verifies constructors that have extensible annotation types, i.e. that have
 * parameters marked by annotations which are themselves processed by some other
 * implementation processor
 * 
 * @version $Rev$ $Date$
 */
public class HeutisticExtensibleConstructorTestCase extends AbstractProcessorTest {

    private org.apache.tuscany.sca.implementation.java.introspect.impl.HeuristicPojoProcessor processor;
    private JavaImplementationFactory javaImplementationFactory;

    public HeutisticExtensibleConstructorTestCase() {
        ExtensionPointRegistry registry = new DefaultExtensionPointRegistry();
        processor = new HeuristicPojoProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry));
        javaImplementationFactory = new DefaultJavaImplementationFactory();
    }

    private <T> void visitEnd(Class<T> clazz, JavaImplementation type) throws IntrospectionException {
        for (Constructor<?> constructor : clazz.getConstructors()) {
            visitConstructor(constructor, type);
        }
        processor.visitEnd(clazz, type);
    }

    /**
     * Verifies heuristic processing can be called prior to an extension
     * annotation processors being called.
     */
    @Test
    public void testBarAnnotationProcessedFirst() throws Exception {
        JavaImplementation type = javaImplementationFactory.createJavaImplementation();
        Constructor<Foo> ctor = Foo.class.getConstructor(String.class, String.class);
        JavaConstructorImpl<Foo> definition = new JavaConstructorImpl<Foo>(ctor);
        type.setConstructor(definition);
        Property property = factory.createProperty();
        property.setName("myBar");
        definition.getParameters()[0].setName("myBar");
        type.getProperties().add(property);
        visitEnd(Foo.class, type);
        assertEquals(2, type.getProperties().size());
    }

    /**
     * Verifies heuristic processing can be called before an extension
     * annotation processors is called. <p/> For example, given:
     * 
     * <pre>
     *  Foo(@Bar String prop, @org.oasisopen.sca.annotation.Property(name = &quot;foo&quot;) String prop2)
     * </pre>
     * 
     * <p/> Heuristic evaluation of
     * @Property can occur prior to another implementation processor evaluating
     * @Bar
     * @throws Exception
     */
    @Test
    public void testBarAnnotationProcessedLast() throws Exception {
        JavaImplementation type = javaImplementationFactory.createJavaImplementation();
        visitEnd(Foo.class, type);

        // now simulate process the bar impl
        JavaConstructorImpl<?> definition = type.getConstructor();
        definition.getParameters()[0].setName("myBar");
        Property property = factory.createProperty();
        property.setName("myBar");
        type.getProperties().add(property);

        assertEquals(2, type.getProperties().size());
        assertEquals("foo", definition.getParameters()[1].getName());
    }

    /**
     * Verifies heuristic processing can be called before an extension
     * annotation processors is called with the extension parameter in a middle
     * position. Specifically, verifies that the heuristic processor updates
     * injection names and preserves their ordering.
     */
    @Test
    public void testBarAnnotationProcessedFirstInMiddle() throws Exception {
        JavaImplementation type = javaImplementationFactory.createJavaImplementation();
        Constructor<Foo2> ctor = Foo2.class.getConstructor(String.class, String.class, String.class);
        JavaConstructorImpl<Foo2> definition = new JavaConstructorImpl<Foo2>(ctor);
        type.setConstructor(definition);
        // insert placeholder for first param, which would be done by a
        // processor
        definition.getParameters()[0].setName("");
        Property property = factory.createProperty();
        // Hack to add a property member
        JavaElementImpl element = new JavaElementImpl("myBar", String.class, null);
        type.getPropertyMembers().put("myBar", element);
        property.setName("myBar");
        definition.getParameters()[1].setName("myBar");
        type.getProperties().add(property);
        visitEnd(Foo2.class, type);
        assertEquals("baz", definition.getParameters()[0].getName());
        assertEquals(2, type.getProperties().size());
        assertEquals(1, type.getReferences().size());
    }

    public @interface Bar {

    }

    public static class Foo {
        public Foo(@Bar
        String prop, @org.oasisopen.sca.annotation.Property(name = "foo")
        String prop2) {
        }
    }

    public static class Foo2 {
        public Foo2(@org.oasisopen.sca.annotation.Reference(name = "baz")
        String prop1, @Bar
        String prop2, @org.oasisopen.sca.annotation.Property(name = "foo")
        String prop3) {
        }
    }

}