Logger extends ServiceTracker implements LogService { public void log(int level, String message) { LogService logService = (LogService) getService(); if (logService != null) { logService.log(level, message); } } Logger(BundleContext context); void log(int level, String message); void log(int level, String message, Throwable exception); void log(ServiceReference ref, int level, String message); void log(ServiceReference ref, int level, String message, Throwable exception);  }
@Test public void testLogger2() { Logger logger = new Logger(ctx); logger.open(); TestLogService tls = new TestLogService(); ctx.registerService(LogService.class.getName(), tls, null); Assert.assertEquals("Precondition", 0, tls.logData.size()); Throwable th = new Throwable(); logger.log(LogService.LOG_INFO, "Hi", th); Assert.assertEquals(1, tls.logData.size()); LogData ld = tls.logData.get(0); Assert.assertEquals(LogService.LOG_INFO, ld.level); Assert.assertEquals("Hi", ld.message); Assert.assertEquals(th, ld.throwable); }
@Test public void testLogger3() { Logger logger = new Logger(ctx); logger.open(); TestLogService tls = new TestLogService(); ctx.registerService(LogService.class.getName(), tls, null); Assert.assertEquals("Precondition", 0, tls.logData.size()); ServiceReference sr = new MockServiceReference(); logger.log(sr, LogService.LOG_INFO, "Hi"); Assert.assertEquals(1, tls.logData.size()); LogData ld = tls.logData.get(0); Assert.assertSame(sr, ld.serviceReference); Assert.assertEquals(LogService.LOG_INFO, ld.level); Assert.assertEquals("Hi", ld.message); }
@Test public void testLogger4() { Logger logger = new Logger(ctx); logger.open(); TestLogService tls = new TestLogService(); ctx.registerService(LogService.class.getName(), tls, null); Assert.assertEquals("Precondition", 0, tls.logData.size()); ServiceReference sr = new MockServiceReference(); Throwable th = new Throwable(); logger.log(sr, LogService.LOG_INFO, "Hi", th); Assert.assertEquals(1, tls.logData.size()); LogData ld = tls.logData.get(0); Assert.assertSame(sr, ld.serviceReference); Assert.assertEquals(LogService.LOG_INFO, ld.level); Assert.assertEquals("Hi", ld.message); Assert.assertEquals(th, ld.throwable); }