1 17 package org.apache.geronimo.kernel.config; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 23 import java.util.Collections ; 24 import java.util.List ; 25 26 import javax.xml.namespace.QName ; 27 28 import junit.framework.TestCase; 29 30 import org.apache.geronimo.gbean.AbstractName; 31 import org.apache.geronimo.gbean.AbstractNameQuery; 32 import org.apache.geronimo.gbean.GBeanData; 33 import org.apache.geronimo.kernel.Jsr77Naming; 34 import org.apache.geronimo.kernel.MockGBean; 35 import org.apache.geronimo.kernel.config.xstream.XStreamConfigurationMarshaler; 36 import org.apache.geronimo.kernel.repository.Artifact; 37 38 41 public class ConfigurationUtilTest extends TestCase { 42 private XStreamConfigurationMarshaler xstreamConfigurationMarshaler = new XStreamConfigurationMarshaler(); 43 private SerializedConfigurationMarshaler serializedConfigurationMarshaler = new SerializedConfigurationMarshaler(); 44 private static Artifact artifact3 = new Artifact("test", "3", "3.3", "bar"); 45 private static final Jsr77Naming naming = new Jsr77Naming(); 46 47 public void test() throws Exception { 48 ConfigurationData configurationData = createConfigurationData(serializedConfigurationMarshaler); 49 ConfigurationData data = copy(configurationData, serializedConfigurationMarshaler, serializedConfigurationMarshaler); 50 assertEquals(data, configurationData); 51 52 configurationData = createConfigurationData(xstreamConfigurationMarshaler); 53 ByteArrayOutputStream out = new ByteArrayOutputStream (); 54 xstreamConfigurationMarshaler.writeConfigurationData(configurationData, out); 55 data = copy(configurationData, xstreamConfigurationMarshaler, xstreamConfigurationMarshaler); 56 assertEquals(data, configurationData); 57 58 configurationData = createConfigurationData(serializedConfigurationMarshaler); 59 data = copy(configurationData, serializedConfigurationMarshaler, xstreamConfigurationMarshaler); 60 assertEquals(data, configurationData); 61 62 configurationData = createConfigurationData(xstreamConfigurationMarshaler); 63 data = copy(configurationData, xstreamConfigurationMarshaler, serializedConfigurationMarshaler); 64 assertEquals(data, configurationData); 65 } 66 67 private void assertEquals(ConfigurationData data, ConfigurationData configurationData) throws InvalidConfigException { 68 List gbeans; 69 gbeans = data.getGBeans(getClass().getClassLoader()); 70 assertEquals(configurationData.getId(), data.getId()); 71 ConfigurationData data3 = (ConfigurationData) data.getChildConfigurations().get(artifact3); 72 gbeans = data3.getGBeans(getClass().getClassLoader()); 73 assertEquals(new QName ("namespaceURI", "localPart"), ((GBeanData)gbeans.get(0)).getAttribute("someObject")); 74 } 75 76 private static ConfigurationData copy(ConfigurationData configurationData, ConfigurationMarshaler writer, ConfigurationMarshaler reader) throws IOException , ClassNotFoundException { 77 ByteArrayOutputStream out = new ByteArrayOutputStream (); 78 writer.writeConfigurationData(configurationData, out); 79 ByteArrayInputStream in = new ByteArrayInputStream (out.toByteArray()); 80 ConfigurationData data = reader.readConfigurationData(in); 81 return data; 82 } 83 84 private static ConfigurationData createConfigurationData(ConfigurationMarshaler marshaler) throws Exception { 85 Artifact artifact1 = new Artifact("test", "1", "1.1", "bar"); 86 ConfigurationData configurationData = new ConfigurationData(artifact1, naming, marshaler.newGBeanState(Collections.EMPTY_SET)); 87 88 GBeanData mockBean1 = configurationData.addGBean("MyMockGMBean1", MockGBean.getGBeanInfo()); 89 AbstractName gbeanName1 = mockBean1.getAbstractName(); 90 mockBean1.setAttribute("value", "1234"); 91 mockBean1.setAttribute("name", "child"); 92 mockBean1.setAttribute("finalInt", new Integer (1)); 93 94 GBeanData mockBean2 = configurationData.addGBean("MyMockGMBean2", MockGBean.getGBeanInfo()); 95 mockBean2.setAttribute("value", "5678"); 96 mockBean2.setAttribute("name", "Parent"); 97 mockBean2.setAttribute("finalInt", new Integer (3)); 98 mockBean2.setAttribute("someObject", new GBeanData(gbeanName1, MockGBean.getGBeanInfo())); 99 mockBean2.setReferencePattern("MockEndpoint", gbeanName1); 100 mockBean2.setReferencePattern("EndpointCollection", new AbstractNameQuery(gbeanName1, MockGBean.getGBeanInfo().getInterfaces())); 101 102 103 ConfigurationData childConfigurationData = new ConfigurationData(artifact3, naming, marshaler.newGBeanState(Collections.EMPTY_SET)); 104 configurationData.addChildConfiguration(childConfigurationData); 105 GBeanData childConfigurationGBean = childConfigurationData.addGBean("ChildConfigurationGBean", MockGBean.getGBeanInfo()); 106 childConfigurationGBean.setAttribute("name", "foo"); 107 childConfigurationGBean.setAttribute("someObject", new QName ("namespaceURI", "localPart")); 108 109 return configurationData; 110 } 111 } 112 | Popular Tags |