• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.googlecode.guice;
18 
19 import aQute.bnd.main.bnd;
20 import com.googlecode.guice.bundle.OSGiTestActivator;
21 import java.io.BufferedOutputStream;
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.util.Properties;
27 import java.util.ServiceLoader;
28 import junit.framework.TestCase;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.launch.Framework;
31 import org.osgi.framework.launch.FrameworkFactory;
32 
33 /**
34  * Run various tests inside one or more OSGi containers.
35  *
36  * @author mcculls@gmail.com (Stuart McCulloch)
37  */
38 public class OSGiContainerTest extends TestCase {
39 
40   // build properties passed from Ant
41   static final String VERSION = System.getProperty("version", "snapshot");
42   static final String BUILD_DIR = System.getProperty("build.dir", "build");
43 
44   static final String BUILD_DIST_DIR = BUILD_DIR + "/dist";
45   static final String BUILD_TEST_DIR = BUILD_DIR + "/test";
46 
47   static final String GUICE_JAR = BUILD_DIST_DIR + "/guice-" + VERSION + ".jar";
48 
49   /*if[AOP]*/
50   static final String AOPALLIANCE_JAR =
51       System.getProperty("aopalliance.jar", "lib/aopalliance.jar");
52   /*end[AOP]*/
53   static final String JAVAX_INJECT_JAR =
54       System.getProperty("javax.inject.jar", "lib/javax.inject.jar");
55   static final String GUAVA_JAR = System.getProperty("guava.jar", "lib/guava-19.0.jar");
56 
57   // dynamically build test bundles
58   @Override
setUp()59   protected void setUp() throws Exception {
60 
61     // verify properties
62     assertTrue(failMsg(), new File(BUILD_DIR).isDirectory());
63     assertTrue(failMsg(), new File(GUICE_JAR).isFile());
64 
65     /*if[AOP]*/
66     assertTrue(failMsg(), new File(AOPALLIANCE_JAR).isFile());
67     /*end[AOP]*/
68     assertTrue(failMsg(), new File(JAVAX_INJECT_JAR).isFile());
69     assertTrue(failMsg(), new File(GUAVA_JAR).isFile());
70 
71     Properties instructions = new Properties();
72 
73     /*if[AOP]*/
74     // aopalliance is an API bundle --> export the full API
75     instructions.setProperty("Export-Package", "org.aopalliance.*");
76     buildBundle("aopalliance", instructions, AOPALLIANCE_JAR);
77     instructions.clear();
78     /*end[AOP]*/
79 
80     // javax.inject is an API bundle --> export the full API
81     instructions.setProperty("Export-Package", "javax.inject.*");
82     buildBundle("javax.inject", instructions, JAVAX_INJECT_JAR);
83     instructions.clear();
84 
85     // early versions of guava did not ship with OSGi metadata
86     instructions.setProperty("Export-Package", "com.google.common.*");
87     instructions.setProperty("Import-Package", "*;resolution:=optional");
88     buildBundle("guava", instructions, GUAVA_JAR);
89     instructions.clear();
90 
91     // strict imports to make sure test bundle only has access to these packages
92     instructions.setProperty(
93         "Import-Package",
94         "org.osgi.framework,"
95             /*if[AOP]*/
96             + "org.aopalliance.intercept,"
97             /*end[AOP]*/
98             + "com.google.inject(|.binder|.matcher|.name)");
99 
100     // test bundle should only contain the local test classes, nothing else
101     instructions.setProperty("Bundle-Activator", OSGiTestActivator.class.getName());
102     instructions.setProperty("Private-Package", OSGiTestActivator.class.getPackage().getName());
103     buildBundle("osgitests", instructions, BUILD_TEST_DIR);
104     instructions.clear();
105   }
106 
107   // build an OSGi bundle at runtime
buildBundle(String name, Properties instructions, String classpath)108   private static void buildBundle(String name, Properties instructions, String classpath)
109       throws IOException {
110 
111     // write BND instructions to temporary test directory
112     String bndFileName = BUILD_TEST_DIR + '/' + name + ".bnd";
113     OutputStream os = new BufferedOutputStream(new FileOutputStream(bndFileName));
114     instructions.store(os, "BND instructions");
115     os.close();
116 
117     // assemble bundle, use -failok switch to avoid early exit
118     bnd.main(new String[] {"-failok", "build", "-classpath", classpath, bndFileName});
119   }
120 
failMsg()121   private String failMsg() {
122     return "This test may fail if it is not run from ant, or if it is not run after ant has "
123         + "compiled & built jars. This is because the test is validating that the Guice jar "
124         + "is properly setup to load in an OSGi container";
125   }
126 
127   //This test may fail if it is not run from ant, or if it is not run after ant has
128   //compiled & built jars. This is because the test is validating that the Guice jar
129   //is properly setup to load in an OSGi container
testGuiceWorksInOSGiContainer()130   public void testGuiceWorksInOSGiContainer() throws Throwable {
131 
132     // ask framework to clear cache on startup
133     Properties properties = new Properties();
134     properties.setProperty("org.osgi.framework.storage", BUILD_TEST_DIR + "/bundle.cache");
135     properties.setProperty("org.osgi.framework.storage.clean", "onFirstInit");
136 
137     // test each available OSGi framework in turn
138     for (FrameworkFactory frameworkFactory : ServiceLoader.load(FrameworkFactory.class)) {
139       Framework framework = frameworkFactory.newFramework(properties);
140 
141       framework.start();
142       BundleContext systemContext = framework.getBundleContext();
143 
144       // load all the necessary bundles and start the OSGi test bundle
145       /*if[AOP]*/
146       systemContext.installBundle("reference:file:" + BUILD_TEST_DIR + "/aopalliance.jar");
147       /*end[AOP]*/
148       systemContext.installBundle("reference:file:" + BUILD_TEST_DIR + "/javax.inject.jar");
149       systemContext.installBundle("reference:file:" + BUILD_TEST_DIR + "/guava.jar");
150       systemContext.installBundle("reference:file:" + GUICE_JAR);
151       systemContext.installBundle("reference:file:" + BUILD_TEST_DIR + "/osgitests.jar").start();
152 
153       framework.stop();
154     }
155   }
156 }
157