1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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
18 //
19 // This source code implements specifications defined by the Java
20 // Community Process. In order to remain compliant with the specification
21 // DO NOT add / change / or delete method signatures!
22 //
23
24 package javax.enterprise.deploy.shared.factories;
25
26 import junit.framework.TestCase;
27
28 import javax.enterprise.deploy.spi.factories.DeploymentFactory;
29 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
30 import javax.enterprise.deploy.spi.DeploymentManager;
31
32 import javax.enterprise.deploy.spi.factories.MockDeploymentFactory;
33
34 /**
35 * Low level tests on the DeploymentFactoryManager.
36 */
37 public class DeploymentFactoryManagerTest extends TestCase {
38 private DeploymentFactoryManager factoryManager;
39
40 protected void setUp() throws Exception {
41 super.setUp();
42 factoryManager = DeploymentFactoryManager.getInstance();
43 }
44
45 protected void tearDown() throws Exception {
46 factoryManager = null;
47 super.tearDown();
48 }
49
50 public void testGetDeploymentManagerWithoutAnyRegisteredFactories() {
51 try {
52 factoryManager.getDeploymentManager("invalid-uri", null, null);
53 fail("Expected a DeploymentManagerCreationException");
54 } catch (DeploymentManagerCreationException e) {
55 assertTrue(e.getMessage().startsWith("Could not get DeploymentManager"));
56 }
57 }
58
59 public void testDisconnectedGetDeploymentManagerWithoutAnyRegisteredFactories() {
60 try {
61 factoryManager.getDisconnectedDeploymentManager("invalid-uri");
62 fail("Expected a DeploymentManagerCreationException");
63 } catch (DeploymentManagerCreationException e) {
64 assertTrue(e.getMessage().startsWith("Could not get DeploymentManager"));
65 }
66 }
67
68 public void testGetDeploymentManagerWithNullURI() {
69 try {
70 factoryManager.getDeploymentManager(null, null, null);
71 fail("Expected an IllegalArgumentException");
72 } catch (IllegalArgumentException e) {
73 } catch(DeploymentManagerCreationException e) {
74 fail("Unexpected Exception: "+e.getMessage());
75 }
76 }
77
78 public void testDisconnectedGetDeploymentManagerWithNullURI() {
79 try {
80 factoryManager.getDisconnectedDeploymentManager(null);
81 fail("Expected an IllegalArgumentException");
82 } catch (IllegalArgumentException e) {
83 } catch(DeploymentManagerCreationException e) {
84 fail("Unexpected Exception: "+e.getMessage());
85 }
86 }
87
88 public void testRegisterNull() {
89 try {
90 factoryManager.registerDeploymentFactory(null);
91 fail("Should have gotten an IllegalArgumentException");
92 } catch(IllegalArgumentException e) {
93 }
94 }
95
96 public void testRegisterDeploymentFactory() {
97 int initialNumberOfFactories = factoryManager.getDeploymentFactories().length;
98
99 DeploymentFactory factory = new MockDeploymentFactory();
100 factoryManager.registerDeploymentFactory(factory);
101
102 int expectedNumberOfFactories = initialNumberOfFactories + 1;
103 int currentNumberOfFactories = factoryManager.getDeploymentFactories().length;
104
105 assertEquals(expectedNumberOfFactories, currentNumberOfFactories);
106 }
107
108 public void testGetDeploymentManager() {
109 ensureFactoryRegistered();
110 DeploymentManager deploymentManager = null;
111 try {
112 deploymentManager = factoryManager.getDeploymentManager("deployer:geronimo://server:port/application", "username", "password");
113 } catch (DeploymentManagerCreationException e) {
114 fail("Didn't expect a DeploymentManagerException here.");
115 }
116 assertNotNull("Expected an instance of the DeploymentManager", deploymentManager);
117 }
118
119 public void testGetDisconnectedDeploymentManager() {
120 ensureFactoryRegistered();
121 DeploymentManager deploymentManager = null;
122 try {
123 deploymentManager = factoryManager.getDeploymentManager("deployer:geronimo:", null, null);
124 } catch (DeploymentManagerCreationException e) {
125 fail("Didn't expect a DeploymentManagerException here.");
126 }
127 assertNotNull("Expected an instance of the DeploymentManager", deploymentManager);
128 }
129
130 public void testDeploymentManagerCreationException() {
131 ensureFactoryRegistered();
132 try {
133 factoryManager.getDisconnectedDeploymentManager("throw-exception");
134 fail("Expected a DeploymentManagerCreationException");
135 } catch (DeploymentManagerCreationException e) {
136 //
137 // jason: probably not a hot idea to validate the message here
138 //
139 // assertTrue(e.getMessage().startsWith("Could not get DeploymentManager"));
140 }
141 }
142
143 private void ensureFactoryRegistered() {
144 int numberOfFactories = factoryManager.getDeploymentFactories().length;
145 if(numberOfFactories == 0) {
146 factoryManager.registerDeploymentFactory(new MockDeploymentFactory());
147 }
148 assertTrue("We should have a registered DeploymentFactory", numberOfFactories > 0);
149 }
150 }