1 package org.jboss.cache.eviction; 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 import org.jboss.cache.config.EvictionConfig; 9 import org.jboss.cache.config.EvictionRegionConfig; 10 import org.jboss.cache.factories.XmlConfigurationParser; 11 import org.jboss.cache.xml.XmlHelper; 12 import org.w3c.dom.Element ; 13 14 import java.util.List ; 15 16 23 public class RegionManagerTest extends TestCase 24 { 25 private final Fqn DEFAULT_REGION = Fqn.ROOT; 26 Fqn A_B_C = Fqn.fromString("/a/b/c"); 27 Fqn A_B = Fqn.fromString("/a/b"); 28 Fqn A_BC = Fqn.fromString("/a/bc"); 29 Fqn AOP = Fqn.fromString("/aop"); 30 31 32 EvictionPolicy policy; 33 EvictionPolicyConfig config; 34 35 public RegionManagerTest(String s) 36 { 37 super(s); 38 } 39 40 public void setUp() throws Exception 41 { 42 super.setUp(); 43 policy = new DummyEvictionPolicy(); 44 config = new DummyEvictionConfiguration(); 45 } 46 47 public void tearDown() throws Exception 48 { 49 super.tearDown(); 50 } 51 52 public void testCreateRegion() 53 { 54 RegionManager regionManager = new RegionManager(); 55 regionManager.setUsingEvictions(true); 56 regionManager.getRegion(DEFAULT_REGION, true).setEvictionPolicy(config); 57 regionManager.getRegion(A_B_C, true).setEvictionPolicy(config); 58 regionManager.getRegion(A_B, true).setEvictionPolicy(config); 59 regionManager.getRegion(AOP, true).setEvictionPolicy(config); 60 61 List <Region> regions = regionManager.getAllRegions(); 62 assertEquals("Region size ", 4, regions.size()); 63 } 64 65 public void testCreateRegion2() 66 { 67 68 RegionManager regionManager = new RegionManager(); 69 regionManager.setUsingEvictions(true); 70 regionManager.getRegion(A_B_C, true).setEvictionPolicy(config); 71 regionManager.getRegion(A_B, true).setEvictionPolicy(config); 72 regionManager.getRegion(DEFAULT_REGION, true).setEvictionPolicy(config); 73 74 List <Region> regions = regionManager.getAllRegions(); 75 assertEquals("Region size ", 3, regions.size()); 76 assertEquals("Region 0", DEFAULT_REGION, regions.get(0).getFqn()); 77 assertEquals("Region 1 ", A_B, regions.get(1).getFqn()); 78 assertEquals("Region 2 ", A_B_C, regions.get(2).getFqn()); 79 Region region = regionManager.getRegion("/a/b/c/d", false); 80 assertNotNull("Region ", region); 81 assertEquals("Region ", A_B_C, region.getFqn()); 82 region = regionManager.getRegion(A_B, false); 83 assertNotNull("Region ", region); 84 assertEquals("Region ", A_B, region.getFqn()); 85 region = regionManager.getRegion("/a", false); 86 assertNotNull("Region ", region); 88 assertEquals("Region ", DEFAULT_REGION, region.getFqn()); 89 } 90 91 public void testNoDefaultRegion() 92 { 93 RegionManager regionManager = new RegionManager(); 94 regionManager.setUsingEvictions(true); 95 regionManager.getRegion(A_B_C, true).setEvictionPolicy(config); 96 regionManager.getRegion(A_B, true).setEvictionPolicy(config); 97 98 try 99 { 100 regionManager.getRegion("/a", false); 101 } 102 catch (Exception e) 103 { 104 return; 106 } 107 fail("If we don't configure the default region, we should throw a RTE!"); 108 } 109 110 public void testGetRegion() 111 { 112 RegionManager regionManager = new RegionManager(); 113 regionManager.setUsingEvictions(true); 114 regionManager.getRegion(DEFAULT_REGION, true).setEvictionPolicy(config); 115 regionManager.getRegion(A_BC, true).setEvictionPolicy(config); 116 regionManager.getRegion(A_B, true).setEvictionPolicy(config); 117 118 Region region = regionManager.getRegion(A_BC, true); 119 assertNotSame("Region ", DEFAULT_REGION, region.getFqn()); 120 } 121 122 public void testConfigureWithXML() throws Exception 123 { 124 String xml = "<region name=\"/test/\" policyClass=\"org.jboss.cache.eviction.LFUPolicy\">" + 126 "<attribute name=\"minNodes\">10</attribute>" + 127 "<attribute name=\"maxNodes\">20</attribute>" + 128 "</region>"; 129 Element element = XmlHelper.stringToElement(xml); 130 RegionManager regionManager = new RegionManager(); 131 regionManager.setUsingEvictions(true); 132 EvictionRegionConfig erc = XmlConfigurationParser.parseEvictionRegionConfig(element, null, EvictionConfig.EVENT_QUEUE_SIZE_DEFAULT); 133 Region region = regionManager.getRegion(erc.getRegionFqn(), true); 134 region.setEvictionPolicy(erc.getEvictionPolicyConfig()); 135 136 assertTrue(region.getEvictionPolicy() instanceof LFUPolicy); 137 assertTrue(region.getEvictionPolicyConfig() instanceof LFUConfiguration); 138 LFUConfiguration config = (LFUConfiguration) region.getEvictionPolicyConfig(); 139 assertEquals(20, config.getMaxNodes()); 140 assertEquals(10, config.getMinNodes()); 141 assertEquals(Fqn.fromString("/test/"), region.getFqn()); 142 143 CacheImpl cache = new CacheImpl(); 144 145 xml = "<region name=\"abc\">" + 147 "<attribute name=\"minNodes\">10</attribute>" + 148 "<attribute name=\"maxNodes\">20</attribute>" + 149 "</region>"; 150 element = XmlHelper.stringToElement(xml); 151 erc = XmlConfigurationParser.parseEvictionRegionConfig(element, "org.jboss.cache.eviction.LFUPolicy", EvictionConfig.EVENT_QUEUE_SIZE_DEFAULT); 152 regionManager = new RegionManager(); 153 regionManager.setUsingEvictions(true); 154 region = regionManager.getRegion(erc.getRegionFqn(), true); 155 region.setEvictionPolicy(erc.getEvictionPolicyConfig()); 156 157 assertTrue(region.getEvictionPolicy() instanceof LFUPolicy); 158 assertTrue(region.getEvictionPolicyConfig() instanceof LFUConfiguration); 159 config = (LFUConfiguration) region.getEvictionPolicyConfig(); 160 assertEquals(20, config.getMaxNodes()); 161 assertEquals(10, config.getMinNodes()); 162 assertEquals(Fqn.fromString("/abc/"), region.getFqn()); 163 } 164 165 public void testRegionOrdering() throws Exception 166 { 167 Fqn A_B_C_D_E = Fqn.fromString("/a/b/c/d/e/"); 168 Fqn A_B_C_D = Fqn.fromString("/a/b/c/d/"); 169 170 RegionManager rm = new RegionManager(); 171 rm.setUsingEvictions(true); 172 rm.getRegion(DEFAULT_REGION, true).setEvictionPolicy(config); 173 rm.getRegion(A_B_C_D_E, true).setEvictionPolicy(config); 174 rm.getRegion(A_B_C_D, true).setEvictionPolicy(config); 175 rm.getRegion(A_B_C, true).setEvictionPolicy(config); 176 177 Region region = rm.getRegion("/a/b/c/d/e/f", false); 178 Region region2 = rm.getRegion("/e/f/g", false); 179 180 assertEquals(A_B_C_D_E, region.getFqn()); 181 assertEquals(DEFAULT_REGION, region2.getFqn()); 182 183 List <Region> regions = rm.getAllRegions(); 184 for (int i = 0; i < regions.size(); i++) 185 { 186 switch (i) 187 { 188 case 0: 189 assertEquals(DEFAULT_REGION, regions.get(i).getFqn()); 190 break; 191 case 1: 192 assertEquals(A_B_C, regions.get(i).getFqn()); 193 break; 194 case 2: 195 assertEquals(A_B_C_D, regions.get(i).getFqn()); 196 break; 197 case 3: 198 assertEquals(A_B_C_D_E, regions.get(i).getFqn()); 199 break; 200 default: 201 fail("This error condition should never be reached"); 202 break; 203 } 204 } 205 } 206 } 207 | Popular Tags |