1   package ch.qos.logback.core.joran.implicitAction;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertNotNull;
5   
6   import java.util.HashMap;
7   import java.util.List;
8   
9   import org.junit.Before;
10  import org.junit.Test;
11  
12  import ch.qos.logback.core.joran.SimpleConfigurator;
13  import ch.qos.logback.core.joran.action.Action;
14  import ch.qos.logback.core.joran.spi.Pattern;
15  import ch.qos.logback.core.util.Constants;
16  import ch.qos.logback.core.util.StatusPrinter;
17  
18  public class ImplicitActionTest {
19  
20    static final String IMPLCIT_DIR = Constants.TEST_DIR_PREFIX
21        + "input/joran/implicitAction/";
22  
23    FruitContext fruitContext = new FruitContext();
24    SimpleConfigurator simpleConfigurator;
25  
26    public ImplicitActionTest() {
27    }
28  
29    @Before
30    public void setUp() throws Exception {
31      fruitContext.setName("fruits");
32      HashMap<Pattern, Action> rulesMap = new HashMap<Pattern, Action>();
33      rulesMap.put(new Pattern("/context/"), new FruitContextAction());
34      simpleConfigurator = new SimpleConfigurator(rulesMap);
35      simpleConfigurator.setContext(fruitContext);
36    }
37  
38    void verifyFruit() {
39      List<Fruit> fList = fruitContext.getFruitList();
40      assertNotNull(fList);
41      assertEquals(1, fList.size());
42  
43      Fruit f0 = fList.get(0);
44      assertEquals("blue", f0.getName());
45      assertEquals(2, f0.textList.size());
46      assertEquals("hello", f0.textList.get(0));
47      assertEquals("world", f0.textList.get(1));
48    }
49  
50    @Test
51    public void nestedComplex() throws Exception {
52      try {
53        simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplex.xml");
54        verifyFruit();
55  
56      } catch (Exception je) {
57        StatusPrinter.print(fruitContext);
58        throw je;
59      }
60    }
61  
62    @Test
63    public void nestedComplexWithoutClassAtrribute() throws Exception {
64      try {
65        simpleConfigurator.doConfigure(IMPLCIT_DIR
66            + "nestedComplexWithoutClassAtrribute.xml");
67  
68        verifyFruit();
69  
70      } catch (Exception je) {
71        StatusPrinter.print(fruitContext);
72        throw je;
73      }
74    }
75  
76    
77    void verifyFruitList() {
78      List<Fruit> fList = fruitContext.getFruitList();
79      assertNotNull(fList);
80      assertEquals(1, fList.size());
81  
82      Fruit f0 = fList.get(0);
83      assertEquals(2, f0.cakeList.size());
84  
85      Cake cakeA = f0.cakeList.get(0);
86      assertEquals("A", cakeA.getType());
87  
88      Cake cakeB = f0.cakeList.get(1);
89      assertEquals("B", cakeB.getType());
90    }
91    @Test
92    public void nestedComplexCollection() throws Exception {
93      try {
94        simpleConfigurator.doConfigure(IMPLCIT_DIR
95            + "nestedComplexCollection.xml");
96        verifyFruitList();
97      } catch (Exception je) {
98        StatusPrinter.print(fruitContext);
99        throw je;
100     }
101   }
102 
103   
104   @Test
105   public void nestedComplexCollectionWithoutClassAtrribute() throws Exception {
106     try {
107       simpleConfigurator.doConfigure(IMPLCIT_DIR
108           + "nestedComplexCollectionWithoutClassAtrribute.xml");
109       verifyFruitList();
110     } catch (Exception je) {
111       StatusPrinter.print(fruitContext);
112       throw je;
113     }
114   }
115 
116 }