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 static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertTrue;
18  
19  import java.util.Iterator;
20  
21  import org.junit.After;
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  import ch.qos.logback.core.Context;
26  import ch.qos.logback.core.ContextBase;
27  import ch.qos.logback.core.joran.spi.InterpretationContext;
28  import ch.qos.logback.core.status.ErrorStatus;
29  import ch.qos.logback.core.status.Status;
30  import ch.qos.logback.core.testUtil.CoreTestConstants;
31  import ch.qos.logback.core.util.StatusPrinter;
32  
33  /**
34   * Test {@link PropertyAction}.
35   * @author Ceki Gülcü
36   */
37  public class PropertyActionTest {
38  
39      Context context;
40      InterpretationContext ec;
41      PropertyAction propertyAction;
42      DummyAttributes atts = new DummyAttributes();
43  
44      @Before
45      public void setUp() throws Exception {
46          context = new ContextBase();
47          ec = new InterpretationContext(context, null);
48          propertyAction = new PropertyAction();
49          propertyAction.setContext(context);
50      }
51  
52      @After
53      public void tearDown() throws Exception {
54          context = null;
55          propertyAction = null;
56          atts = null;
57      }
58  
59      @Test
60      public void nameValuePair() {
61          atts.setValue("name", "v1");
62          atts.setValue("value", "work");
63          propertyAction.begin(ec, null, atts);
64          assertEquals("work", ec.getProperty("v1"));
65      }
66  
67      @Test
68      public void nameValuePairWithPrerequisiteSubsitution() {
69          context.putProperty("w", "wor");
70          atts.setValue("name", "v1");
71          atts.setValue("value", "${w}k");
72          propertyAction.begin(ec, null, atts);
73          assertEquals("work", ec.getProperty("v1"));
74      }
75  
76      @Test
77      public void noValue() {
78          atts.setValue("name", "v1");
79          propertyAction.begin(ec, null, atts);
80          assertEquals(1, context.getStatusManager().getCount());
81          assertTrue(checkError());
82      }
83  
84      @Test
85      public void noName() {
86          atts.setValue("value", "v1");
87          propertyAction.begin(ec, null, atts);
88          assertEquals(1, context.getStatusManager().getCount());
89          assertTrue(checkError());
90      }
91  
92      @Test
93      public void noAttributes() {
94          propertyAction.begin(ec, null, atts);
95          assertEquals(1, context.getStatusManager().getCount());
96          assertTrue(checkError());
97          StatusPrinter.print(context);
98      }
99  
100     @Test
101     public void testFileNotLoaded() {
102         atts.setValue("file", "toto");
103         atts.setValue("value", "work");
104         propertyAction.begin(ec, null, atts);
105         assertEquals(1, context.getStatusManager().getCount());
106         assertTrue(checkError());
107     }
108 
109     @Test
110     public void testLoadFileWithPrerequisiteSubsitution() {
111         context.putProperty("STEM", CoreTestConstants.TEST_SRC_PREFIX + "input/joran");
112         atts.setValue("file", "${STEM}/propertyActionTest.properties");
113         propertyAction.begin(ec, null, atts);
114         assertEquals("tata", ec.getProperty("v1"));
115         assertEquals("toto", ec.getProperty("v2"));
116     }
117 
118     @Test
119     public void testLoadFile() {
120         atts.setValue("file", CoreTestConstants.TEST_SRC_PREFIX + "input/joran/propertyActionTest.properties");
121         propertyAction.begin(ec, null, atts);
122         assertEquals("tata", ec.getProperty("v1"));
123         assertEquals("toto", ec.getProperty("v2"));
124     }
125 
126     @Test
127     public void testLoadResource() {
128         atts.setValue("resource", "asResource/joran/propertyActionTest.properties");
129         propertyAction.begin(ec, null, atts);
130         assertEquals("tata", ec.getProperty("r1"));
131         assertEquals("toto", ec.getProperty("r2"));
132     }
133 
134     @Test
135     public void testLoadResourceWithPrerequisiteSubsitution() {
136         context.putProperty("STEM", "asResource/joran");
137         atts.setValue("resource", "${STEM}/propertyActionTest.properties");
138         propertyAction.begin(ec, null, atts);
139         assertEquals("tata", ec.getProperty("r1"));
140         assertEquals("toto", ec.getProperty("r2"));
141     }
142 
143     @Test
144     public void testLoadNotPossible() {
145         atts.setValue("file", "toto");
146         propertyAction.begin(ec, null, atts);
147         assertEquals(1, context.getStatusManager().getCount());
148         assertTrue(checkFileErrors());
149     }
150 
151     private boolean checkError() {
152         Iterator<Status> it = context.getStatusManager().getCopyOfStatusList().iterator();
153         ErrorStatus es = (ErrorStatus) it.next();
154         return PropertyAction.INVALID_ATTRIBUTES.equals(es.getMessage());
155     }
156 
157     private boolean checkFileErrors() {
158         Iterator<Status> it = context.getStatusManager().getCopyOfStatusList().iterator();
159         ErrorStatus es1 = (ErrorStatus) it.next();
160         return "Could not find properties file [toto].".equals(es1.getMessage());
161     }
162 }