BUG-1051: logback configuration loader proposal
[controller.git] / opendaylight / config / logback-config-loader / src / test / java / org / opendaylight / controller / logback / config / loader / test / LogbackConfigurationLoaderTest.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.logback.config.loader.test;
9
10 import java.io.File;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.junit.Assert;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.junit.runners.JUnit4;
18 import org.opendaylight.controller.logback.config.loader.impl.LogbackConfigUtil;
19 import org.opendaylight.controller.logback.config.loader.impl.LogbackConfigurationLoader;
20 import org.opendaylight.controller.logback.config.loader.test.logwork.Debugger;
21 import org.opendaylight.controller.logback.config.loader.test.logwork.Errorer;
22 import org.opendaylight.controller.logback.config.loader.test.logwork.Informer;
23 import org.opendaylight.controller.logback.config.loader.test.logwork.Tracer;
24 import org.opendaylight.controller.logback.config.loader.test.logwork.Warner;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * test of logging config loader - {@link LogbackConfigurationLoader}
30  */
31 @RunWith(JUnit4.class)
32 public class LogbackConfigurationLoaderTest {
33
34     /** logback config root */
35     private static final String LOGBACK_D = "/logback.d";
36     private static Logger LOG = LoggerFactory
37             .getLogger(LogbackConfigurationLoaderTest.class);
38
39     /**
40      * Test of method {@link LogbackConfigurationLoader#load(boolean, Object[])}
41      *
42      * @throws Exception
43      */
44     @Test
45     public void testLoad() throws Exception {
46         File logConfigRoot = new File(LogbackConfigurationLoaderTest.class
47                 .getResource(LOGBACK_D).getFile());
48         List<File> sortedConfigFiles = LogbackConfigUtil.harvestSortedConfigFiles(logConfigRoot);
49         LogbackConfigurationLoader.load(true, sortedConfigFiles.toArray());
50
51         LOG.info("LOGBACK ready -> about to use it");
52
53         Tracer.doSomeAction();
54         Debugger.doSomeAction();
55         Informer.doSomeAction();
56         Warner.doSomeAction();
57         Errorer.doSomeAction();
58
59         // check logs
60         String[] expectedLogs = new String[] {
61                 "LoggingEvent -> [INFO] org.opendaylight.controller.logback.config.loader.test.LogbackConfigurationLoaderTest: LOGBACK ready -> about to use it",
62                 "LoggingEvent -> [TRACE] org.opendaylight.controller.logback.config.loader.test.logwork.Tracer: tracing",
63                 "LoggingEvent -> [DEBUG] org.opendaylight.controller.logback.config.loader.test.logwork.Tracer: debugging",
64                 "LoggingEvent -> [INFO] org.opendaylight.controller.logback.config.loader.test.logwork.Tracer: infoing",
65                 "LoggingEvent -> [WARN] org.opendaylight.controller.logback.config.loader.test.logwork.Tracer: warning",
66                 "LoggingEvent -> [ERROR] org.opendaylight.controller.logback.config.loader.test.logwork.Tracer: erroring",
67                 "LoggingEvent -> [DEBUG] org.opendaylight.controller.logback.config.loader.test.logwork.Debugger: debugging",
68                 "LoggingEvent -> [INFO] org.opendaylight.controller.logback.config.loader.test.logwork.Debugger: infoing",
69                 "LoggingEvent -> [WARN] org.opendaylight.controller.logback.config.loader.test.logwork.Debugger: warning",
70                 "LoggingEvent -> [ERROR] org.opendaylight.controller.logback.config.loader.test.logwork.Debugger: erroring",
71                 "LoggingEvent -> [INFO] org.opendaylight.controller.logback.config.loader.test.logwork.Informer: infoing",
72                 "LoggingEvent -> [WARN] org.opendaylight.controller.logback.config.loader.test.logwork.Informer: warning",
73                 "LoggingEvent -> [ERROR] org.opendaylight.controller.logback.config.loader.test.logwork.Informer: erroring",
74                 "LoggingEvent -> [WARN] org.opendaylight.controller.logback.config.loader.test.logwork.Warner: warning",
75                 "LoggingEvent -> [ERROR] org.opendaylight.controller.logback.config.loader.test.logwork.Warner: erroring",
76                 "LoggingEvent -> [ERROR] org.opendaylight.controller.logback.config.loader.test.logwork.Errorer: erroring"
77
78         };
79
80         List<String> logSnapshot = new ArrayList<>(TestAppender.getLogRecord());
81         for (String logLine : logSnapshot) {
82             LOG.info("\"{}\",", logLine);
83         }
84
85         Assert.assertArrayEquals(expectedLogs, logSnapshot.toArray());
86     }
87 }