Search in sources :

Example 31 with LogService

use of org.osgi.service.log.LogService in project bnd by bndtools.

the class TestIndexer method testLogErrorsFromAnalyzer.

public void testLogErrorsFromAnalyzer() throws Exception {
    ResourceAnalyzer badAnalyzer = new ResourceAnalyzer() {

        public void analyzeResource(Resource resource, List<Capability> capabilities, List<Requirement> requirements) throws Exception {
            throw new Exception("Bang!");
        }
    };
    ResourceAnalyzer goodAnalyzer = mock(ResourceAnalyzer.class);
    LogService log = mock(LogService.class);
    RepoIndex indexer = new RepoIndex(log);
    indexer.addAnalyzer(badAnalyzer, null);
    indexer.addAnalyzer(goodAnalyzer, null);
    // Run the indexer
    Map<String, String> props = new HashMap<String, String>();
    props.put(ResourceIndexer.ROOT_URL, new File("testdata").getAbsoluteFile().toURI().toURL().toString());
    StringWriter writer = new StringWriter();
    indexer.indexFragment(Collections.singleton(new File("testdata/subdir/01-bsn+version.jar")), writer, props);
    // The "good" analyzer should have been called
    verify(goodAnalyzer).analyzeResource(any(Resource.class), anyListOf(Capability.class), anyListOf(Requirement.class));
    // The log service should have been notified about the exception
    ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
    verify(log).log(eq(LogService.LOG_ERROR), any(String.class), exceptionCaptor.capture());
    assertEquals("Bang!", exceptionCaptor.getValue().getMessage());
}
Also used : ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) Capability(org.osgi.service.indexer.Capability) HashMap(java.util.HashMap) Resource(org.osgi.service.indexer.Resource) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) Requirement(org.osgi.service.indexer.Requirement) StringWriter(java.io.StringWriter) List(java.util.List) File(java.io.File) LogService(org.osgi.service.log.LogService)

Example 32 with LogService

use of org.osgi.service.log.LogService in project bnd by bndtools.

the class TestIndexer method testBundleOutsideRootDirectory.

public void testBundleOutsideRootDirectory() throws Exception {
    LogService log = mock(LogService.class);
    RepoIndex indexer = new RepoIndex(log);
    Map<String, String> props = new HashMap<String, String>();
    props.put(ResourceIndexer.ROOT_URL, new File("testdata/subdir").getAbsoluteFile().toURI().toURL().toString());
    StringWriter writer = new StringWriter();
    indexer.indexFragment(Collections.singleton(new File("testdata/01-bsn+version.jar")), writer, props);
    verify(log).log(eq(LogService.LOG_ERROR), anyString(), isA(IllegalArgumentException.class));
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) File(java.io.File) LogService(org.osgi.service.log.LogService)

Example 33 with LogService

use of org.osgi.service.log.LogService in project bnd by bndtools.

the class TestIndexer method testFragmentPlainJar.

public void testFragmentPlainJar() throws Exception {
    LogService mockLog = Mockito.mock(LogService.class);
    RepoIndex indexer = new RepoIndex(mockLog);
    indexer.addAnalyzer(new KnownBundleAnalyzer(), FrameworkUtil.createFilter("(name=*)"));
    assertFragmentMatch(indexer, "testdata/fragment-plainjar.txt", "testdata/jcip-annotations.jar");
    Mockito.verifyZeroInteractions(mockLog);
}
Also used : LogService(org.osgi.service.log.LogService)

Example 34 with LogService

use of org.osgi.service.log.LogService 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)

Example 35 with LogService

use of org.osgi.service.log.LogService in project sling by apache.

the class OsgiInstallerTestBase method log.

protected void log(int level, String msg) {
    final LogService log = getService(LogService.class);
    log.log(level, msg);
}
Also used : LogService(org.osgi.service.log.LogService)

Aggregations

LogService (org.osgi.service.log.LogService)57 ServiceTracker (org.osgi.util.tracker.ServiceTracker)15 File (java.io.File)10 ServiceReference (org.osgi.framework.ServiceReference)10 AbstractDeploymentPackage (org.apache.felix.deploymentadmin.AbstractDeploymentPackage)7 Bundle (org.osgi.framework.Bundle)7 HashMap (java.util.HashMap)6 PrintStream (java.io.PrintStream)5 StringWriter (java.io.StringWriter)5 Builder (aQute.bnd.osgi.Builder)4 Jar (aQute.bnd.osgi.Jar)4 Resource (aQute.bnd.osgi.Resource)4 XmlTester (aQute.bnd.test.XmlTester)4 Attributes (java.util.jar.Attributes)4 BundleInfoImpl (org.apache.felix.deploymentadmin.BundleInfoImpl)4 Before (org.junit.Before)4 BundleContext (org.osgi.framework.BundleContext)4 IOException (java.io.IOException)3 Date (java.util.Date)3 Matchers.anyString (org.mockito.Matchers.anyString)3