summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/1.5.1-RC4/vtest/java-api/apis/componentcontext/src/test/java/org/apache/tuscany/sca/vtest/javaapi/apis/componentcontext/ComponentContextTestCase.java
blob: 3f4150c64fba32d0e965711305696aea8abf59f2 (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
/*
 * 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.apis.componentcontext;

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

import org.osoa.sca.ServiceRuntimeException;

/**
 * This test class tests the ComponentContext interface described in 1.7.1 of
 * the SCA Java Annotations & APIs Specification 1.0. Relevant sections of 1.4
 * will also be covered here.
 */
public class ComponentContextTestCase {

    protected static String compositeName = "ab.composite";
    protected static AComponent a;
    protected static AComponent aUnannotated;
    protected static BService b;

    @BeforeClass
    public static void init() throws Exception {
        try {
            System.out.println("Setting up");
            ServiceFinder.init(compositeName);
            a = ServiceFinder.getService(AComponent.class, "AComponent");
            aUnannotated = ServiceFinder.getService(AComponent.class, "AUnannotatedComponent");
            b = ServiceFinder.getService(BService.class, "BComponent/BService");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @AfterClass
    public static void destroy() throws Exception {
        System.out.println("Cleaning up");
        ServiceFinder.cleanup();
    }

    /**
     * Lines 776 <br>
     * getURI() - Returns the absolute URI of the component within the SCA
     * domain.
     * 
     * @throws Exception
     */
    @Test
    public void testGetURI() throws Exception {
        Assert.assertEquals("AComponent", a.getContextURI());
        Assert.assertEquals("AUnannotatedComponent", aUnannotated.getContextURI());
    }

    /**
     * Lines 778 <br>
     * getService(Class&lt;B&gt; businessInterface, String referenceName) ?
     * Returns a proxy for the reference defined by the current component.
     * 
     * @throws Exception
     */
    @Test
    public void testGetService() throws Exception {
        Assert.assertEquals(a.getServiceBName(), "ServiceB");
    }

    /**
     * Lines 780 <br>
     * getServiceReference(Class&lt;B&gt; businessInterface, String
     * referenceName) ? Returns a ServiceReference defined by the current
     * component.
     * 
     * @throws Exception
     */
    @Test
    public void testGetServiceReference() throws Exception {
        Assert.assertEquals(a.getServiceReferenceBName(), "ServiceB");
    }

    /**
     * Lines 783 <br>
     * createSelfReference(Class&lt;B&gt; businessInterface) ? Returns a
     * ServiceReference that can be used to invoke this component over the
     * designated service.
     * 
     * @throws Exception
     */
    @Test
    public void testCreateSelfReference() throws Exception {
        Assert.assertEquals(a.getSelfReferenceName(), "ComponentA");
    }

    /**
     * Lines 785 <br>
     * getSelfReference(Class&lt;B&gt; businessInterface, String serviceName) -
     * Returns a ServiceReference that can be used to invoke this component over
     * the designated service. Service name explicitly declares the service name
     * to invoke.
     * 
     * @throws Exception
     */
    @Test
    public void testCreateSelfReferenceWithServiceName() throws Exception {
        Assert.assertEquals("ServiceC", b.getSelfReferenceWithServiceName());
    }

    /**
     * Lines 788 <br>
     * getProperty (Class&lt;B&gt; type, String propertyName) - Returns the
     * value of an SCA property defined by this component.
     * 
     * @throws Exception
     */
    @Test
    public void testGetProperty() throws Exception {
        Assert.assertEquals("PropertyA", a.getProperty());
    }

    /**
     * Lines 793 <br>
     * getRequestContext() - Returns the context for the current SCA service
     * request, or null if there is no current request or if the context is
     * unavailable.
     * 
     * @throws Exception
     */
    @Test
    public void testGetRequestContext() throws Exception {
        Assert.assertEquals("AComponent", a.getRequestContextServiceName());
        Assert.assertEquals("NotNull", a.getRequestContextContent());
    }

    /**
     * Lines 790,794 <br>
     * cast(B target) - Casts a type-safe reference to a CallableReference.
     * 
     * @throws Exception
     */
    @Test
    public void testCast() throws Exception {
        Assert.assertEquals("ServiceB", a.getCastCallableReferenceServiceName());
        Assert.assertEquals("ServiceB", a.getCastServiceReferenceServiceName());

        String check = "";
        try {
            a.illegalCast();
        } catch (IllegalArgumentException iae) {
            check = "IllegalCast";
        } catch (ServiceRuntimeException sre) {
            if (sre.getCause() instanceof IllegalArgumentException) { 
                check = "IllegalCast";
            }
        }
        Assert.assertEquals("IllegalCast", check);
    }

    /**
     * Lines 342-344 <br>
     * When a component implementation needs access to a service where the
     * reference to the service is not known at compile time, the reference can
     * be located using the component?s ComponentContext.
     * 
     * @throws Exception
     */
    @Test
    @Ignore("TUSCANY-2609")
    public void testServiceLookup() throws Exception {
        Assert.assertEquals("ComponentD", a.testServiceLookup());
    }

}