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.util;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertNull;
19  import static org.junit.Assert.assertTrue;
20  
21  import java.lang.reflect.Method;
22  import java.nio.charset.Charset;
23  import java.nio.charset.UnsupportedCharsetException;
24  
25  import org.junit.After;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import ch.qos.logback.core.Context;
30  import ch.qos.logback.core.ContextBase;
31  import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
32  import ch.qos.logback.core.joran.util.beans.BeanDescriptionCache;
33  import ch.qos.logback.core.spi.FilterReply;
34  import ch.qos.logback.core.testUtil.StatusChecker;
35  import ch.qos.logback.core.util.AggregationType;
36  import ch.qos.logback.core.util.StatusPrinter;
37  
38  public class PropertySetterTest {
39  
40      DefaultNestedComponentRegistry defaultComponentRegistry = new DefaultNestedComponentRegistry();
41  
42      Context context = new ContextBase();
43      StatusChecker checker = new StatusChecker(context);
44      House house = new House();
45      
46      PropertySetter setter = new PropertySetter(new BeanDescriptionCache(context), house);
47  
48      @Before
49      public void setUp() {
50          setter.setContext(context);
51      }
52  
53      @After
54      public void tearDown() {
55      }
56  
57      @Test
58      public void testCanAggregateComponent() {
59          assertEquals(AggregationType.AS_COMPLEX_PROPERTY, setter.computeAggregationType("door"));
60  
61          assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("count"));
62          assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("Count"));
63  
64          assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("name"));
65          assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("Name"));
66  
67          assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("Duration"));
68          assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("fs"));
69  
70          assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("open"));
71          assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("Open"));
72  
73          assertEquals(AggregationType.AS_COMPLEX_PROPERTY_COLLECTION, setter.computeAggregationType("Window"));
74          assertEquals(AggregationType.AS_BASIC_PROPERTY_COLLECTION, setter.computeAggregationType("adjective"));
75  
76          assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("filterReply"));
77          assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("houseColor"));
78      }
79  
80      @Test
81      public void testSetProperty() {
82          {
83              House house = new House();
84              PropertySetter setter = new PropertySetter(new BeanDescriptionCache(context), house);
85              setter.setProperty("count", "10");
86              setter.setProperty("temperature", "33.1");
87  
88              setter.setProperty("name", "jack");
89              setter.setProperty("open", "true");
90  
91              assertEquals(10, house.getCount());
92              assertEquals(33.1d, (double) house.getTemperature(), 0.01);
93              assertEquals("jack", house.getName());
94              assertTrue(house.isOpen());
95          }
96  
97          {
98              House house = new House();
99              PropertySetter setter = new PropertySetter(new BeanDescriptionCache(context), house);
100             setter.setProperty("Count", "10");
101             setter.setProperty("Name", "jack");
102             setter.setProperty("Open", "true");
103 
104             assertEquals(10, house.getCount());
105             assertEquals("jack", house.getName());
106             assertTrue(house.isOpen());
107         }
108     }
109 
110     @Test
111     public void testSetCamelProperty() {
112         setter.setProperty("camelCase", "trot");
113         assertEquals("trot", house.getCamelCase());
114 
115         setter.setProperty("camelCase", "gh");
116         assertEquals("gh", house.getCamelCase());
117     }
118 
119     @Test
120     public void testSetComplexProperty() {
121         Door door = new Door();
122         setter.setComplexProperty("door", door);
123         assertEquals(door, house.getDoor());
124     }
125 
126     @Test
127     public void testgetClassNameViaImplicitRules() {
128         Class<?> compClass = setter.getClassNameViaImplicitRules("door", AggregationType.AS_COMPLEX_PROPERTY, defaultComponentRegistry);
129         assertEquals(Door.class, compClass);
130     }
131 
132     @Test
133     public void testgetComplexPropertyColleClassNameViaImplicitRules() {
134         Class<?> compClass = setter.getClassNameViaImplicitRules("window", AggregationType.AS_COMPLEX_PROPERTY_COLLECTION, defaultComponentRegistry);
135         assertEquals(Window.class, compClass);
136     }
137 
138     @Test
139     public void testPropertyCollection() {
140         setter.addBasicProperty("adjective", "nice");
141         setter.addBasicProperty("adjective", "big");
142 
143         assertEquals(2, house.adjectiveList.size());
144         assertEquals("nice", house.adjectiveList.get(0));
145         assertEquals("big", house.adjectiveList.get(1));
146     }
147 
148     @Test
149     public void testComplexCollection() {
150         Window w1 = new Window();
151         w1.handle = 10;
152         Window w2 = new Window();
153         w2.handle = 20;
154 
155         setter.addComplexProperty("window", w1);
156         setter.addComplexProperty("window", w2);
157         assertEquals(2, house.windowList.size());
158         assertEquals(10, house.windowList.get(0).handle);
159         assertEquals(20, house.windowList.get(1).handle);
160     }
161 
162     @Test
163     public void testSetComplexWithCamelCaseName() {
164         SwimmingPool pool = new SwimmingPoolImpl();
165         setter.setComplexProperty("swimmingPool", pool);
166         assertEquals(pool, house.getSwimmingPool());
167     }
168 
169     @Test
170     public void testDuration() {
171         setter.setProperty("duration", "1.4 seconds");
172         assertEquals(1400, house.getDuration().getMilliseconds());
173     }
174 
175     @Test
176     public void testFileSize() {
177         setter.setProperty("fs", "2 kb");
178         assertEquals(2 * 1024, house.getFs().getSize());
179     }
180 
181     @Test
182     public void testFilterReply() {
183         // test case reproducing bug #52
184         setter.setProperty("filterReply", "ACCEPT");
185         assertEquals(FilterReply.ACCEPT, house.getFilterReply());
186     }
187 
188     @Test
189     public void testEnum() {
190         setter.setProperty("houseColor", "BLUE");
191         assertEquals(HouseColor.BLUE, house.getHouseColor());
192     }
193 
194     @Test
195     public void testDefaultClassAnnonation() {
196         Method relevantMethod = setter.getRelevantMethod("SwimmingPool", AggregationType.AS_COMPLEX_PROPERTY);
197         assertNotNull(relevantMethod);
198         Class<?> spClass = setter.getDefaultClassNameByAnnonation("SwimmingPool", relevantMethod);
199         assertEquals(SwimmingPoolImpl.class, spClass);
200 
201         Class<?> classViaImplicitRules = setter.getClassNameViaImplicitRules("SwimmingPool", AggregationType.AS_COMPLEX_PROPERTY, defaultComponentRegistry);
202         assertEquals(SwimmingPoolImpl.class, classViaImplicitRules);
203     }
204 
205     @Test
206     public void testDefaultClassAnnotationForLists() {
207         Method relevantMethod = setter.getRelevantMethod("LargeSwimmingPool", AggregationType.AS_COMPLEX_PROPERTY_COLLECTION);
208         assertNotNull(relevantMethod);
209         Class<?> spClass = setter.getDefaultClassNameByAnnonation("LargeSwimmingPool", relevantMethod);
210         assertEquals(LargeSwimmingPoolImpl.class, spClass);
211 
212         Class<?> classViaImplicitRules = setter.getClassNameViaImplicitRules("LargeSwimmingPool", AggregationType.AS_COMPLEX_PROPERTY_COLLECTION,
213                         defaultComponentRegistry);
214         assertEquals(LargeSwimmingPoolImpl.class, classViaImplicitRules);
215     }
216 
217     @Test
218     public void charset() {
219         setter.setProperty("charset", "UTF-8");
220         assertEquals(Charset.forName("UTF-8"), house.getCharset());
221 
222         house.setCharset(null);
223         setter.setProperty("charset", "UTF");
224         assertNull(house.getCharset());
225 
226         StatusChecker checker = new StatusChecker(context);
227         checker.containsException(UnsupportedCharsetException.class);
228     }
229 
230     // see also http://jira.qos.ch/browse/LOGBACK-1164
231     @Test
232     public void bridgeMethodsShouldBeIgnored() {
233         Orange orange = new Orange();
234         
235         PropertySetter orangeSetter = new PropertySetter(new BeanDescriptionCache(context), orange);
236         assertEquals(AggregationType.AS_BASIC_PROPERTY, orangeSetter.computeAggregationType(Citrus.PRECARP_PROPERTY_NAME));
237         assertEquals(AggregationType.AS_BASIC_PROPERTY, orangeSetter.computeAggregationType(Citrus.PREFIX_PROPERTY_NAME));
238         
239         StatusPrinter.print(context);
240         checker.assertIsWarningOrErrorFree();
241     }
242 }