summaryrefslogtreecommitdiffstats
path: root/sandbox/sebastien/java/sca-node/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyTestHarness.java
blob: 3f3774899def64abdbcffdc734fae5eb6ddff7a0 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * 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.test.osgi.harness;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;

import junit.framework.Assert;
import junit.framework.TestCase;
import junit.framework.TestResult;

import org.apache.tuscany.sca.test.osgi.runtime.impl.OSGiTestRuntime;
import org.apache.tuscany.sca.test.util.OSGiRuntimeLoader;
import org.apache.tuscany.sca.test.util.TuscanyLoader;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

/*
 * Test Tuscany running in an OSGi container
 * Harness can be used to run Tuscany samples with Tuscany running in OSGi
 */
public class OSGiTuscanyTestHarness {
    

    private OSGiTestRuntime osgiRuntime;
    protected Bundle tuscanyRuntime;
    private BundleContext bundleContext;
    private Bundle testBundle;

    public void setUp() throws Exception {
        
        osgiRuntime = OSGiRuntimeLoader.startOSGiTestRuntime();
        bundleContext = osgiRuntime.getBundleContext();
        
        // Uninstall any previously installed test bundles
        for (Bundle bundle : bundleContext.getBundles()) {
            if ("org.apache.tuscany.sca.test.samples".equals(bundle.getSymbolicName())) {
                bundle.uninstall();
            }
        }
    }
    

    public void tearDown() throws Exception {
        if (tuscanyRuntime != null) {
            tuscanyRuntime.stop();
        }
        OSGiRuntimeLoader.shutdownOSGiRuntime();
    }
    
    public BundleContext getBundleContext() {
        return bundleContext;
    }
   
    public void runTest(String... testDirs) throws Exception {
        
        String mainTestDir = testDirs[0];
        
        File testDir = new File(mainTestDir + "/target/test-classes");
        if (!testDir.exists()) {
            System.err.println("Test directory " + testDir + " does not exist");
            return;
        }

        System.out.println("Run tests from : " + mainTestDir);

        long startTime = System.currentTimeMillis();
        
        tuscanyRuntime = TuscanyLoader.loadTuscanyIntoOSGi(bundleContext);
        
        String[] dirs = new String[testDirs.length + 2];
        int i = 0;
        dirs[i++] = mainTestDir + "/target/test-classes";
        dirs[i++] = "target/test-classes";
        for (int j = 0; j < testDirs.length; j++) {
            dirs[i++] = testDirs[j] + "/target/classes";
        }
        
        String manifestFile = "target/test-classes/META-INF/MANIFEST.MF";
        
        testBundle = createAndInstallBundle(
                 "file:" + mainTestDir + "/target/classes",    // Bundle location: used to get File URLs for DefaultSCADomain
                 manifestFile,                                 // Test bundle manifest file
                 dirs                                          // Directory entries to be added to bundle
                 );
    
        

        long endTime = System.currentTimeMillis();
        
        System.out.println("Loaded Tuscany, time taken = " + (endTime-startTime) + " ms" );
        
        testBundle.start();
        
        Class<?> testClass = testBundle.loadClass(this.getClass().getName());
        Method testMethod = testClass.getMethod("runAllTestsFromBundle", Bundle.class);
        Object testObject = testClass.newInstance();
        testMethod.invoke(testObject, testBundle);
        
        testBundle.stop();
        testBundle.uninstall();
    }
    
    // Create and install a bundle with the specified manifest file
    // The bundle contains all files from the list of directories specified
    public Bundle createAndInstallBundle(String bundleLocation, String manifestFileName,
            String[] dirNames) throws Exception {

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        File manifestFile = new File(manifestFileName);
        Manifest manifest = new Manifest();
        manifest.read(new FileInputStream(manifestFile));
        manifest.getMainAttributes().putValue("Bundle-Version", (String)tuscanyRuntime.getHeaders().get("Bundle-Version"));

        JarOutputStream jarOut = new JarOutputStream(out, manifest);
        
        for (int i = 0; i < dirNames.length; i++) {
            File dir = new File(dirNames[i]);
            addFilesToJar(dir, dirNames[i], jarOut);
        }

        jarOut.close();
        out.close();

        ByteArrayInputStream inStream = new ByteArrayInputStream(out.toByteArray());
        return bundleContext.installBundle(bundleLocation, inStream);

    }
     
    // Add all the files from a build directory into a jar file
    // This method is used to create bundles on the fly
    private void addFilesToJar(File dir, String rootDirName, JarOutputStream jarOut) throws Exception {
        
        if (dir.getName().equals(".svn"))
            return;
        
        File[] files = dir.listFiles();
        
        if (files == null)
            return;

        for (int i = 0; i < files.length; i++) {

            if (files[i].isDirectory()) {
                addFilesToJar(files[i], rootDirName, jarOut);
            }
            if (files[i].getName().endsWith("MANIFEST.MF"))
                continue;

            String entryName = files[i].getPath().substring(rootDirName.length()+1);
            entryName = entryName.replaceAll("\\\\", "/");
            if (files[i].isDirectory()) {
                entryName += "/";
            }
            ZipEntry ze = new ZipEntry(entryName);

            try {
                jarOut.putNextEntry(ze);
                FileInputStream file = new FileInputStream(files[i]);
                byte[] fileContents = new byte[file.available()];
                file.read(fileContents);
                jarOut.write(fileContents);
            } catch (Exception e) {
                // Ignore duplicate entry errors 
            }
        }
    }
    

    public void runAllTestsFromBundle(Bundle bundle) throws Exception {
        
        TestResult testResult = new TestResult();
        Enumeration entries = bundle.findEntries("/", "*TestCase.class", true);
        while (entries.hasMoreElements()) {
            URL entry = (URL)entries.nextElement();
            String className = entry.getFile();
            className = className.substring(1, className.length()-6); // remove leading / and trailing .class
            className = className.replaceAll("/", ".");
            Class testClass = bundle.loadClass(className);
            runTestCase(testClass, testResult);
        }    
        
        Assert.assertEquals(0, testResult.errorCount());

    }
    

    public void runTestCase(Class testClass, TestResult testResult) throws Exception {
        
        boolean isJunitTest = TestCase.class.isAssignableFrom(testClass);
        if (testClass.getName().endsWith("TestCase") &&
                    !testClass.getPackage().getName().startsWith("org.apache.tuscany.sca.test.osgi")) {
            Object test = (Object)testClass.newInstance();

            System.out.println("Running test " + test + " ");
            int ran = 0;
            int failed = 0;
            ArrayList<Method> testMethods = new ArrayList<Method>();
            Method setupMethod = null;
            Method tearDownMethod = null;
            Method setupClassMethod = null;
            Method tearDownClassMethod = null;
            Method[] methods = testClass.getDeclaredMethods();
            for (final Method method : methods) {
                if ((isJunitTest && method.getName().startsWith("test"))
                        || method.getAnnotation(Test.class) != null) {
                    testMethods.add(method);

                } else if ((isJunitTest && method.getName().equals("setUp"))
                        || method.getAnnotation(Before.class) != null) {

                    setupMethod = method;
                    AccessController.doPrivileged(new PrivilegedAction<Object>() {
                        public Object run() {
                            method.setAccessible(true);
                            return null;
                        }
                    });

                } else if ((isJunitTest && method.getName().equals("tearDown"))
                        || method.getAnnotation(After.class) != null) {

                    tearDownMethod = method;
                    AccessController.doPrivileged(new PrivilegedAction<Object>() {
                        public Object run() {
                            method.setAccessible(true);
                            return null;
                        }
                    });

                } else if (method.getAnnotation(BeforeClass.class) != null) {

                    setupClassMethod = method;
                    AccessController.doPrivileged(new PrivilegedAction<Object>() {
                        public Object run() {
                            method.setAccessible(true);
                            return null;
                        }
                    });

                } else if (method.getAnnotation(AfterClass.class) != null) {

                    tearDownClassMethod = method;
                    AccessController.doPrivileged(new PrivilegedAction<Object>() {
                        public Object run() {
                            method.setAccessible(true);
                            return null;
                        }
                    });

                }
            }
            try {
                if (setupClassMethod != null)
                    setupClassMethod.invoke(null);
                for (Method testMethod : testMethods) {

                    ran++;
                    failed++;
                    try {
                        if (setupMethod != null)
                            setupMethod.invoke(test);

                        testMethod.invoke(test);
                        failed--;

                    } catch (Exception e) {
                        e.printStackTrace();
                        throw e;
                    } finally {
                        if (tearDownMethod != null)
                            tearDownMethod.invoke(test);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            } finally {

                System.out.println("Ran: " + ran + ", Passed: " + (ran - failed) + ", Failed: " + failed);
                if (tearDownClassMethod != null)
                    tearDownClassMethod.invoke(null);
            }
        }    
    }
}