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