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