1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  package org.nuiton.decorator;
23  
24  import org.apache.commons.jxpath.JXPathIntrospector;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.junit.Assert;
28  import org.junit.Before;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  
32  import java.util.Map;
33  import java.util.TreeMap;
34  
35  
36  
37  
38  
39  
40  
41  public class MapPropertyHandlerTest extends JXPathContextTester {
42  
43  
44      
45      private static final Log log =
46              LogFactory.getLog(MapPropertyHandlerTest.class);
47  
48      private static final String TWO_ID = "two";
49  
50      private static final String ONE_ID = "one";
51  
52      @BeforeClass
53      public static void beforeClass() {
54  
55          
56          JXPathIntrospector.registerDynamicClass(
57                  Map.class,
58                  MapPropertyHandler.class
59          );
60      }
61  
62      @Before
63      public void setUp() {
64  
65          Map<String, Boolean> map = new TreeMap<String, Boolean>();
66          map.put(ONE_ID, true);
67          map.put(TWO_ID, false);
68  
69          if (log.isInfoEnabled()) {
70              log.info("init map : " + map);
71          }
72          newContext(map);
73      }
74  
75      @Test
76      public void testValues() throws Exception {
77  
78          Boolean value;
79  
80          value = (Boolean) getValue(".[@name='one']");
81          Assert.assertNotNull(value);
82          Assert.assertTrue(value);
83  
84          value = (Boolean) getValue(".[@name='two']");
85          Assert.assertNotNull(value);
86          Assert.assertFalse(value);
87  
88          value = (Boolean) getValue(".[@name='three']");
89          Assert.assertNull(value);
90      }
91  
92      @Test
93      public void testKey() throws Exception {
94  
95          String key;
96  
97          key = (String) getValue(".[@name='key:one']");
98          Assert.assertNotNull(key);
99          Assert.assertEquals(ONE_ID, key);
100 
101         key = (String) getValue(".[@name='key:two']");
102         Assert.assertNotNull(key);
103         Assert.assertEquals(TWO_ID, key);
104 
105         key = (String) getValue(".[@name='key:fake']");
106         Assert.assertNull(key);
107     }
108 
109     @Test
110     public void testValue() throws Exception {
111 
112         Boolean value;
113 
114         value = (Boolean) getValue(".[@name='value:true']");
115         Assert.assertNotNull(value);
116         Assert.assertTrue(value);
117 
118         value = (Boolean) getValue(".[@name='value:false']");
119         Assert.assertNotNull(value);
120         Assert.assertFalse(value);
121 
122         value = (Boolean) getValue(".[@name='value:fake']");
123         Assert.assertNull(value);
124     }
125 }