001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.joran.util;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertNotNull;
018import static org.junit.Assert.assertNull;
019import static org.junit.Assert.assertTrue;
020
021import java.lang.reflect.Method;
022import java.nio.charset.Charset;
023import java.nio.charset.UnsupportedCharsetException;
024
025import org.junit.After;
026import org.junit.Before;
027import org.junit.Test;
028
029import ch.qos.logback.core.Context;
030import ch.qos.logback.core.ContextBase;
031import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
032import ch.qos.logback.core.joran.util.beans.BeanDescriptionCache;
033import ch.qos.logback.core.spi.FilterReply;
034import ch.qos.logback.core.testUtil.StatusChecker;
035import ch.qos.logback.core.util.AggregationType;
036import ch.qos.logback.core.util.StatusPrinter;
037
038public class PropertySetterTest {
039
040    DefaultNestedComponentRegistry defaultComponentRegistry = new DefaultNestedComponentRegistry();
041
042    Context context = new ContextBase();
043    StatusChecker checker = new StatusChecker(context);
044    House house = new House();
045    
046    PropertySetter setter = new PropertySetter(new BeanDescriptionCache(context), house);
047
048    @Before
049    public void setUp() {
050        setter.setContext(context);
051    }
052
053    @After
054    public void tearDown() {
055    }
056
057    @Test
058    public void testCanAggregateComponent() {
059        assertEquals(AggregationType.AS_COMPLEX_PROPERTY, setter.computeAggregationType("door"));
060
061        assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("count"));
062        assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("Count"));
063
064        assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("name"));
065        assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("Name"));
066
067        assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("Duration"));
068        assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("fs"));
069
070        assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("open"));
071        assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("Open"));
072
073        assertEquals(AggregationType.AS_COMPLEX_PROPERTY_COLLECTION, setter.computeAggregationType("Window"));
074        assertEquals(AggregationType.AS_BASIC_PROPERTY_COLLECTION, setter.computeAggregationType("adjective"));
075
076        assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("filterReply"));
077        assertEquals(AggregationType.AS_BASIC_PROPERTY, setter.computeAggregationType("houseColor"));
078    }
079
080    @Test
081    public void testSetProperty() {
082        {
083            House house = new House();
084            PropertySetter setter = new PropertySetter(new BeanDescriptionCache(context), house);
085            setter.setProperty("count", "10");
086            setter.setProperty("temperature", "33.1");
087
088            setter.setProperty("name", "jack");
089            setter.setProperty("open", "true");
090
091            assertEquals(10, house.getCount());
092            assertEquals(33.1d, (double) house.getTemperature(), 0.01);
093            assertEquals("jack", house.getName());
094            assertTrue(house.isOpen());
095        }
096
097        {
098            House house = new House();
099            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}