001: /*
002: * Copyright 2006-2007, Unitils.org
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.unitils.reflectionassert;
017:
018: import junit.framework.TestCase;
019: import static org.unitils.reflectionassert.ReflectionAssert.assertLenEquals;
020: import static org.unitils.reflectionassert.ReflectionAssert.assertRefEquals;
021:
022: import java.util.HashSet;
023: import java.util.Set;
024:
025: /**
026: * Test class for {@link ReflectionAssert} tests for cyclic dependencies between collections.
027: * <p/>
028: * Thanks to contributions of mtowler
029: *
030: * @author Tim Ducheyne
031: * @author Filip Neven
032: */
033: public class ReflectionAssertCyclicCollectionTest extends TestCase {
034:
035: /* Test object containing a collection that contains a loop */
036: private TestObject testObjectA;
037:
038: /* Same as testObjectA but different instance */
039: private TestObject testObjectB;
040:
041: /**
042: * Initializes the test fixture.
043: */
044: public void setUp() throws Exception {
045: super .setUp();
046:
047: testObjectA = createTestObject();
048: testObjectB = createTestObject();
049: }
050:
051: /**
052: * Tests the comparison of objects containing the cyclic dependency.
053: * This should pass and should not cause a StackOverflow.
054: */
055: public void testAssertRefEquals_infiniteLoop() {
056: assertRefEquals(testObjectA, testObjectB);
057: }
058:
059: /**
060: * Tests the comparison of objects containing the cyclic dependency.
061: * This should pass and should not cause a StackOverflow.
062: */
063: public void testAssertLenEquals_infiniteLoop() {
064: assertLenEquals(testObjectA, testObjectB);
065: }
066:
067: /**
068: * Creates a test object that contains cyclic dependencies through collections.
069: * <p/>
070: * root -> collection( leaf1, leaf2 )
071: * leaf1 -> collection ( root )
072: * leaf2 -> collection ( root )
073: *
074: * @return The test object, not null
075: */
076: private TestObject createTestObject() {
077: TestObject root = new TestObject();
078: TestObject leaf1 = new TestObject();
079: TestObject leaf2 = new TestObject();
080:
081: root.getTestObjects().add(leaf1);
082: root.getTestObjects().add(leaf2);
083: leaf1.getTestObjects().add(root);
084: leaf2.getTestObjects().add(root);
085: return root;
086: }
087:
088: /**
089: * Test class with inner collection.
090: */
091: private static class TestObject {
092:
093: private Set<TestObject> testObjects;
094:
095: public TestObject() {
096: this .testObjects = new HashSet<TestObject>();
097: }
098:
099: public Set<TestObject> getTestObjects() {
100: return testObjects;
101: }
102: }
103: }
|