1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.joran.replay;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertNotNull;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.util.HashMap;
17  import java.util.List;
18  
19  import org.junit.Test;
20  
21  import ch.qos.logback.core.joran.SimpleConfigurator;
22  import ch.qos.logback.core.joran.action.Action;
23  import ch.qos.logback.core.joran.action.NOPAction;
24  import ch.qos.logback.core.joran.spi.Pattern;
25  import ch.qos.logback.core.util.Constants;
26  import ch.qos.logback.core.util.StatusPrinter;
27  
28  /** 
29   * The Fruit* code is intended to test Joran's replay capability
30   * */
31  public class FruitConfigurationTest  {
32  
33    FruitContext fruitContext = new FruitContext();
34  
35    public List<FruitShell> doFirstPart(String filename) throws Exception {
36  
37      try {
38        HashMap<Pattern, Action> rulesMap = new HashMap<Pattern, Action>();
39        rulesMap.put(new Pattern("group/fruitShell"), new FruitShellAction());
40        rulesMap.put(new Pattern("group/fruitShell/fruit"),
41            new FruitFactoryAction());
42        rulesMap.put(new Pattern("group/fruitShell/fruit/*"), new NOPAction());
43        SimpleConfigurator simpleConfigurator = new SimpleConfigurator(rulesMap);
44  
45        simpleConfigurator.setContext(fruitContext);
46  
47        simpleConfigurator.doConfigure(Constants.TEST_DIR_PREFIX + "input/joran/replay/"
48            + filename);
49  
50        return fruitContext.getFruitShellList();
51      } catch (Exception je) {
52        StatusPrinter.print(fruitContext);
53        throw je;
54      }
55    }
56  
57    @Test
58    public void fruit1() throws Exception {
59      List<FruitShell> fsList = doFirstPart("fruit1.xml");
60      assertNotNull(fsList);
61      assertEquals(1, fsList.size());
62  
63      FruitShell fs0 = fsList.get(0);
64      assertNotNull(fs0);
65      assertEquals("fs0", fs0.getName());
66      Fruit fruit0 = fs0.fruitFactory.buildFruit();
67      assertTrue(fruit0 instanceof Fruit);
68      assertEquals("blue", fruit0.getName());
69    }
70  
71  
72    @Test
73    public void fruit2() throws Exception {
74      List<FruitShell> fsList = doFirstPart("fruit2.xml");
75      assertNotNull(fsList);
76      assertEquals(2, fsList.size());
77  
78      FruitShell fs0 = fsList.get(0);
79      assertNotNull(fs0);
80      assertEquals("fs0", fs0.getName());
81      Fruit fruit0 = fs0.fruitFactory.buildFruit();
82      assertTrue(fruit0 instanceof Fruit);
83      assertEquals("blue", fruit0.getName());
84  
85      FruitShell fs1 = fsList.get(1);
86      assertNotNull(fs1);
87      assertEquals("fs1", fs1.getName());
88      Fruit fruit1 = fs1.fruitFactory.buildFruit();
89      assertTrue(fruit1 instanceof WeightytFruit);
90      assertEquals("orange", fruit1.getName());
91      assertEquals(1.2, ((WeightytFruit) fruit1).getWeight(), 0.01);
92    }
93  
94    @Test
95    public void withSubst() throws Exception {
96      List<FruitShell> fsList = doFirstPart("fruitWithSubst.xml");
97      assertNotNull(fsList);
98      assertEquals(1, fsList.size());
99  
100     FruitShell fs0 = fsList.get(0);
101     assertNotNull(fs0);
102     assertEquals("fs0", fs0.getName());
103     int oldCount = FruitFactory.count;
104     Fruit fruit0 = fs0.fruitFactory.buildFruit();
105     assertTrue(fruit0 instanceof WeightytFruit);
106     assertEquals("orange-" + oldCount, fruit0.getName());
107     assertEquals(1.2, ((WeightytFruit) fruit0).getWeight(), 0.01);
108   }
109 
110 }