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();
}
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);
}
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();
}
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);
}
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);
}
}
Aggregations