001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt;
019:
020: /** <p> This is a bean specifically designed to test cyclic references.
021: * The idea is that there's a count that counts every time <code>getFriend</code>
022: * gets called and throws a <code>RuntimeException</code> if the count gets too high.</p>
023: *
024: * @author Robert Burrell Donkin
025: * @version $Revision: 438373 $
026: */
027: public class LoopBean {
028: private static int count = 0;
029:
030: private static final int max_count = 100;
031:
032: private LoopBean friend;
033:
034: private String name;
035:
036: public static final LoopBean createNoLoopExampleBean() {
037: LoopBean root = new LoopBean("Root");
038: LoopBean levelOne = new LoopBean("level1");
039: LoopBean levelTwo = new LoopBean("level2");
040: LoopBean levelThree = new LoopBean("level3");
041: LoopBean levelFour = new LoopBean("level4");
042: LoopBean levelFive = new LoopBean("level5");
043:
044: root.setFriend(levelOne);
045: levelOne.setFriend(levelTwo);
046: levelTwo.setFriend(levelThree);
047: levelThree.setFriend(levelFour);
048: levelFour.setFriend(levelFive);
049:
050: return root;
051: }
052:
053: public static final LoopBean createLoopExampleBean() {
054: LoopBean root = new LoopBean("Root");
055: LoopBean levelOne = new LoopBean("level1");
056: LoopBean levelTwo = new LoopBean("level2");
057: LoopBean levelThree = new LoopBean("level3");
058: LoopBean levelFour = new LoopBean("level4");
059: LoopBean levelFive = new LoopBean("level5");
060:
061: root.setFriend(levelOne);
062: levelOne.setFriend(levelTwo);
063: levelTwo.setFriend(levelThree);
064: levelThree.setFriend(levelFour);
065: levelFour.setFriend(levelFive);
066: levelFive.setFriend(root);
067:
068: return root;
069: }
070:
071: public static final LoopBean createNotEmptyNoLoopExampleBean() {
072: LoopBean root = new LoopBean("");
073: LoopBean levelOne = new LoopBean("");
074: LoopBean levelTwo = new LoopBean("");
075: LoopBean levelThree = new LoopBean("");
076: LoopBean levelFour = new LoopBean("");
077: LoopBean levelFive = new LoopBean("Not Empty");
078:
079: root.setFriend(levelOne);
080: levelOne.setFriend(levelTwo);
081: levelTwo.setFriend(levelThree);
082: levelThree.setFriend(levelFour);
083: levelFour.setFriend(levelFive);
084:
085: return root;
086: }
087:
088: public static final LoopBean createEmptyLoopExampleBean() {
089: LoopBean root = new LoopBean("");
090: LoopBean levelOne = new LoopBean("");
091: LoopBean levelTwo = new LoopBean("");
092: LoopBean levelThree = new LoopBean("");
093: LoopBean levelFour = new LoopBean("");
094: LoopBean levelFive = new LoopBean("");
095:
096: root.setFriend(levelOne);
097: levelOne.setFriend(levelTwo);
098: levelTwo.setFriend(levelThree);
099: levelThree.setFriend(levelFour);
100: levelFour.setFriend(levelFive);
101: levelFive.setFriend(root);
102:
103: return root;
104: }
105:
106: public static final LoopBean createIdOnlyLoopExampleBean() {
107: LoopBean root = new LoopBean("Root");
108: LoopBean levelOne = new LoopBean("level1");
109: LoopBean levelTwo = new LoopBean("level2");
110: LoopBean levelThree = new LoopBean("level3");
111: LoopBean levelFour = new LoopBean("level4");
112: LoopBean levelFive = new LoopBean("level5");
113: LoopBean notRoot = new LoopBean("Root");
114:
115: root.setFriend(levelOne);
116: levelOne.setFriend(levelTwo);
117: levelTwo.setFriend(levelThree);
118: levelThree.setFriend(levelFour);
119: levelFour.setFriend(levelFive);
120: levelFive.setFriend(notRoot);
121:
122: return root;
123: }
124:
125: public LoopBean(String name) {
126: this .name = name;
127: }
128:
129: public LoopBean getFriend() {
130: if (++count > max_count) {
131: throw new RuntimeException("Looping!");
132: }
133: return friend;
134: }
135:
136: public void setFriend(LoopBean friend) {
137: this .friend = friend;
138: }
139:
140: public String getName() {
141: return name;
142: }
143:
144: public String toString() {
145: return "[LoopBean] name=" + name;
146: }
147: }
|