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.replay;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertNotNull;
018import static org.junit.Assert.assertTrue;
019
020import java.util.HashMap;
021import java.util.List;
022
023import ch.qos.logback.core.joran.spi.ElementSelector;
024import ch.qos.logback.core.testUtil.CoreTestConstants;
025
026import org.junit.Test;
027
028import ch.qos.logback.core.joran.SimpleConfigurator;
029import ch.qos.logback.core.joran.action.Action;
030import ch.qos.logback.core.joran.action.NOPAction;
031import ch.qos.logback.core.util.StatusPrinter;
032
033/** 
034 * The Fruit* code is intended to test Joran's replay capability
035 * */
036public class FruitConfigurationTest {
037
038    FruitContext fruitContext = new FruitContext();
039
040    public List<FruitShell> doFirstPart(String filename) throws Exception {
041
042        try {
043            HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>();
044            rulesMap.put(new ElementSelector("group/fruitShell"), new FruitShellAction());
045            rulesMap.put(new ElementSelector("group/fruitShell/fruit"), new FruitFactoryAction());
046            rulesMap.put(new ElementSelector("group/fruitShell/fruit/*"), new NOPAction());
047            SimpleConfigurator simpleConfigurator = new SimpleConfigurator(rulesMap);
048
049            simpleConfigurator.setContext(fruitContext);
050
051            simpleConfigurator.doConfigure(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/replay/" + filename);
052
053            return fruitContext.getFruitShellList();
054        } catch (Exception je) {
055            StatusPrinter.print(fruitContext);
056            throw je;
057        }
058    }
059
060    @Test
061    public void fruit1() throws Exception {
062        List<FruitShell> fsList = doFirstPart("fruit1.xml");
063        assertNotNull(fsList);
064        assertEquals(1, fsList.size());
065
066        FruitShell fs0 = fsList.get(0);
067        assertNotNull(fs0);
068        assertEquals("fs0", fs0.getName());
069        Fruit fruit0 = fs0.fruitFactory.buildFruit();
070        assertTrue(fruit0 instanceof Fruit);
071        assertEquals("blue", fruit0.getName());
072    }
073
074    @Test
075    public void fruit2() throws Exception {
076        List<FruitShell> fsList = doFirstPart("fruit2.xml");
077        assertNotNull(fsList);
078        assertEquals(2, fsList.size());
079
080        FruitShell fs0 = fsList.get(0);
081        assertNotNull(fs0);
082        assertEquals("fs0", fs0.getName());
083        Fruit fruit0 = fs0.fruitFactory.buildFruit();
084        assertTrue(fruit0 instanceof Fruit);
085        assertEquals("blue", fruit0.getName());
086
087        FruitShell fs1 = fsList.get(1);
088        assertNotNull(fs1);
089        assertEquals("fs1", fs1.getName());
090        Fruit fruit1 = fs1.fruitFactory.buildFruit();
091        assertTrue(fruit1 instanceof WeightytFruit);
092        assertEquals("orange", fruit1.getName());
093        assertEquals(1.2, ((WeightytFruit) fruit1).getWeight(), 0.01);
094    }
095
096    @Test
097    public void withSubst() throws Exception {
098        List<FruitShell> fsList = doFirstPart("fruitWithSubst.xml");
099        assertNotNull(fsList);
100        assertEquals(1, fsList.size());
101
102        FruitShell fs0 = fsList.get(0);
103        assertNotNull(fs0);
104        assertEquals("fs0", fs0.getName());
105        int oldCount = FruitFactory.count;
106        Fruit fruit0 = fs0.fruitFactory.buildFruit();
107        assertTrue(fruit0 instanceof WeightytFruit);
108        assertEquals("orange-" + oldCount, fruit0.getName());
109        assertEquals(1.2, ((WeightytFruit) fruit0).getWeight(), 0.01);
110    }
111
112}