KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependency > TestNode


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependency;
34
35 import java.util.*;
36
37 import junit.framework.*;
38
39 public class TestNode extends TestCase {
40     Node a;
41     Node a_A;
42     Node a_A_a;
43     Node a_A_b;
44     Node a_B;
45     Node a_B_a;
46     Node a_B_b;
47
48     Node b;
49     Node b_B;
50     Node b_B_b;
51
52     protected void setUp() throws Exception JavaDoc {
53         NodeFactory factory = new NodeFactory();
54
55         a = factory.createPackage("a");
56         a_A = factory.createClass("a.A");
57         a_A_a = factory.createFeature("a.A.a");
58         a_A_b = factory.createFeature("a.A.b");
59         a_B = factory.createClass("a.B");
60         a_B_a = factory.createFeature("a.B.a");
61         a_B_b = factory.createFeature("a.B.b");
62
63         b = factory.createPackage("b");
64         b_B = factory.createClass("b.B");
65         b_B_b = factory.createFeature("b.B.b");
66     }
67
68     public void testPackageCanAddDependency() {
69         assertFalse(a.canAddDependencyTo(a));
70         assertTrue(a.canAddDependencyTo(a_A));
71         assertTrue(a.canAddDependencyTo(a_A_a));
72         assertTrue(a.canAddDependencyTo(a_A_b));
73         assertTrue(a.canAddDependencyTo(a_B));
74         assertTrue(a.canAddDependencyTo(a_B_a));
75         assertTrue(a.canAddDependencyTo(a_B_b));
76
77         assertTrue(a.canAddDependencyTo(b));
78         assertTrue(a.canAddDependencyTo(b_B));
79         assertTrue(a.canAddDependencyTo(b_B_b));
80     }
81
82     public void testClassCanAddDependency() {
83         assertFalse(a_A.canAddDependencyTo(a));
84         assertFalse(a_A.canAddDependencyTo(a_A));
85         assertTrue(a_A.canAddDependencyTo(a_A_a));
86         assertTrue(a_A.canAddDependencyTo(a_A_b));
87         assertTrue(a_A.canAddDependencyTo(a_B));
88         assertTrue(a_A.canAddDependencyTo(a_B_a));
89         assertTrue(a_A.canAddDependencyTo(a_B_b));
90
91         assertTrue(a_A.canAddDependencyTo(b));
92         assertTrue(a_A.canAddDependencyTo(b_B));
93         assertTrue(a_A.canAddDependencyTo(b_B_b));
94     }
95
96     public void testFeatureCanAddDependency() {
97         assertFalse(a_A_a.canAddDependencyTo(a));
98         assertFalse(a_A_a.canAddDependencyTo(a_A));
99         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.getOutboundDependencies().contains(b_B_b));
113         assertTrue("Missing b.B.b <-- a.A.a", b_B_b.getInboundDependencies().contains(a_A_a));
114
115         a_A_a.removeDependency(b_B_b);
116         assertFalse("Did not remove a.A.a --> b.B.b", a_A_a.getOutboundDependencies().contains(b_B_b));
117         assertFalse("Did not remove b.B.b <-- a.A.a", b_B_b.getInboundDependencies().contains(a_A_a));
118     }
119
120     public void testRemoveOneDependencyButNotAnother() {
121         a_A_a.addDependency(a_A_b);
122         assertTrue("Missing a.A.a --> a.A.b", a_A_a.getOutboundDependencies().contains(a_A_b));
123         assertTrue("Missing a.A.b <-- a.A.a", a_A_b.getInboundDependencies().contains(a_A_a));
124
125         a_A_a.addDependency(b_B_b);
126         assertTrue("Missing a.A.a --> b.B.b", a_A_a.getOutboundDependencies().contains(b_B_b));
127         assertTrue("Missing b.B.b <-- a.A.a", b_B_b.getInboundDependencies().contains(a_A_a));
128
129         a_A_a.removeDependency(b_B_b);
130         assertTrue("Missing a.A.a --> a.A.b", a_A_a.getOutboundDependencies().contains(a_A_b));
131         assertTrue("Missing a.A.b <-- a.A.a", a_A_b.getInboundDependencies().contains(a_A_a));
132         assertFalse("Did not remove a.A.a --> b.B.b", a_A_a.getOutboundDependencies().contains(b_B_b));
133         assertFalse("Did not remove b.B.b <-- a.A.a", b_B_b.getInboundDependencies().contains(a_A_a));
134     }
135
136     public void testRemoveDependencies() {
137         a_A_a.addDependency(a_A_b);
138         a_A_a.addDependency(b_B_b);
139         assertTrue("Missing a.A.a --> a.A.b", a_A_a.getOutboundDependencies().contains(a_A_b));
140         assertTrue("Missing a.A.b <-- a.A.a", a_A_b.getInboundDependencies().contains(a_A_a));
141         assertTrue("Missing a.A.a --> b.B.b", a_A_a.getOutboundDependencies().contains(b_B_b));
142         assertTrue("Missing b.B.b <-- a.A.a", b_B_b.getInboundDependencies().contains(a_A_a));
143
144         Collection dependencies = new ArrayList(2);
145         dependencies.add(a_A_b);
146         dependencies.add(b_B_b);
147         
148         a_A_a.removeDependencies(dependencies);
149         assertFalse("Did not remove a.A.a --> a.A.b", a_A_a.getOutboundDependencies().contains(a_A_b));
150         assertFalse("Did not remove a.A.b <-- a.A.a", a_A_b.getInboundDependencies().contains(a_A_a));
151         assertFalse("Did not remove a.A.a --> b.B.b", a_A_a.getOutboundDependencies().contains(b_B_b));
152         assertFalse("Did not remove b.B.b <-- a.A.a", b_B_b.getInboundDependencies().contains(a_A_a));
153     }
154 }
155
Popular Tags