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.replay;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertTrue;
19  
20  import java.util.HashMap;
21  import java.util.List;
22  
23  import ch.qos.logback.core.joran.spi.ElementSelector;
24  import ch.qos.logback.core.testUtil.CoreTestConstants;
25  
26  import org.junit.Test;
27  
28  import ch.qos.logback.core.joran.SimpleConfigurator;
29  import ch.qos.logback.core.joran.action.Action;
30  import ch.qos.logback.core.joran.action.NOPAction;
31  import ch.qos.logback.core.util.StatusPrinter;
32  
33  /** 
34   * The Fruit* code is intended to test Joran's replay capability
35   * */
36  public class FruitConfigurationTest {
37  
38      FruitContext fruitContext = new FruitContext();
39  
40      public List<FruitShell> doFirstPart(String filename) throws Exception {
41  
42          try {
43              HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>();
44              rulesMap.put(new ElementSelector("group/fruitShell"), new FruitShellAction());
45              rulesMap.put(new ElementSelector("group/fruitShell/fruit"), new FruitFactoryAction());
46              rulesMap.put(new ElementSelector("group/fruitShell/fruit/*"), new NOPAction());
47              SimpleConfigurator simpleConfigurator = new SimpleConfigurator(rulesMap);
48  
49              simpleConfigurator.setContext(fruitContext);
50  
51              simpleConfigurator.doConfigure(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/replay/" + filename);
52  
53              return fruitContext.getFruitShellList();
54          } catch (Exception je) {
55              StatusPrinter.print(fruitContext);
56              throw je;
57          }
58      }
59  
60      @Test
61      public void fruit1() throws Exception {
62          List<FruitShell> fsList = doFirstPart("fruit1.xml");
63          assertNotNull(fsList);
64          assertEquals(1, fsList.size());
65  
66          FruitShell fs0 = fsList.get(0);
67          assertNotNull(fs0);
68          assertEquals("fs0", fs0.getName());
69          Fruit fruit0 = fs0.fruitFactory.buildFruit();
70          assertTrue(fruit0 instanceof Fruit);
71          assertEquals("blue", fruit0.getName());
72      }
73  
74      @Test
75      public void fruit2() throws Exception {
76          List<FruitShell> fsList = doFirstPart("fruit2.xml");
77          assertNotNull(fsList);
78          assertEquals(2, fsList.size());
79  
80          FruitShell fs0 = fsList.get(0);
81          assertNotNull(fs0);
82          assertEquals("fs0", fs0.getName());
83          Fruit fruit0 = fs0.fruitFactory.buildFruit();
84          assertTrue(fruit0 instanceof Fruit);
85          assertEquals("blue", fruit0.getName());
86  
87          FruitShell fs1 = fsList.get(1);
88          assertNotNull(fs1);
89          assertEquals("fs1", fs1.getName());
90          Fruit fruit1 = fs1.fruitFactory.buildFruit();
91          assertTrue(fruit1 instanceof WeightytFruit);
92          assertEquals("orange", fruit1.getName());
93          assertEquals(1.2, ((WeightytFruit) fruit1).getWeight(), 0.01);
94      }
95  
96      @Test
97      public void withSubst() throws Exception {
98          List<FruitShell> fsList = doFirstPart("fruitWithSubst.xml");
99          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 }