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  
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertNull;
20  
21  import java.util.HashMap;
22  
23  import org.junit.After;
24  import org.junit.Before;
25  import org.junit.Ignore;
26  import org.junit.Test;
27  
28  import ch.qos.logback.core.Context;
29  import ch.qos.logback.core.ContextBase;
30  import ch.qos.logback.core.joran.SimpleConfigurator;
31  import ch.qos.logback.core.joran.spi.ElementSelector;
32  import ch.qos.logback.core.joran.spi.InterpretationContext;
33  import ch.qos.logback.core.joran.spi.JoranException;
34  import ch.qos.logback.core.status.Status;
35  import ch.qos.logback.core.testUtil.CoreTestConstants;
36  import ch.qos.logback.core.testUtil.StatusChecker;
37  
38  /**
39   * Test {@link DefinePropertyAction}.
40   * 
41   * @author Aleksey Didik
42   */
43  public class DefinePropertyActionTest {
44  
45      private static final String DEFINE_INPUT_DIR = CoreTestConstants.JORAN_INPUT_PREFIX + "define/";
46      private static final String GOOD_XML = "good.xml";
47      private static final String NONAME_XML = "noname.xml";
48      private static final String NOCLASS_XML = "noclass.xml";
49      private static final String BADCLASS_XML = "badclass.xml";
50  
51      SimpleConfigurator simpleConfigurator;
52      Context context = new ContextBase();
53      DefinePropertyAction definerAction;
54      InterpretationContext ic;
55      StatusChecker checker = new StatusChecker(context);
56  
57      @Before
58      public void setUp() throws Exception {
59  
60          HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>();
61          rulesMap.put(new ElementSelector("define"), new DefinePropertyAction());
62          simpleConfigurator = new SimpleConfigurator(rulesMap);
63          simpleConfigurator.setContext(context);
64      }
65  
66      @After
67      public void tearDown() throws Exception {
68          // StatusPrinter.printInCaseOfErrorsOrWarnings(context);
69      }
70  
71      @Test
72      public void good() throws JoranException {
73          simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + GOOD_XML);
74          InterpretationContext ic = simpleConfigurator.getInterpreter().getInterpretationContext();
75          String inContextFoo = ic.getProperty("foo");
76          assertEquals("monster", inContextFoo);
77      }
78  
79      @Test
80      public void noName() throws JoranException {
81          simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + NONAME_XML);
82          // get from context
83          String inContextFoo = context.getProperty("foo");
84          assertNull(inContextFoo);
85          // check context errors
86          checker.assertContainsMatch(Status.ERROR, "Missing property name for property definer. Near \\[define\\] line 1");
87      }
88  
89      @Test
90      public void noClass() throws JoranException {
91          simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + NOCLASS_XML);
92          String inContextFoo = context.getProperty("foo");
93          assertNull(inContextFoo);
94          checker.assertContainsMatch(Status.ERROR, "Missing class name for property definer. Near \\[define\\] line 1");
95      }
96  
97      @Test
98      public void testBadClass() throws JoranException {
99          simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + BADCLASS_XML);
100         // get from context
101         String inContextFoo = context.getProperty("foo");
102         assertNull(inContextFoo);
103         // check context errors
104         checker.assertContainsMatch(Status.ERROR, "Could not create an PropertyDefiner of type");
105     }
106 
107     @Ignore // on certain hosts this test takes 5 seconds to complete
108     @Test
109     public void canonicalHostNameProperty() throws JoranException {
110         String configFileAsStr = DEFINE_INPUT_DIR + "canonicalHostname.xml";
111         simpleConfigurator.doConfigure(configFileAsStr);
112         assertNotNull(context.getProperty("CANONICAL_HOST_NAME"));
113     }
114 
115 }