KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > installation > AbstractManagementTest


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.servicemix.jbi.installation;
18
19 import java.io.File JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.FilterOutputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.jar.JarOutputStream JavaDoc;
24 import java.util.jar.Manifest JavaDoc;
25 import java.util.zip.ZipEntry JavaDoc;
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     /*
47      * @see TestCase#setUp()
48      */

49     protected void setUp() throws Exception JavaDoc {
50         super.setUp();
51     }
52     
53     /*
54      * @see TestCase#tearDown()
55      */

56     protected void tearDown() throws Exception JavaDoc {
57         super.tearDown();
58         try {
59             shutdownContainer();
60         } catch (Exception JavaDoc e) {
61             logger.info("Error shutting down container", e);
62         }
63     }
64     
65     protected void startContainer(boolean clean) throws Exception JavaDoc {
66         shutdownContainer();
67         if (clean) {
68             Thread.sleep(1000);
69             assertTrue(FileUtil.deleteFile(new File JavaDoc("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 JavaDoc {
85         if (container != null) {
86             container.shutDown();
87         }
88     }
89     
90     protected JBIContainer getContainer() {
91         return container;
92     }
93     
94     protected File JavaDoc createInstallerArchive(String JavaDoc jbi) throws Exception JavaDoc {
95         InputStream JavaDoc is = getClass().getResourceAsStream(jbi + "-jbi.xml");
96         File JavaDoc jar = File.createTempFile("jbi", ".zip");
97         JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(jar));
98         jos.putNextEntry(new ZipEntry JavaDoc("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 JavaDoc createDummyArchive() throws Exception JavaDoc {
109         File JavaDoc jar = File.createTempFile("jbi", ".zip");
110         JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(jar));
111         jos.putNextEntry(new ZipEntry JavaDoc("test.txt"));
112         jos.closeEntry();
113         jos.close();
114         return jar;
115     }
116
117     protected File JavaDoc createServiceAssemblyArchive(String JavaDoc saName, String JavaDoc suName, String JavaDoc compName) throws Exception JavaDoc {
118         return createServiceAssemblyArchive(saName,
119                                             new String JavaDoc[] { suName },
120                                             new String JavaDoc[] { compName });
121     }
122     
123     protected File JavaDoc createServiceAssemblyArchive(String JavaDoc saName, String JavaDoc[] suName, String JavaDoc[] compName) throws Exception JavaDoc {
124         File JavaDoc jar = File.createTempFile("jbi", ".zip");
125         JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(jar));
126         // Write jbi.xml
127
jos.putNextEntry(new ZipEntry JavaDoc("META-INF/jbi.xml"));
128         XMLOutputFactory xof = XMLOutputFactory.newInstance();
129         //xof.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
130
XMLStreamWriter xsw = xof.createXMLStreamWriter(new FilterOutputStream JavaDoc(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         // Put su artifact
166
for (int i = 0; i < suName.length; i++) {
167             jos.putNextEntry(new ZipEntry JavaDoc(suName[i] + ".zip"));
168             JarOutputStream JavaDoc jos2 = new JarOutputStream JavaDoc(jos, new Manifest JavaDoc());
169             jos2.finish();
170             jos2.flush();
171             jos.closeEntry();
172         }
173         // Close jar
174
jos.close();
175         return jar;
176     }
177     
178     protected InstallationServiceMBean getInstallationService() throws Exception JavaDoc {
179         return container.getInstallationService();
180     }
181     
182     protected DeploymentServiceMBean getDeploymentService() throws Exception JavaDoc {
183         return container.getDeploymentService();
184     }
185     
186     protected AdminServiceMBean getAdminService() throws Exception JavaDoc {
187         return container.getManagementContext();
188     }
189     
190     
191 }
192
Popular Tags