1 17 18 package org.apache.geronimo.system.configuration; 19 20 import java.io.BufferedOutputStream ; 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.ObjectOutputStream ; 24 import java.net.URI ; 25 import java.net.URL ; 26 import java.util.jar.JarOutputStream ; 27 import java.util.zip.ZipEntry ; 28 import java.util.Collections ; 29 import javax.management.ObjectName ; 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 45 public class LocalConfigStoreTest extends TestCase { 46 private File root; 47 private URL source; 48 private File sourceFile; 49 private URI uri; 50 private Kernel kernel; 51 private byte[] state; 52 private ObjectName gbeanName1; 53 private ObjectName gbeanName2; 54 private ObjectName storeName; 55 private ConfigurationManager configurationManager; 56 57 public void testInstall() throws Exception { 58 kernel.invoke(storeName, "install", new Object [] {source}, new String [] {"java.net.URL"}); 59 assertTrue(new File (root, "1/META-INF/config.ser").exists()); 60 } 61 62 public void testReInstall() throws Exception { 63 kernel.invoke(storeName, "install", new Object [] {source}, new String [] {"java.net.URL"}); 64 kernel.invoke(storeName, "install", new Object [] {source}, new String [] {"java.net.URL"}); 65 assertTrue(new File (root, "2/META-INF/config.ser").exists()); 66 kernel.invoke(storeName, "install", new Object [] {source}, new String [] {"java.net.URL"}); 67 assertTrue(new File (root, "3/META-INF/config.ser").exists()); 68 kernel.invoke(storeName, "install", new Object [] {source}, new String [] {"java.net.URL"}); 69 assertTrue(new File (root, "4/META-INF/config.ser").exists()); 70 kernel.invoke(storeName, "install", new Object [] {source}, new String [] {"java.net.URL"}); 71 assertTrue(new File (root, "5/META-INF/config.ser").exists()); 72 kernel.invoke(storeName, "install", new Object [] {source}, new String [] {"java.net.URL"}); 73 assertTrue(new File (root, "6/META-INF/config.ser").exists()); 74 } 75 76 public void testUpdateConfig() throws Exception { 77 kernel.invoke(storeName, "install", new Object [] {source}, new String [] {"java.net.URL"}); 79 80 ObjectName configName = configurationManager.load(uri); 82 kernel.startRecursiveGBean(configName); 83 84 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(configName)); 86 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbeanName1)); 87 88 assertEquals(State.STOPPED_INDEX, kernel.getGBeanState(gbeanName2)); 90 91 kernel.setAttribute(gbeanName1, "value", "9900990099"); 93 assertEquals("9900990099", kernel.getAttribute(gbeanName1, "value")); 94 95 kernel.stopGBean(configName); 97 kernel.unloadGBean(configName); 98 99 assertFalse(kernel.isLoaded(configName)); 101 102 configName = configurationManager.load(uri); 104 kernel.startRecursiveGBean(configName); 105 106 assertEquals("9900990099", kernel.getAttribute(gbeanName1, "value")); 108 109 kernel.stopGBean(configName); 111 kernel.unloadGBean(configName); 112 } 113 114 protected void setUp() throws Exception { 115 try { 116 kernel = KernelFactory.newInstance().createKernel("test.kernel"); 117 kernel.boot(); 118 119 gbeanName1 = new ObjectName ("geronimo.test:name=MyMockGMBean1"); 120 GBeanData mockBean1 = new GBeanData(gbeanName1, MockGBean.getGBeanInfo()); 121 mockBean1.setAttribute("value", "1234"); 122 123 gbeanName2 = new ObjectName ("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 (System.getProperty("java.io.tmpdir") + "/config-store"); 131 recursiveDelete(root); 132 root.mkdir(); 133 134 storeName = new ObjectName ("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 configurationManagerName = new ObjectName (":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 ("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 jos = new JarOutputStream (new BufferedOutputStream (new FileOutputStream (sourceFile))); 156 jos.putNextEntry(new ZipEntry ("META-INF/config.ser")); 157 ObjectOutputStream oos = new ObjectOutputStream (jos); 158 gbean.writeExternal(oos); 159 oos.flush(); 160 jos.closeEntry(); 161 jos.close(); 162 } catch (Exception e) { 163 if (sourceFile != null) { 164 sourceFile.delete(); 165 } 166 throw e; 167 } 168 } 169 170 protected void tearDown() throws Exception { 171 if (sourceFile != null) { 172 sourceFile.delete(); 173 } 174 recursiveDelete(root); 175 kernel.shutdown(); 176 } 177 178 private static void recursiveDelete(File root) throws Exception { 179 File [] files = root.listFiles(); 180 if (files != null) { 181 for (int i = 0; i < files.length; i++) { 182 File 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 |