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.implicitAction;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertNotNull;
018
019import java.util.HashMap;
020import java.util.List;
021
022import org.junit.Before;
023import org.junit.Test;
024
025import ch.qos.logback.core.joran.SimpleConfigurator;
026import ch.qos.logback.core.joran.action.Action;
027import ch.qos.logback.core.joran.action.StatusListenerAction;
028import ch.qos.logback.core.joran.spi.ElementSelector;
029import ch.qos.logback.core.testUtil.CoreTestConstants;
030import ch.qos.logback.core.testUtil.StatusChecker;
031import ch.qos.logback.core.util.StatusPrinter;
032
033public class ImplicitActionTest {
034
035    static final String IMPLCIT_DIR = CoreTestConstants.TEST_SRC_PREFIX + "input/joran/implicitAction/";
036
037    FruitContext fruitContext = new FruitContext();
038    SimpleConfigurator simpleConfigurator;
039    StatusChecker checker = new StatusChecker(fruitContext);
040    
041    @Before
042    public void setUp() throws Exception {
043        fruitContext.setName("fruits");
044        HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>();
045        rulesMap.put(new ElementSelector("/context/"), new FruitContextAction());
046        rulesMap.put(new ElementSelector("/context/statusListener"), new StatusListenerAction());
047        simpleConfigurator = new SimpleConfigurator(rulesMap);
048        simpleConfigurator.setContext(fruitContext);
049    }
050
051    void verifyFruit() {
052        List<Fruit> fList = fruitContext.getFruitList();
053        assertNotNull(fList);
054        assertEquals(1, fList.size());
055
056        Fruit f0 = fList.get(0);
057        assertEquals("blue", f0.getName());
058        assertEquals(2, f0.textList.size());
059        assertEquals("hello", f0.textList.get(0));
060        assertEquals("world", f0.textList.get(1));
061    }
062
063    @Test
064    public void nestedComplex() throws Exception {
065        try {
066            simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplex.xml");
067            verifyFruit();
068
069        } catch (Exception je) {
070            StatusPrinter.print(fruitContext);
071            throw je;
072        }
073    }
074
075    @Test
076    public void nestedComplexWithoutClassAtrribute() throws Exception {
077        try {
078            simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplexWithoutClassAtrribute.xml");
079
080            verifyFruit();
081
082        } catch (Exception je) {
083            StatusPrinter.print(fruitContext);
084            throw je;
085        }
086    }
087
088    void verifyFruitList() {
089        List<Fruit> fList = fruitContext.getFruitList();
090        assertNotNull(fList);
091        assertEquals(1, fList.size());
092
093        Fruit f0 = fList.get(0);
094        assertEquals(2, f0.cakeList.size());
095
096        Cake cakeA = f0.cakeList.get(0);
097        assertEquals("A", cakeA.getType());
098
099        Cake cakeB = f0.cakeList.get(1);
100        assertEquals("B", cakeB.getType());
101    }
102
103    @Test
104    public void nestedComplexCollection() throws Exception {
105        try {
106            simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplexCollection.xml");
107            verifyFruitList();
108        } catch (Exception je) {
109            StatusPrinter.print(fruitContext);
110            throw je;
111        }
112    }
113
114    @Test
115    public void nestedComplexCollectionWithoutClassAtrribute() throws Exception {
116        try {
117            simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplexCollectionWithoutClassAtrribute.xml");
118            verifyFruitList();
119        } catch (Exception je) {
120            StatusPrinter.print(fruitContext);
121            throw je;
122        }
123    }
124
125    @Test
126    public void statusListenerWithPrefix() throws Exception {
127        try {                                             
128            simpleConfigurator.doConfigure(IMPLCIT_DIR + "statusListenerWithPrefix.xml");
129            StatusPrinter.print(fruitContext);
130            checker.assertIsErrorFree();
131        } catch (Exception je) {
132            StatusPrinter.print(fruitContext);
133            throw je;
134        }
135    }
136
137}