KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > config > ConfigurationModelTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.kernel.config;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedHashSet JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import junit.framework.TestCase;
28 import org.apache.geronimo.kernel.repository.Artifact;
29
30 /**
31  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
32  */

33 public class ConfigurationModelTest extends TestCase {
34     private static final Artifact rootId = new Artifact("root", "", "", "");
35     private static final Artifact midId = new Artifact("mid", "", "", "");
36     private static final Artifact leftId = new Artifact("left", "", "", "");
37     private static final Artifact rightId = new Artifact("right", "", "", "");
38     private static final Artifact childId = new Artifact("child", "", "", "");
39     private static final Set JavaDoc all = asSet(rootId, midId, leftId, rightId, childId);
40
41
42     /**
43      * The model below, with root and child user loaded and started
44      *
45      * root
46      * |
47      * mid
48      * / \
49      * left right
50      * \ /
51      * child
52      *
53      */

54     private final ConfigurationModel diamondModel = new ConfigurationModel();
55
56     protected void setUp() throws Exception JavaDoc {
57         super.setUp();
58
59         diamondModel.addConfiguation(rootId, Collections.EMPTY_SET, Collections.EMPTY_SET);
60         diamondModel.addConfiguation(midId, Collections.singleton(rootId), Collections.singleton(rootId));
61         diamondModel.addConfiguation(leftId, Collections.singleton(midId), Collections.singleton(midId));
62         diamondModel.addConfiguation(rightId, Collections.singleton(midId), Collections.singleton(midId));
63
64         Set JavaDoc leftAndRight = asSet(leftId, rightId);
65         diamondModel.addConfiguation(childId, leftAndRight, leftAndRight);
66
67         // Load and start the root and child
68
diamondModel.load(rootId);
69         diamondModel.start(rootId);
70         diamondModel.load(childId);
71         diamondModel.start(childId);
72
73         // all nodes should be loaded and started
74
assertEquals(all, diamondModel.getLoaded());
75         assertEquals(all, diamondModel.getStarted());
76
77         // only root and child should be user loaded and started
78
assertEquals(asSet(rootId, childId), diamondModel.getUserLoaded());
79         assertEquals(asSet(rootId, childId), diamondModel.getUserStarted());
80     }
81
82     public void testStopChild() throws NoSuchConfigException {
83         LinkedHashSet JavaDoc stopList = diamondModel.stop(childId);
84
85         // the only thing left running should be the root node
86
assertEquals(asSet(rootId), diamondModel.getStarted());
87         assertEquals(asSet(rootId), diamondModel.getUserStarted());
88
89         // everything should still be loaded
90
assertEquals(all, diamondModel.getLoaded());
91         assertEquals(asSet(rootId, childId), diamondModel.getUserLoaded());
92
93         // checke the order of the list
94
assertEquals(4, stopList.size());
95         assertFalse(stopList.contains(rootId));
96         assertBefore(childId, leftId, stopList);
97         assertBefore(childId, rightId, stopList);
98         assertBefore(leftId, midId, stopList);
99         assertBefore(rightId, midId, stopList);
100     }
101
102     public void testStopLeft() throws NoSuchConfigException {
103         LinkedHashSet JavaDoc stopList = diamondModel.stop(leftId);
104
105         // the only thing left running should be the root node
106
assertEquals(asSet(rootId), diamondModel.getStarted());
107         assertEquals(asSet(rootId), diamondModel.getUserStarted());
108
109         // everything should still be loaded
110
assertEquals(all, diamondModel.getLoaded());
111         assertEquals(asSet(rootId, childId), diamondModel.getUserLoaded());
112
113         // checke the order of the list
114
assertEquals(4, stopList.size());
115         assertFalse(stopList.contains(rootId));
116         assertBefore(childId, leftId, stopList);
117         assertBefore(childId, rightId, stopList);
118         assertBefore(leftId, midId, stopList);
119         assertBefore(rightId, midId, stopList);
120     }
121
122     public void testPinRightStopLeft() throws NoSuchConfigException {
123         LinkedHashSet JavaDoc startList = diamondModel.start(rightId);
124         assertTrue(startList.isEmpty());
125
126         LinkedHashSet JavaDoc stopList = diamondModel.stop(leftId);
127
128         // the right, mid and root nodes should be started
129
assertEquals(asSet(rootId, midId, rightId), diamondModel.getStarted());
130         assertEquals(asSet(rootId, rightId), diamondModel.getUserStarted());
131
132         // everything should still be loaded
133
assertEquals(all, diamondModel.getLoaded());
134         assertEquals(asSet(rootId, rightId, childId), diamondModel.getUserLoaded());
135
136         // checke the order of the list
137
assertContainsNone(stopList, asSet(rootId, midId, rightId));
138         assertEquals(2, stopList.size());
139         assertBefore(childId, leftId, stopList);
140     }
141
142     public void testAddRightChildStopLeft() throws NoSuchConfigException {
143         Artifact rightChildId = new Artifact("rightChild", "", "", "");
144         diamondModel.addConfiguation(rightChildId, Collections.singleton(rightId), Collections.singleton(rightId));
145
146         LinkedHashSet JavaDoc loadList = diamondModel.load(rightChildId);
147         assertEquals(asSet(rightChildId), asSet(loadList));
148         LinkedHashSet JavaDoc startList = diamondModel.start(rightChildId);
149         assertEquals(asSet(rightChildId), asSet(startList));
150
151         LinkedHashSet JavaDoc stopList = diamondModel.stop(leftId);
152
153         // the right, mid, root, and new right child nodes should be started
154
assertEquals(asSet(rootId, midId, rightId, rightChildId), diamondModel.getStarted());
155         assertEquals(asSet(rootId, rightChildId), diamondModel.getUserStarted());
156
157         // everything should still be loaded
158
assertEquals(asSet(all, rightChildId), diamondModel.getLoaded());
159         assertEquals(asSet(rootId, childId, rightChildId), diamondModel.getUserLoaded());
160
161         // checke the order of the list
162
assertContainsNone(stopList, asSet(rootId, midId, rightId));
163         assertEquals(2, stopList.size());
164         assertBefore(childId, leftId, stopList);
165     }
166
167
168     public static void assertContainsNone(Collection JavaDoc collection, Collection JavaDoc unexpected) {
169         for (Iterator JavaDoc iterator = unexpected.iterator(); iterator.hasNext();) {
170             Object JavaDoc item = iterator.next();
171             assertFalse("Did not expecte " + item + " in the collection " + collection,
172                     collection.contains(item));
173         }
174     }
175
176     public static void assertBefore(Object JavaDoc before, Object JavaDoc after, LinkedHashSet JavaDoc set) {
177         List JavaDoc list = new ArrayList JavaDoc(set);
178         int beforeIndex = list.indexOf(before);
179         assertTrue("Expected " + before + " to be contained in the list " + list,
180                 beforeIndex >= 0);
181
182         int afterIndex = list.indexOf(after);
183         assertTrue("Expected " + after + " to be contained in the list " + list,
184                 afterIndex >= 0);
185
186         assertTrue("Expected " + before + " to be before " + after + " in the list " + list,
187                 beforeIndex < afterIndex);
188     }
189
190     public static LinkedHashSet JavaDoc asSet(Object JavaDoc a) {
191         return asSet(new Object JavaDoc[] {a});
192     }
193
194     public static LinkedHashSet JavaDoc asSet(Object JavaDoc a, Object JavaDoc b) {
195         return asSet(new Object JavaDoc[] {a, b});
196     }
197     public static LinkedHashSet JavaDoc asSet(Object JavaDoc a, Object JavaDoc b, Object JavaDoc c) {
198         return asSet(new Object JavaDoc[] {a, b, c});
199     }
200     public static LinkedHashSet JavaDoc asSet(Object JavaDoc a, Object JavaDoc b, Object JavaDoc c, Object JavaDoc d) {
201         return asSet(new Object JavaDoc[] {a, b, c, d});
202     }
203     public static LinkedHashSet JavaDoc asSet(Object JavaDoc a, Object JavaDoc b, Object JavaDoc c, Object JavaDoc d, Object JavaDoc e) {
204         return asSet(new Object JavaDoc[] {a, b, c, d, e});
205     }
206     public static LinkedHashSet JavaDoc asSet(Object JavaDoc[] list) {
207         LinkedHashSet JavaDoc set = new LinkedHashSet JavaDoc();
208         for (int i = 0; i < list.length; i++) {
209             Object JavaDoc o = list[i];
210             if (o instanceof Collection JavaDoc) {
211                 set.addAll((Collection JavaDoc)o);
212             } else {
213                 set.add(o);
214             }
215         }
216         return set;
217     }
218 }
219
Popular Tags