From: Mathieu Baudier Date: Sun, 26 Dec 2021 07:03:27 +0000 (+0100) Subject: Move unit tests X-Git-Tag: argeo-commons-2.3.5~122 X-Git-Url: https://git.argeo.org/?a=commitdiff_plain;h=3a492c414f60df15966de9efff817e32aba0e196;p=lgpl%2Fargeo-commons.git Move unit tests --- diff --git a/org.argeo.cms.jcr/.classpath b/org.argeo.cms.jcr/.classpath index 20cad808a..4a00becd8 100644 --- a/org.argeo.cms.jcr/.classpath +++ b/org.argeo.cms.jcr/.classpath @@ -7,6 +7,5 @@ - diff --git a/org.argeo.cms.jcr/ext/test/org/argeo/cms/tabular/JcrTabularTest.java b/org.argeo.cms.jcr/ext/test/org/argeo/cms/tabular/JcrTabularTest.java deleted file mode 100644 index 18150c791..000000000 --- a/org.argeo.cms.jcr/ext/test/org/argeo/cms/tabular/JcrTabularTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.argeo.cms.tabular; - -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.List; - -import javax.jcr.Node; -import javax.jcr.PropertyType; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.jackrabbit.commons.cnd.CndImporter; -import org.argeo.api.tabular.TabularColumn; -import org.argeo.api.tabular.TabularRow; -import org.argeo.api.tabular.TabularRowIterator; -import org.argeo.api.tabular.TabularWriter; -import org.argeo.cms.ArgeoTypes; -import org.argeo.jackrabbit.unit.AbstractJackrabbitTestCase; - -public class JcrTabularTest extends AbstractJackrabbitTestCase { - private final static Log log = LogFactory.getLog(JcrTabularTest.class); - - public void testWriteReadCsv() throws Exception { - // session().setNamespacePrefix("argeo", ArgeoNames.ARGEO_NAMESPACE); - InputStreamReader reader = new InputStreamReader(getClass().getResourceAsStream("/org/argeo/cms/jcr/ldap.cnd")); - CndImporter.registerNodeTypes(reader, session()); - reader.close(); - reader = new InputStreamReader(getClass().getResourceAsStream("/org/argeo/cms/jcr/argeo.cnd")); - CndImporter.registerNodeTypes(reader, session()); - reader.close(); -// reader = new InputStreamReader(getClass().getResourceAsStream("/org/argeo/cms/cms.cnd")); -// CndImporter.registerNodeTypes(reader, session()); -// reader.close(); - - // write - Integer columnCount = 15; - Long rowCount = 1000l; - String stringValue = "test, \ntest"; - - List header = new ArrayList(); - for (int i = 0; i < columnCount; i++) { - header.add(new TabularColumn("col" + i, PropertyType.STRING)); - } - Node tableNode = session().getRootNode().addNode("table", ArgeoTypes.ARGEO_TABLE); - TabularWriter writer = new JcrTabularWriter(tableNode, header, ArgeoTypes.ARGEO_CSV); - for (int i = 0; i < rowCount; i++) { - List objs = new ArrayList(); - for (int j = 0; j < columnCount; j++) { - objs.add(stringValue); - } - writer.appendRow(objs.toArray()); - } - writer.close(); - session().save(); - - if (log.isDebugEnabled()) - log.debug("Wrote tabular content " + rowCount + " rows, " + columnCount + " columns"); - // read - TabularRowIterator rowIt = new JcrTabularRowIterator(tableNode); - Long count = 0l; - while (rowIt.hasNext()) { - TabularRow tr = rowIt.next(); - assertEquals(header.size(), tr.size()); - count++; - } - assertEquals(rowCount, count); - if (log.isDebugEnabled()) - log.debug("Read tabular content " + rowCount + " rows, " + columnCount + " columns"); - } -} diff --git a/org.argeo.cms.jcr/ext/test/org/argeo/jcr/fs/JcrFileSystemTest.java b/org.argeo.cms.jcr/ext/test/org/argeo/jcr/fs/JcrFileSystemTest.java deleted file mode 100644 index 2d03b8f2c..000000000 --- a/org.argeo.cms.jcr/ext/test/org/argeo/jcr/fs/JcrFileSystemTest.java +++ /dev/null @@ -1,191 +0,0 @@ -package org.argeo.jcr.fs; - -import java.io.InputStream; -import java.io.OutputStream; -import java.net.URI; -import java.nio.file.DirectoryStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.attribute.BasicFileAttributes; -import java.nio.file.attribute.FileTime; -import java.nio.file.spi.FileSystemProvider; -import java.util.Arrays; -import java.util.Map; - -import javax.jcr.Property; -import javax.jcr.RepositoryException; -import javax.jcr.Session; -import javax.jcr.nodetype.NodeType; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.jackrabbit.core.RepositoryImpl; -import org.argeo.jackrabbit.fs.JackrabbitMemoryFsProvider; - -import junit.framework.TestCase; - -public class JcrFileSystemTest extends TestCase { - private final static Log log = LogFactory.getLog(JcrFileSystemTest.class); - - public void testMounts() throws Exception { - JackrabbitMemoryFsProvider fsProvider = new JackrabbitMemoryFsProvider() { - - @Override - protected void postRepositoryCreation(RepositoryImpl repositoryImpl) throws RepositoryException { - // create workspace - Session session = login(); - session.getWorkspace().createWorkspace("test"); - } - - }; - - Path rootPath = fsProvider.getPath(new URI("jcr+memory:/")); - log.debug("Got root " + rootPath); - Path testDir = rootPath.resolve("testDir"); - Files.createDirectory(testDir); - - Path testMount = fsProvider.getPath(new URI("jcr+memory:/test")); - log.debug("Test path"); - assertEquals(rootPath, testMount.getParent()); - assertEquals(testMount.getFileName(), rootPath.relativize(testMount)); - - Path testPath = testMount.resolve("test.txt"); - log.debug("Create file " + testPath); - Files.createFile(testPath); - BasicFileAttributes bfa = Files.readAttributes(testPath, BasicFileAttributes.class); - FileTime ft = bfa.creationTime(); - assertNotNull(ft); - assertTrue(bfa.isRegularFile()); - log.debug("Created " + testPath + " (" + ft + ")"); - Files.delete(testPath); - log.debug("Deleted " + testPath); - - // Browse directories from root - DirectoryStream files = Files.newDirectoryStream(rootPath); - int directoryCount = 0; - for (Path file : files) { - if (Files.isDirectory(file)) { - directoryCount++; - } - } - assertEquals(2, directoryCount); - - // Browse directories from mount - Path mountSubDir = testMount.resolve("mountSubDir"); - Files.createDirectory(mountSubDir); - Path otherSubDir = testMount.resolve("otherSubDir"); - Files.createDirectory(otherSubDir); - testPath = testMount.resolve("test.txt"); - Files.createFile(testPath); - files = Files.newDirectoryStream(testMount); - int fileCount = 0; - for (Path file : files) { - fileCount++; - } - assertEquals(3, fileCount); - - } - - public void testSimple() throws Exception { - FileSystemProvider fsProvider = new JackrabbitMemoryFsProvider(); - - // Simple file - Path rootPath = fsProvider.getPath(new URI("jcr+memory:/")); - log.debug("Got root " + rootPath); - Path testPath = fsProvider.getPath(new URI("jcr+memory:/test.txt")); - log.debug("Test path"); - assertEquals("test.txt", testPath.getFileName().toString()); - assertEquals(rootPath, testPath.getParent()); - assertEquals(testPath.getFileName(), rootPath.relativize(testPath)); - // relativize self should be empty path - Path selfRelative = testPath.relativize(testPath); - assertEquals("", selfRelative.toString()); - - log.debug("Create file " + testPath); - Files.createFile(testPath); - BasicFileAttributes bfa = Files.readAttributes(testPath, BasicFileAttributes.class); - FileTime ft = bfa.creationTime(); - assertNotNull(ft); - assertTrue(bfa.isRegularFile()); - log.debug("Created " + testPath + " (" + ft + ")"); - Files.delete(testPath); - log.debug("Deleted " + testPath); - String txt = "TEST\nTEST2\n"; - byte[] arr = txt.getBytes(); - Files.write(testPath, arr); - log.debug("Wrote " + testPath); - byte[] read = Files.readAllBytes(testPath); - assertTrue(Arrays.equals(arr, read)); - assertEquals(txt, new String(read)); - log.debug("Read " + testPath); - Path testDir = rootPath.resolve("testDir"); - log.debug("Resolved " + testDir); - // Copy - Files.createDirectory(testDir); - log.debug("Created directory " + testDir); - Path subsubdir = Files.createDirectories(testDir.resolve("subdir/subsubdir")); - log.debug("Created sub directories " + subsubdir); - Path copiedFile = testDir.resolve("copiedFile.txt"); - log.debug("Resolved " + copiedFile); - Path relativeCopiedFile = testDir.relativize(copiedFile); - assertEquals(copiedFile.getFileName().toString(), relativeCopiedFile.toString()); - log.debug("Relative copied file " + relativeCopiedFile); - try (OutputStream out = Files.newOutputStream(copiedFile); InputStream in = Files.newInputStream(testPath)) { - IOUtils.copy(in, out); - } - log.debug("Copied " + testPath + " to " + copiedFile); - Files.delete(testPath); - log.debug("Deleted " + testPath); - byte[] copiedRead = Files.readAllBytes(copiedFile); - assertTrue(Arrays.equals(copiedRead, read)); - log.debug("Read " + copiedFile); - // Browse directories - DirectoryStream files = Files.newDirectoryStream(testDir); - int fileCount = 0; - Path listedFile = null; - for (Path file : files) { - fileCount++; - if (!Files.isDirectory(file)) - listedFile = file; - } - assertEquals(2, fileCount); - assertEquals(copiedFile, listedFile); - assertEquals(copiedFile.toString(), listedFile.toString()); - log.debug("Listed " + testDir); - // Generic attributes - Map attrs = Files.readAttributes(copiedFile, "*"); - assertEquals(3, attrs.size()); - log.debug("Read attributes of " + copiedFile + ": " + attrs.keySet()); - // Direct node access - NodeFileAttributes nfa = Files.readAttributes(copiedFile, NodeFileAttributes.class); - nfa.getNode().addMixin(NodeType.MIX_LANGUAGE); - nfa.getNode().getSession().save(); - log.debug("Add mix:language"); - Files.setAttribute(copiedFile, Property.JCR_LANGUAGE, "fr"); - log.debug("Set language"); - attrs = Files.readAttributes(copiedFile, "*"); - assertEquals(4, attrs.size()); - log.debug("Read attributes of " + copiedFile + ": " + attrs.keySet()); - } - - public void testIllegalCharacters() throws Exception { - FileSystemProvider fsProvider = new JackrabbitMemoryFsProvider(); - String fileName = "tüßçt[1].txt"; - String pathStr = "/testDir/" + fileName; - Path testDir = fsProvider.getPath(new URI("jcr+memory:/testDir")); - Files.createDirectory(testDir); - Path testPath = testDir.resolve(fileName); - assertEquals(pathStr, testPath.toString()); - Files.createFile(testPath); - DirectoryStream files = Files.newDirectoryStream(testDir); - Path listedPath = files.iterator().next(); - assertEquals(pathStr, listedPath.toString()); - - String dirName = "*[~WeirdDir~]*"; - Path subDir = testDir.resolve(dirName); - Files.createDirectory(subDir); - subDir = testDir.resolve(dirName); - assertEquals(dirName, subDir.getFileName().toString()); - } -} diff --git a/org.argeo.cms/.classpath b/org.argeo.cms/.classpath index 9a0a269a2..4a00becd8 100644 --- a/org.argeo.cms/.classpath +++ b/org.argeo.cms/.classpath @@ -1,8 +1,11 @@ - + + + + + - diff --git a/org.argeo.cms/ext/test/org/argeo/cms/security/PasswordBasedEncryptionTest.java b/org.argeo.cms/ext/test/org/argeo/cms/security/PasswordBasedEncryptionTest.java deleted file mode 100644 index 62f97936d..000000000 --- a/org.argeo.cms/ext/test/org/argeo/cms/security/PasswordBasedEncryptionTest.java +++ /dev/null @@ -1,120 +0,0 @@ -package org.argeo.cms.security; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.InputStream; -import java.util.Base64; - -import javax.crypto.Cipher; -import javax.crypto.CipherInputStream; -import javax.crypto.CipherOutputStream; -import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.PBEKeySpec; -import javax.crypto.spec.PBEParameterSpec; -import javax.crypto.spec.SecretKeySpec; - -import junit.framework.TestCase; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.argeo.util.PasswordEncryption; - -public class PasswordBasedEncryptionTest extends TestCase { - private final static Log log = LogFactory.getLog(PasswordBasedEncryptionTest.class); - - public void testEncryptDecrypt() { - final String password = "test long password since they are safer"; - PasswordEncryption pbeEnc = new PasswordEncryption(password.toCharArray()); - String message = "Hello World!"; - log.info("Password:\t'" + password + "'"); - log.info("Message:\t'" + message + "'"); - byte[] encrypted = pbeEnc.encryptString(message); - log.info("Encrypted:\t'" + Base64.getEncoder().encode(encrypted) + "'"); - PasswordEncryption pbeDec = new PasswordEncryption(password.toCharArray()); - InputStream in = null; - in = new ByteArrayInputStream(encrypted); - String decrypted = pbeDec.decryptAsString(in); - log.info("Decrypted:\t'" + decrypted + "'"); - IOUtils.closeQuietly(in); - assertEquals(message, decrypted); - } - - public void testPBEWithMD5AndDES() throws Exception { - String password = "test"; - String message = "Hello World!"; - - byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, - (byte) 0x99 }; - - int count = 1024; - - String cipherAlgorithm = "PBEWithMD5AndDES"; - String secretKeyAlgorithm = "PBEWithMD5AndDES"; - SecretKeyFactory keyFac = SecretKeyFactory.getInstance(secretKeyAlgorithm); - PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); - PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count); - SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); - Cipher ecipher = Cipher.getInstance(cipherAlgorithm); - ecipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); - Cipher dcipher = Cipher.getInstance(cipherAlgorithm); - dcipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); - - byte[] encrypted = ecipher.doFinal(message.getBytes()); - byte[] decrypted = dcipher.doFinal(encrypted); - assertEquals(message, new String(decrypted)); - - } - - public void testPBEWithSHA1AndAES() throws Exception { - String password = "test"; - String message = "Hello World!"; - - byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, - (byte) 0x99 }; - byte[] iv = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, - (byte) 0x99, (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, - (byte) 0x99 }; - - int count = 1024; - // int keyLength = 256; - int keyLength = 128; - - String cipherAlgorithm = "AES/CBC/PKCS5Padding"; - String secretKeyAlgorithm = "PBKDF2WithHmacSHA1"; - SecretKeyFactory keyFac = SecretKeyFactory.getInstance(secretKeyAlgorithm); - PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, count, keyLength); - SecretKey tmp = keyFac.generateSecret(pbeKeySpec); - SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); - Cipher ecipher = Cipher.getInstance(cipherAlgorithm); - ecipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(iv)); - - // decrypt - keyFac = SecretKeyFactory.getInstance(secretKeyAlgorithm); - pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, count, keyLength); - tmp = keyFac.generateSecret(pbeKeySpec); - secret = new SecretKeySpec(tmp.getEncoded(), "AES"); - // AlgorithmParameters params = ecipher.getParameters(); - // byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV(); - Cipher dcipher = Cipher.getInstance(cipherAlgorithm); - dcipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv)); - - byte[] encrypted = ecipher.doFinal(message.getBytes()); - byte[] decrypted = dcipher.doFinal(encrypted); - assertEquals(message, new String(decrypted)); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - CipherOutputStream cipherOut = new CipherOutputStream(out, ecipher); - cipherOut.write(message.getBytes()); - IOUtils.closeQuietly(cipherOut); - byte[] enc = out.toByteArray(); - - ByteArrayInputStream in = new ByteArrayInputStream(enc); - CipherInputStream cipherIn = new CipherInputStream(in, dcipher); - ByteArrayOutputStream dec = new ByteArrayOutputStream(); - IOUtils.copy(cipherIn, dec); - assertEquals(message, new String(dec.toByteArray())); - } -} diff --git a/org.argeo.cms/ext/test/org/argeo/cms/security/RunHttpSpnego.java b/org.argeo.cms/ext/test/org/argeo/cms/security/RunHttpSpnego.java deleted file mode 100644 index f090ac471..000000000 --- a/org.argeo.cms/ext/test/org/argeo/cms/security/RunHttpSpnego.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.argeo.cms.security; -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.Authenticator; -import java.net.PasswordAuthentication; -import java.net.URL; - -public class RunHttpSpnego { - - static final String kuser = "mbaudier@ARGEO.EU"; // your account name - static final String kpass = "test"; // retrieve password for your account - - static class MyAuthenticator extends Authenticator { - public PasswordAuthentication getPasswordAuthentication() { - // I haven't checked getRequestingScheme() here, since for NTLM - // and Negotiate, the usrname and password are all the same. - System.err.println("Feeding username and password for " + getRequestingScheme()); - return (new PasswordAuthentication(kuser, kpass.toCharArray())); - } - } - - public static void main(String[] args) throws Exception { - Authenticator.setDefault(new MyAuthenticator()); - URL url = new URL(args[0]); - InputStream ins = url.openConnection().getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(ins)); - String str; - while((str = reader.readLine()) != null) - System.out.println(str); - } -} diff --git a/org.argeo.enterprise/.classpath b/org.argeo.enterprise/.classpath index b21df7df8..71eb16789 100644 --- a/org.argeo.enterprise/.classpath +++ b/org.argeo.enterprise/.classpath @@ -1,10 +1,11 @@ - - - - - + + + + + + + + diff --git a/org.argeo.osgi.boot/.classpath b/org.argeo.osgi.boot/.classpath index 190db038c..71eb16789 100644 --- a/org.argeo.osgi.boot/.classpath +++ b/org.argeo.osgi.boot/.classpath @@ -1,8 +1,11 @@ - - + + + + + diff --git a/org.argeo.osgi.boot/ext/test/org/argeo/osgi/a2/ClasspathSourceTest.java b/org.argeo.osgi.boot/ext/test/org/argeo/osgi/a2/ClasspathSourceTest.java deleted file mode 100644 index d087e5a3f..000000000 --- a/org.argeo.osgi.boot/ext/test/org/argeo/osgi/a2/ClasspathSourceTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.argeo.osgi.a2; - -import java.io.IOException; - -import org.argeo.osgi.a2.ClasspathSource; -import org.argeo.osgi.a2.ProvisioningManager; -import org.argeo.osgi.boot.equinox.EquinoxUtils; -import org.junit.Test; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleException; -import org.osgi.framework.launch.Framework; - -public class ClasspathSourceTest { - @Test - public void testProvisioning() throws IOException { - Framework framework = EquinoxUtils.launch(null); - ProvisioningManager provisioningManager = new ProvisioningManager(framework.getBundleContext()); - ClasspathSource classpathSource = new ClasspathSource(); - classpathSource.load(); - provisioningManager.addSource(classpathSource); - provisioningManager.install(null); - for (Bundle bundle : framework.getBundleContext().getBundles()) { - System.out.println(bundle.getSymbolicName() + ":" + bundle.getVersion()); - } - try { - framework.stop(); - } catch (BundleException e) { - // silent - } - } -} diff --git a/org.argeo.osgi.boot/ext/test/org/argeo/osgi/boot/OsgiBootNoRuntimeTest.java b/org.argeo.osgi.boot/ext/test/org/argeo/osgi/boot/OsgiBootNoRuntimeTest.java deleted file mode 100644 index 6870a2629..000000000 --- a/org.argeo.osgi.boot/ext/test/org/argeo/osgi/boot/OsgiBootNoRuntimeTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.argeo.osgi.boot; - -import java.util.List; - -import junit.framework.TestCase; - -/** Tests which do not require a runtime. */ -@SuppressWarnings("rawtypes") -public class OsgiBootNoRuntimeTest extends TestCase { - public final static String BUNDLES = "src/test/bundles/some;in=*;ex=excluded," - + "src/test/bundles/others;in=**/org.argeo.*"; - - /** Tests that location lists are properly parsed. */ - // public void testLocations() { - // String baseUrl = "file:"; - // String locations = "/mydir/myfile" + File.pathSeparator - // + "/myotherdir/myotherfile"; - // - // OsgiBoot osgiBoot = new OsgiBoot(null); - // osgiBoot.setExcludeSvn(true); - // List urls = osgiBoot.getLocationsUrls(baseUrl, locations); - // assertEquals(2, urls.size()); - // assertEquals("file:/mydir/myfile", urls.get(0)); - // assertEquals("file:/myotherdir/myotherfile", urls.get(1)); - // } - - /** Tests that bundle lists are properly parsed. */ - public void testBundles() { - String baseUrl = "file:"; - String bundles = BUNDLES; - OsgiBoot osgiBoot = new OsgiBoot(null); -// osgiBoot.setExcludeSvn(true); - List urls = osgiBoot.getBundlesUrls(baseUrl, bundles); - for (int i = 0; i < urls.size(); i++) - System.out.println(urls.get(i)); - assertEquals(3, urls.size()); - - List jarUrls = osgiBoot.getBundlesUrls(baseUrl, - "src/test/bundles/jars;in=*.jar"); - for (int i = 0; i < jarUrls.size(); i++) - System.out.println(jarUrls.get(i)); - assertEquals(1, jarUrls.size()); - } -} diff --git a/org.argeo.osgi.boot/ext/test/org/argeo/osgi/boot/OsgiBootRuntimeTest.java b/org.argeo.osgi.boot/ext/test/org/argeo/osgi/boot/OsgiBootRuntimeTest.java deleted file mode 100644 index 25404bad6..000000000 --- a/org.argeo.osgi.boot/ext/test/org/argeo/osgi/boot/OsgiBootRuntimeTest.java +++ /dev/null @@ -1,84 +0,0 @@ -package org.argeo.osgi.boot; - -import java.util.Iterator; -import java.util.Map; -import java.util.TreeMap; - -import junit.framework.TestCase; - -import org.eclipse.core.runtime.adaptor.EclipseStarter; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; - -/** Starts an Equinox runtime and provision it with OSGi boot. */ -public class OsgiBootRuntimeTest extends TestCase { - protected OsgiBoot osgiBoot = null; - private boolean osgiRuntimeAlreadyRunning = false; - - public void testInstallAndStart() throws Exception { - if (osgiRuntimeAlreadyRunning) { - System.out - .println("OSGi runtime already running, skipping test..."); - return; - } - osgiBoot.installUrls(osgiBoot.getBundlesUrls(OsgiBoot.DEFAULT_BASE_URL, - OsgiBootNoRuntimeTest.BUNDLES)); - Map map = new TreeMap( - osgiBoot.getBundlesBySymbolicName()); - for (Iterator keys = map.keySet().iterator(); keys.hasNext();) { - String key = keys.next(); - Bundle bundle = map.get(key); - System.out.println(key + " : " + bundle.getLocation()); - } - assertEquals(4, map.size()); - Iterator keys = map.keySet().iterator(); - assertEquals("org.argeo.osgi.boot.test.bundle1", keys.next()); - assertEquals("org.argeo.osgi.boot.test.bundle2", keys.next()); - assertEquals("org.argeo.osgi.boot.test.bundle3", keys.next()); - assertEquals("org.eclipse.osgi", keys.next()); - - // osgiBoot.startBundles("org.argeo.osgi.boot.test.bundle2"); - long begin = System.currentTimeMillis(); - while (System.currentTimeMillis() - begin < 10000) { - Map mapBundles = osgiBoot - .getBundlesBySymbolicName(); - Bundle bundle = mapBundles.get("org.argeo.osgi.boot.test.bundle2"); - if (bundle.getState() == Bundle.ACTIVE) { - System.out.println("Bundle " + bundle + " started."); - return; - } - } - fail("Bundle not started after timeout limit."); - } - - protected BundleContext startRuntime() throws Exception { - String[] args = { "-console", "-clean" }; - BundleContext bundleContext = EclipseStarter.startup(args, null); - -// ServiceLoader ff = ServiceLoader.load(FrameworkFactory.class); -// Map config = new HashMap(); -// Framework fwk = ff.iterator().next().newFramework(config); -// fwk.start(); - return bundleContext; - } - - protected void stopRuntime() throws Exception { - EclipseStarter.shutdown(); - } - - public void setUp() throws Exception { - osgiRuntimeAlreadyRunning = EclipseStarter.isRunning(); - if (osgiRuntimeAlreadyRunning) - return; - BundleContext bundleContext = startRuntime(); - osgiBoot = new OsgiBoot(bundleContext); - } - - public void tearDown() throws Exception { - if (osgiRuntimeAlreadyRunning) - return; - osgiBoot = null; - stopRuntime(); - } - -}