1 17 18 package org.apache.geronimo.gbean; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.FileInputStream ; 23 import java.io.ObjectInputStream ; 24 import java.io.ObjectOutputStream ; 25 import java.util.List ; 26 import java.util.Set ; 27 28 import org.apache.geronimo.testsupport.TestSupport; 29 30 33 public class GBeanInfoTest extends TestSupport { 34 private static final String CONSTRUCTOR_ARG_0 = "ConstructorArg_0"; 35 private static final String CONSTRUCTOR_ARG_1 = "ConstructorArg_1"; 36 37 public void testGetGBeanInfo() { 38 GBeanInfo gbeanInfo = GBeanInfo.getGBeanInfo(MockGBean.class.getName(), MockGBean.class.getClassLoader()); 40 assertNotNull(gbeanInfo); 41 42 try { 44 GBeanInfo.getGBeanInfo("ClassThatDoesNotExist", this.getClass().getClassLoader()); 45 fail("InvalidConfigurationException expected"); 46 } catch (InvalidConfigurationException expected) { 47 } 48 49 try { 52 GBeanInfo.getGBeanInfo(String .class.getName(), this.getClass().getClassLoader()); 53 fail("InvalidConfigurationException expected"); 54 } catch (InvalidConfigurationException expected) { 55 } 56 } 57 58 public void testGetName() { 59 assertEquals(MockGBean.class.getName(), gbeanInfo.getName()); 60 } 61 62 public void testGetClassName() { 63 assertEquals(MockGBean.class.getName(), gbeanInfo.getClassName()); 64 } 65 66 public void testGetAttributeSet() { 67 Set attrSet = gbeanInfo.getAttributes(); 68 assertEquals(6, attrSet.size()); 69 assertTrue(attrSet.contains(persistentAttrInfo)); 70 assertTrue(attrSet.contains(nonPersistentAttrInfo)); 71 } 72 73 public void testGetPersistentAttributes() { 74 List attrList = gbeanInfo.getPersistentAttributes(); 75 assertEquals(3, attrList.size()); 76 } 77 78 public void testGetConstructor() { 79 GConstructorInfo gctorInfo = gbeanInfo.getConstructor(); 80 List attrNameList = gctorInfo.getAttributeNames(); 81 assertEquals(2, attrNameList.size()); 82 assertEquals(CONSTRUCTOR_ARG_0, attrNameList.get(0)); 83 assertEquals(CONSTRUCTOR_ARG_1, attrNameList.get(1)); 84 } 85 86 public void testGetOperationsSet() { 87 Set gbeanOpSet = gbeanInfo.getOperations(); 88 assertEquals(1, gbeanOpSet.size()); 89 assertTrue(gbeanOpSet.contains(opInfo)); 90 } 91 92 public void testGetReferencesSet() { 93 Set gbeanRefSet = gbeanInfo.getReferences(); 94 assertEquals(1, gbeanRefSet.size()); 95 GReferenceInfo newRefInfo = (GReferenceInfo) gbeanRefSet.iterator().next(); 96 assertEquals(refInfo.getName(), newRefInfo.getName()); 97 } 98 99 public void testToString() { 100 assertNotNull(gbeanInfo.toString()); 101 assertEquals(gbeanInfo.toString(), MockGBean.getGBeanInfo().toString()); 102 } 103 104 public void xtestBackwardCompatibility() throws Exception { 105 FileInputStream fis = new FileInputStream (resolveFile("src/test/data/gbeaninfo/SERIALIZATION_-6198804067155550221.ser")); 106 ObjectInputStream is = new ObjectInputStream (fis); 107 GBeanInfo beanInfo = (GBeanInfo) is.readObject(); 108 assertEquals(GBeanInfo.PRIORITY_NORMAL, beanInfo.getPriority()); 109 } 110 111 public void testCurrentSerialization() throws Exception { 112 GBeanInfo beanInfo = MockGBean.GBEAN_INFO; 113 114 ByteArrayOutputStream memOut = new ByteArrayOutputStream (); 115 ObjectOutputStream os = new ObjectOutputStream (memOut); 116 os.writeObject(beanInfo); 117 118 ByteArrayInputStream memIn = new ByteArrayInputStream (memOut.toByteArray()); 119 ObjectInputStream is = new ObjectInputStream (memIn); 120 beanInfo = (GBeanInfo) is.readObject(); 121 assertEquals(GBeanInfo.PRIORITY_CLASSLOADER, beanInfo.getPriority()); 122 } 123 124 GBeanInfo gbeanInfo; 125 126 final static String nonPersistentAttrName = "nonPersistentAttribute"; 127 128 final static GAttributeInfo nonPersistentAttrInfo = new GAttributeInfo(nonPersistentAttrName, String .class.getName(), false, false, "getFoo", "setFoo"); 129 130 final static String persistentAttrName = "persistentAttribute"; 131 132 final static GAttributeInfo persistentAttrInfo = new GAttributeInfo(persistentAttrName, String .class.getName(), true, false, "getFoo", "setFoo"); 133 134 final static GOperationInfo opInfo = new GOperationInfo("operation", "java.lang.Object"); 135 136 final static GReferenceInfo refInfo = new GReferenceInfo("reference", String .class.getName(), String .class.getName(), "setReference", "Fooifier"); 137 138 public void setUp() throws Exception { 139 super.setUp(); 140 gbeanInfo = MockGBean.getGBeanInfo(); 141 } 142 143 protected void tearDown() throws Exception { 144 gbeanInfo = null; 145 super.tearDown(); 146 } 147 148 public static final class MockGBean { 149 public static final GBeanInfo GBEAN_INFO; 150 151 static { 152 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(MockGBean.class); 153 154 infoFactory.addAttribute(nonPersistentAttrInfo); 155 infoFactory.addAttribute(persistentAttrInfo); 156 157 infoFactory.addOperation(opInfo); 158 159 infoFactory.addReference(refInfo); 160 161 infoFactory.addAttribute(CONSTRUCTOR_ARG_0, String .class, true); 162 infoFactory.addAttribute(CONSTRUCTOR_ARG_1, String .class, true); 163 infoFactory.setPriority(GBeanInfo.PRIORITY_CLASSLOADER); 164 infoFactory.setConstructor(new String []{CONSTRUCTOR_ARG_0, CONSTRUCTOR_ARG_1}); 165 166 167 GBEAN_INFO = infoFactory.getBeanInfo(); 168 } 169 170 public static GBeanInfo getGBeanInfo() { 171 return GBEAN_INFO; 172 } 173 174 public MockGBean(String ConstructorArg_0, String ConstructorArg_1) { 175 } 176 177 public void setReference(String reference) { 178 } 179 } 180 } 181 | Popular Tags |