View Javadoc
1   /*
2    * Copyright 2018 SPF4J.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.spf4j.test.log;
17  
18  import com.google.common.collect.ImmutableList;
19  import com.google.common.collect.ImmutableMap;
20  import java.util.Collections;
21  import java.util.List;
22  import org.junit.Assert;
23  import org.junit.Test;
24  import org.spf4j.log.Level;
25  
26  /**
27   * @author Zoltan Farkas
28   */
29  public class LogConfigImplTest {
30  
31  
32    @Test
33    public void testLogConfigImpl() {
34      LogPrinter h1 = new LogPrinter(Level.INFO);
35      LogPrinter h2 = new LogPrinter(Level.DEBUG);
36      LogPrinter h3 = new LogPrinter(Level.DEBUG);
37      LogPrinter h4 = new LogPrinter(Level.ERROR);
38      LogPrinter h5 = new LogPrinter(Level.DEBUG);
39      LogConfigImpl cfg = new LogConfigImpl(ImmutableList.of(h1),
40              ImmutableMap.of("a.b", Collections.singletonList(h2),
41              "a.c", Collections.singletonList(h3),
42              "c.d", Collections.singletonList(h4)));
43      LogConsumer handler = cfg.getLogConsumer("a", Level.DEBUG);
44      Assert.assertNull(handler);
45      List<LogHandler> handlers = ((CompositeLogHandler) cfg.getLogConsumer("a", Level.INFO)).logHandlers;
46      Assert.assertSame(h1, handlers.get(0));
47      handlers = ((CompositeLogHandler) cfg.getLogConsumer("a.b", Level.INFO)).logHandlers;
48      Assert.assertSame(h2, handlers.get(0));
49      Assert.assertSame(h1, handlers.get(1));
50      cfg = (LogConfigImpl) cfg.add("a", h5);
51      handlers = ((CompositeLogHandler) cfg.getLogConsumer("a.b", Level.INFO)).logHandlers;
52      Assert.assertSame(h2, handlers.get(0));
53      Assert.assertSame(h5, handlers.get(1));
54      Assert.assertSame(h1, handlers.get(2));
55      cfg = cfg.remove("a", h5);
56      handlers = ((CompositeLogHandler) cfg.getLogConsumer("a.b", Level.INFO)).logHandlers;
57      Assert.assertSame(h2, handlers.get(0));
58      Assert.assertSame(h1, handlers.get(1));
59   }
60  
61  }