View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.joran.action;
15  
16  import java.util.HashMap;
17  import java.util.function.Supplier;
18  
19  import org.junit.jupiter.api.AfterEach;
20  import org.junit.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.Disabled;
22  import org.junit.jupiter.api.Test;
23  
24  import ch.qos.logback.core.Context;
25  import ch.qos.logback.core.ContextBase;
26  import ch.qos.logback.core.joran.SimpleConfigurator;
27  import ch.qos.logback.core.joran.spi.ElementSelector;
28  import ch.qos.logback.core.joran.spi.JoranException;
29  import ch.qos.logback.core.model.DefineModel;
30  import ch.qos.logback.core.model.ImplicitModel;
31  import ch.qos.logback.core.model.TopModel;
32  import ch.qos.logback.core.model.processor.DefaultProcessor;
33  import ch.qos.logback.core.model.processor.DefineModelHandler;
34  import ch.qos.logback.core.model.processor.ImplicitModelHandler;
35  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
36  import ch.qos.logback.core.model.processor.NOPModelHandler;
37  import ch.qos.logback.core.status.Status;
38  import ch.qos.logback.core.testUtil.CoreTestConstants;
39  import ch.qos.logback.core.status.testUtil.StatusChecker;
40  import ch.qos.logback.core.util.StatusPrinter;
41  
42  import static org.junit.jupiter.api.Assertions.assertEquals;
43  import static org.junit.jupiter.api.Assertions.assertNotNull;
44  import static org.junit.jupiter.api.Assertions.assertNull;
45  
46  /**
47   * Test {@link DefinePropertyAction}.
48   * 
49   * @author Aleksey Didik
50   */
51  public class DefinePropertyActionTest {
52  
53      private static final String DEFINE_INPUT_DIR = CoreTestConstants.JORAN_INPUT_PREFIX + "define/";
54      private static final String GOOD_XML = "good.xml";
55      private static final String NONAME_XML = "noname.xml";
56      private static final String NOCLASS_XML = "noclass.xml";
57      private static final String BADCLASS_XML = "badclass.xml";
58  
59      SimpleConfigurator simpleConfigurator;
60      Context context = new ContextBase();
61      StatusChecker checker = new StatusChecker(context);
62  
63      @BeforeEach
64      public void setUp() throws Exception {
65  
66          HashMap<ElementSelector, Supplier<Action>> rulesMap = new HashMap<>();
67          rulesMap.put(new ElementSelector("top"), TopElementAction::new);
68          rulesMap.put(new ElementSelector("top/define"), DefinePropertyAction::new);
69  
70          simpleConfigurator = new SimpleConfigurator(rulesMap) {
71              
72              @Override
73              protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
74                  defaultProcessor.addHandler(TopModel.class, NOPModelHandler::makeInstance);
75                  defaultProcessor.addHandler(DefineModel.class, DefineModelHandler::makeInstance);
76                  defaultProcessor.addHandler(ImplicitModel.class, ImplicitModelHandler::makeInstance);
77              }
78          };
79          simpleConfigurator.setContext(context);
80      }
81  
82      @AfterEach
83      public void tearDown() throws Exception {
84      }
85  
86      @Test
87      public void good() throws JoranException {
88          simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + GOOD_XML);
89          ModelInterpretationContext mic = simpleConfigurator.getModelInterpretationContext();
90          String inContextFoo = mic.getProperty("foo");
91          assertEquals("monster", inContextFoo);
92      }
93  
94      @Test
95      public void noName() throws JoranException {
96          try {
97              simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + NONAME_XML);
98          } finally {
99              StatusPrinter.print(context);
100         }
101         // get from context
102         String inContextFoo = context.getProperty("foo");
103         assertNull(inContextFoo);
104         // check context errors
105 
106         checker.assertContainsMatch(Status.ERROR, "Missing attribute \\[name\\] in element \\[define\\]");
107     }
108 
109     @Test
110     public void noClass() throws JoranException {
111         simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + NOCLASS_XML);
112         String inContextFoo = context.getProperty("foo");
113 
114         StatusPrinter.print(context);
115         assertNull(inContextFoo);
116         checker.assertContainsMatch(Status.ERROR, "Missing attribute \\[class\\] in element \\[define\\]");
117     }
118 
119     @Test
120     public void testBadClass() throws JoranException {
121         simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + BADCLASS_XML);
122         // get from context
123         String inContextFoo = context.getProperty("foo");
124         assertNull(inContextFoo);
125         // check context errors
126         checker.assertContainsMatch(Status.ERROR, "Could not create an PropertyDefiner of type");
127     }
128 
129     @Disabled // on certain hosts this test takes 5 seconds to complete
130     @Test
131     public void canonicalHostNameProperty() throws JoranException {
132         String configFileAsStr = DEFINE_INPUT_DIR + "canonicalHostname.xml";
133         simpleConfigurator.doConfigure(configFileAsStr);
134         assertNotNull(context.getProperty("CANONICAL_HOST_NAME"));
135     }
136 
137 }