1 17 package org.apache.servicemix.jbi.installation; 18 19 import java.io.File ; 20 import java.io.FileOutputStream ; 21 import java.io.FilterOutputStream ; 22 import java.io.InputStream ; 23 import java.util.jar.JarOutputStream ; 24 import java.util.jar.Manifest ; 25 import java.util.zip.ZipEntry ; 26 27 import javax.jbi.management.AdminServiceMBean; 28 import javax.jbi.management.DeploymentServiceMBean; 29 import javax.jbi.management.InstallationServiceMBean; 30 import javax.xml.stream.XMLOutputFactory; 31 import javax.xml.stream.XMLStreamWriter; 32 33 import junit.framework.TestCase; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.apache.servicemix.jbi.container.JBIContainer; 38 import org.apache.servicemix.jbi.util.FileUtil; 39 40 public abstract class AbstractManagementTest extends TestCase { 41 42 protected Log logger = LogFactory.getLog(getClass()); 43 44 protected JBIContainer container; 45 46 49 protected void setUp() throws Exception { 50 super.setUp(); 51 } 52 53 56 protected void tearDown() throws Exception { 57 super.tearDown(); 58 try { 59 shutdownContainer(); 60 } catch (Exception e) { 61 logger.info("Error shutting down container", e); 62 } 63 } 64 65 protected void startContainer(boolean clean) throws Exception { 66 shutdownContainer(); 67 if (clean) { 68 Thread.sleep(1000); 69 assertTrue(FileUtil.deleteFile(new File ("target/testWDR"))); 70 } 71 container = new JBIContainer(); 72 container.setRootDir("target/testWDR"); 73 initContainer(); 74 container.init(); 75 container.start(); 76 } 77 78 protected void initContainer() { 79 container.setCreateMBeanServer(true); 80 container.setMonitorInstallationDirectory(false); 81 container.setMonitorDeploymentDirectory(false); 82 } 83 84 protected void shutdownContainer() throws Exception { 85 if (container != null) { 86 container.shutDown(); 87 } 88 } 89 90 protected JBIContainer getContainer() { 91 return container; 92 } 93 94 protected File createInstallerArchive(String jbi) throws Exception { 95 InputStream is = getClass().getResourceAsStream(jbi + "-jbi.xml"); 96 File jar = File.createTempFile("jbi", ".zip"); 97 JarOutputStream jos = new JarOutputStream (new FileOutputStream (jar)); 98 jos.putNextEntry(new ZipEntry ("META-INF/jbi.xml")); 99 byte[] buffer = new byte[is.available()]; 100 is.read(buffer); 101 jos.write(buffer); 102 jos.closeEntry(); 103 jos.close(); 104 is.close(); 105 return jar; 106 } 107 108 protected File createDummyArchive() throws Exception { 109 File jar = File.createTempFile("jbi", ".zip"); 110 JarOutputStream jos = new JarOutputStream (new FileOutputStream (jar)); 111 jos.putNextEntry(new ZipEntry ("test.txt")); 112 jos.closeEntry(); 113 jos.close(); 114 return jar; 115 } 116 117 protected File createServiceAssemblyArchive(String saName, String suName, String compName) throws Exception { 118 return createServiceAssemblyArchive(saName, 119 new String [] { suName }, 120 new String [] { compName }); 121 } 122 123 protected File createServiceAssemblyArchive(String saName, String [] suName, String [] compName) throws Exception { 124 File jar = File.createTempFile("jbi", ".zip"); 125 JarOutputStream jos = new JarOutputStream (new FileOutputStream (jar)); 126 jos.putNextEntry(new ZipEntry ("META-INF/jbi.xml")); 128 XMLOutputFactory xof = XMLOutputFactory.newInstance(); 129 XMLStreamWriter xsw = xof.createXMLStreamWriter(new FilterOutputStream (jos) { 131 public void close() {} 132 }); 133 xsw.writeStartDocument(); 134 xsw.writeStartElement("jbi"); 135 xsw.writeAttribute("xmlns", "http://java.sun.com/xml/ns/jbi"); 136 xsw.writeAttribute("version", "1.0"); 137 xsw.writeStartElement("service-assembly"); 138 xsw.writeStartElement("identification"); 139 xsw.writeStartElement("name"); 140 xsw.writeCharacters(saName); 141 xsw.writeEndElement(); 142 xsw.writeEndElement(); 143 for (int i = 0; i < suName.length; i++) { 144 xsw.writeStartElement("service-unit"); 145 xsw.writeStartElement("identification"); 146 xsw.writeStartElement("name"); 147 xsw.writeCharacters(suName[i]); 148 xsw.writeEndElement(); 149 xsw.writeEndElement(); 150 xsw.writeStartElement("target"); 151 xsw.writeStartElement("artifacts-zip"); 152 xsw.writeCharacters(suName[i] + ".zip"); 153 xsw.writeEndElement(); 154 xsw.writeStartElement("component-name"); 155 xsw.writeCharacters(compName[i]); 156 xsw.writeEndElement(); 157 xsw.writeEndElement(); 158 xsw.writeEndElement(); 159 } 160 xsw.writeEndElement(); 161 xsw.writeEndElement(); 162 xsw.writeEndDocument(); 163 xsw.flush(); 164 jos.closeEntry(); 165 for (int i = 0; i < suName.length; i++) { 167 jos.putNextEntry(new ZipEntry (suName[i] + ".zip")); 168 JarOutputStream jos2 = new JarOutputStream (jos, new Manifest ()); 169 jos2.finish(); 170 jos2.flush(); 171 jos.closeEntry(); 172 } 173 jos.close(); 175 return jar; 176 } 177 178 protected InstallationServiceMBean getInstallationService() throws Exception { 179 return container.getInstallationService(); 180 } 181 182 protected DeploymentServiceMBean getDeploymentService() throws Exception { 183 return container.getDeploymentService(); 184 } 185 186 protected AdminServiceMBean getAdminService() throws Exception { 187 return container.getManagementContext(); 188 } 189 190 191 } 192 | Popular Tags |