View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jxpath.ri.model.container;
18  
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.jxpath.Container;
25  import org.apache.commons.jxpath.JXPathContext;
26  import org.apache.commons.jxpath.JXPathTestCase;
27  
28  /**
29   * Tests JXPath with containers as root or value of a variable, property, etc.
30   *
31   * @author Dmitri Plotnikov
32   * @version $Revision: 1523201 $ $Date: 2013-09-14 11:48:41 +0200 (Sa, 14 Sep 2013) $
33   */
34  
35  public class ContainerModelTest extends JXPathTestCase {
36  
37      private class ArrayContainer implements Container
38      {
39          private String[] array = new String[]{"foo", "bar"};
40          public Object getValue() {
41              return array;
42          }
43  
44          public void setValue(Object value) {
45              throw new UnsupportedOperationException();
46          }
47      }
48  
49      public class ListContainer implements Container
50      {
51          private List list;
52  
53          public ListContainer() {
54              list = new ArrayList();
55              list.add("foo");
56              list.add("bar");
57          }
58  
59          public Object getValue() {
60              return list;
61          }
62  
63          public void setValue(Object value) {
64              throw new UnsupportedOperationException();
65          }
66      }
67  
68      public class Bean
69      {
70          private ListContainer container = new ListContainer();
71  
72          public ListContainer getContainer() {
73              return container;
74          }
75      }
76          
77      public void testContainerVariableWithCollection() {
78          ArrayContainer container = new ArrayContainer();
79          String[] array = (String[]) container.getValue();
80          
81          JXPathContext context = JXPathContext.newContext(null);
82          context.getVariables().declareVariable("list", container);
83          
84          assertXPathValueAndPointer(context, "$list", array, "$list");
85          assertXPathValueAndPointer(context, "$list[1]", "foo", "$list[1]");
86          assertXPathValueAndPointer(context, "$list[2]", "bar", "$list[2]");
87          
88          assertXPathSetValue(context, "$list[1]", "baz");
89          assertEquals("Checking setValue(index)", "baz", array[0]);
90      }
91      
92      public void testContainerPropertyWithCollection() {
93          Bean bean = new Bean();
94          List list = (List) bean.getContainer().getValue();
95          
96          JXPathContext context = JXPathContext.newContext(bean);
97          
98          assertXPathValueAndPointer(context, "/container", 
99                  list, "/container");
100         assertXPathValueAndPointer(context, "/container[1]",
101                 list.get(0), "/container[1]");
102         assertXPathValueAndPointer(context, "/container[2]",
103                 list.get(1), "/container[2]");
104         
105         assertXPathSetValue(context, "/container[1]", "baz");
106         assertEquals("Checking setValue(index)", "baz", list.get(0));
107     }
108     
109     public void testContainerMapWithCollection() {
110         ListContainer container = new ListContainer();
111         List list = (List) container.getValue();
112                 
113         Map map = new HashMap();
114         map.put("container", container);
115         
116         JXPathContext context = JXPathContext.newContext(map);
117         
118         assertXPathValueAndPointer(context, "/container", 
119                 list, "/.[@name='container']");
120         assertXPathValueAndPointer(context, "/container[1]",
121                 list.get(0), "/.[@name='container'][1]");
122         assertXPathValueAndPointer(context, "/container[2]",
123                 list.get(1), "/.[@name='container'][2]");
124         
125         assertXPathSetValue(context, "/container[1]", "baz");
126         assertEquals("Checking setValue(index)", "baz", list.get(0));
127     }
128     
129     public void testContainerRootWithCollection() {
130         ArrayContainer container = new ArrayContainer();
131         String[] array = (String[]) container.getValue();
132         
133         JXPathContext context = JXPathContext.newContext(container);
134         context.getVariables().declareVariable("list", container);
135         
136         assertXPathValueAndPointer(context, "/", array, "/");
137         assertXPathValueAndPointer(context, "/.[1]", "foo", "/.[1]");
138         assertXPathValueAndPointer(context, "/.[2]", "bar", "/.[2]");
139         
140         assertXPathSetValue(context, "/.[1]", "baz");
141         assertEquals("Checking setValue(index)", "baz", array[0]);    }
142     
143 }