]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleTestScriptRepository.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.osgi / src / org / simantics / scl / osgi / internal / BundleTestScriptRepository.java
diff --git a/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleTestScriptRepository.java b/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleTestScriptRepository.java
new file mode 100644 (file)
index 0000000..9bbebd3
--- /dev/null
@@ -0,0 +1,76 @@
+package org.simantics.scl.osgi.internal;\r
+\r
+import java.net.URL;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Enumeration;\r
+\r
+import org.osgi.framework.Bundle;\r
+import org.osgi.framework.BundleContext;\r
+import org.osgi.framework.BundleEvent;\r
+import org.osgi.service.component.ComponentContext;\r
+import org.osgi.service.component.annotations.Activate;\r
+import org.osgi.service.component.annotations.Component;\r
+import org.osgi.service.component.annotations.Deactivate;\r
+import org.osgi.util.tracker.BundleTracker;\r
+import org.simantics.scl.compiler.testing.TestRunnable;\r
+import org.simantics.scl.compiler.testing.repository.TestRepository;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+\r
+@Component\r
+public class BundleTestScriptRepository implements TestRepository {\r
+    \r
+    Tracker tracker;\r
+    THashMap<String, BundleTestScriptRunnable> testsRunnables = new THashMap<String, BundleTestScriptRunnable>();\r
+    THashMap<Bundle, ArrayList<String>> testsPerBundle = new THashMap<Bundle, ArrayList<String>>();\r
+\r
+    @Activate\r
+    public void activate(ComponentContext context) {\r
+        tracker = new Tracker(context.getBundleContext());\r
+        tracker.open();\r
+    }\r
+    \r
+    @Deactivate\r
+    public void deactivate() {\r
+        tracker.close();\r
+    }\r
+    \r
+    class Tracker extends BundleTracker<Bundle> {\r
+        public Tracker(BundleContext context) {\r
+            super(context, 0xffffffff, null);\r
+        }\r
+        \r
+        @Override\r
+        synchronized public Bundle addingBundle(Bundle bundle, BundleEvent event) {\r
+            Enumeration<URL> urls = bundle.findEntries("sclTests", "*.sts", true);\r
+            if(urls != null) {\r
+                ArrayList<String> modulesInThisBundle = new ArrayList<String>();\r
+                while(urls.hasMoreElements()) {\r
+                    URL url = urls.nextElement();\r
+                    String path = url.getPath();\r
+                    String testName = path.substring(10, path.length()-4);\r
+                    testsRunnables.put(testName, new BundleTestScriptRunnable(testName, url));\r
+                    modulesInThisBundle.add(testName);\r
+                }\r
+                testsPerBundle.put(bundle, modulesInThisBundle);\r
+            }\r
+            return bundle;\r
+        }\r
+        \r
+        @Override\r
+        synchronized public void removedBundle(Bundle bundle, BundleEvent event,\r
+                Bundle object) {\r
+            ArrayList<String> moduleList = testsPerBundle.get(bundle);\r
+            if(moduleList != null)\r
+                for(String moduleName : moduleList)\r
+                    testsRunnables.remove(moduleName);\r
+        }\r
+    };\r
+    \r
+    @Override\r
+    public void collectTests(Collection<TestRunnable> tests) {\r
+        tests.addAll(testsRunnables.values());\r
+    }\r
+\r
+}\r