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