001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.HashSet;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import org.apache.commons.jxpath.util.ValueUtils;
026:
027: /**
028: * General purpose test bean for JUnit tests for the "jxpath" component.
029: *
030: * @author Dmitri Plotnikov
031: * @version $Revision: 1.8 $ $Date: 2004/02/29 14:17:40 $
032: */
033: public class TestBean {
034:
035: // ------------------------------------------------------------- Properties
036:
037: /**
038: * An array of nested java beans.
039: */
040: private NestedTestBean[] beans;
041: {
042: beans = new NestedTestBean[2];
043: beans[0] = new NestedTestBean("Name 1");
044: beans[1] = new NestedTestBean("Name 2");
045: beans[1].setInt(3);
046: }
047:
048: public NestedTestBean[] getBeans() {
049: return beans;
050: }
051:
052: public void setBeans(NestedTestBean[] beans) {
053: this .beans = beans;
054: }
055:
056: /**
057: * A boolean property.
058: */
059: private boolean bool = false;
060:
061: public boolean getBoolean() {
062: return bool;
063: }
064:
065: public void setBoolean(boolean bool) {
066: this .bool = bool;
067: }
068:
069: private int integer = 1;
070:
071: /**
072: * A read-only integer property
073: */
074: public int getInt() {
075: return integer;
076: }
077:
078: public void setInt(int integer) {
079: this .integer = integer;
080: }
081:
082: /**
083: * A read-only array of integers
084: */
085: private int[] array = { 1, 2, 3, 4 };
086:
087: public int[] getIntegers() {
088: return array;
089: }
090:
091: public int getIntegers(int index) {
092: return array[index];
093: }
094:
095: public void setIntegers(int index, int value) {
096: if (index >= array.length) {
097: array = (int[]) ValueUtils.expandCollection(array,
098: index + 1);
099: }
100: array[index] = value;
101: }
102:
103: /**
104: * A heterogeneous list: String, Integer, NestedTestBean
105: */
106: private ArrayList list;
107:
108: public List getList() {
109: if (list == null) {
110: list = new ArrayList();
111: list.add("String 3");
112: list.add(new Integer(3));
113: list.add(new NestedTestBean("Name 3"));
114: }
115: return list;
116: }
117:
118: /**
119: * A Map
120: */
121: private HashMap map;
122: {
123: map = new HashMap();
124: map.put("Key1", "Value 1");
125: map.put("Key2", new NestedTestBean("Name 6"));
126: }
127:
128: public Map getMap() {
129: return map;
130: }
131:
132: public void setMap(Map map) {
133: this .map = (HashMap) map;
134: }
135:
136: /**
137: * A nested read-only java bean
138: */
139: private NestedTestBean nestedBean = new NestedTestBean("Name 0");
140:
141: public NestedTestBean getNestedBean() {
142: return nestedBean;
143: }
144:
145: public void setNestedBean(NestedTestBean bean) {
146: this .nestedBean = bean;
147: }
148:
149: private NestedTestBean object = new NestedTestBean("Name 5");
150:
151: /**
152: * Returns a NestedTestBean: testing recognition of generic objects
153: */
154: public Object getObject() {
155: return object;
156: }
157:
158: /**
159: * Returns an array of ints: testing recognition of generic objects
160: */
161: public Object getObjects() {
162: return getIntegers();
163: }
164:
165: /**
166: * A heterogeneous set: String, Integer, NestedTestBean
167: */
168: private HashSet set;
169:
170: public Set getSet() {
171: if (set == null) {
172: set = new HashSet();
173: set.add("String 4");
174: set.add(new Integer(4));
175: set.add(new NestedTestBean("Name 4"));
176: }
177: return set;
178: }
179:
180: public String toString() {
181: return "ROOT";
182: }
183: }
|