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;
18  
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.apache.commons.jxpath.util.ValueUtils;
27  
28  /**
29   * General purpose test bean for JUnit tests for the "jxpath" component.
30   *
31   * @author Dmitri Plotnikov
32   * @version $Revision: 502244 $ $Date: 2007-02-01 16:39:01 +0100 (Do, 01 Feb 2007) $
33   */
34  public class TestBean {
35  
36      // ------------------------------------------------------------- Properties
37  
38      /**
39       * An array of nested java beans.
40       */
41      private NestedTestBean[] beans;
42      {
43          beans = new NestedTestBean[2];
44          beans[0] = new NestedTestBean("Name 1");
45          beans[1] = new NestedTestBean("Name 2");
46          beans[1].setInt(3);
47      }
48  
49      public NestedTestBean[] getBeans() {
50          return beans;
51      }
52  
53      public void setBeans(NestedTestBean[] beans) {
54          this.beans = beans;
55      }
56  
57      /**
58       * A boolean property.
59       */
60      private boolean bool = false;
61      public boolean isBoolean() {
62          return bool;
63      }
64  
65      public void setBoolean(boolean bool) {
66          this.bool = bool;
67      }
68  
69      private int integer = 1;
70      /**
71       * A read-only integer property
72       */
73      public int getInt() {
74          return integer;
75      }
76  
77      public void setInt(int integer) {
78          this.integer = integer;
79      }
80  
81      /**
82       * A read-only array of integers
83       */
84      private int[] array = { 1, 2, 3, 4 };
85      public int[] getIntegers() {
86          return array;
87      }
88  
89      public int getIntegers(int index) {
90          return array[index];
91      }
92  
93      public void setIntegers(int index, int value) {
94          if (index >= array.length) {
95              array = (int[]) ValueUtils.expandCollection(array, index + 1);
96          }
97          array[index] = value;
98      }
99  
100     /**
101      * A heterogeneous list: String, Integer, NestedTestBean
102      */
103     private ArrayList list;
104     public List getList() {
105         if (list == null) {
106             list = new ArrayList();
107             list.add("String 3");
108             list.add(new Integer(3));
109             list.add(new NestedTestBean("Name 3"));
110         }
111         return list;
112     }
113 
114     /**
115      * A Map
116      */
117     private HashMap map;
118     {
119         map = new HashMap();
120         map.put("Key1", "Value 1");
121         map.put("Key2", new NestedTestBean("Name 6"));
122     }
123 
124     public Map getMap() {
125         return map;
126     }
127 
128     public void setMap(Map map) {
129         this.map = (HashMap) map;
130     }
131 
132     /**
133      * A nested read-only java bean
134      */
135     private NestedTestBean nestedBean = new NestedTestBean("Name 0");
136     public NestedTestBean getNestedBean() {
137         return nestedBean;
138     }
139 
140     public void setNestedBean(NestedTestBean bean) {
141         this.nestedBean = bean;
142     }
143 
144     private NestedTestBean object = new NestedTestBean("Name 5");
145 
146     /**
147      * Returns a NestedTestBean: testing recognition of generic objects
148      */
149     public Object getObject() {
150         return object;
151     }
152 
153     /**
154      * Returns an array of ints: testing recognition of generic objects
155      */
156     public Object getObjects() {
157         return getIntegers();
158     }
159 
160     /**
161      * A heterogeneous set: String, Integer, NestedTestBean
162      */
163     private HashSet set;
164     public Set getSet() {
165         if (set == null) {
166             set = new HashSet();
167             set.add("String 4");
168             set.add(new Integer(4));
169             set.add(new NestedTestBean("Name 4"));
170         }
171         return set;
172     }
173 
174     public String toString() {
175         return "ROOT";
176     }
177 }