summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoaderTestCase.java
blob: 8409dd94912fb43b8a49930c7fe2bf373b5b75d6 (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
/*
 * 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.contribution.java.impl;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;

import org.apache.tuscany.sca.contribution.Contribution;
import org.apache.tuscany.sca.contribution.ContributionFactory;
import org.apache.tuscany.sca.contribution.java.JavaExport;
import org.apache.tuscany.sca.contribution.java.JavaImport;
import org.apache.tuscany.sca.contribution.java.JavaImportExportFactory;
import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.core.FactoryExtensionPoint;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;


/**
 * Test ContributionClassLoader.
 *
 */
public class ContributionClassLoaderTestCase  {
    
    private static ContributionFactory contributionFactory;
    private static JavaImportExportFactory javaImportExportFactory;
    
    @BeforeClass
    public static void setUp() throws Exception {
        ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry();
        FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class);
        contributionFactory = modelFactories.getFactory(ContributionFactory.class);
        javaImportExportFactory = modelFactories.getFactory(JavaImportExportFactory.class);
    }
    
    private Contribution createContribution(String fileName) throws MalformedURLException {
        Contribution contrib = contributionFactory.createContribution();
        File contribDir = new File(fileName);        
        contrib.setLocation(contribDir.toURI().toURL().toString());
        ClassLoader contextClassLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
            public ClassLoader run() {
                return Thread.currentThread().getContextClassLoader();
            }
        });           
        contrib.setClassLoader(new ContributionClassLoader(contrib, contextClassLoader));
        return contrib;
    }
    
   
    @Test
    public void testClassLoadingFromContribution() throws ClassNotFoundException, MalformedURLException {
        
        Contribution contribA = createContribution("target/test-classes");
        Contribution contribB = createContribution("target");
        Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar");
        
        // Class present in contribution, also in parent. Class is loaded from parent
        Class<?> testClassA = contribA.getClassLoader().loadClass(this.getClass().getName());        
        Assert.assertNotNull(testClassA);
        Assert.assertSame(this.getClass(), testClassA);
        
        // Class not present in contribution, but present in parent ClassLoader
        Class<?> testClassB = contribB.getClassLoader().loadClass(this.getClass().getName());
        Assert.assertNotNull(testClassB);
        Assert.assertSame(this.getClass(), testClassB);
        
        // Class present in contribution, but not in parent
        Class<?> testClassC = contribC.getClassLoader().loadClass("calculator.AddService");        
        Assert.assertNotNull(testClassC);
        
        // Class not present in contribution or in parent
        try {
            contribA.getClassLoader().loadClass("NonExistent");
            
            Assert.assertTrue("ClassNotFoundException not thrown as expected", false);
            
        } catch (ClassNotFoundException e) {
        }
        
        
        
    }
    
    @Test
    public void testResourceLoadingFromContribution() throws ClassNotFoundException, MalformedURLException {
        
        Contribution contribA = createContribution("target/test-classes");
        Contribution contribB = createContribution("target");
        Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar");
        
        // Resource present in contribution, and in parent
        URL resA = contribA.getClassLoader().getResource("deployables/sample-calculator.jar");
        Assert.assertNotNull(resA);
        
        // Resource not present in contribution, but present in parent ClassLoader
        URL resB = contribB.getClassLoader().getResource("deployables/sample-calculator.jar");
        Assert.assertNotNull(resB);
        
        // Resource present in contribution, but not in parent
        URL resC = contribC.getClassLoader().getResource("calculator/AddService.class");
        Assert.assertNotNull(resC);        
        
        // Load Java class as resource from parent
        String classResName = this.getClass().getName().replaceAll("\\.", "/") + ".class";
        URL classResA = contribA.getClassLoader().getResource(classResName);
        Assert.assertNotNull(classResA);
               
        // Non-existent resource
        URL res = contribA.getClassLoader().getResource("deployables/NonExistent");
        Assert.assertNull(res);
        
    }
    
    private static String getPackageName(Class<?> cls) {
        String name = cls.getName();
        int index = name.lastIndexOf('.');
        return index == -1 ? "" : name.substring(0, index);
    }

    @Test
    public void testClassLoadingFromImportedContribution() throws ClassNotFoundException, MalformedURLException {
        
        Contribution contribA = createContribution("target/test-classes");
        Contribution contribB = createContribution("target");
        Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar");
        ArrayList<Contribution> exportContribList = new ArrayList<Contribution>();
        exportContribList.add(contribA);
        exportContribList.add(contribC);
        
        JavaImport import_ = javaImportExportFactory.createJavaImport();
        import_.setPackage(getPackageName(getClass()));
        import_.setModelResolver(new JavaImportModelResolver(exportContribList, null));
        contribB.getImports().add(import_);
        import_ = javaImportExportFactory.createJavaImport();
        import_.setPackage("calculator");
        import_.setModelResolver(new JavaImportModelResolver(exportContribList, null));
        contribB.getImports().add(import_);
        
        JavaExport export = javaImportExportFactory.createJavaExport();
        export.setPackage(getPackageName(getClass()));
        contribA.getExports().add(export);
        export = javaImportExportFactory.createJavaExport();
        export.setPackage("calculator");
        contribC.getExports().add(export);        
        
        // Load class from parent, class is also present in imported contribution. Class should
        // be loaded from parent
        Class<?> testClassB = contribB.getClassLoader().loadClass(this.getClass().getName());        
        Assert.assertNotNull(testClassB);
        Assert.assertSame(this.getClass(), testClassB);
        
        // Load class from parent, class is also present in parent. Class should be loaded
        // from parent.
        Class<?> testClassA = contribA.getClassLoader().loadClass(this.getClass().getName());        
        Assert.assertNotNull(testClassA);
        Assert.assertSame(this.getClass(), testClassA);
        
        // Imported class should be the same as the one loaded by the exporting contribution
        Assert.assertSame(testClassA, testClassB);
        
        // Load class from imported contribution, class is not present in parent
        Class<?> testClassB1 = contribB.getClassLoader().loadClass("calculator.AddService");
        Assert.assertNotNull(testClassB1);
        
        // Imported class should be the same as the one loaded by the exporting contribution
        Class<?> testClassC = contribC.getClassLoader().loadClass("calculator.AddService");
        Assert.assertNotNull(testClassC);        
        Assert.assertSame(testClassC, testClassB1);
        

        // Try to load class from package which is not explicitly imported - should throw ClassNotFoundException
        try {
            contribA.getClassLoader().loadClass("calculator.AddService");
            
            Assert.assertTrue("ClassNotFoundException not thrown as expected", false);
            
        } catch (ClassNotFoundException e) {
        }
        
        // Try to load non-existent class from imported package - should throw ClassNotFoundException
        try {
            contribB.getClassLoader().loadClass(getPackageName(getClass()) + ".NonExistentClass");
            
            Assert.assertTrue("ClassNotFoundException not thrown as expected", false);
            
        } catch (ClassNotFoundException e) {
        }
        
    }

    @Test
    public void testResourceLoadingFromImportedContribution() throws ClassNotFoundException, MalformedURLException {
        
        Contribution contribA = createContribution("target/test-classes");
        Contribution contribB = createContribution("target");
        Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar");
        
        ArrayList<Contribution> exportContribList = new ArrayList<Contribution>();
        exportContribList.add(contribA);
        exportContribList.add(contribC);
        
        JavaImport import_ = javaImportExportFactory.createJavaImport();
        import_.setPackage(getPackageName(getClass()));
        import_.setModelResolver(new JavaImportModelResolver(exportContribList, null));
        contribB.getImports().add(import_);
        JavaImport import1_ = javaImportExportFactory.createJavaImport();
        import1_.setPackage("calculator");
        import1_.setModelResolver(new JavaImportModelResolver(exportContribList, null));
        contribB.getImports().add(import1_);
        
        JavaExport export = javaImportExportFactory.createJavaExport();
        export.setPackage(getPackageName(getClass()));
        contribA.getExports().add(export);
        JavaExport export1 = javaImportExportFactory.createJavaExport();
        export1.setPackage("calculator");
        contribC.getExports().add(export1);

        
        // Load resource from parent
        URL resB = contribB.getClassLoader().getResource("deployables/sample-calculator.jar"); 
        Assert.assertNotNull(resB);
        
        // Load Java class as resource from imported contribution with JavaImport
        String classResName = this.getClass().getName().replaceAll("\\.", "/") + ".class";               
        URL classResB = contribB.getClassLoader().getResource(classResName);
        Assert.assertNotNull(classResB);
        
        // Load Java class as resource from imported contribution with JavaImport
        URL classResB1 = contribB.getClassLoader().getResource("calculator/AddService.class");
        Assert.assertNotNull(classResB1);
        
        // Try to load resource not explicitly imported by contribution
        URL classResA1 = contribA.getClassLoader().getResource("calculator/AddService.class");
        Assert.assertNull(classResA1);
        
        
    }

}