KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > config > ConfigurationUtilTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.kernel.config;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.xml.namespace.QName JavaDoc;
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 /**
39  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
40  */

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 JavaDoc {
48         ConfigurationData configurationData = createConfigurationData(serializedConfigurationMarshaler);
49         ConfigurationData data = copy(configurationData, serializedConfigurationMarshaler, serializedConfigurationMarshaler);
50         assertEquals(data, configurationData);
51
52         configurationData = createConfigurationData(xstreamConfigurationMarshaler);
53         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
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 JavaDoc 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 JavaDoc("namespaceURI", "localPart"), ((GBeanData)gbeans.get(0)).getAttribute("someObject"));
74     }
75
76     private static ConfigurationData copy(ConfigurationData configurationData, ConfigurationMarshaler writer, ConfigurationMarshaler reader) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
77         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
78         writer.writeConfigurationData(configurationData, out);
79         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
80         ConfigurationData data = reader.readConfigurationData(in);
81         return data;
82     }
83
84     private static ConfigurationData createConfigurationData(ConfigurationMarshaler marshaler) throws Exception JavaDoc {
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 JavaDoc(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 JavaDoc(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 JavaDoc("namespaceURI", "localPart"));
108
109         return configurationData;
110     }
111 }
112
Popular Tags