1 17 package org.apache.geronimo.system.configuration; 18 19 import junit.framework.TestCase; 20 import org.apache.geronimo.gbean.GAttributeInfo; 21 import org.apache.geronimo.gbean.GBeanData; 22 import org.apache.geronimo.gbean.GBeanInfo; 23 import org.apache.geronimo.gbean.GBeanInfoBuilder; 24 import org.apache.geronimo.gbean.GReferenceInfo; 25 import org.apache.geronimo.gbean.AbstractNameQuery; 26 import org.apache.geronimo.gbean.AbstractName; 27 import org.apache.geronimo.gbean.ReferencePatterns; 28 import org.apache.geronimo.kernel.repository.Artifact; 29 import org.apache.geronimo.kernel.Naming; 30 import org.apache.geronimo.kernel.Jsr77Naming; 31 import org.apache.geronimo.kernel.config.InvalidConfigException; 32 import org.apache.geronimo.system.serverinfo.BasicServerInfo; 33 34 import javax.management.ObjectName ; 35 36 import java.util.ArrayList ; 37 import java.util.Arrays ; 38 import java.util.Collection ; 39 import java.util.Collections ; 40 import java.util.HashSet ; 41 import java.util.LinkedHashSet ; 42 import java.util.Set ; 43 44 47 public class LocalAttributeManagerTest extends TestCase { 48 private static final String basedir = System.getProperties().getProperty("basedir", "."); 49 50 private LocalAttributeManager localAttributeManager; 51 private Artifact configurationName; 52 private AbstractName gbeanName; 53 private GAttributeInfo attributeInfo; 54 private GReferenceInfo referenceInfo; 55 56 public void testConfigurationShouldLoad() throws Exception { 57 Set originalDatas = new HashSet (); 59 GBeanData gbeanData = new GBeanData(gbeanName, GBEAN_INFO); 60 originalDatas.add(gbeanData); 61 62 Set newDatas; 63 newDatas = new HashSet (localAttributeManager.applyOverrides(configurationName, originalDatas, getClass().getClassLoader())); 64 assertEquals(1, newDatas.size()); 65 assertEquals(originalDatas, newDatas); 66 67 String attributeValue = "attribute value"; 69 localAttributeManager.addConfiguration(configurationName); 70 localAttributeManager.setValue(configurationName, gbeanName, attributeInfo, attributeValue); 71 72 newDatas = new HashSet (localAttributeManager.applyOverrides(configurationName, originalDatas, getClass().getClassLoader())); 74 assertEquals(1, newDatas.size()); 75 assertEquals(originalDatas, newDatas); 76 77 localAttributeManager.removeConfiguration(configurationName); 79 80 newDatas = new HashSet (localAttributeManager.applyOverrides(configurationName, originalDatas, getClass().getClassLoader())); 82 assertEquals(1, newDatas.size()); 83 assertEquals(originalDatas, newDatas); 84 } 85 86 public void testGBeanShouldLoad() throws Exception { 87 ObjectName objectName = ObjectName.getInstance(":name=gbean2"); 88 AbstractName gbeanName2 = new AbstractName(configurationName, objectName.getKeyPropertyList(), objectName); 89 90 Set originalDatas = new HashSet (); 92 GBeanData gbeanData = new GBeanData(gbeanName, GBEAN_INFO); 93 GBeanData gbeanData2 = new GBeanData(gbeanName2, GBEAN_INFO); 94 originalDatas.add(gbeanData); 95 originalDatas.add(gbeanData2); 96 97 Set newDatas; 98 newDatas = new HashSet (localAttributeManager.applyOverrides(configurationName, originalDatas, getClass().getClassLoader())); 99 assertEquals(2, newDatas.size()); 100 assertEquals(originalDatas, newDatas); 101 102 String attributeValue = "attribute value"; 104 localAttributeManager.addConfiguration(configurationName); 105 localAttributeManager.setValue(configurationName, gbeanName, attributeInfo, attributeValue); 106 107 newDatas = new HashSet (localAttributeManager.applyOverrides(configurationName, originalDatas, getClass().getClassLoader())); 109 assertEquals(2, newDatas.size()); 110 assertEquals(originalDatas, newDatas); 111 112 localAttributeManager.setShouldLoad(configurationName, gbeanName, false); 114 115 newDatas = new HashSet (localAttributeManager.applyOverrides(configurationName, originalDatas, getClass().getClassLoader())); 117 assertEquals(1, newDatas.size()); 118 GBeanData newGBeanData = (GBeanData) newDatas.iterator().next(); 119 assertSame(gbeanData2, newGBeanData); 120 assertEquals(attributeValue, gbeanData.getAttribute(attributeInfo.getName())); 121 } 122 123 public void testSetAttribute() throws Exception { 124 String attributeValue = "attribute value"; 125 localAttributeManager.setValue(configurationName, gbeanName, attributeInfo, attributeValue); 126 Collection gbeanDatas = new ArrayList (); 127 GBeanData gbeanData = new GBeanData(gbeanName, GBEAN_INFO); 128 gbeanDatas.add(gbeanData); 129 gbeanDatas = localAttributeManager.applyOverrides(configurationName, gbeanDatas, getClass().getClassLoader()); 130 assertEquals(attributeValue, gbeanData.getAttribute(attributeInfo.getName())); 131 } 132 133 public void testSetReference() throws Exception { 134 ObjectName referencePatternObjectName = new ObjectName (":name=referencePattern"); 135 AbstractName referencePattern = new AbstractName(configurationName, referencePatternObjectName.getKeyPropertyList(), referencePatternObjectName); 136 ReferencePatterns referencePatterns = new ReferencePatterns(referencePattern); 137 localAttributeManager.setReferencePatterns(configurationName, gbeanName, referenceInfo, referencePatterns); 138 Collection gbeanDatas = new ArrayList (); 139 GBeanData gbeanData = new GBeanData(gbeanName, GBEAN_INFO); 140 gbeanDatas.add(gbeanData); 141 gbeanDatas = localAttributeManager.applyOverrides(configurationName, gbeanDatas, getClass().getClassLoader()); 142 assertEquals(referencePatterns, gbeanData.getReferencePatterns(referenceInfo.getName())); 143 } 144 145 public void testSetReferences() throws Exception { 146 Naming naming = new Jsr77Naming(); 147 148 AbstractName referencePattern1 = naming.createRootName(gbeanName.getArtifact(), "name", "referencePattern1"); 149 AbstractName referencePattern2 = naming.createRootName(gbeanName.getArtifact(), "name", "referencePattern2"); 150 ReferencePatterns referencePatterns = new ReferencePatterns(new LinkedHashSet (Arrays.asList(new AbstractName[]{referencePattern1, referencePattern2}))); 151 localAttributeManager.setReferencePatterns(configurationName, gbeanName, referenceInfo, referencePatterns); 152 Collection gbeanDatas = new ArrayList (); 153 GBeanData gbeanData = new GBeanData(gbeanName, GBEAN_INFO); 154 gbeanDatas.add(gbeanData); 155 gbeanDatas = localAttributeManager.applyOverrides(configurationName, gbeanDatas, getClass().getClassLoader()); 156 assertEquals(referencePatterns, gbeanData.getReferencePatterns(referenceInfo.getName())); 157 } 158 159 public void testAddGBean() throws Exception { 160 String attributeValue = "attribute value"; 161 AbstractNameQuery referencePattern = new AbstractNameQuery(LocalAttributeManagerTest.class.getName()); 162 163 GBeanData gbeanData = new GBeanData(gbeanName, GBEAN_INFO); 164 gbeanData.setAttribute(attributeInfo.getName(), attributeValue); 165 gbeanData.setReferencePattern(referenceInfo.getName(), referencePattern); 166 localAttributeManager.addConfiguration(configurationName); 167 localAttributeManager.addGBean(configurationName, gbeanData); 168 169 170 Collection gbeanDatas = new ArrayList (); 171 gbeanDatas = localAttributeManager.applyOverrides(configurationName, gbeanDatas, getClass().getClassLoader()); 172 assertEquals(1, gbeanDatas.size()); 173 GBeanData newGBeanData = (GBeanData) gbeanDatas.iterator().next(); 174 175 assertNotSame(gbeanData, newGBeanData); 176 assertSame(gbeanData.getGBeanInfo(), newGBeanData.getGBeanInfo()); 177 assertSame(gbeanData.getAbstractName(), newGBeanData.getAbstractName()); 178 assertEquals(Collections.singleton(referencePattern), newGBeanData.getReferencePatterns(referenceInfo.getName()).getPatterns()); 179 assertEquals(attributeValue, newGBeanData.getAttribute(attributeInfo.getName())); 180 } 181 182 public void testBadGBeanSpec() throws Exception { 183 String attributeValue = "attribute value"; 184 localAttributeManager.addConfiguration(configurationName); 185 localAttributeManager.setValue(configurationName, gbeanName, attributeInfo, attributeValue); 186 try { 187 localAttributeManager.applyOverrides(configurationName, Collections.EMPTY_SET, getClass().getClassLoader()); 188 fail("no gbeans were specified in the 'plan' so overrides should fail"); 189 } catch (InvalidConfigException e) { 190 } 192 } 193 194 protected void setUp() throws Exception { 195 super.setUp(); 196 localAttributeManager = new LocalAttributeManager("target/test-config.xml", false, new BasicServerInfo(basedir)); 197 configurationName = Artifact.create("configuration/name/1/car"); 198 ObjectName objectName = ObjectName.getInstance(":name=gbean"); 199 gbeanName = new AbstractName(configurationName, objectName.getKeyPropertyList(), objectName); 200 attributeInfo = GBEAN_INFO.getAttribute("attribute"); 201 referenceInfo = GBEAN_INFO.getReference("reference"); 202 } 203 204 protected void tearDown() throws Exception { 205 super.tearDown(); 206 localAttributeManager = null; 207 } 208 209 public static final GBeanInfo GBEAN_INFO; 210 211 static { 212 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(LocalAttributeManagerTest.class); 213 infoFactory.addReference("reference", String .class); 214 infoFactory.addAttribute("attribute", String .class, true); 215 GBEAN_INFO = infoFactory.getBeanInfo(); 216 } 217 218 public static GBeanInfo getGBeanInfo() { 219 return GBEAN_INFO; 220 } 221 222 public String getAttribute() { 223 throw new UnsupportedOperationException ("Fake method for gbean info"); 224 } 225 226 public void setAttribute(String attribute) { 227 throw new UnsupportedOperationException ("Fake method for gbean info"); 228 } 229 230 public void setReference(String reference) { 231 throw new UnsupportedOperationException ("Fake method for gbean info"); 232 } 233 } 234 | Popular Tags |