001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.geronimo.farm.deployment;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.File;
024: import java.io.IOException;
025: import java.io.OutputStream;
026: import java.util.ArrayList;
027: import java.util.Collections;
028: import java.util.LinkedHashSet;
029: import java.util.List;
030:
031: import junit.framework.AssertionFailedError;
032:
033: import org.apache.geronimo.farm.config.ClusterInfo;
034: import org.apache.geronimo.farm.config.NodeInfo;
035: import org.apache.geronimo.gbean.AbstractName;
036: import org.apache.geronimo.gbean.GBeanData;
037: import org.apache.geronimo.kernel.Jsr77Naming;
038: import org.apache.geronimo.kernel.Kernel;
039: import org.apache.geronimo.kernel.config.ConfigurationData;
040: import org.apache.geronimo.kernel.config.ConfigurationInfo;
041: import org.apache.geronimo.kernel.config.ConfigurationModuleType;
042: import org.apache.geronimo.kernel.config.ConfigurationStore;
043: import org.apache.geronimo.kernel.config.InvalidConfigException;
044: import org.apache.geronimo.kernel.config.NoSuchConfigException;
045: import org.apache.geronimo.kernel.repository.Artifact;
046: import org.apache.geronimo.kernel.repository.Environment;
047: import org.apache.geronimo.kernel.repository.WritableListableRepository;
048:
049: import com.agical.rmock.core.describe.ExpressionDescriber;
050: import com.agical.rmock.core.match.operator.AbstractExpression;
051: import com.agical.rmock.extension.junit.RMockTestCase;
052:
053: /**
054: *
055: * @version $Rev:$ $Date:$
056: */
057: public class MasterConfigurationStoreTest extends RMockTestCase {
058:
059: private Kernel kernel;
060: private WritableListableRepository repository;
061: private ClusterInfo clusterInfo;
062: private ClusterConfigurationStoreClient storeClient;
063: private AbstractName clusterInfoName;
064: private ConfigurationStore delegate;
065: private SlaveConfigurationNameBuilder nameBuilder;
066: private Artifact configId;
067:
068: @Override
069: protected void setUp() throws Exception {
070: kernel = (Kernel) mock(Kernel.class);
071: repository = (WritableListableRepository) mock(WritableListableRepository.class);
072: clusterInfo = (ClusterInfo) mock(ClusterInfo.class);
073: storeClient = (ClusterConfigurationStoreClient) mock(ClusterConfigurationStoreClient.class);
074: kernel.getAbstractNameFor(clusterInfo);
075: configId = new Artifact("groupId", "artifactId", "2.0", "car");
076: clusterInfoName = new AbstractName(configId, Collections
077: .singletonMap("name", "ClusterInfo"));
078: modify().returnValue(clusterInfoName);
079:
080: delegate = (ConfigurationStore) mock(ConfigurationStore.class);
081: nameBuilder = (SlaveConfigurationNameBuilder) mock(SlaveConfigurationNameBuilder.class);
082: }
083:
084: private MasterConfigurationStore newMasterConfigurationStore() {
085: return new MasterConfigurationStore(kernel, "objectName", null,
086: repository, new Environment(), clusterInfo, storeClient) {
087: @Override
088: protected ConfigurationStore newConfigurationStore(
089: Kernel kernel, String objectName,
090: AbstractName abstractName,
091: WritableListableRepository repository) {
092: return delegate;
093: }
094:
095: @Override
096: protected SlaveConfigurationNameBuilder newSlaveConfigurationNameBuilder() {
097: return nameBuilder;
098: }
099: };
100: }
101:
102: public void testContainsConfigurationOK() throws Exception {
103: nameBuilder.isSlaveConfigurationName(configId);
104: modify().returnValue(false);
105:
106: delegate.containsConfiguration(configId);
107: modify().returnValue(true);
108:
109: startVerification();
110:
111: MasterConfigurationStore store = newMasterConfigurationStore();
112: assertTrue(store.containsConfiguration(configId));
113: }
114:
115: public void testContainsConfigurationFailsWhenNotMasterConfigId()
116: throws Exception {
117: nameBuilder.isSlaveConfigurationName(configId);
118: modify().returnValue(true);
119:
120: startVerification();
121:
122: MasterConfigurationStore store = newMasterConfigurationStore();
123: assertFalse(store.containsConfiguration(configId));
124: }
125:
126: public void testDelegateCreateNewConfigurationDir()
127: throws Exception {
128: Artifact slaveId = new Artifact("groupId", "slaveId", "2.0",
129: "car");
130: nameBuilder.buildSlaveConfigurationName(configId);
131: modify().returnValue(slaveId);
132:
133: delegate.createNewConfigurationDir(slaveId);
134: File expectedFile = new File("confDir");
135: modify().returnValue(expectedFile);
136:
137: startVerification();
138:
139: MasterConfigurationStore store = newMasterConfigurationStore();
140: assertSame(expectedFile, store
141: .createNewConfigurationDir(configId));
142: }
143:
144: public void testExportFailsWhenNotMasterConfigId() throws Exception {
145: nameBuilder.isSlaveConfigurationName(configId);
146: modify().returnValue(true);
147:
148: startVerification();
149:
150: MasterConfigurationStore store = newMasterConfigurationStore();
151: try {
152: store.exportConfiguration(configId, null);
153: fail();
154: } catch (NoSuchConfigException e) {
155: }
156: }
157:
158: public void testDelegateExport() throws Exception {
159: OutputStream out = new ByteArrayOutputStream();
160:
161: nameBuilder.isSlaveConfigurationName(configId);
162: modify().returnValue(false);
163:
164: delegate.exportConfiguration(configId, out);
165:
166: startVerification();
167:
168: MasterConfigurationStore store = newMasterConfigurationStore();
169: store.exportConfiguration(configId, out);
170: }
171:
172: public void testDelegateGetAbstractName() throws Exception {
173: delegate.getAbstractName();
174: modify().returnValue(clusterInfoName);
175:
176: startVerification();
177:
178: MasterConfigurationStore store = newMasterConfigurationStore();
179: assertSame(clusterInfoName, store.getAbstractName());
180: }
181:
182: public void testDelegateGetObjectName() throws Exception {
183: delegate.getObjectName();
184: String expectedName = "name";
185: modify().returnValue(expectedName);
186:
187: startVerification();
188:
189: MasterConfigurationStore store = newMasterConfigurationStore();
190: assertSame(expectedName, store.getObjectName());
191: }
192:
193: public void testIsInPlaceConfigurationWhenNotMasterConfigId()
194: throws Exception {
195: nameBuilder.isSlaveConfigurationName(configId);
196: modify().returnValue(true);
197:
198: startVerification();
199:
200: MasterConfigurationStore store = newMasterConfigurationStore();
201: try {
202: store.isInPlaceConfiguration(configId);
203: fail();
204: } catch (NoSuchConfigException e) {
205: }
206: }
207:
208: public void testIsInPlaceConfigurationReturnsFalse()
209: throws Exception {
210: nameBuilder.isSlaveConfigurationName(configId);
211: modify().returnValue(false);
212:
213: startVerification();
214:
215: MasterConfigurationStore store = newMasterConfigurationStore();
216: assertFalse(store.isInPlaceConfiguration(configId));
217: }
218:
219: public void testListConfigurationFilterNoneMasterConfigIds()
220: throws Exception {
221: List<ConfigurationInfo> configurationInfos = new ArrayList<ConfigurationInfo>();
222:
223: ConfigurationInfo configurationInfo = newConfigurationInfo(configId);
224: configurationInfos.add(configurationInfo);
225:
226: Artifact configId2 = new Artifact("groupId", "artifactId2",
227: "2.0", "car");
228: ConfigurationInfo configurationInfo2 = newConfigurationInfo(configId2);
229: configurationInfos.add(configurationInfo2);
230:
231: delegate.listConfigurations();
232: modify().returnValue(configurationInfos);
233:
234: nameBuilder.isSlaveConfigurationName(configId);
235: modify().returnValue(false);
236:
237: nameBuilder.isSlaveConfigurationName(configId2);
238: modify().returnValue(true);
239:
240: startVerification();
241:
242: MasterConfigurationStore store = newMasterConfigurationStore();
243: List<ConfigurationInfo> listedConfigurations = store
244: .listConfigurations();
245: assertEquals(1, listedConfigurations.size());
246: assertTrue(listedConfigurations.contains(configurationInfo));
247: }
248:
249: public void testLoadConfigurationWhenNotMasterConfigId()
250: throws Exception {
251: nameBuilder.isSlaveConfigurationName(configId);
252: modify().returnValue(true);
253:
254: startVerification();
255:
256: MasterConfigurationStore store = newMasterConfigurationStore();
257: try {
258: store.loadConfiguration(configId);
259: fail();
260: } catch (NoSuchConfigException e) {
261: }
262: }
263:
264: public void testDelegateLoadConfiguration() throws Exception {
265: nameBuilder.isSlaveConfigurationName(configId);
266: modify().returnValue(false);
267:
268: delegate.loadConfiguration(configId);
269:
270: startVerification();
271:
272: MasterConfigurationStore store = newMasterConfigurationStore();
273: store.loadConfiguration(configId);
274: }
275:
276: public void testResolveWhenNotMasterConfigId() throws Exception {
277: nameBuilder.isSlaveConfigurationName(configId);
278: modify().returnValue(true);
279:
280: startVerification();
281:
282: MasterConfigurationStore store = newMasterConfigurationStore();
283: try {
284: store.resolve(configId, null, null);
285: fail();
286: } catch (NoSuchConfigException e) {
287: }
288: }
289:
290: public void testDelegateResolve() throws Exception {
291: nameBuilder.isSlaveConfigurationName(configId);
292: modify().returnValue(false);
293:
294: delegate.resolve(configId, null, null);
295:
296: startVerification();
297:
298: MasterConfigurationStore store = newMasterConfigurationStore();
299: store.resolve(configId, null, null);
300: }
301:
302: public void testUninstall() throws Exception {
303: nameBuilder.isSlaveConfigurationName(configId);
304: modify().returnValue(false);
305:
306: nameBuilder.buildSlaveConfigurationName(configId);
307: Artifact slaveId = new Artifact("groupId", "slaveId", "2.0",
308: "car");
309: modify().returnValue(slaveId);
310:
311: storeClient.uninstall(clusterInfo, slaveId);
312:
313: delegate.uninstall(slaveId);
314: delegate.uninstall(configId);
315:
316: startVerification();
317:
318: MasterConfigurationStore store = newMasterConfigurationStore();
319: store.uninstall(configId);
320: }
321:
322: public void testUninstallWhenNotMasterConfigId() throws Exception {
323: nameBuilder.isSlaveConfigurationName(configId);
324: modify().returnValue(true);
325:
326: startVerification();
327:
328: MasterConfigurationStore store = newMasterConfigurationStore();
329: try {
330: store.uninstall(configId);
331: fail();
332: } catch (NoSuchConfigException e) {
333: }
334: }
335:
336: public void testInstallOK() throws Exception {
337: ConfigurationData configurationData = new ConfigurationData(
338: ConfigurationModuleType.CAR, new LinkedHashSet(),
339: new ArrayList(), Collections.EMPTY_MAP,
340: new Environment(configId),
341: new File("configurationDir"), null, new Jsr77Naming());
342:
343: final Artifact slaveId = new Artifact("groupId", "slaveId",
344: "2.0", "car");
345: nameBuilder.buildSlaveConfigurationName(configId);
346: modify().returnValue(slaveId);
347:
348: storeClient.install(clusterInfo, configurationData);
349: modify().args(is.AS_RECORDED, new AbstractExpression() {
350: public void describeWith(ExpressionDescriber arg)
351: throws IOException {
352: }
353:
354: public boolean passes(Object arg) {
355: ConfigurationData configurationData = (ConfigurationData) arg;
356: assertSame(slaveId, configurationData.getId());
357: return true;
358: }
359: });
360:
361: delegate.install(configurationData);
362: modify().args(new AbstractExpression() {
363: public void describeWith(ExpressionDescriber arg)
364: throws IOException {
365: }
366:
367: public boolean passes(Object arg) {
368: ConfigurationData configurationData = (ConfigurationData) arg;
369: assertSame(slaveId, configurationData.getId());
370: return true;
371: }
372: });
373:
374: NodeInfo nodeInfo = (NodeInfo) mock(NodeInfo.class);
375: nodeInfo.getName();
376: final String nodeName = "nodeName";
377: modify().multiplicity(expect.from(0)).returnValue(nodeName);
378: clusterInfo.getNodeInfos();
379: modify().returnValue(Collections.singleton(nodeInfo));
380:
381: delegate.createNewConfigurationDir(configId);
382: final File masterDir = new File("masterDir");
383: modify().returnValue(masterDir);
384:
385: delegate.install(null);
386: modify().args(new AbstractExpression() {
387: public void describeWith(ExpressionDescriber arg)
388: throws IOException {
389: }
390:
391: public boolean passes(Object arg) {
392: ConfigurationData configurationData = (ConfigurationData) arg;
393: assertSame(configId, configurationData.getId());
394: assertSame(masterDir, configurationData
395: .getConfigurationDir());
396:
397: List<GBeanData> gbeans;
398: try {
399: gbeans = configurationData.getGBeans(getClass()
400: .getClassLoader());
401: } catch (InvalidConfigException e) {
402: throw new AssertionFailedError();
403: }
404: assertEquals(1, gbeans.size());
405: GBeanData gbean = gbeans.get(0);
406: assertEquals(
407: BasicClusterConfigurationController.GBEAN_INFO,
408: gbean.getGBeanInfo());
409: assertEquals(
410: slaveId,
411: gbean
412: .getAttribute(BasicClusterConfigurationController.GBEAN_ATTR_ARTIFACT));
413: assertEquals(
414: nodeName,
415: gbean
416: .getAttribute(BasicClusterConfigurationController.GBEAN_ATTR_NODE_NAME));
417: return true;
418: }
419: });
420:
421: startVerification();
422:
423: MasterConfigurationStore store = newMasterConfigurationStore();
424: store.install(configurationData);
425: }
426:
427: private ConfigurationInfo newConfigurationInfo(Artifact configId) {
428: return new ConfigurationInfo(clusterInfoName, configId,
429: ConfigurationModuleType.CAR, 1l, Collections.EMPTY_SET,
430: Collections.EMPTY_SET, null);
431: }
432:
433: }
|