001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependency;
034:
035: import java.util.*;
036:
037: import junit.framework.*;
038:
039: public class TestNode extends TestCase {
040: Node a;
041: Node a_A;
042: Node a_A_a;
043: Node a_A_b;
044: Node a_B;
045: Node a_B_a;
046: Node a_B_b;
047:
048: Node b;
049: Node b_B;
050: Node b_B_b;
051:
052: protected void setUp() throws Exception {
053: NodeFactory factory = new NodeFactory();
054:
055: a = factory.createPackage("a");
056: a_A = factory.createClass("a.A");
057: a_A_a = factory.createFeature("a.A.a");
058: a_A_b = factory.createFeature("a.A.b");
059: a_B = factory.createClass("a.B");
060: a_B_a = factory.createFeature("a.B.a");
061: a_B_b = factory.createFeature("a.B.b");
062:
063: b = factory.createPackage("b");
064: b_B = factory.createClass("b.B");
065: b_B_b = factory.createFeature("b.B.b");
066: }
067:
068: public void testPackageCanAddDependency() {
069: assertFalse(a.canAddDependencyTo(a));
070: assertTrue(a.canAddDependencyTo(a_A));
071: assertTrue(a.canAddDependencyTo(a_A_a));
072: assertTrue(a.canAddDependencyTo(a_A_b));
073: assertTrue(a.canAddDependencyTo(a_B));
074: assertTrue(a.canAddDependencyTo(a_B_a));
075: assertTrue(a.canAddDependencyTo(a_B_b));
076:
077: assertTrue(a.canAddDependencyTo(b));
078: assertTrue(a.canAddDependencyTo(b_B));
079: assertTrue(a.canAddDependencyTo(b_B_b));
080: }
081:
082: public void testClassCanAddDependency() {
083: assertFalse(a_A.canAddDependencyTo(a));
084: assertFalse(a_A.canAddDependencyTo(a_A));
085: assertTrue(a_A.canAddDependencyTo(a_A_a));
086: assertTrue(a_A.canAddDependencyTo(a_A_b));
087: assertTrue(a_A.canAddDependencyTo(a_B));
088: assertTrue(a_A.canAddDependencyTo(a_B_a));
089: assertTrue(a_A.canAddDependencyTo(a_B_b));
090:
091: assertTrue(a_A.canAddDependencyTo(b));
092: assertTrue(a_A.canAddDependencyTo(b_B));
093: assertTrue(a_A.canAddDependencyTo(b_B_b));
094: }
095:
096: public void testFeatureCanAddDependency() {
097: assertFalse(a_A_a.canAddDependencyTo(a));
098: assertFalse(a_A_a.canAddDependencyTo(a_A));
099: assertFalse(a_A_a.canAddDependencyTo(a_A_a));
100: assertTrue(a_A_a.canAddDependencyTo(a_A_b));
101: assertTrue(a_A_a.canAddDependencyTo(a_B));
102: assertTrue(a_A_a.canAddDependencyTo(a_B_a));
103: assertTrue(a_A_a.canAddDependencyTo(a_B_b));
104:
105: assertTrue(a_A_a.canAddDependencyTo(b));
106: assertTrue(a_A_a.canAddDependencyTo(b_B));
107: assertTrue(a_A_a.canAddDependencyTo(b_B_b));
108: }
109:
110: public void testRemoveOneDependency() {
111: a_A_a.addDependency(b_B_b);
112: assertTrue("Missing a.A.a --> b.B.b", a_A_a
113: .getOutboundDependencies().contains(b_B_b));
114: assertTrue("Missing b.B.b <-- a.A.a", b_B_b
115: .getInboundDependencies().contains(a_A_a));
116:
117: a_A_a.removeDependency(b_B_b);
118: assertFalse("Did not remove a.A.a --> b.B.b", a_A_a
119: .getOutboundDependencies().contains(b_B_b));
120: assertFalse("Did not remove b.B.b <-- a.A.a", b_B_b
121: .getInboundDependencies().contains(a_A_a));
122: }
123:
124: public void testRemoveOneDependencyButNotAnother() {
125: a_A_a.addDependency(a_A_b);
126: assertTrue("Missing a.A.a --> a.A.b", a_A_a
127: .getOutboundDependencies().contains(a_A_b));
128: assertTrue("Missing a.A.b <-- a.A.a", a_A_b
129: .getInboundDependencies().contains(a_A_a));
130:
131: a_A_a.addDependency(b_B_b);
132: assertTrue("Missing a.A.a --> b.B.b", a_A_a
133: .getOutboundDependencies().contains(b_B_b));
134: assertTrue("Missing b.B.b <-- a.A.a", b_B_b
135: .getInboundDependencies().contains(a_A_a));
136:
137: a_A_a.removeDependency(b_B_b);
138: assertTrue("Missing a.A.a --> a.A.b", a_A_a
139: .getOutboundDependencies().contains(a_A_b));
140: assertTrue("Missing a.A.b <-- a.A.a", a_A_b
141: .getInboundDependencies().contains(a_A_a));
142: assertFalse("Did not remove a.A.a --> b.B.b", a_A_a
143: .getOutboundDependencies().contains(b_B_b));
144: assertFalse("Did not remove b.B.b <-- a.A.a", b_B_b
145: .getInboundDependencies().contains(a_A_a));
146: }
147:
148: public void testRemoveDependencies() {
149: a_A_a.addDependency(a_A_b);
150: a_A_a.addDependency(b_B_b);
151: assertTrue("Missing a.A.a --> a.A.b", a_A_a
152: .getOutboundDependencies().contains(a_A_b));
153: assertTrue("Missing a.A.b <-- a.A.a", a_A_b
154: .getInboundDependencies().contains(a_A_a));
155: assertTrue("Missing a.A.a --> b.B.b", a_A_a
156: .getOutboundDependencies().contains(b_B_b));
157: assertTrue("Missing b.B.b <-- a.A.a", b_B_b
158: .getInboundDependencies().contains(a_A_a));
159:
160: Collection<Node> dependencies = new ArrayList<Node>(2);
161: dependencies.add(a_A_b);
162: dependencies.add(b_B_b);
163:
164: a_A_a.removeDependencies(dependencies);
165: assertFalse("Did not remove a.A.a --> a.A.b", a_A_a
166: .getOutboundDependencies().contains(a_A_b));
167: assertFalse("Did not remove a.A.b <-- a.A.a", a_A_b
168: .getInboundDependencies().contains(a_A_a));
169: assertFalse("Did not remove a.A.a --> b.B.b", a_A_a
170: .getOutboundDependencies().contains(b_B_b));
171: assertFalse("Did not remove b.B.b <-- a.A.a", b_B_b
172: .getInboundDependencies().contains(a_A_a));
173: }
174: }
|