1 package org.jboss.cache.marshall; 2 3 import junit.framework.TestCase; 4 import org.jboss.cache.CacheImpl; 5 import org.jboss.cache.Fqn; 6 import org.jboss.cache.Region; 7 import org.jboss.cache.RegionManager; 8 9 import java.util.ArrayList ; 10 import java.util.Collections ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 17 public class RegionManagerTest extends TestCase 18 { 19 private final Fqn DEFAULT_REGION = Fqn.ROOT; 20 private RegionManager r; 21 22 public void setUp() throws Exception 23 { 24 CacheImpl c = new CacheImpl(); 25 c.create(); 26 r = new RegionManager(c); 27 } 28 29 public void tearDown() throws Exception 30 { 31 r = null; 32 } 33 34 35 public void testGetAllMarshallingRegions() 36 { 37 Fqn fqn1 = Fqn.fromString("/a/b/c"); 38 Fqn fqn2 = Fqn.fromString("/a/b"); 39 Fqn fqn3 = Fqn.fromString("/aop"); 40 41 List <Region> expected = new ArrayList <Region>(4); 42 43 Region region = r.getRegion(DEFAULT_REGION, true); 44 region.registerContextClassLoader(getClass().getClassLoader()); 45 assertEquals(DEFAULT_REGION, region.getFqn()); 46 expected.add(region); 47 48 region = r.getRegion(fqn1, true); 49 region.registerContextClassLoader(getClass().getClassLoader()); 50 assertEquals(fqn1, region.getFqn()); 51 expected.add(region); 52 53 region = r.getRegion(fqn2, true); 54 region.registerContextClassLoader(getClass().getClassLoader()); 55 assertEquals(fqn2, region.getFqn()); 56 expected.add(region); 57 58 region = r.getRegion(fqn3, true); 59 region.registerContextClassLoader(getClass().getClassLoader()); 60 assertEquals(fqn3, region.getFqn()); 61 expected.add(region); 62 63 Collections.sort(expected); 65 Iterator <Region> expectedRegions = expected.iterator(); 66 67 for (Region reg : r.getAllMarshallingRegions()) 68 { 69 assertSame("Unexpected region " + reg, expectedRegions.next(), reg); 70 } 71 72 assertFalse("Should not be expecting any more regions", expectedRegions.hasNext()); 73 } 74 75 public void testNoDefaultRegion() 76 { 77 Fqn fqn1 = Fqn.fromString("/a/b/c"); 78 Fqn fqn2 = Fqn.fromString("/a/b/"); 79 80 r.getRegion(fqn1, true); 81 r.getRegion(fqn2, true); 82 83 Region region = null; 84 try 85 { 86 region = r.getRegion("/a", false); 87 } 88 catch (Exception e) 89 { 90 fail("If we don't configure the default region, it still should be ok!"); 91 } 92 93 assertNull("Default region is not null!", region); 94 } 95 96 97 public void testGetParentRegion() 98 { 99 String fqn1 = "/a/b/c"; 100 String fqn2 = "/a/b"; 101 String fqn3 = "/a"; 102 103 r.getRegion(fqn1, true); 104 r.getRegion(fqn3, true); 105 106 Region region = r.getRegion(fqn2, false); 107 assertEquals("Should be the same region as in " + fqn3, r.getRegion(fqn3, false), region); 108 } 109 110 public void testRemoveRegion() 111 { 112 String fqn1 = "/a"; 113 String fqn2 = "/a/b"; 114 String fqn3 = "/a/b/c"; 115 116 Region r1 = r.getRegion(fqn1, true); 117 Region r2 = r.getRegion(fqn2, true); 118 Region r3 = r.getRegion(fqn3, true); 119 120 assertEquals("Expecting 3 regions", 3, r.getAllRegions().size()); 121 122 assertEquals(r3, r.getRegion(fqn3, false)); 124 125 r.removeRegion(fqn3); 126 127 assertEquals("Expecting 2 regions", 2, r.getAllRegions().size()); 128 129 assertEquals("Should have retrieved parent region", r2, r.getRegion(fqn3, false)); 131 132 r.removeRegion(fqn2); 133 134 assertEquals("Expecting 1 region", 1, r.getAllRegions().size()); 135 136 assertEquals("Should have retrieved parent region", r1, r.getRegion(fqn3, false)); 138 139 r.removeRegion(fqn1); 140 141 assertEquals("Expecting 0 regions", 0, r.getAllRegions().size()); 142 } 143 144 public void testGetRegionsMethods() 145 { 146 String f1 = "/a", f2 = "/b", f3 = "/c", f4 = "/d"; 147 148 r.setDefaultInactive(true); 149 150 Region r1 = r.getRegion(f1, true), r2 = r.getRegion(f2, true), r3 = r.getRegion(f3, true), r4 = r.getRegion(f4, true); 151 152 assertEquals("4 regions should exist", 4, r.getAllRegions().size()); 153 154 assertEquals("None of the regions should marshalling or active", 0, r.getAllMarshallingRegions().size()); 155 156 r3.registerContextClassLoader(getClass().getClassLoader()); 157 r3.activate(); 158 159 assertEquals("r3 should be marshalling and active", 1, r.getAllMarshallingRegions().size()); 160 assertSame("r3 should be marshalling and active", r3, r.getAllMarshallingRegions().get(0)); 161 162 r4.activate(); 164 assertEquals("r3 should be marshalling and active", 1, r.getAllMarshallingRegions().size()); 165 assertSame("r3 should be marshalling and active", r3, r.getAllMarshallingRegions().get(0)); 166 167 r2.registerContextClassLoader(getClass().getClassLoader()); 169 assertEquals("r3 should be marshalling and active", 1, r.getAllMarshallingRegions().size()); 170 assertSame("r3 should be marshalling and active", r3, r.getAllMarshallingRegions().get(0)); 171 172 r2.activate(); 173 174 assertEquals("r2 + r3 should be marshalling and active", 2, r.getAllMarshallingRegions().size()); 175 assertSame("r2 should be marshalling and active", r2, r.getAllMarshallingRegions().get(0)); 176 assertSame("r3 should be marshalling and active", r3, r.getAllMarshallingRegions().get(1)); 177 178 r4.registerContextClassLoader(getClass().getClassLoader()); 179 180 assertEquals("r2 + r3 + r4 should be marshalling and active", 3, r.getAllMarshallingRegions().size()); 181 assertSame("r2 should be marshalling and active", r2, r.getAllMarshallingRegions().get(0)); 182 assertSame("r3 should be marshalling and active", r3, r.getAllMarshallingRegions().get(1)); 183 assertSame("r4 should be marshalling and active", r4, r.getAllMarshallingRegions().get(2)); 184 185 } 186 187 } 188 | Popular Tags |