summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060518/java/sca/core/src/test/java/org/apache/tuscany/core/config/impl/CoreAnnotationsProcessingTestCase.java
blob: aeb614e9c6a55da45bcb86c3176c4e2b3da5da04 (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
/**
 *
 * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 * 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.apache.tuscany.core.config.impl;

import java.util.List;

import junit.framework.TestCase;
import org.apache.tuscany.core.config.ComponentTypeIntrospector;
import org.apache.tuscany.core.config.JavaIntrospectionHelper;
import org.apache.tuscany.core.config.processor.ProcessorUtils;
import org.apache.tuscany.core.system.assembly.impl.SystemAssemblyFactoryImpl;
import org.apache.tuscany.model.assembly.AssemblyFactory;
import org.apache.tuscany.model.assembly.ComponentType;
import org.apache.tuscany.model.assembly.Multiplicity;
import org.apache.tuscany.model.assembly.Reference;
import org.apache.tuscany.model.assembly.Scope;
import org.apache.tuscany.model.assembly.Service;
import org.apache.tuscany.model.assembly.ServiceContract;

/**
 * @version $$Rev$$ $$Date$$
 */
public class CoreAnnotationsProcessingTestCase extends TestCase {

    private ComponentTypeIntrospector introspector;
    private AssemblyFactory factory;

    public void testServiceBasicProcessing() throws Exception {
        ComponentType type = factory.createComponentType();
        introspector.introspect(TestComponentImpl.class, type);
        assertEquals(1, type.getServices().size());
        ServiceContract contract = type.getServices().get(0).getServiceContract();
        assertEquals(TestComponent.class, contract.getInterface());
        assertEquals(Scope.MODULE, contract.getScope());
    }

    public void testServiceNameSet() throws Exception {
        ComponentType type = factory.createComponentType();
        introspector.introspect(TestComponentImpl.class, type);
        assertEquals(1, type.getServices().size());
        Service service = type.getServices().get(0);
        assertEquals(JavaIntrospectionHelper.getBaseName(TestComponent.class), service.getName());
    }

    /**
     * Tests the case where a class implements one interface not marked as with <code>Remotable</code>
     */
    public void testSingleServiceProcessing() throws Exception {
        ComponentType type = factory.createComponentType();
        introspector.introspect(TestLocalComponentImpl.class, type);
        assertEquals(1, type.getServices().size());
        ServiceContract contract = type.getServices().get(0).getServiceContract();
        assertEquals(TestLocalComponent.class, contract.getInterface());
        assertEquals(Scope.MODULE, contract.getScope());
    }

    /**
     * Tests the case where an implementation specifies a service interface of its parent as opposed to the
     * single interface it directly implements
     */
    public void testInteraceHierarchyServiceProcessing() throws Exception {
        ComponentType type = factory.createComponentType();
        introspector.introspect(SuperFooImpl.class, type);
        assertEquals(1, type.getServices().size());
        ServiceContract contract = type.getServices().get(0).getServiceContract();
        assertEquals(SuperSuperFoo.class, contract.getInterface());
    }

    /**
     * Tests the case where a class implements two interfaces, with one specified using <code>@Service</code>
     * and one marked with <code>@Remotable</code>
     */
    public void testMutlipleServiceProcessing() throws Exception {
        ComponentType type = factory.createComponentType();
        introspector.introspect(TestMultipleInterfacesComponentImpl.class, type);
        assertEquals(2, type.getServices().size());
        for (Service service : type.getServices()) {
            if (!service.getServiceContract().equals(TestComponent.class) &&
                    service.getServiceContract().equals(TestLocalComponent.class)) {
                fail("Expected multiple interfaces not found");
            }
        }
    }

    /**
     * Test case when an class implements two non-Remotable interfaces and does not specify one with
     * <code>@Service</code>
     */
    public void testNonServiceProcessing() throws Exception {
        ComponentType type = factory.createComponentType();
        introspector.introspect(TestNonServiceInterfacesImpl.class, type);
        assertEquals(1, type.getServices().size());
        ServiceContract contract = type.getServices().get(0).getServiceContract();
        assertEquals(TestNonServiceInterfacesImpl.class, contract.getInterface());
        assertEquals(Scope.MODULE, contract.getScope());
    }

    /**
     * Tests the case where a class implements two non-Remotable interfaces, with one specified using
     * <code>@Service</code>
     */
    public void testNonServiceSpecifiedProcessing() throws Exception {
        ComponentType type = factory.createComponentType();
        introspector.introspect(TestNonServiceSpecifiedImpl.class, type);
        assertEquals(1, type.getServices().size());
        ServiceContract contract = type.getServices().get(0).getServiceContract();
        assertEquals(TestNonServiceInterface.class, contract.getInterface());
        assertEquals(Scope.MODULE, contract.getScope());
    }

    /**
     * Tests the case where a component's scope is specified by its superclass
     */
    public void testParentScopeEvaluation() throws Exception {
        ComponentType type = factory.createComponentType();
        introspector.introspect(ScopeTestComponent.class, type);
        assertEquals(1, type.getServices().size());
        ServiceContract contract = type.getServices().get(0).getServiceContract();
        assertEquals(Scope.MODULE, contract.getScope());
    }

    /**
     * FIXME JFM - temporarily disabled until non-annotated properties are fixed public void
     * testPropertyProcessing() throws Exception { ComponentType type = factory.createComponentType();
     * introspector.introspect(TestComponentImpl.class, type); List<Property>properties =
     * type.getProperties(); assertEquals(3, properties.size()); for (Property property : properties) { if
     * (!property.getName().equals("foo") && !property.getName().equals("fooRequired") &&
     * !property.getName().equals("baz")) { fail("Property names not handled properly"); } if
     * (property.getName().equals("fooRequired")) { assertTrue(property.isRequired()); } else {
     * assertFalse(property.isRequired()); } } } *
     */

    public void testReferenceProcessing() throws Exception {
        ComponentType type = factory.createComponentType();
        introspector.introspect(TestComponentImpl.class, type);
        List<Reference>references = type.getReferences();
        assertEquals(5, references.size());
        for (Reference reference : references) {
            if (reference.getName().equals("setBarRequired")) {
                assertTrue(reference.getMultiplicity() == Multiplicity.ONE_N);
            } else if (reference.getName().equals("setBar")) {
                assertTrue(reference.getMultiplicity() == Multiplicity.ZERO_N);
            } else if (reference.getName().equals("bazRefeference")) {
                assertTrue(reference.getMultiplicity() == Multiplicity.ZERO_ONE);
            } else if (reference.getName().equals("wombat")) {
                assertTrue(reference.getMultiplicity() == Multiplicity.ONE_ONE);
            } else if (reference.getName().equals("bar")) {
                assertTrue(reference.getMultiplicity() == Multiplicity.ZERO_ONE);
            } else {
                fail("Reference names not handled properly");
            }
        }
    }


    protected void setUp() throws Exception {
        super.setUp();
        factory = new SystemAssemblyFactoryImpl();
        introspector = ProcessorUtils.createCoreIntrospector(factory);
    }

}