Fix resource leaks in test cases
[controller.git] / opendaylight / config / logback-config / src / test / java / org / opendaylight / controller / config / yang / logback / config / ContextSetterImplTest.java
index 7ba7a24..9547112 100644 (file)
@@ -10,13 +10,20 @@ package org.opendaylight.controller.config.yang.logback.config;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.Appender;
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Multimap;
+import java.io.IOException;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
@@ -25,14 +32,6 @@ import org.opendaylight.controller.config.api.DependencyResolver;
 import org.opendaylight.controller.config.api.ModuleIdentifier;
 import org.slf4j.LoggerFactory;
 
-import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.spi.ILoggingEvent;
-import ch.qos.logback.core.Appender;
-
-import com.google.common.collect.HashMultimap;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Multimap;
-
 public class ContextSetterImplTest {
 
     @Mock
@@ -44,8 +43,8 @@ public class ContextSetterImplTest {
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
         LogbackRuntimeRegistration reg = mock(LogbackRuntimeRegistration.class);
-        doReturn(reg).when(runtimeRegistratorMock).register(
-                any(LogbackRuntimeMXBean.class));
+        doReturn(reg).when(runtimeRegistratorMock).register(any(LogbackRuntimeMXBean.class));
+        doNothing().when(reg).close();
     }
 
     @Test
@@ -90,30 +89,28 @@ public class ContextSetterImplTest {
         assertLoggerWithAppenders("l2", "a22");
     }
 
-    private void createContextSetter(Multimap<String, String> loggersToAppenders) {
-        ContextSetterImpl setter = new ContextSetterImpl(runtimeRegistratorMock);
+    private void createContextSetter(Multimap<String, String> loggersToAppenders) throws IOException {
+        try (ContextSetterImpl setter = new ContextSetterImpl(runtimeRegistratorMock)) {
+
+            List<LoggerTO> logger = Lists.newArrayList();
+            List<ConsoleAppenderTO> consoleAppenders = Lists.newArrayList();
 
-        List<LoggerTO> logger = Lists.newArrayList();
-        List<ConsoleAppenderTO> consoleAppenders = Lists.newArrayList();
+            for (String loggerName : loggersToAppenders.keySet()) {
+                LoggerTO l1 = createLogger(loggerName, loggersToAppenders.get(loggerName));
+                logger.add(l1);
+                for (String appenderName : loggersToAppenders.get(loggerName)) {
+                    consoleAppenders.add(createConsoleAppender(appenderName));
+                }
 
-        for (String loggerName : loggersToAppenders.keySet()) {
-            LoggerTO l1 = createLogger(loggerName,
-                    loggersToAppenders.get(loggerName));
-            logger.add(l1);
-            for (String appenderName : loggersToAppenders.get(loggerName)) {
-                consoleAppenders.add(createConsoleAppender(appenderName));
             }
 
+            LogbackModule logbackModule = createLogbackModule(logger, consoleAppenders);
+            setter.updateContext(logbackModule);
         }
-
-        LogbackModule logbackModule = createLogbackModule(logger,
-                consoleAppenders);
-        setter.updateContext(logbackModule);
     }
 
     private void assertLoggerWithAppenders(String name, String... appenders) {
-        LoggerContext context = (LoggerContext) LoggerFactory
-                .getILoggerFactory();
+        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
         ch.qos.logback.classic.Logger logger = context.getLogger(name);
         Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders();
 
@@ -129,22 +126,18 @@ public class ContextSetterImplTest {
 
         for (String appender : appenders) {
             boolean isPresent = foundAppenders.get(appender).isEmpty();
-            assertFalse(
-                    "Appender " + appender + " for logger " + name
-                            + " was not present, present appenders: "
-                            + foundAppenders.keys(), isPresent);
+            assertFalse("Appender " + appender + " for logger " + name + " was not present, present appenders: "
+                    + foundAppenders.keys(), isPresent);
         }
 
     }
 
-    private LogbackModule createLogbackModule(List<LoggerTO> logger,
-            List<ConsoleAppenderTO> consoleAppenders) {
-        LogbackModule logbackModule = new LogbackModule(new ModuleIdentifier(
-                "fact", "first"), dependencyResolverMock);
+    private LogbackModule createLogbackModule(List<LoggerTO> logger, List<ConsoleAppenderTO> consoleAppenders) {
+        LogbackModule logbackModule = new LogbackModule(new ModuleIdentifier("fact", "first"), dependencyResolverMock);
         logbackModule.setLoggerTO(logger);
         logbackModule.setConsoleAppenderTO(consoleAppenders);
-        logbackModule.setRollingFileAppenderTO(Lists
-                .<RollingFileAppenderTO> newArrayList());
+        logbackModule.setRollingFileAppenderTO(Lists.<RollingFileAppenderTO> newArrayList());
+        logbackModule.setFileAppenderTO(Lists.<FileAppenderTO> newArrayList());
         return logbackModule;
     }
 
@@ -162,4 +155,5 @@ public class ContextSetterImplTest {
         a.setEncoderPattern("%-4relative [%thread] %-5level %logger{35} - %msg%n");
         return a;
     }
+
 }