KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > configuration > LocalAttributeManagerTest


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.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 JavaDoc;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.Arrays JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.util.HashSet JavaDoc;
41 import java.util.LinkedHashSet JavaDoc;
42 import java.util.Set JavaDoc;
43
44 /**
45  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
46  */

47 public class LocalAttributeManagerTest extends TestCase {
48     private static final String JavaDoc 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 JavaDoc {
57         // should load by default
58
Set JavaDoc originalDatas = new HashSet JavaDoc();
59         GBeanData gbeanData = new GBeanData(gbeanName, GBEAN_INFO);
60         originalDatas.add(gbeanData);
61
62         Set JavaDoc newDatas;
63         newDatas = new HashSet JavaDoc(localAttributeManager.applyOverrides(configurationName, originalDatas, getClass().getClassLoader()));
64         assertEquals(1, newDatas.size());
65         assertEquals(originalDatas, newDatas);
66
67         // declare an attribute value so this configuration will exist in the store
68
String JavaDoc attributeValue = "attribute value";
69         localAttributeManager.addConfiguration(configurationName);
70         localAttributeManager.setValue(configurationName, gbeanName, attributeInfo, attributeValue);
71
72         // should still load
73
newDatas = new HashSet JavaDoc(localAttributeManager.applyOverrides(configurationName, originalDatas, getClass().getClassLoader()));
74         assertEquals(1, newDatas.size());
75         assertEquals(originalDatas, newDatas);
76
77         // remove the configuration from the store
78
localAttributeManager.removeConfiguration(configurationName);
79
80         // should still get the same gbeans, config list and gbean attribute override functions are separate interfaces.
81
newDatas = new HashSet JavaDoc(localAttributeManager.applyOverrides(configurationName, originalDatas, getClass().getClassLoader()));
82         assertEquals(1, newDatas.size());
83         assertEquals(originalDatas, newDatas);
84     }
85
86     public void testGBeanShouldLoad() throws Exception JavaDoc {
87         ObjectName JavaDoc objectName = ObjectName.getInstance(":name=gbean2");
88         AbstractName gbeanName2 = new AbstractName(configurationName, objectName.getKeyPropertyList(), objectName);
89
90         // should load by default
91
Set JavaDoc originalDatas = new HashSet JavaDoc();
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 JavaDoc newDatas;
98         newDatas = new HashSet JavaDoc(localAttributeManager.applyOverrides(configurationName, originalDatas, getClass().getClassLoader()));
99         assertEquals(2, newDatas.size());
100         assertEquals(originalDatas, newDatas);
101
102         // declare an attribute value so this configuration will exist in the store
103
String JavaDoc attributeValue = "attribute value";
104         localAttributeManager.addConfiguration(configurationName);
105         localAttributeManager.setValue(configurationName, gbeanName, attributeInfo, attributeValue);
106
107         // should still load
108
newDatas = new HashSet JavaDoc(localAttributeManager.applyOverrides(configurationName, originalDatas, getClass().getClassLoader()));
109         assertEquals(2, newDatas.size());
110         assertEquals(originalDatas, newDatas);
111
112         // set the gbean to not load
113
localAttributeManager.setShouldLoad(configurationName, gbeanName, false);
114
115         // should not load
116
newDatas = new HashSet JavaDoc(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 JavaDoc {
124         String JavaDoc attributeValue = "attribute value";
125         localAttributeManager.setValue(configurationName, gbeanName, attributeInfo, attributeValue);
126         Collection JavaDoc gbeanDatas = new ArrayList JavaDoc();
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 JavaDoc {
134         ObjectName JavaDoc referencePatternObjectName = new ObjectName JavaDoc(":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 JavaDoc gbeanDatas = new ArrayList JavaDoc();
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 JavaDoc {
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 JavaDoc(Arrays.asList(new AbstractName[]{referencePattern1, referencePattern2})));
151         localAttributeManager.setReferencePatterns(configurationName, gbeanName, referenceInfo, referencePatterns);
152         Collection JavaDoc gbeanDatas = new ArrayList JavaDoc();
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 JavaDoc {
160         String JavaDoc 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 JavaDoc gbeanDatas = new ArrayList JavaDoc();
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 JavaDoc {
183         String JavaDoc 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             //pass
191
}
192     }
193
194     protected void setUp() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc {
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 JavaDoc.class);
214         infoFactory.addAttribute("attribute", String JavaDoc.class, true);
215         GBEAN_INFO = infoFactory.getBeanInfo();
216     }
217
218     public static GBeanInfo getGBeanInfo() {
219         return GBEAN_INFO;
220     }
221
222     public String JavaDoc getAttribute() {
223         throw new UnsupportedOperationException JavaDoc("Fake method for gbean info");
224     }
225
226     public void setAttribute(String JavaDoc attribute) {
227         throw new UnsupportedOperationException JavaDoc("Fake method for gbean info");
228     }
229
230     public void setReference(String JavaDoc reference) {
231         throw new UnsupportedOperationException JavaDoc("Fake method for gbean info");
232     }
233 }
234
Popular Tags