KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > deployment > test > DeploymentServiceUnitTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.deployment.test;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.nio.channels.FileChannel JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 import javax.management.InstanceNotFoundException JavaDoc;
37 import javax.management.MBeanAttributeInfo JavaDoc;
38 import javax.management.MBeanException JavaDoc;
39 import javax.management.MBeanInfo JavaDoc;
40 import javax.management.MBeanServer JavaDoc;
41 import javax.management.MBeanServerConnection JavaDoc;
42 import javax.management.ObjectName JavaDoc;
43 import javax.management.ReflectionException JavaDoc;
44 import javax.management.remote.JMXConnector JavaDoc;
45 import javax.management.remote.JMXConnectorFactory JavaDoc;
46 import javax.management.remote.JMXServiceURL JavaDoc;
47 import javax.naming.InitialContext JavaDoc;
48 import javax.naming.NameClassPair JavaDoc;
49 import javax.naming.NamingEnumeration JavaDoc;
50 import javax.sql.DataSource JavaDoc;
51
52 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
53 import org.jboss.mq.server.MessageCache;
54 import org.jboss.mx.util.MBeanServerLocator;
55 import org.jboss.mx.util.ObjectNameFactory;
56 import org.jboss.services.deployment.MBeanData;
57 import org.jboss.test.JBossTestCase;
58
59 /**
60  * DeploymentService tests
61  *
62  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis </a>
63  * @author <a HREF="mailto:peter.johnson2@unisys.com">Peter Johnson </a>
64  * @version $Revision: 42540 $
65  */

66 public class DeploymentServiceUnitTestCase extends JBossTestCase {
67     private ObjectName JavaDoc deploymentService = ObjectNameFactory
68             .create("jboss:service=DeploymentService");
69
70     private ObjectName JavaDoc mainDeployer = ObjectNameFactory
71             .create("jboss.system:service=MainDeployer");
72
73     public DeploymentServiceUnitTestCase(String JavaDoc name) {
74         super(name);
75     }
76
77     /**
78      * Check if I can get the available templates
79      */

80     public void testListModuleTemplates() throws Exception JavaDoc {
81         log.info("+++ testListModuleTemplates");
82         MBeanServerConnection JavaDoc server = getServer();
83         try {
84             boolean isRegistered = server.isRegistered(deploymentService);
85             assertTrue(deploymentService + " is registered", isRegistered);
86             log.info("Loaded templates: "
87                     + server.invoke(deploymentService, "listModuleTemplates",
88                             new Object JavaDoc[] {}, new String JavaDoc[] {}));
89         } finally {
90             // empty
91
}
92     }
93
94     /**
95      * Try to create, remove and re-create a jms topic (don't deploy)
96      */

97     public void testCreateAndRemoveAndCreateTopic() throws Exception JavaDoc {
98         log.info("+++ testCreateAndRemoveAndCreateTopic");
99         try {
100             String JavaDoc module = "testTopic1-service.xml";
101
102             // remove module in case it exists
103
removeModule(module);
104
105             // Prepare the template properties
106
HashMap JavaDoc props = new HashMap JavaDoc();
107             props.put("TopicName", "testTopic1"); // the topic name (mandatory)
108
props.put("InMemory", new Boolean JavaDoc(true)); // set this to true to
109
// persist the topic in
110
// memory
111
props.put("MaxDepth", new Integer JavaDoc(5)); // set the MaxDepth property
112
// to 5
113

114             String JavaDoc template = "jms-topic";
115
116             // Create a topic destination module, in case of any problem an
117
// exception will be thrown
118
module = createModule(module, template, props);
119
120             // remove the module
121
removeModule(module);
122
123             // Re-create the topic destination module with the same module name.
124
// In case of any problem an exception will be thrown
125
module = createModule(module, template, props);
126         } catch (Exception JavaDoc e) {
127             super.fail("Caught exception, message: " + e.getMessage());
128         } finally {
129             // empty
130
}
131     }
132
133     /**
134      * Try to create a no-tx-datasource (don't deploy)
135      */

136     public void testCreateNoTxDataSource() throws Exception JavaDoc {
137         log.info("+++ testCreateNoTxDataSource");
138         try {
139             String JavaDoc module = "test-no-tx-hsqldb-ds.xml";
140
141             // remove module in case it exists
142
removeModule(module);
143
144             // Prepare the template properties
145
HashMap JavaDoc props = new HashMap JavaDoc();
146             props.put("jndi-name", "TestNoTxDataSource");
147             props.put("connection-url", "jdbc:hsqldb:hsql://localhost:1701");
148             props.put("driver-class", "org.hsqldb.jdbcDriver");
149
150             // Add some fake connection properties
151
Hashtable JavaDoc ht = new Hashtable JavaDoc();
152             ht.put("property1", "someString");
153             ht.put("property2", new Boolean JavaDoc(true));
154             ht.put("property3", new Integer JavaDoc(666));
155             props.put("connection-properties", ht);
156
157             props.put("user-name", "sa");
158             props.put("password", "");
159
160             props.put("min-pool-size", new Integer JavaDoc(5));
161             props.put("max-pool-size", new Integer JavaDoc(20));
162             props.put("track-statements", "NOWARN");
163             props.put("security-config", "APPLICATION-MANAGED-SECURITY");
164             props.put("type-mapping", "Hypersonic SQL");
165             props.put("dependencies", new ObjectName JavaDoc[] { new ObjectName JavaDoc(
166                     "jboss:service=Hypersonic") });
167
168             String JavaDoc template = "no-tx-datasource";
169
170             // In case of any problem an exception will be thrown
171
module = createModule(module, template, props);
172         } catch (Exception JavaDoc e) {
173             super.fail("Caught exception, message: " + e.getMessage());
174         } finally {
175             // empty
176
}
177     }
178
179     /**
180      * Try to create an xa-datasource (don't deploy)
181      */

182     public void testCreateXaDataSource() throws Exception JavaDoc {
183         log.info("+++ testCreateXaDataSource");
184         try {
185             String JavaDoc module = "test-xa-oracle-ds.xml";
186
187             // remove module in case it exists
188
removeModule(module);
189
190             // Prepare the template properties
191
HashMap JavaDoc props = new HashMap JavaDoc();
192             props.put("jndi-name", "TestOracleXaDataSource");
193             props.put("track-connection-by-tx", new Boolean JavaDoc(true));
194             props.put("is-same-RM-override-value", new Boolean JavaDoc(false));
195             props.put("xa-datasource-class",
196                     "oracle.jdbc.xa.client.OracleXADataSource");
197
198             // Add some xa-datasource-properties
199
Hashtable JavaDoc ht = new Hashtable JavaDoc();
200             ht.put("URL", "jdbc:oracle:oci8:@tc");
201             ht.put("User", "scott");
202             ht.put("Password", "tiger");
203             props.put("xa-datasource-properties", ht);
204
205             props
206                     .put("exception-sorter-class-name",
207                             "org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter");
208             props.put("no-tx-separate-pools", new Boolean JavaDoc(true));
209             props.put("type-mapping", "Oracle9i");
210
211             String JavaDoc template = "xa-datasource";
212
213             // In case of any problem an exception will be thrown
214
module = createModule(module, template, props);
215         } catch (Exception JavaDoc e) {
216             super.fail("Caught exception, message: " + e.getMessage());
217         } finally {
218             // empty
219
}
220     }
221
222     /**
223      * Try to create and deploy a local-tx-datasource
224      */

225     public void testCreateAndDeployLocalTxDataSource() throws Exception JavaDoc {
226         log.info("+++ testCreateAndDeployLocalTxDataSource");
227         try {
228             String JavaDoc module = "test-local-tx-hsqldb-ds.xml";
229             String JavaDoc jndiName = "TestLocalTxHsqlDataSource";
230
231             // undeploye module in case it's deployed
232
undeployModule(module);
233             // remove module in case it exists
234
removeModule(module);
235
236             // Prepare the template properties
237
HashMap JavaDoc props = new HashMap JavaDoc();
238             props.put("jndi-name", jndiName); // use a name other than default
239
props.put("use-java-context", new Boolean JavaDoc(false)); // set this to
240
// false to allow
241
// remote lookup
242
props.put("connection-url", "jdbc:hsqldb:hsql://localhost:1701"); // using
243
// hsqldb
244
props.put("driver-class", "org.hsqldb.jdbcDriver");
245             props.put("user-name", "sa");
246             props.put("password", "");
247             props.put("min-pool-size", new Integer JavaDoc(5));
248             props.put("max-pool-size", new Integer JavaDoc(20));
249             props.put("idle-timeout-minutes", new Integer JavaDoc(0));
250             props.put("track-statements", "TRUE");
251             props.put("security-config", "APPLICATION-MANAGED-SECURITY");
252             props.put("type-mapping", "Hypersonic SQL");
253             props.put("dependencies", new ObjectName JavaDoc[] { new ObjectName JavaDoc(
254                     "jboss:service=Hypersonic") });
255
256             String JavaDoc template = "local-tx-datasource";
257
258             // In case of any problem an exception will be thrown
259
module = createModule(module, template, props);
260
261             boolean isDeployed = deployModule(module);
262
263             // was deployment succesful?
264
assertTrue("deployed successful : " + isDeployed, isDeployed);
265
266             // see if we can get a connection
267
InitialContext JavaDoc ic = new InitialContext JavaDoc();
268             DataSource JavaDoc ds = (DataSource JavaDoc) ic.lookup(jndiName);
269             Connection JavaDoc connection = ds.getConnection();
270             connection.close();
271
272             // undeploy module
273
undeployModule(module);
274             // remove module
275
removeModule(module);
276
277             // regenerate with wrong usename
278
props.put("user-name", "rogue-admin");
279             module = createModule(module, template, props);
280
281             // deploy again
282
isDeployed = deployModule(module);
283
284             // was deployment succesful?
285
assertTrue("deployed successful : " + isDeployed, isDeployed);
286
287             // lookup the datasource again and see if we can get a connection
288
// it should fail this time
289
try {
290                 ds = (DataSource JavaDoc) ic.lookup(jndiName);
291                 connection = ds.getConnection();
292                 fail("Shouldn't reach this point");
293             } catch (Exception JavaDoc e) {
294                 // ok
295
}
296             // undeploy module
297
undeployModule(module);
298         } finally {
299             // empty
300
}
301     }
302
303     /**
304      * Try to update an mbean with a standard set of properties. In this case,
305      * the mbean name is bad and an error should be returned from the deployment
306      * service.
307      */

308     public void testUpdateMBeanBadName() throws Exception JavaDoc {
309         log.info("+++ testUpdateMBeanBadName");
310
311         // Establish the property values
312
Properties JavaDoc attrs = new Properties JavaDoc();
313         attrs.put("Attr1", "aaaaa");
314         attrs.put("Attr2", "bbbbb");
315         ;
316
317         // Set up the MBean configuration bean:
318
MBeanData data = new MBeanData();
319         data.setTemplateName("mbean-update");
320         data.setAttributes(attrs);
321
322         // Try with a null mbean name:
323
try {
324             data.setName(null);
325             updateMBean(data);
326             String JavaDoc msg = "Unexpectedly found mbean with invalid name: " + data;
327             log.error(msg);
328             fail(msg);
329         } catch (MBeanException JavaDoc e) {
330             // expected
331
log.info("passed");
332         }
333
334         // Try with an empty mbean name:
335
try {
336             data.setName("");
337             updateMBean(data);
338             String JavaDoc msg = "Unexpectedly found mbean with invalid name: " + data;
339             log.error(msg);
340             fail(msg);
341         } catch (MBeanException JavaDoc e) {
342             // expected
343
log.info("passed");
344         }
345
346         // Try with an unknown mbean name:
347
try {
348             data.setName("jboss.xxx:service=NoneSuch");
349             updateMBean(data);
350             String JavaDoc msg = "Unexpectedly found mbean with name: " + data;
351             log.error(msg);
352             fail(msg);
353         } catch (MBeanException JavaDoc e) {
354             // expected
355
log.info("passed");
356         }
357
358         // Try with an unknown mbean name with multiple attributes::
359
try {
360             data
361                     .setName("jboss.xxx:service=NoneSuch,type=Unknown,Alias=whatever");
362             updateMBean(data);
363             String JavaDoc msg = "Unexpectedly found mbean with name: " + data;
364             log.error(msg);
365             fail(msg);
366         } catch (MBeanException JavaDoc e) {
367             // expected
368
log.info("passed");
369         }
370     }
371
372     /**
373      * Try to update an mbean with a standard set of properties. In this case,
374      * no template is given for doing the update. The update should fail.
375      */

376     public void testUpdateMBeanBadTemplate() throws Exception JavaDoc {
377         log.info("+++ testUpdateMBeanBadTemplate");
378
379         // Establish the property values
380
Properties JavaDoc attrs = new Properties JavaDoc();
381         attrs.put("Attr1", "aaaaa");
382         attrs.put("Attr2", "bbbbb");
383         ;
384
385         // Set up the MBean configuration bean:
386
MBeanData data = new MBeanData();
387         data.setName("jboss.mq:service=MessageCache");
388         data.setAttributes(attrs);
389
390         // Try with a null template name:
391
try {
392             data.setTemplateName(null);
393             log.info("Template=" + data.getTemplateName());
394             updateMBean(data);
395             String JavaDoc msg = "Update was successful with null template name: "
396                     + data.getTemplateName();
397             log.error(msg);
398             fail(msg);
399         } catch (MBeanException JavaDoc e) {
400             // expected
401
log.info("passed");
402         }
403
404         // Try again, but with an empty template name:
405
try {
406             data.setTemplateName("");
407             log.info("Template=" + data.getTemplateName());
408             updateMBean(data);
409             String JavaDoc msg = "Update was successful with empty template name "
410                     + data.getTemplateName();
411             log.error(msg);
412             fail(msg);
413         } catch (MBeanException JavaDoc e) {
414             // expected
415
log.info("passed");
416         }
417
418         // Try again, but with an unknown template name:
419
try {
420             data.setTemplateName("nonesuch");
421             log.info("Template=" + data.getTemplateName());
422             updateMBean(data);
423             String JavaDoc msg = "Update was successful with null template "
424                     + data.getTemplateName();
425             log.error(msg);
426             fail(msg);
427         } catch (MBeanException JavaDoc e) {
428             // expected
429
log.info("passed");
430         }
431     }
432
433     /**
434      * Try to update an mbean with a standard set of properties. In this case,
435      * the mbean attributes are bad. The update should succeed.
436      */

437     public void testUpdateMBeanBadAttributes() throws Exception JavaDoc {
438         log.info("+++ testUpdateMBeanBadAttributes");
439
440         // Set up the MBean configuration bean:
441
MBeanData data = new MBeanData();
442         data.setName("jboss.mq:service=MessageCache");
443         data.setTemplateName("mbean-update");
444         data.setAttributes(null);
445
446         // Update the mbean
447
boolean result = updateMBean(data);
448         // Yes, I could have used assertTrue, but I want to log all errors:
449
if (result) {
450             log.info("passed");
451         } else {
452             String JavaDoc msg = "Failed to update mbean when attributes were null: "
453                     + data;
454             log.error(msg);
455             fail(msg);
456         }
457     }
458
459     /**
460      * Try to update an mbean with a standard set of properties. In this case,
461      * the mbean data is bad. The update should fail.
462      */

463     public void testUpdateMBeanBadData() throws Exception JavaDoc {
464         log.info("+++ testUpdateMBeanBadData");
465
466         // Attempt to update the mbean with no data
467
try {
468             updateMBean(null);
469             String JavaDoc msg = "Update was successful with null data";
470             log.error(msg);
471             fail(msg);
472         } catch (MBeanException JavaDoc e) {
473             // expected
474
log.info("passed");
475         }
476     }
477
478     /**
479      * Try to update an mbean with a standard set of properties. The mbean we
480      * use is the MessageCache mbean for the messaging service. The mbean
481      * properties are changed to innocuous values that should cause no problems.
482      * This test might have to be updated when the new messing service is rolled
483      * out.
484      */

485     public void testUpdateMBeanStandard() throws Exception JavaDoc {
486         log.info("+++ testUpdateMBeanStandard");
487
488         // Establish the property values
489
String JavaDoc value = "999";
490         Properties JavaDoc attrs = new Properties JavaDoc();
491         attrs.put("MaxMemoryMark", value);
492         attrs.put("HighMemoryMark", value);
493         attrs.put("CacheStore", "jboss.mq:service=PersistenceManager");
494         attrs.put("MaximumHard", value);
495         attrs.put("MinimumHard", value);
496         attrs.put("SoftenAtLeastEveryMillis", value);
497         attrs.put("SoftenNoMoreOftenThanMillis", value);
498         attrs.put("MakeSoftReferences", "true");
499         // For some reason, the following value is rounded up to 1000 if set to
500
// 999, so we will use 1000 as the value:
501
attrs.put("SoftenWaitMillis", "1000");
502
503         // Set up the MBean configuration bean:
504
MBeanData data = new MBeanData();
505         String JavaDoc name = "jboss.mq:service=MessageCache";
506         data.setName(name);
507         data.setTemplateName("mbean-update");
508         data.setAttributes(attrs);
509
510         // Update and verify the mbean:
511
boolean result = updateMBean(data);
512         // Yes, I could have used assertTrue, but I want to log all errors:
513
if (!result) {
514             String JavaDoc msg = "Failed to update mbean " + data;
515             log.error(msg);
516             fail(msg);
517         }
518         verifyMBean(data, null);
519     }
520
521     /**
522      * Try to update an mbean where one of the properties has a value expressed
523      * as a nested XML property rather than as simple text. The mbean we use is
524      * the SecurityManager mbean for the messaging service. The mbean property
525      * that is of interest is the DefaultSecurityConfig. This test might have to
526      * be updated when the new messing service is rolled out.
527      */

528     public void testUpdateMBeanNested() throws Exception JavaDoc {
529         log.info("+++ testUpdateMBeanNested");
530
531         // Establish the property values
532
Properties JavaDoc attrs = new Properties JavaDoc();
533         String JavaDoc nestedAttr = "DefaultSecurityConfig";
534         attrs
535                 .put(
536                         nestedAttr,
537                         "<security><role name=\"guest\" read=\"true\" write=\"false\" create=\"false\"/></security>");
538         attrs.put("SecurityDomain", "java:/jaas/jbossmq");
539
540         // Establish the dependency values
541
Properties JavaDoc depends = new Properties JavaDoc();
542         depends.put("NextInterceptor", "jboss.mq:service=DestinationManager");
543
544         // Set up the MBean configuration bean:
545
MBeanData data = new MBeanData();
546         String JavaDoc name = "jboss.mq:service=SecurityManager";
547         data.setName(name);
548         data.setTemplateName("mbean-update");
549         data.setAttributes(attrs);
550         data.setDepends(depends);
551
552         // Update and verify the mbean:
553
boolean result = updateMBean(data);
554         // Yes, I could have used assertTrue, but I want to log all errors:
555
if (!result) {
556             String JavaDoc msg = "Failed to update mbean " + data;
557             log.error(msg);
558             fail(msg);
559         }
560         verifyMBean(data, nestedAttr);
561     }
562
563     /**
564      * Try to update an mbean that has a name with multiple attributes. We will
565      * update the mbean twice, each time presenting the name attributes in a
566      * different order. We will use one of the InvocationLayer mbeans from the
567      * uil2-service.xml file.
568      */

569     public void testUpdateMBeanXpath() throws Exception JavaDoc {
570         log.info("+++ testUpdateMBeanXpath");
571
572         // Establish the property values
573
Properties JavaDoc attrs = new Properties JavaDoc();
574         attrs.put("FromName", "XXXXXXXXX");
575         attrs.put("ToName", "AAAAAAAAAA");
576
577         // Establish the dependency values
578
Properties JavaDoc depends = new Properties JavaDoc();
579         depends.put("", "jboss:service=Naming");
580
581         // Set up the MBean configuration bean, this time with the attributes
582
// in the order in which they appear when asking the mbean for the name
583
MBeanData data = new MBeanData();
584         data
585                 .setName("jboss.mq:alias=UIL2XAConnectionFactory,service=InvocationLayer,type=UIL2XA");
586         data.setTemplateName("mbean-update");
587         data.setAttributes(attrs);
588         data.setDepends(depends);
589
590         // Update and verify the mbean:
591
if (!updateMBean(data)) {
592             String JavaDoc msg = "Failed to update mbean: " + data;
593             log.error(msg);
594             fail(msg);
595         }
596         verifyMBean(data, null);
597
598         // Try again, but with the name attributes in a different order. This
599
// time in the order in which they appear in the xml file:
600
attrs = new Properties JavaDoc();
601         attrs.put("FromName", "RRRRRRRRRRR");
602         attrs.put("ToName", "QQQQQQQQQQQQQ");
603         data
604                 .setName("jboss.mq:service=InvocationLayer,type=UIL2XA,alias=UIL2XAConnectionFactory");
605         data.setAttributes(attrs);
606
607         // Update and verify the mbean:
608
if (!updateMBean(data)) {
609             String JavaDoc msg = "Failed to update mbean: " + data;
610             log.error(msg);
611             fail(msg);
612         }
613         verifyMBean(data, null);
614
615         // Try again, but with the name attributes in another order. This
616
// time in the order is different that the mbean name or xml file:
617
attrs = new Properties JavaDoc();
618         attrs.put("FromName", "LLLLLLLLLLLL");
619         attrs.put("ToName", "KKKKKKKKKKKK");
620         data
621                 .setName("jboss.mq:type=UIL2XA,alias=UIL2XAConnectionFactory,service=InvocationLayer");
622         data.setAttributes(attrs);
623
624         // Update and verify the mbean:
625
if (!updateMBean(data)) {
626             String JavaDoc msg = "Failed to update mbean: " + data;
627             log.error(msg);
628             fail(msg);
629         }
630         verifyMBean(data, null);
631     }
632
633     /**
634      * Try to update a local transaction data source. This test case uses the
635      * DefaultDS data source.
636      */

637     public void testUpdateDataSourceLocal() throws Exception JavaDoc {
638         log.info("+++ testUpdateDataSourceLocal");
639
640         try {
641             String JavaDoc jndiName = "DefaultDS";
642
643             // Prepare the template properties
644
HashMap JavaDoc props = new HashMap JavaDoc();
645             props.put("jndi-name", jndiName);
646             props.put("use-java-context", new Boolean JavaDoc(false)); // set this to
647
// false to allow
648
// remote lookup
649
props.put("connection-url", "jdbc:hsqldb:hsql://localhost:1701"); // using
650
// hsqldb
651
props.put("driver-class", "org.hsqldb.jdbcDriver");
652             props.put("user-name", "sa");
653             props.put("password", "");
654             props.put("min-pool-size", new Integer JavaDoc(9));
655             props.put("max-pool-size", new Integer JavaDoc(99));
656             props.put("idle-timeout-minutes", new Integer JavaDoc(99));
657             props.put("track-statements", "TRUE");
658             props.put("security-config", "APPLICATION-MANAGED-SECURITY");
659             props.put("type-mapping", "Hypersonic SQL");
660             props.put("dependencies", new ObjectName JavaDoc[] { new ObjectName JavaDoc(
661                     "jboss:service=Hypersonic") });
662
663             String JavaDoc template = "local-tx-datasource";
664
665             // In case of any problem an exception will be thrown
666
boolean isDeployed = updateDataSource("hsqldb-ds.xml", template,
667                     props);
668             assertTrue("deployed successful : " + isDeployed, isDeployed);
669
670             // see if we can get a connection
671
boolean connected = connectToDataSource(jndiName);
672             assertTrue("Data source " + jndiName + " connected successful : " + connected,
673                     connected);
674
675             // Try with the other module name
676
isDeployed = updateDataSource("hsqldb", template, props);
677             assertTrue("deployed successful : " + isDeployed, isDeployed);
678
679             // see if we can get a connection
680
connected = connectToDataSource(jndiName);
681             assertTrue("Data source " + jndiName + " connected successful : " + connected,
682                     connected);
683
684             log.info("passed");
685         } catch (Exception JavaDoc e) {
686             log.error("failed", e);
687             fail("Caught exception, message: " + e.getMessage());
688         } finally {
689             // empty
690
}
691     }
692
693     /**
694      * Try to update a no transaction data source. This test case uses the data
695      * source created by testCreateNoTxDataSource. This test is hidden for now,
696      * the test-no-tx-hsqldb data source is never deployed and is thus not
697      * visible.
698      */

699     public void hide_testUpdateDataSourceNo() throws Exception JavaDoc {
700         log.info("+++ testUpdateDataSourceNo");
701
702         try {
703
704             String JavaDoc module = "test-no-tx-hsqldb-ds.xml";
705
706             // remove module in case it exists
707
removeModule(module);
708
709             // Prepare the template properties
710
HashMap JavaDoc props = new HashMap JavaDoc();
711             props.put("jndi-name", "TestNoTxDataSource");
712             props.put("connection-url", "jdbc:hsqldb:hsql://localhost:1701");
713             props.put("driver-class", "org.hsqldb.jdbcDriver");
714
715             // Add some fake connection properties
716
Hashtable JavaDoc ht = new Hashtable JavaDoc();
717             ht.put("property1", "someString");
718             ht.put("property2", new Boolean JavaDoc(true));
719             ht.put("property3", new Integer JavaDoc(666));
720             props.put("connection-properties", ht);
721
722             props.put("user-name", "sa");
723             props.put("password", "");
724
725             props.put("min-pool-size", new Integer JavaDoc(5));
726             props.put("max-pool-size", new Integer JavaDoc(20));
727             props.put("track-statements", "NOWARN");
728             props.put("security-config", "APPLICATION-MANAGED-SECURITY");
729             props.put("type-mapping", "Hypersonic SQL");
730             props.put("dependencies", new ObjectName JavaDoc[] { new ObjectName JavaDoc(
731                     "jboss:service=Hypersonic,") });
732
733             String JavaDoc template = "no-tx-datasource";
734
735             // In case of any problem an exception will be thrown
736
boolean isDeployed = updateDataSource(module, template, props);
737             assertTrue("deployed successful : " + isDeployed, isDeployed);
738
739             // We don't try to get a connection because the original
740
// data source was never deployed.
741

742             log.info("passed");
743         } catch (Exception JavaDoc e) {
744             log.error("failed", e);
745             fail("Caught exception, message: " + e.getMessage());
746         } finally {
747             // empty
748
}
749     }
750
751     /**
752      * Try to update an XA transaction data source. This test case uses the data
753      * source created by testCreateXaDataSource. This test is hidden for now,
754      * the test-xa-oracle data source is never deployed and is thus not visible.
755      */

756     public void hide_testUpdateDataSourceXa() throws Exception JavaDoc {
757         log.info("+++ testUpdateDataSourceNo");
758
759         try {
760             String JavaDoc module = "test-xa-oracle-ds.xml";
761
762             // remove module in case it exists
763
removeModule(module);
764
765             // Prepare the template properties
766
HashMap JavaDoc props = new HashMap JavaDoc();
767             props.put("jndi-name", "TestOracleXaDataSource");
768             props.put("track-connection-by-tx", new Boolean JavaDoc(true));
769             props.put("is-same-RM-override-value", new Boolean JavaDoc(false));
770             props.put("xa-datasource-class",
771                     "oracle.jdbc.xa.client.OracleXADataSource");
772
773             // Add some xa-datasource-properties
774
Hashtable JavaDoc ht = new Hashtable JavaDoc();
775             ht.put("URL", "jdbc:oracle:oci8:@tc");
776             ht.put("User", "scott");
777             ht.put("Password", "tiger");
778             props.put("xa-datasource-properties", ht);
779
780             props
781                     .put("exception-sorter-class-name",
782                             "org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter");
783             props.put("no-tx-separate-pools", new Boolean JavaDoc(true));
784             props.put("type-mapping", "Oracle9i");
785
786             String JavaDoc template = "xa-datasource";
787
788             // In case of any problem an exception will be thrown
789
boolean isDeployed = updateDataSource(module, template, props);
790             assertTrue("deployed successful : " + isDeployed, isDeployed);
791
792             // We don't try to get a connection because the original
793
// data source was never deployed.
794

795             log.info("passed");
796         } catch (Exception JavaDoc e) {
797             log.error("failed", e);
798             fail("Caught exception, message: " + e.getMessage());
799         } finally {
800             // empty
801
}
802     }
803
804     /**
805      * Try to remove a data source using the removeDataSource() method. This
806      * test case uses the "TestLocalTxHsqlDataSource" created by
807      * testCreateAndDeployLocalTxDataSource().
808      *
809      * Note that the "TestLocalTxHsqlDataSource" will be removed from the
810      * test-local-tx-hsqldb-ds.xml file, thus further reference to this data
811      * source may be in-appropriate.
812      */

813     public void testRemoveDataSource() throws Exception JavaDoc {
814         log.info("+++ testRemoveDataSource");
815
816         try {
817
818             String JavaDoc module = "test-local-tx-hsqldb-ds.xml";
819             String JavaDoc jndiName = "TestLocalTxHsqlDataSource";
820
821             // Deploy the module
822
boolean isDeployed = deployModule(module);
823
824             // Was deployment succesful?
825
assertTrue("deployed successful : " + isDeployed, isDeployed);
826
827             // Prepare the template properties
828
HashMap JavaDoc props = new HashMap JavaDoc();
829             props.put("jndi-name", jndiName);
830
831             String JavaDoc template = "datasource-remove";
832
833             // In case of any problem an exception will be thrown
834
isDeployed = removeDataSource(module, template, props);
835
836             // The module should be deployed
837
assertTrue("removed successful : " + isDeployed, isDeployed);
838
839             // But we should NOT be able to get a connection
840
try {
841                 InitialContext JavaDoc ic = new InitialContext JavaDoc();
842                 DataSource JavaDoc ds = (DataSource JavaDoc) ic.lookup(jndiName);
843                 fail("Shouldn't reach this point");
844             } catch (Exception JavaDoc e) {
845                 // Ok, undeploy the module
846
undeployModule(module);
847             }
848
849             log.info("passed");
850         } catch (Exception JavaDoc e) {
851             log.error("failed", e);
852             fail("Caught exception, message: " + e.getMessage());
853         }
854     }
855
856     /**
857      * Try to remove a data source using the removeDataSource() method from a
858      * module with multiple data sources configured. This test case uses the
859      * testMultipleDataSources-ds.xml module located under the
860      * "testsuite/src/etc/deployment-test" directory.
861      */

862     public void testRemoveDataSourceFromMultiNodesModule() throws Exception JavaDoc {
863         log.info("+++ testRemoveDataSourceFromMultiNodesModule");
864
865         try {
866             String JavaDoc module = "testMultipleDataSources-ds.xml";
867             String JavaDoc jndiName1 = "TestDataSource1";
868             String JavaDoc jndiName2 = "TestDataSource2";
869             String JavaDoc delim = File.separator;
870
871             // Find the server directory
872
MBeanServerConnection JavaDoc server = getServer();
873             ObjectName JavaDoc serverConfig = new ObjectName JavaDoc(
874                     "jboss.system:type=ServerConfig");
875             String JavaDoc serverHome = ((File JavaDoc) server.getAttribute(serverConfig,
876                     "ServerHomeDir")).getCanonicalPath();
877             log.info("serverHome = " + serverHome);
878
879             // Find the directory where the test module resides
880
String JavaDoc myPath = new File JavaDoc("").getAbsolutePath();
881             log.info("myPath = " + myPath);
882             int inx = myPath.lastIndexOf(delim);
883             if (inx >= 0)
884                 myPath = myPath.substring(0, myPath.lastIndexOf(delim));
885
886             // Copy the module to the deploy directory
887
String JavaDoc source = myPath + delim + "src" + delim + "etc" + delim
888                     + "deployment-test" + delim + module;
889             String JavaDoc target = serverHome + delim + "deploy" + delim + module;
890             log.info("source = " + source);
891             log.info("target = " + target);
892
893             FileChannel JavaDoc srcChannel = new FileInputStream JavaDoc(source).getChannel();
894             FileChannel JavaDoc destChannel = new FileOutputStream JavaDoc(target).getChannel();
895             srcChannel.transferTo(0, srcChannel.size(), destChannel);
896             srcChannel.close();
897             destChannel.close();
898
899             // See if we can get connection to the data sources:
900
boolean connected = connectToDataSource(jndiName1);
901             assertTrue("Data source " + jndiName1 + " connected successful : " + connected,
902                     connected);
903             connected = connectToDataSource(jndiName2);
904             assertTrue("Data source " + jndiName2 + " connected successful : " + connected,
905                     connected);
906
907             // Prepare the template properties for removing "TestDataSource1"
908
HashMap JavaDoc props = new HashMap JavaDoc();
909             props.put("jndi-name", jndiName1);
910
911             String JavaDoc template = "datasource-remove";
912
913             // In case of any problem an exception will be thrown
914
boolean isDeployed = removeDataSource(module, template, props);
915
916             // The module should be deployed
917
assertTrue("removed successful : " + isDeployed, isDeployed);
918
919             // We should be able to connect to TestDataSource2
920
connected = connectToDataSource(jndiName2);
921             assertTrue("Data source " + jndiName2 + " connected successful : " + connected,
922                     connected);
923
924             // But we should NOT be able connect to TestDataSource1
925
try {
926                 InitialContext JavaDoc ic = new InitialContext JavaDoc();
927                 DataSource JavaDoc ds1 = (DataSource JavaDoc) ic.lookup(jndiName1);
928                 fail("Shouldn't reach this point");
929             } catch (Exception JavaDoc e) {
930                 // Ok, remove the file
931
File JavaDoc targetFile = new File JavaDoc(target);
932                 targetFile.delete();
933
934             }
935
936             log.info("passed");
937         } catch (Exception JavaDoc e) {
938             log.error("failed", e);
939             fail("Caught exception, message: " + e.getMessage());
940         }
941     }
942     
943     /**
944      * Try to remove a data source using the removeDataSource() method.
945      * In this case, the module name is bad and an error should be
946      * returned from the deployment service.
947      */

948     public void testRemoveDataSourceBadModule() throws Exception JavaDoc {
949         log.info("+++ testRemoveDataSourceBadModule");
950         
951         // Setup the data
952
String JavaDoc module = null;
953         String JavaDoc template = "datasource-remove";
954         HashMap JavaDoc props = new HashMap JavaDoc();
955         
956         // Try with a null module name:
957
try {
958             removeDataSource(module, template, props);
959             fail("Shouldn't reach this point");
960         } catch (Exception JavaDoc e) {
961             // expected
962
log.info("passed");
963         }
964         
965         // Try with an empty module name:
966
try {
967             module = "";
968             removeDataSource(module, template, props);
969             fail("Shouldn't reach this point");
970         } catch (Exception JavaDoc e) {
971             // expected
972
log.info("passed");
973         }
974         
975         // Try with an unknown module name:
976
try {
977             module = "unknoenModule";
978             removeDataSource(module, template, props);
979             fail("Shouldn't reach this point");
980         } catch (Exception JavaDoc e) {
981             // expected
982
log.info("passed");
983         }
984     }
985     
986     /**
987      * Try to remove a data source using the removeDataSource() method.
988      * In this case, the template name is bad and an error should be
989      * returned from the deployment service.
990      */

991     public void testRemoveDataSourceBadTemplate() throws Exception JavaDoc {
992         log.info("+++ testRemoveDataSourceBadTemplate");
993
994         try {
995             String JavaDoc module = "test-local-tx-hsqldb-ds.xml";
996             String JavaDoc template = null;
997             HashMap JavaDoc props = new HashMap JavaDoc();
998
999             // Deploy the module
1000
boolean isDeployed = deployModule(module);
1001
1002            // Was deployment succesful?
1003
assertTrue("deployed successful : " + isDeployed, isDeployed);
1004
1005            // Try with a null template name:
1006
try {
1007                removeDataSource(module, template, props);
1008                fail("Shouldn't reach this point");
1009            } catch (Exception JavaDoc e) {
1010                // expected
1011
log.info("passed");
1012            }
1013            
1014            // Try with an empty template name:
1015
try {
1016                template = "";
1017                removeDataSource(module, template, props);
1018                fail("Shouldn't reach this point");
1019            } catch (Exception JavaDoc e) {
1020                // expected
1021
log.info("passed");
1022            }
1023            
1024            // Try with an unknown template name:
1025
try {
1026                template = "unknownTemplate";
1027                removeDataSource(module, template, props);
1028                fail("Shouldn't reach this point");
1029            } catch (Exception JavaDoc e) {
1030                // expected
1031
log.info("passed");
1032            }
1033            
1034            // Undeploy the module:
1035
undeployModule(module);
1036        } catch (Exception JavaDoc e) {
1037            log.error("failed", e);
1038            fail("Caught exception, message: " + e.getMessage());
1039        }
1040    }
1041
1042    private String JavaDoc createModule(String JavaDoc module, String JavaDoc template, HashMap JavaDoc props)
1043            throws Exception JavaDoc {
1044        MBeanServerConnection JavaDoc server = getServer();
1045
1046        // create the module
1047
module = (String JavaDoc) server.invoke(deploymentService, "createModule",
1048                new Object JavaDoc[] { module, template, props }, new String JavaDoc[] {
1049                        "java.lang.String", "java.lang.String",
1050                        "java.util.HashMap" });
1051
1052        log.info("Module '" + module + "' created: " + module);
1053        return module;
1054    }
1055
1056    private boolean removeModule(String JavaDoc module) throws Exception JavaDoc {
1057        MBeanServerConnection JavaDoc server = getServer();
1058
1059        // remove the module, in case it exists
1060
Boolean JavaDoc removed = (Boolean JavaDoc) server.invoke(deploymentService,
1061                "removeModule", new Object JavaDoc[] { module },
1062                new String JavaDoc[] { "java.lang.String" });
1063
1064        log.info("Module '" + module + "' removed: " + removed);
1065
1066        return removed.booleanValue();
1067    }
1068
1069    private boolean deployModule(String JavaDoc module) throws Exception JavaDoc {
1070        MBeanServerConnection JavaDoc server = getServer();
1071
1072        // Deploy the module (move to ./deploy)
1073
server.invoke(deploymentService, "deployModuleAsynch",
1074                new Object JavaDoc[] { module }, new String JavaDoc[] { "java.lang.String" });
1075
1076        return verifyDeploy(server, module);
1077    }
1078
1079    private boolean undeployModule(String JavaDoc module) throws Exception JavaDoc {
1080        MBeanServerConnection JavaDoc server = getServer();
1081        try {
1082            // Get the deployed URL
1083
URL JavaDoc deployedURL = (URL JavaDoc) server.invoke(deploymentService,
1084                    "getDeployedURL", new Object JavaDoc[] { module },
1085                    new String JavaDoc[] { "java.lang.String" });
1086
1087            // Undeploy the module (move to ./undeploy)
1088
server.invoke(deploymentService, "undeployModuleAsynch",
1089                    new Object JavaDoc[] { module },
1090                    new String JavaDoc[] { "java.lang.String" });
1091
1092            // Ask the MainDeployer every 3 secs, 5 times (15secs max wait) if
1093
// the module was undeployed
1094
Boolean JavaDoc isDeployed = new Boolean JavaDoc(false);
1095            for (int tries = 0; tries < 5; tries++) {
1096                // sleep for 3 secs
1097
Thread.sleep(3000);
1098                isDeployed = (Boolean JavaDoc) server.invoke(mainDeployer,
1099                        "isDeployed", new Object JavaDoc[] { deployedURL },
1100                        new String JavaDoc[] { "java.net.URL" });
1101
1102                if (!isDeployed.booleanValue()) {
1103                    break;
1104                }
1105            }
1106            log.info("Module '" + module + "' deployed: " + isDeployed);
1107            return isDeployed.booleanValue();
1108        } catch (Exception JavaDoc e) {
1109            // the module does not exist
1110
log.info("Ignoring caught exception, message: " + e.getMessage());
1111            return false;
1112        }
1113    }
1114
1115    /**
1116     * Proxy method that makes the deployment service call to update an mbean.
1117     *
1118     * @param data
1119     * The data used to update the mbean
1120     * @return True if the mbean was updated, false otherwise.
1121     * @throws Exception
1122     * Bad things happened.
1123     */

1124    private boolean updateMBean(MBeanData data) throws Exception JavaDoc {
1125        MBeanServerConnection JavaDoc server = getServer();
1126        log.info("Updating MBean '" + data + "'");
1127
1128        // create the module
1129
boolean result = false;
1130        result = ((Boolean JavaDoc) server.invoke(deploymentService, "updateMBean",
1131                new Object JavaDoc[] { data },
1132                new String JavaDoc[] { "org.jboss.services.deployment.MBeanData" }))
1133                .booleanValue();
1134
1135        log.info("MBean '" + data + "' update result: " + result);
1136        return result;
1137    }
1138
1139    /**
1140     * Verifies that the mbean was updated successfully by comparing the values
1141     * of all of the attributes.
1142     *
1143     * @param data
1144     * The mbean data that was set.
1145     * @param nestedAttr
1146     * If any of the mbean attribuets were nested xml data, set this
1147     * to the name of said attribute. If not, set to null.
1148     */

1149    private void verifyMBean(MBeanData data, String JavaDoc nestedAttr) {
1150        try {
1151            // Wait for the changes to be deployed (assume 5 second scan delay):
1152
log.info("Wait 10 seconds for changes to deploy");
1153            Thread.sleep(10000);
1154
1155            // Compare all of the changed attribute values to the actual values.
1156
// They must all match for the test to pass.
1157
InitialContext JavaDoc ic = new InitialContext JavaDoc();
1158            RMIAdaptor server = (RMIAdaptor) ic
1159                    .lookup("jmx/invoker/RMIAdaptor");
1160            ObjectName JavaDoc objectName = new ObjectName JavaDoc(data.getName());
1161            Properties JavaDoc attrs = data.getAttributes();
1162            Enumeration JavaDoc keys = attrs.keys();
1163            log.info("Verifying MBean attribute values:");
1164            while (keys.hasMoreElements()) {
1165                String JavaDoc attr = (String JavaDoc) keys.nextElement();
1166                String JavaDoc expected = (String JavaDoc) attrs.get(attr);
1167                Object JavaDoc obj = server.getAttribute(objectName, attr);
1168                String JavaDoc actual = obj.toString();
1169                log.info("-- attribute = " + attr);
1170                log.info(" expect value = " + expected);
1171                log.info(" actual value = " + actual);
1172                log.info(" actual type = " + obj.getClass().getName());
1173                // Note that the value for the nested attribute is not returned
1174
// as expected, so we will not verify that it is correct:
1175
if (!actual.equals(expected) && nestedAttr != null
1176                        && !attr.equals(nestedAttr)) {
1177                    String JavaDoc msg = "Attribute '" + attr + "' has actual value '"
1178                            + actual + "', expected value " + expected + "'";
1179                    log.error(msg);
1180                    fail(msg);
1181                }
1182            }
1183            log.info("passed");
1184        } catch (Exception JavaDoc e) {
1185            log.error(e);
1186            fail("Unexpected error: " + e.getMessage());
1187        }
1188    }
1189
1190    /**
1191     * Calls the deployment service to update the data source.
1192     */

1193    private boolean updateDataSource(String JavaDoc module, String JavaDoc template,
1194            HashMap JavaDoc props) throws Exception JavaDoc {
1195        log.info("updateDataSource('" + module + "', '" + template + "', "
1196                + props);
1197        MBeanServerConnection JavaDoc server = getServer();
1198
1199        // update the data source
1200
module = (String JavaDoc) server.invoke(deploymentService, "updateDataSource",
1201                new Object JavaDoc[] { module, template, props }, new String JavaDoc[] {
1202                        "java.lang.String", "java.lang.String",
1203                        "java.util.HashMap" });
1204
1205        // sleep for 3 secs to allow the old datasource to be undeployed first
1206
// before we verify deployment
1207
Thread.sleep(3000);
1208        return verifyDeploy(server, module);
1209    }
1210
1211    /**
1212     * Calls the deployment service to remove the data source.
1213     */

1214    private boolean removeDataSource(String JavaDoc module, String JavaDoc template,
1215            HashMap JavaDoc props) throws Exception JavaDoc {
1216        log.info("removeDataSource('" + module + "', '" + template + "', "
1217                + props);
1218        MBeanServerConnection JavaDoc server = getServer();
1219
1220        // remove the data source
1221
module = (String JavaDoc) server.invoke(deploymentService, "removeDataSource",
1222                new Object JavaDoc[] { module, template, props }, new String JavaDoc[] {
1223                        "java.lang.String", "java.lang.String",
1224                        "java.util.HashMap" });
1225
1226        // sleep for 3 secs to allow the datasource to be undeployed first
1227
// before we verify deployment
1228
Thread.sleep(3000);
1229        return verifyDeploy(server, module);
1230    }
1231    
1232    /**
1233     * Try to connect to the specified data source. Try the connection
1234     * every 3 seconds, maximum 5 times.
1235     */

1236    private boolean connectToDataSource (String JavaDoc jndiName) throws Exception JavaDoc {
1237        boolean connected = false;
1238        InitialContext JavaDoc ic = new InitialContext JavaDoc();
1239        DataSource JavaDoc ds = null;
1240        Connection JavaDoc connection = null;
1241
1242        // See if we can get a connection
1243
for (int tries = 0; tries < 5; tries++) {
1244            try {
1245                ds = (DataSource JavaDoc) ic.lookup(jndiName);
1246                connection = ds.getConnection();
1247                connection.close();
1248                connected = true;
1249                log.info("Connected to data source: " + jndiName);
1250                break;
1251            } catch (Exception JavaDoc e) {
1252                log.info("Unable to connect to data source: " + jndiName + ". Try again");
1253                // Sleep for 3 secs then try again
1254
Thread.sleep(3000);
1255            }
1256        }
1257        
1258        return connected;
1259    }
1260
1261    /**
1262     * Verifies that a given module is acutally deployed. Waits for a while,
1263     * checking every few seconds, to see if the module has deployed yet.
1264     *
1265     * @param server
1266     * User to invoke methods on the dpeloyment service.
1267     * @param module
1268     * The name of the module (must include the suffix).
1269     * @return
1270     */

1271    private boolean verifyDeploy(MBeanServerConnection JavaDoc server, String JavaDoc module)
1272            throws Exception JavaDoc {
1273        // Get the deployed URL
1274
URL JavaDoc deployedURL = (URL JavaDoc) server.invoke(deploymentService,
1275                "getDeployedURL", new Object JavaDoc[] { module },
1276                new String JavaDoc[] { "java.lang.String" });
1277
1278        // Ask the MainDeployer every 3 secs, 5 times (15secs max wait) if the
1279
// module was deployed
1280
Boolean JavaDoc isDeployed = new Boolean JavaDoc(false);
1281        for (int tries = 0; tries < 5; tries++) {
1282            // sleep for 3 secs
1283
Thread.sleep(3000);
1284            isDeployed = (Boolean JavaDoc) server.invoke(mainDeployer, "isDeployed",
1285                    new Object JavaDoc[] { deployedURL },
1286                    new String JavaDoc[] { "java.net.URL" });
1287
1288            if (isDeployed.booleanValue()) {
1289                break;
1290            }
1291        }
1292        log.info("Module '" + module + "' deployed: " + isDeployed);
1293        return isDeployed.booleanValue();
1294    }
1295}
Popular Tags