KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.apache.servicemix.jbi.framework.ManagementSupport;
24 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
25 import org.apache.servicemix.jbi.jaxp.StringSource;
26 import org.apache.servicemix.jbi.util.DOMUtil;
27 import org.apache.xml.utils.PrefixResolver;
28 import org.apache.xpath.CachedXPathAPI;
29 import org.apache.xpath.objects.XObject;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32
33 public class DeploymentMessageTest extends AbstractManagementTest {
34
35     protected void initContainer() {
36         container.setCreateMBeanServer(false);
37         container.setMonitorInstallationDirectory(false);
38         container.setMonitorDeploymentDirectory(false);
39     }
40     
41     public void testDeployNullJarUrl() throws Exception JavaDoc {
42         startContainer(true);
43         try {
44             getDeploymentService().deploy(null);
45             fail("Deploy with null jar url should have failed");
46         } catch (Exception JavaDoc e) {
47             assertTrue(e instanceof Exception JavaDoc);
48             String JavaDoc str = e.getMessage();
49             System.err.println(str);
50             Node JavaDoc node = new SourceTransformer().toDOMNode(new StringSource(str));
51             assertNotNull(node);
52             assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result"));
53             assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type"));
54             assertEquals("deploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id"));
55         }
56     }
57      
58     public void testDeployNonExistentJar() throws Exception JavaDoc {
59         startContainer(true);
60         try {
61             getDeploymentService().deploy("hello");
62             fail("Deploy with non existent jar url should have failed");
63         } catch (Exception JavaDoc e) {
64             assertTrue(e instanceof Exception JavaDoc);
65             String JavaDoc str = e.getMessage();
66             System.err.println(str);
67             Node JavaDoc node = new SourceTransformer().toDOMNode(new StringSource(str));
68             assertNotNull(node);
69             assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result"));
70             assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type"));
71             assertEquals("deploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id"));
72         }
73     }
74     
75     public void testDeployNoDescriptor() throws Exception JavaDoc {
76         startContainer(true);
77         String JavaDoc jarUrl = createDummyArchive().getAbsolutePath();
78         try {
79             getDeploymentService().deploy(jarUrl);
80             fail("Deploy with non existent descriptor should have failed");
81         } catch (Exception JavaDoc e) {
82             assertTrue(e instanceof Exception JavaDoc);
83             String JavaDoc str = e.getMessage();
84             System.err.println(str);
85             Node JavaDoc node = new SourceTransformer().toDOMNode(new StringSource(str));
86             assertNotNull(node);
87             assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result"));
88             assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type"));
89             assertEquals("deploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id"));
90         }
91     }
92     
93     public void testDeployWithNonSADescriptor() throws Exception JavaDoc {
94         startContainer(true);
95         String JavaDoc jarUrl = createInstallerArchive("component1").getAbsolutePath();
96         try {
97             getDeploymentService().deploy(jarUrl);
98             fail("Deploy with non existent descriptor should have failed");
99         } catch (Exception JavaDoc e) {
100             assertTrue(e instanceof Exception JavaDoc);
101             String JavaDoc str = e.getMessage();
102             System.err.println(str);
103             Node JavaDoc node = new SourceTransformer().toDOMNode(new StringSource(str));
104             assertNotNull(node);
105             assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result"));
106             assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type"));
107             assertEquals("deploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id"));
108         }
109     }
110     
111     public void testDeployWithSuccess() throws Exception JavaDoc {
112         DummyComponent component = new DummyComponent();
113         String JavaDoc deployResult = DOMUtil.asXML(ManagementSupport.createComponentSuccess("deploy", "component1"));
114         component.setResult(deployResult);
115         System.err.println(deployResult);
116         startContainer(true);
117         getContainer().activateComponent(component, "component1");
118         getContainer().getEnvironmentContext().createComponentRootDir("component1");
119         File JavaDoc installSaUrl = createServiceAssemblyArchive("sa", "su", "component1");
120         String JavaDoc result = getDeploymentService().deploy(installSaUrl.getAbsolutePath());
121         System.err.println(result);
122         Node JavaDoc node = new SourceTransformer().toDOMNode(new StringSource(result));
123         assertNotNull(node);
124         // main task
125
assertEquals("SUCCESS", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result"));
126         assertEquals("deploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id"));
127         // component task
128
assertEquals("SUCCESS", textValueOfXPath(node, "//jbi:component-task-result//jbi:task-result"));
129         assertEquals("deploy", textValueOfXPath(node, "//jbi:component-task-result//jbi:task-id"));
130     }
131     
132     public void testDeployWithOneSuccessAndOneError() throws Exception JavaDoc {
133         DummyComponent component1 = new DummyComponent();
134         String JavaDoc deployResult1 = DOMUtil.asXML(ManagementSupport.createComponentFailure("deploy", "component1", "xxx", null));
135         component1.setResult(deployResult1);
136         component1.setException(true);
137         System.err.println(deployResult1);
138         DummyComponent component2 = new DummyComponent();
139         String JavaDoc deployResult2 = DOMUtil.asXML(ManagementSupport.createComponentSuccess("deploy", "component2"));
140         component2.setResult(deployResult2);
141         System.err.println(deployResult2);
142         startContainer(true);
143         getContainer().activateComponent(component1, "component1");
144         getContainer().getEnvironmentContext().createComponentRootDir("component1");
145         getContainer().activateComponent(component2, "component2");
146         getContainer().getEnvironmentContext().createComponentRootDir("component2");
147         File JavaDoc installSaUrl = createServiceAssemblyArchive("sa", new String JavaDoc[] { "su1", "su2" }, new String JavaDoc[] { "component1", "component2"});
148         String JavaDoc result = null;;
149         try {
150             result = getDeploymentService().deploy(installSaUrl.getAbsolutePath());
151             fail("Deployment with an error is not supported");
152         } catch (Exception JavaDoc e) {
153             result = e.getMessage();
154         }
155         System.err.println(result);
156         Node JavaDoc node = new SourceTransformer().toDOMNode(new StringSource(result));
157         assertNotNull(node);
158         // main task
159
assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result"));
160         assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type"));
161         assertEquals("deploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id"));
162         // first component task
163
assertEquals("FAILED", textValueOfXPath(node, "//jbi:component-task-result[1]//jbi:task-result"));
164         assertEquals("deploy", textValueOfXPath(node, "//jbi:component-task-result[1]//jbi:task-id"));
165         // second component task
166
assertEquals("SUCCESS", textValueOfXPath(node, "//jbi:component-task-result[2]//jbi:task-result"));
167         assertEquals("deploy", textValueOfXPath(node, "//jbi:component-task-result[2]//jbi:task-id"));
168     }
169     
170     public void testUndeployNullJarUrl() throws Exception JavaDoc {
171         startContainer(true);
172         try {
173             getDeploymentService().undeploy(null);
174             fail("Deploy with null jar url should have failed");
175         } catch (Exception JavaDoc e) {
176             assertTrue(e instanceof Exception JavaDoc);
177             String JavaDoc str = e.getMessage();
178             System.err.println(str);
179             Node JavaDoc node = new SourceTransformer().toDOMNode(new StringSource(str));
180             assertNotNull(node);
181             assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result"));
182             assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type"));
183             assertEquals("undeploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id"));
184         }
185     }
186      
187     public void testUndeployNonDeployedSA() throws Exception JavaDoc {
188         startContainer(true);
189         try {
190             getDeploymentService().undeploy("my-sa");
191             fail("Deploy with non deployed sa should have failed");
192         } catch (Exception JavaDoc e) {
193             assertTrue(e instanceof Exception JavaDoc);
194             String JavaDoc str = e.getMessage();
195             System.err.println(str);
196             Node JavaDoc node = new SourceTransformer().toDOMNode(new StringSource(str));
197             assertNotNull(node);
198             assertEquals("FAILED", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-result"));
199             assertEquals("ERROR", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:message-type"));
200             assertEquals("undeploy", textValueOfXPath(node, "//jbi:frmwk-task-result//jbi:task-id"));
201         }
202     }
203      
204     protected String JavaDoc textValueOfXPath(Node JavaDoc node, String JavaDoc xpath) throws TransformerException JavaDoc {
205         CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
206         XObject list = cachedXPathAPI.eval(node, xpath, new PrefixResolver() {
207             public String JavaDoc getNamespaceForPrefix(String JavaDoc prefix) {
208                 if (prefix.equals("jbi")) {
209                     return "http://java.sun.com/xml/ns/jbi/management-message";
210                 }
211                 return null;
212             }
213             public String JavaDoc getNamespaceForPrefix(String JavaDoc arg0, Node JavaDoc arg1) {
214                 return null;
215             }
216             public String JavaDoc getBaseIdentifier() {
217                 return null;
218             }
219             public boolean handlesNullPrefixes() {
220                 return false;
221             }
222         });
223         Node JavaDoc root = list.nodeset().nextNode();
224         if (root instanceof Element JavaDoc) {
225             Element JavaDoc element = (Element JavaDoc) root;
226             if (element == null) {
227                 return "";
228             }
229             String JavaDoc text = DOMUtil.getElementText(element);
230             return text;
231         }
232         else if (root != null) {
233             return root.getNodeValue();
234         } else {
235             return null;
236         }
237     }
238     
239 }
240
Popular Tags