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: */package org.apache.geronimo.kernel.config;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.LinkedHashSet;
023: import java.util.List;
024: import java.util.Set;
025:
026: import junit.framework.TestCase;
027: import org.apache.geronimo.kernel.repository.Artifact;
028:
029: /**
030: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
031: */
032: public class ConfigurationModelTest extends TestCase {
033: private static final Artifact rootId = new Artifact("root", "", "",
034: "");
035: private static final Artifact midId = new Artifact("mid", "", "",
036: "");
037: private static final Artifact leftId = new Artifact("left", "", "",
038: "");
039: private static final Artifact rightId = new Artifact("right", "",
040: "", "");
041: private static final Artifact childId = new Artifact("child", "",
042: "", "");
043: private static final Set all = asSet(rootId, midId, leftId,
044: rightId, childId);
045:
046: /**
047: * The model below, with root and child user loaded and started
048: *
049: * root
050: * |
051: * mid
052: * / \
053: * left right
054: * \ /
055: * child
056: *
057: */
058: private final ConfigurationModel diamondModel = new ConfigurationModel();
059:
060: protected void setUp() throws Exception {
061: super .setUp();
062:
063: diamondModel.addConfiguation(rootId, Collections.EMPTY_SET,
064: Collections.EMPTY_SET);
065: diamondModel.addConfiguation(midId, Collections
066: .singleton(rootId), Collections.singleton(rootId));
067: diamondModel.addConfiguation(leftId, Collections
068: .singleton(midId), Collections.singleton(midId));
069: diamondModel.addConfiguation(rightId, Collections
070: .singleton(midId), Collections.singleton(midId));
071:
072: Set leftAndRight = asSet(leftId, rightId);
073: diamondModel.addConfiguation(childId, leftAndRight,
074: leftAndRight);
075:
076: // Load and start the root and child
077: diamondModel.load(rootId);
078: diamondModel.start(rootId);
079: diamondModel.load(childId);
080: diamondModel.start(childId);
081:
082: // all nodes should be loaded and started
083: assertEquals(all, diamondModel.getLoaded());
084: assertEquals(all, diamondModel.getStarted());
085:
086: // only root and child should be user loaded and started
087: assertEquals(asSet(rootId, childId), diamondModel
088: .getUserLoaded());
089: assertEquals(asSet(rootId, childId), diamondModel
090: .getUserStarted());
091: }
092:
093: public void testStopChild() throws NoSuchConfigException {
094: LinkedHashSet stopList = diamondModel.stop(childId);
095:
096: // the only thing left running should be the root node
097: assertEquals(asSet(rootId), diamondModel.getStarted());
098: assertEquals(asSet(rootId), diamondModel.getUserStarted());
099:
100: // everything should still be loaded
101: assertEquals(all, diamondModel.getLoaded());
102: assertEquals(asSet(rootId, childId), diamondModel
103: .getUserLoaded());
104:
105: // checke the order of the list
106: assertEquals(4, stopList.size());
107: assertFalse(stopList.contains(rootId));
108: assertBefore(childId, leftId, stopList);
109: assertBefore(childId, rightId, stopList);
110: assertBefore(leftId, midId, stopList);
111: assertBefore(rightId, midId, stopList);
112: }
113:
114: public void testStopLeft() throws NoSuchConfigException {
115: LinkedHashSet stopList = diamondModel.stop(leftId);
116:
117: // the only thing left running should be the root node
118: assertEquals(asSet(rootId), diamondModel.getStarted());
119: assertEquals(asSet(rootId), diamondModel.getUserStarted());
120:
121: // everything should still be loaded
122: assertEquals(all, diamondModel.getLoaded());
123: assertEquals(asSet(rootId, childId), diamondModel
124: .getUserLoaded());
125:
126: // checke the order of the list
127: assertEquals(4, stopList.size());
128: assertFalse(stopList.contains(rootId));
129: assertBefore(childId, leftId, stopList);
130: assertBefore(childId, rightId, stopList);
131: assertBefore(leftId, midId, stopList);
132: assertBefore(rightId, midId, stopList);
133: }
134:
135: public void testPinRightStopLeft() throws NoSuchConfigException {
136: LinkedHashSet startList = diamondModel.start(rightId);
137: assertTrue(startList.isEmpty());
138:
139: LinkedHashSet stopList = diamondModel.stop(leftId);
140:
141: // the right, mid and root nodes should be started
142: assertEquals(asSet(rootId, midId, rightId), diamondModel
143: .getStarted());
144: assertEquals(asSet(rootId, rightId), diamondModel
145: .getUserStarted());
146:
147: // everything should still be loaded
148: assertEquals(all, diamondModel.getLoaded());
149: assertEquals(asSet(rootId, rightId, childId), diamondModel
150: .getUserLoaded());
151:
152: // checke the order of the list
153: assertContainsNone(stopList, asSet(rootId, midId, rightId));
154: assertEquals(2, stopList.size());
155: assertBefore(childId, leftId, stopList);
156: }
157:
158: public void testAddRightChildStopLeft()
159: throws NoSuchConfigException {
160: Artifact rightChildId = new Artifact("rightChild", "", "", "");
161: diamondModel.addConfiguation(rightChildId, Collections
162: .singleton(rightId), Collections.singleton(rightId));
163:
164: LinkedHashSet loadList = diamondModel.load(rightChildId);
165: assertEquals(asSet(rightChildId), asSet(loadList));
166: LinkedHashSet startList = diamondModel.start(rightChildId);
167: assertEquals(asSet(rightChildId), asSet(startList));
168:
169: LinkedHashSet stopList = diamondModel.stop(leftId);
170:
171: // the right, mid, root, and new right child nodes should be started
172: assertEquals(asSet(rootId, midId, rightId, rightChildId),
173: diamondModel.getStarted());
174: assertEquals(asSet(rootId, rightChildId), diamondModel
175: .getUserStarted());
176:
177: // everything should still be loaded
178: assertEquals(asSet(all, rightChildId), diamondModel.getLoaded());
179: assertEquals(asSet(rootId, childId, rightChildId), diamondModel
180: .getUserLoaded());
181:
182: // checke the order of the list
183: assertContainsNone(stopList, asSet(rootId, midId, rightId));
184: assertEquals(2, stopList.size());
185: assertBefore(childId, leftId, stopList);
186: }
187:
188: public static void assertContainsNone(Collection collection,
189: Collection unexpected) {
190: for (Iterator iterator = unexpected.iterator(); iterator
191: .hasNext();) {
192: Object item = iterator.next();
193: assertFalse("Did not expecte " + item
194: + " in the collection " + collection, collection
195: .contains(item));
196: }
197: }
198:
199: public static void assertBefore(Object before, Object after,
200: LinkedHashSet set) {
201: List list = new ArrayList(set);
202: int beforeIndex = list.indexOf(before);
203: assertTrue("Expected " + before
204: + " to be contained in the list " + list,
205: beforeIndex >= 0);
206:
207: int afterIndex = list.indexOf(after);
208: assertTrue("Expected " + after
209: + " to be contained in the list " + list,
210: afterIndex >= 0);
211:
212: assertTrue("Expected " + before + " to be before " + after
213: + " in the list " + list, beforeIndex < afterIndex);
214: }
215:
216: public static LinkedHashSet asSet(Object a) {
217: return asSet(new Object[] { a });
218: }
219:
220: public static LinkedHashSet asSet(Object a, Object b) {
221: return asSet(new Object[] { a, b });
222: }
223:
224: public static LinkedHashSet asSet(Object a, Object b, Object c) {
225: return asSet(new Object[] { a, b, c });
226: }
227:
228: public static LinkedHashSet asSet(Object a, Object b, Object c,
229: Object d) {
230: return asSet(new Object[] { a, b, c, d });
231: }
232:
233: public static LinkedHashSet asSet(Object a, Object b, Object c,
234: Object d, Object e) {
235: return asSet(new Object[] { a, b, c, d, e });
236: }
237:
238: public static LinkedHashSet asSet(Object[] list) {
239: LinkedHashSet set = new LinkedHashSet();
240: for (int i = 0; i < list.length; i++) {
241: Object o = list[i];
242: if (o instanceof Collection) {
243: set.addAll((Collection) o);
244: } else {
245: set.add(o);
246: }
247: }
248: return set;
249: }
250: }
|