KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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
18 package org.apache.geronimo.system.configuration;
19
20 import java.io.BufferedOutputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.jar.JarOutputStream JavaDoc;
27 import java.util.zip.ZipEntry JavaDoc;
28 import java.util.Collections JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import junit.framework.TestCase;
32 import org.apache.geronimo.gbean.GBeanData;
33 import org.apache.geronimo.kernel.KernelFactory;
34 import org.apache.geronimo.kernel.Kernel;
35 import org.apache.geronimo.kernel.config.ConfigurationManagerImpl;
36 import org.apache.geronimo.kernel.config.Configuration;
37 import org.apache.geronimo.kernel.config.ConfigurationManager;
38 import org.apache.geronimo.kernel.management.State;
39
40 /**
41  *
42  *
43  * @version $Rev: 169154 $ $Date: 2005-05-08 12:35:23 -0700 (Sun, 08 May 2005) $
44  */

45 public class LocalConfigStoreTest extends TestCase {
46     private File JavaDoc root;
47     private URL JavaDoc source;
48     private File JavaDoc sourceFile;
49     private URI JavaDoc uri;
50     private Kernel kernel;
51     private byte[] state;
52     private ObjectName JavaDoc gbeanName1;
53     private ObjectName JavaDoc gbeanName2;
54     private ObjectName JavaDoc storeName;
55     private ConfigurationManager configurationManager;
56
57     public void testInstall() throws Exception JavaDoc {
58         kernel.invoke(storeName, "install", new Object JavaDoc[] {source}, new String JavaDoc[] {"java.net.URL"});
59         assertTrue(new File JavaDoc(root, "1/META-INF/config.ser").exists());
60     }
61
62     public void testReInstall() throws Exception JavaDoc {
63         kernel.invoke(storeName, "install", new Object JavaDoc[] {source}, new String JavaDoc[] {"java.net.URL"});
64         kernel.invoke(storeName, "install", new Object JavaDoc[] {source}, new String JavaDoc[] {"java.net.URL"});
65         assertTrue(new File JavaDoc(root, "2/META-INF/config.ser").exists());
66         kernel.invoke(storeName, "install", new Object JavaDoc[] {source}, new String JavaDoc[] {"java.net.URL"});
67         assertTrue(new File JavaDoc(root, "3/META-INF/config.ser").exists());
68         kernel.invoke(storeName, "install", new Object JavaDoc[] {source}, new String JavaDoc[] {"java.net.URL"});
69         assertTrue(new File JavaDoc(root, "4/META-INF/config.ser").exists());
70         kernel.invoke(storeName, "install", new Object JavaDoc[] {source}, new String JavaDoc[] {"java.net.URL"});
71         assertTrue(new File JavaDoc(root, "5/META-INF/config.ser").exists());
72         kernel.invoke(storeName, "install", new Object JavaDoc[] {source}, new String JavaDoc[] {"java.net.URL"});
73         assertTrue(new File JavaDoc(root, "6/META-INF/config.ser").exists());
74     }
75
76     public void testUpdateConfig() throws Exception JavaDoc {
77         // install the config
78
kernel.invoke(storeName, "install", new Object JavaDoc[] {source}, new String JavaDoc[] {"java.net.URL"});
79
80         // load and start the config
81
ObjectName JavaDoc configName = configurationManager.load(uri);
82         kernel.startRecursiveGBean(configName);
83
84         // make sure the config and the enabled gbean are running
85
assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(configName));
86         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbeanName1));
87
88         // make sure the config and the disabled gbean are NOT running
89
assertEquals(State.STOPPED_INDEX, kernel.getGBeanState(gbeanName2));
90
91         // set the value
92
kernel.setAttribute(gbeanName1, "value", "9900990099");
93         assertEquals("9900990099", kernel.getAttribute(gbeanName1, "value"));
94
95         // stop and unload the config
96
kernel.stopGBean(configName);
97         kernel.unloadGBean(configName);
98
99         // assure it was unloaded
100
assertFalse(kernel.isLoaded(configName));
101
102         // now reload and restart the config
103
configName = configurationManager.load(uri);
104         kernel.startRecursiveGBean(configName);
105
106         // make sure the value was reloaded correctly
107
assertEquals("9900990099", kernel.getAttribute(gbeanName1, "value"));
108
109         // stop and unload the config
110
kernel.stopGBean(configName);
111         kernel.unloadGBean(configName);
112     }
113
114     protected void setUp() throws Exception JavaDoc {
115         try {
116             kernel = KernelFactory.newInstance().createKernel("test.kernel");
117             kernel.boot();
118
119             gbeanName1 = new ObjectName JavaDoc("geronimo.test:name=MyMockGMBean1");
120             GBeanData mockBean1 = new GBeanData(gbeanName1, MockGBean.getGBeanInfo());
121             mockBean1.setAttribute("value", "1234");
122
123             gbeanName2 = new ObjectName JavaDoc("geronimo.test:name=MyMockGMBean2");
124             GBeanData mockBean2 = new GBeanData(gbeanName2, MockGBean.getGBeanInfo());
125             mockBean2.setAttribute("gbeanEnabled", Boolean.FALSE);
126             mockBean2.setAttribute("value", "1234");
127
128             state = Configuration.storeGBeans(new GBeanData[] {mockBean1, mockBean2});
129
130             root = new File JavaDoc(System.getProperty("java.io.tmpdir") + "/config-store");
131             recursiveDelete(root);
132             root.mkdir();
133
134             storeName = new ObjectName JavaDoc("geronimo.test:j2eeType=ConfigurationStore,name=LocalConfigStore");
135             GBeanData store = new GBeanData(storeName, LocalConfigStore.getGBeanInfo());
136             store.setAttribute("root", root.toURI());
137             kernel.loadGBean(store, getClass().getClassLoader());
138             kernel.startGBean(storeName);
139
140             ObjectName JavaDoc configurationManagerName = new ObjectName JavaDoc(":j2eeType=ConfigurationManager,name=Basic");
141             GBeanData configurationManagerData = new GBeanData(configurationManagerName, ConfigurationManagerImpl.GBEAN_INFO);
142             configurationManagerData.setReferencePatterns("Stores", Collections.singleton(store.getName()));
143             kernel.loadGBean(configurationManagerData, getClass().getClassLoader());
144             kernel.startGBean(configurationManagerName);
145             configurationManager = (ConfigurationManager) kernel.getProxyManager().createProxy(configurationManagerName, ConfigurationManager.class);
146
147             uri = new URI JavaDoc("test");
148             GBeanData gbean = new GBeanData(Configuration.getConfigurationObjectName(uri), Configuration.GBEAN_INFO);
149             gbean.setAttribute("id", uri);
150             gbean.setAttribute("gBeanState", state);
151
152
153             sourceFile = File.createTempFile("test", ".car");
154             source = sourceFile.toURL();
155             JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(sourceFile)));
156             jos.putNextEntry(new ZipEntry JavaDoc("META-INF/config.ser"));
157             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(jos);
158             gbean.writeExternal(oos);
159             oos.flush();
160             jos.closeEntry();
161             jos.close();
162         } catch (Exception JavaDoc e) {
163             if (sourceFile != null) {
164                 sourceFile.delete();
165             }
166             throw e;
167         }
168     }
169
170     protected void tearDown() throws Exception JavaDoc {
171         if (sourceFile != null) {
172             sourceFile.delete();
173         }
174         recursiveDelete(root);
175         kernel.shutdown();
176     }
177
178     private static void recursiveDelete(File JavaDoc root) throws Exception JavaDoc {
179         File JavaDoc[] files = root.listFiles();
180         if (files != null) {
181             for (int i = 0; i < files.length; i++) {
182                 File JavaDoc file = files[i];
183                 if (file.isDirectory()) {
184                     recursiveDelete(file);
185                 } else {
186                     file.delete();
187                 }
188             }
189         }
190         root.delete();
191     }
192 }
193
Popular Tags