View Javadoc
1   /*
2    * #%L
3    * Nuiton Decorator
4    * %%
5    * Copyright (C) 2011 CodeLutin, Tony Chemit
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
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   * Tests the class {@link MapPropertyHandler}.
37   *
38   * @author tchemit <chemit@codelutin.com>
39   * @since 2.3
40   */
41  public class MapPropertyHandlerTest extends JXPathContextTester {
42  
43  
44      /** Logger */
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          // pour initialiser le JXPathContext
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 }