Search in sources :

Example 6 with ResourceIndexer

use of org.osgi.service.indexer.ResourceIndexer in project bnd by bndtools.

the class TestOSGiServices method testWhiteboardAnalyzer.

// Test whiteboard registration of Resource Analyzers.
public void testWhiteboardAnalyzer() throws Exception {
    ServiceRegistration<ResourceAnalyzer> reg = context.registerService(ResourceAnalyzer.class, new WibbleAnalyzer(), null);
    ServiceReference<ResourceIndexer> ref = context.getServiceReference(ResourceIndexer.class);
    ResourceIndexer indexer = context.getService(ref);
    StringWriter writer = new StringWriter();
    Map<String, String> config = new HashMap<String, String>();
    config.put(ResourceIndexer.ROOT_URL, tempDir.getAbsoluteFile().toURI().toString());
    indexer.indexFragment(Collections.singleton(copyToTempFile(tempDir, "testdata/01-bsn+version.jar")), writer, config);
    assertEquals(readStream(TestOSGiServices.class.getResourceAsStream("/testdata/fragment-wibble.txt")), writer.toString().trim());
    context.ungetService(ref);
    reg.unregister();
}
Also used : WibbleAnalyzer(org.example.tests.utils.WibbleAnalyzer) ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) ResourceIndexer(org.osgi.service.indexer.ResourceIndexer)

Example 7 with ResourceIndexer

use of org.osgi.service.indexer.ResourceIndexer in project bnd by bndtools.

the class TestOSGiServices method testBasicServiceInvocation.

public void testBasicServiceInvocation() throws Exception {
    ServiceReference<ResourceIndexer> ref = context.getServiceReference(ResourceIndexer.class);
    ResourceIndexer indexer = context.getService(ref);
    StringWriter writer = new StringWriter();
    Map<String, String> config = new HashMap<String, String>();
    config.put(ResourceIndexer.ROOT_URL, tempDir.getAbsoluteFile().toURI().toString());
    indexer.indexFragment(Collections.singleton(copyToTempFile(tempDir, "testdata/01-bsn+version.jar")), writer, config);
    assertEquals(readStream(TestOSGiServices.class.getResourceAsStream("/testdata/fragment-basic.txt")), writer.toString().trim());
    context.ungetService(ref);
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) ResourceIndexer(org.osgi.service.indexer.ResourceIndexer)

Example 8 with ResourceIndexer

use of org.osgi.service.indexer.ResourceIndexer in project bnd by bndtools.

the class TestOSGiServices method testLogNotification.

// Test that exceptions thrown by analyzers are forwarded to the OSGi Log
// Service
public void testLogNotification() throws Exception {
    // Register mock LogService, to receive notifications
    LogService mockLog = mock(LogService.class);
    ServiceRegistration<LogService> mockLogReg = context.registerService(LogService.class, mockLog, null);
    // Register a broken analyzer that throws exceptions
    ResourceAnalyzer brokenAnalyzer = new ResourceAnalyzer() {

        public void analyzeResource(Resource resource, List<Capability> capabilities, List<Requirement> requirements) throws Exception {
            throw new Exception("Bang!");
        }
    };
    ServiceRegistration<ResourceAnalyzer> mockAnalyzerReg = context.registerService(ResourceAnalyzer.class, brokenAnalyzer, null);
    // Call the indexer
    ServiceReference<ResourceIndexer> ref = context.getServiceReference(ResourceIndexer.class);
    ResourceIndexer indexer = context.getService(ref);
    StringWriter writer = new StringWriter();
    Set<File> files = Collections.singleton(copyToTempFile(tempDir, "testdata/01-bsn+version.jar"));
    Map<String, String> config = new HashMap<String, String>();
    config.put(ResourceIndexer.ROOT_URL, tempDir.getAbsoluteFile().toURI().toString());
    indexer.indexFragment(files, writer, config);
    // Verify log output
    ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
    verify(mockLog).log(any(ServiceReference.class), eq(LogService.LOG_ERROR), anyString(), exceptionCaptor.capture());
    assertEquals("Bang!", exceptionCaptor.getValue().getMessage());
    mockAnalyzerReg.unregister();
    mockLogReg.unregister();
}
Also used : ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) HashMap(java.util.HashMap) Resource(org.osgi.service.indexer.Resource) Matchers.anyString(org.mockito.Matchers.anyString) Utils.deleteWithException(org.example.tests.utils.Utils.deleteWithException) ResourceIndexer(org.osgi.service.indexer.ResourceIndexer) ServiceReference(org.osgi.framework.ServiceReference) StringWriter(java.io.StringWriter) List(java.util.List) Utils.copyToTempFile(org.example.tests.utils.Utils.copyToTempFile) File(java.io.File) LogService(org.osgi.service.log.LogService)

Example 9 with ResourceIndexer

use of org.osgi.service.indexer.ResourceIndexer in project bnd by bndtools.

the class TestStandaloneLibrary method testBasicServiceInvocation.

public void testBasicServiceInvocation() throws Exception {
    ResourceIndexer indexer = new RepoIndex();
    StringWriter writer = new StringWriter();
    File tempDir = createTempDir();
    File tempFile = copyToTempFile(tempDir, "testdata/01-bsn+version.jar");
    Map<String, String> config = new HashMap<String, String>();
    config.put(ResourceIndexer.ROOT_URL, tempDir.getAbsoluteFile().toURI().toString());
    indexer.indexFragment(Collections.singleton(tempFile), writer, config);
    assertEquals(readStream(TestStandaloneLibrary.class.getResourceAsStream("/testdata/fragment-basic.txt")), writer.toString().trim());
    deleteWithException(tempDir);
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) File(java.io.File) Utils.copyToTempFile(org.example.tests.utils.Utils.copyToTempFile) ResourceIndexer(org.osgi.service.indexer.ResourceIndexer) RepoIndex(org.osgi.service.indexer.impl.RepoIndex)

Example 10 with ResourceIndexer

use of org.osgi.service.indexer.ResourceIndexer in project bnd by bndtools.

the class TestStandaloneLibrary method assertIndexPath.

private void assertIndexPath(String root, String file, String dest) throws Exception {
    File tempDir = IO.getFile(root);
    tempDir.mkdirs();
    final List<String> errors = new ArrayList<>();
    final List<String> warnings = new ArrayList<>();
    File target = IO.getFile(tempDir, dest);
    target.getParentFile().mkdirs();
    try {
        IO.copy(IO.getFile("src/testdata/01-bsn+version.jar"), target);
        ResourceIndexer indexer = new RepoIndex(new LogService() {

            @Override
            public void log(ServiceReference sr, int level, String message, Throwable exception) {
                log(level, message + " " + exception);
            }

            @Override
            public void log(ServiceReference sr, int level, String message) {
                log(level, message);
            }

            @Override
            public void log(int level, String message, Throwable exception) {
                log(level, message + " " + exception);
            }

            @Override
            public void log(int level, String message) {
                switch(level) {
                    case LogService.LOG_ERROR:
                        errors.add(message);
                        break;
                    case LogService.LOG_WARNING:
                        warnings.add(message);
                        break;
                    default:
                        break;
                }
            }
        });
        StringWriter writer = new StringWriter();
        String osRootPath = root.replace('/', File.separatorChar);
        String rootURI = new File(osRootPath).toURI().toString();
        File osFile = new File(file.replace('/', File.separatorChar));
        assertTrue(osFile + " does not exist", osFile.isFile());
        Map<String, String> config = new HashMap<String, String>();
        config.put(ResourceIndexer.ROOT_URL, rootURI);
        indexer.indexFragment(Collections.singleton(osFile), writer, config);
        assertEquals(0, errors.size());
        assertEquals(0, warnings.size());
    } finally {
        deleteWithException(tempDir);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResourceIndexer(org.osgi.service.indexer.ResourceIndexer) ServiceReference(org.osgi.framework.ServiceReference) StringWriter(java.io.StringWriter) File(java.io.File) Utils.copyToTempFile(org.example.tests.utils.Utils.copyToTempFile) RepoIndex(org.osgi.service.indexer.impl.RepoIndex) LogService(org.osgi.service.log.LogService)

Aggregations

ResourceIndexer (org.osgi.service.indexer.ResourceIndexer)11 HashMap (java.util.HashMap)8 File (java.io.File)6 StringWriter (java.io.StringWriter)6 Utils.copyToTempFile (org.example.tests.utils.Utils.copyToTempFile)4 Matchers.anyString (org.mockito.Matchers.anyString)4 OutputStream (java.io.OutputStream)3 LinkedHashSet (java.util.LinkedHashSet)3 ResourceAnalyzer (org.osgi.service.indexer.ResourceAnalyzer)3 PojoServiceRegistryFactoryImpl (de.kalpatec.pojosr.framework.PojoServiceRegistryFactoryImpl)2 ClasspathScanner (de.kalpatec.pojosr.framework.launch.ClasspathScanner)2 IOException (java.io.IOException)2 WibbleAnalyzer (org.example.tests.utils.WibbleAnalyzer)2 ServiceReference (org.osgi.framework.ServiceReference)2 Framework (org.osgi.framework.launch.Framework)2 RepoIndex (org.osgi.service.indexer.impl.RepoIndex)2 LogService (org.osgi.service.log.LogService)2 ServiceTracker (org.osgi.util.tracker.ServiceTracker)2 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1