1 17 package org.apache.geronimo.kernel.config; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.LinkedHashSet ; 24 import java.util.List ; 25 import java.util.Set ; 26 27 import junit.framework.TestCase; 28 import org.apache.geronimo.kernel.repository.Artifact; 29 30 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 all = asSet(rootId, midId, leftId, rightId, childId); 40 41 42 54 private final ConfigurationModel diamondModel = new ConfigurationModel(); 55 56 protected void setUp() throws Exception { 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 leftAndRight = asSet(leftId, rightId); 65 diamondModel.addConfiguation(childId, leftAndRight, leftAndRight); 66 67 diamondModel.load(rootId); 69 diamondModel.start(rootId); 70 diamondModel.load(childId); 71 diamondModel.start(childId); 72 73 assertEquals(all, diamondModel.getLoaded()); 75 assertEquals(all, diamondModel.getStarted()); 76 77 assertEquals(asSet(rootId, childId), diamondModel.getUserLoaded()); 79 assertEquals(asSet(rootId, childId), diamondModel.getUserStarted()); 80 } 81 82 public void testStopChild() throws NoSuchConfigException { 83 LinkedHashSet stopList = diamondModel.stop(childId); 84 85 assertEquals(asSet(rootId), diamondModel.getStarted()); 87 assertEquals(asSet(rootId), diamondModel.getUserStarted()); 88 89 assertEquals(all, diamondModel.getLoaded()); 91 assertEquals(asSet(rootId, childId), diamondModel.getUserLoaded()); 92 93 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 stopList = diamondModel.stop(leftId); 104 105 assertEquals(asSet(rootId), diamondModel.getStarted()); 107 assertEquals(asSet(rootId), diamondModel.getUserStarted()); 108 109 assertEquals(all, diamondModel.getLoaded()); 111 assertEquals(asSet(rootId, childId), diamondModel.getUserLoaded()); 112 113 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 startList = diamondModel.start(rightId); 124 assertTrue(startList.isEmpty()); 125 126 LinkedHashSet stopList = diamondModel.stop(leftId); 127 128 assertEquals(asSet(rootId, midId, rightId), diamondModel.getStarted()); 130 assertEquals(asSet(rootId, rightId), diamondModel.getUserStarted()); 131 132 assertEquals(all, diamondModel.getLoaded()); 134 assertEquals(asSet(rootId, rightId, childId), diamondModel.getUserLoaded()); 135 136 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 loadList = diamondModel.load(rightChildId); 147 assertEquals(asSet(rightChildId), asSet(loadList)); 148 LinkedHashSet startList = diamondModel.start(rightChildId); 149 assertEquals(asSet(rightChildId), asSet(startList)); 150 151 LinkedHashSet stopList = diamondModel.stop(leftId); 152 153 assertEquals(asSet(rootId, midId, rightId, rightChildId), diamondModel.getStarted()); 155 assertEquals(asSet(rootId, rightChildId), diamondModel.getUserStarted()); 156 157 assertEquals(asSet(all, rightChildId), diamondModel.getLoaded()); 159 assertEquals(asSet(rootId, childId, rightChildId), diamondModel.getUserLoaded()); 160 161 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 collection, Collection unexpected) { 169 for (Iterator iterator = unexpected.iterator(); iterator.hasNext();) { 170 Object 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 before, Object after, LinkedHashSet set) { 177 List list = new ArrayList (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 asSet(Object a) { 191 return asSet(new Object [] {a}); 192 } 193 194 public static LinkedHashSet asSet(Object a, Object b) { 195 return asSet(new Object [] {a, b}); 196 } 197 public static LinkedHashSet asSet(Object a, Object b, Object c) { 198 return asSet(new Object [] {a, b, c}); 199 } 200 public static LinkedHashSet asSet(Object a, Object b, Object c, Object d) { 201 return asSet(new Object [] {a, b, c, d}); 202 } 203 public static LinkedHashSet asSet(Object a, Object b, Object c, Object d, Object e) { 204 return asSet(new Object [] {a, b, c, d, e}); 205 } 206 public static LinkedHashSet asSet(Object [] list) { 207 LinkedHashSet set = new LinkedHashSet (); 208 for (int i = 0; i < list.length; i++) { 209 Object o = list[i]; 210 if (o instanceof Collection ) { 211 set.addAll((Collection )o); 212 } else { 213 set.add(o); 214 } 215 } 216 return set; 217 } 218 } 219 | Popular Tags |