001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestUpdaterImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.registry.xml;
030:
031: import com.sun.jbi.EnvironmentContext;
032: import com.sun.jbi.ServiceUnitState;
033: import com.sun.jbi.ComponentInfo;
034: import com.sun.jbi.ServiceUnitInfo;
035: import com.sun.jbi.ComponentState;
036: import com.sun.jbi.ComponentType;
037: import com.sun.jbi.management.ConfigurationCategory;
038: import com.sun.jbi.management.registry.data.ComponentInfoImpl;
039: import com.sun.jbi.management.registry.data.ServiceUnitInfoImpl;
040:
041: import com.sun.jbi.management.registry.GenericQuery;
042: import com.sun.jbi.management.registry.Updater;
043: import com.sun.jbi.management.registry.Registry;
044: import com.sun.jbi.management.registry.RegistryBuilder;
045: import com.sun.jbi.management.registry.RegistryException;
046:
047: import com.sun.jbi.management.repository.Repository;
048: import com.sun.jbi.management.repository.ArchiveType;
049:
050: import com.sun.jbi.management.system.Util;
051:
052: import java.io.File;
053: import java.math.BigInteger;
054: import java.util.List;
055: import java.util.Properties;
056: import java.util.Map;
057:
058: import javax.xml.parsers.DocumentBuilder;
059: import javax.xml.parsers.DocumentBuilderFactory;
060:
061: public class TestUpdaterImpl extends junit.framework.TestCase {
062: /**
063: * The sample Configuration Directory.
064: */
065: private String mConfigDir = null;
066: private File mRegFile;
067: private File mRegBkupFile;
068: String mRegFilePath;
069: String mRegGoodFilePath;
070: String mRegBkupFilePath;
071: String mComponentZipPath;
072: String mSharedLibraryZipPath;
073: String mServiceAssemblyZipPath;
074:
075: static final String COMPONENT_NAME = "SunSequencingEngine";
076: static final String SHARED_LIBRARY_NAME = "sun-wsdl-library";
077: static final String SERVICE_ASSEMBLY_NAME = "CompositeApplication";
078:
079: public TestUpdaterImpl(String aTestName) {
080: super (aTestName);
081:
082: String srcroot = System.getProperty("junit.srcroot");
083: String manage = "/runtime/manage"; // open-esb build
084: mConfigDir = srcroot + manage + "/bld/test-classes/testdata/";
085:
086: java.io.File f = new java.io.File(srcroot + manage);
087: if (!f.exists()) {
088: manage = "/shasta/manage"; // mainline/whitney build
089: mConfigDir = srcroot + manage + "/bld/regress/testdata/";
090: }
091:
092: mRegFilePath = mConfigDir + File.separator + "jbi-registry.xml";
093: mRegGoodFilePath = mConfigDir + File.separator
094: + "jbi-registry-good.xml";
095: mRegBkupFilePath = mConfigDir + File.separator
096: + "jbi-registry-backup.xml";
097: mComponentZipPath = mConfigDir + "component.zip";
098: mServiceAssemblyZipPath = mConfigDir + "service-assembly.zip";
099: mSharedLibraryZipPath = mConfigDir + "wsdlsl.jar";
100:
101: mRegFile = new File(mRegFilePath);
102: mRegBkupFile = new File(mRegBkupFilePath);
103:
104: }
105:
106: public void setUp() throws Exception {
107: super .setUp();
108: if (mRegFile.exists()) {
109: mRegFile.delete();
110: }
111: if (mRegBkupFile.exists()) {
112: mRegBkupFile.delete();
113: }
114: }
115:
116: public void tearDown() throws Exception {
117: }
118:
119: /**
120: * @throws Exception if an unexpected error occurs
121: */
122: public void testAddServer() throws Exception {
123: Registry reg = Util.createRegistry();
124: Updater updater = reg.getUpdater();
125:
126: updater.addServer("another-server");
127: }
128:
129: /**
130: * @throws Exception if an unexpected error occurs
131: */
132: public void testAddCluster() throws Exception {
133: Registry reg = Util.createRegistry();
134: Updater updater = reg.getUpdater();
135:
136: updater.addCluster("another-cluster");
137: }
138:
139: /**
140: * @throws Exception if an unexpected error occurs
141: */
142: public void testAddComponent() throws Exception {
143: Registry reg = Util.createRegistry();
144: Updater updater = reg.getUpdater();
145:
146: ComponentInfo comp = createTestComponent();
147:
148: updater.addComponent("another-server", comp);
149: updater.addComponent("another-cluster", comp);
150: }
151:
152: /**
153: * @throws Exception if an unexpected error occurs
154: */
155: public void testAddSharedLibrary() throws Exception {
156: Registry reg = Util.createRegistry();
157: Updater updater = reg.getUpdater();
158:
159: ComponentInfo sl = createTestSharedLibrary();
160:
161: updater.addSharedLibrary("another-server", sl);
162: updater.addSharedLibrary("another-cluster", sl);
163: }
164:
165: public void testAddServiceUnitToComponent() throws Exception {
166: Registry reg = Util.createRegistry();
167: Updater updater = reg.getUpdater();
168:
169: ServiceUnitInfo suInfo = createTestServiceUnit("SU2");
170: updater.addServiceUnitToComponent("another-server",
171: "SunBPELEngine", suInfo);
172: }
173:
174: public void testRemoveServiceUnitFromComponent() throws Exception {
175: Registry reg = Util.createRegistry();
176: Updater updater = reg.getUpdater();
177:
178: updater.removeServiceUnitFromComponent("another-server",
179: "SunBPELEngine", "SU2");
180: }
181:
182: public void testRemoveSharedLibrary() throws Exception {
183: Registry reg = Util.createRegistry();
184: Updater updater = reg.getUpdater();
185:
186: updater.removeSharedLibrary("another-server",
187: "SunBPELEngineLibrary");
188: updater.removeSharedLibrary("another-cluster",
189: "SunBPELEngineLibrary");
190: updater.removeSharedLibrary("domain", "SunBPELEngineLibrary");
191: }
192:
193: public void testRemoveComponent() throws Exception {
194: Registry reg = Util.createRegistry();
195: Updater updater = reg.getUpdater();
196:
197: updater.removeComponent("another-server", "SunBPELEngine");
198: updater.removeComponent("another-cluster", "SunBPELEngine");
199: updater.removeComponent("domain", "SunBPELEngine");
200: }
201:
202: public void testRemoveServer() throws Exception {
203: Registry reg = Util.createRegistry();
204: Updater updater = reg.getUpdater();
205:
206: updater.removeServer("another-server");
207: }
208:
209: public void testRemoveCluster() throws Exception {
210: Registry reg = Util.createRegistry();
211: Updater updater = reg.getUpdater();
212:
213: updater.removeCluster("another-cluster");
214: }
215:
216: /**
217: * Test setting a configuration attribute.
218: */
219: public void testSetAttribute() throws Exception {
220: Registry reg = Util.createRegistry();
221: Updater updater = reg.getUpdater();
222:
223: updater.setAttribute("domain", ConfigurationCategory.System,
224: "newAttribute", "newValue");
225:
226: // -- Verify
227: GenericQuery query = reg.getGenericQuery();
228: String value = query.getAttribute("domain",
229: ConfigurationCategory.System, "newAttribute");
230:
231: assertEquals("newValue", value);
232: }
233:
234: /**
235: * Test deleting a configuration attribute for target=domain, no action should
236: * be taken
237: */
238: public void testDeleteDomainAttribute() throws Exception {
239: Registry reg = Util.createRegistry();
240: Updater updater = reg.getUpdater();
241:
242: updater.setAttribute("domain", ConfigurationCategory.System,
243: "newAttribute", "newValue");
244: updater.deleteAttribute("domain", ConfigurationCategory.System,
245: "newAttribute");
246:
247: // -- Verify
248: GenericQuery query = reg.getGenericQuery();
249: String value = query.getAttribute("domain",
250: ConfigurationCategory.System, "newAttribute");
251:
252: assertNotNull(value);
253: assertEquals("newValue", value);
254: }
255:
256: /**
257: * Test deleting a configuration attribute for non-domain target, no action should
258: * be taken
259: */
260: public void testDeleteNonDomainAttribute() throws Exception {
261: Registry reg = Util.createRegistry();
262: Updater updater = reg.getUpdater();
263:
264: updater.addServer("server");
265: updater.setAttribute("server", ConfigurationCategory.System,
266: "newAttribute", "newValue");
267: updater.deleteAttribute("server", ConfigurationCategory.System,
268: "newAttribute");
269:
270: // -- Verify
271: GenericQuery query = reg.getGenericQuery();
272: assertFalse(query.isAttributeOverriden("server",
273: ConfigurationCategory.System, "newAttribute"));
274: }
275:
276: /**
277: * This method is used to test the setComponentUpgradeNumber method
278: * @throws Exception if the test could not be completed
279: */
280: public void testSetComponentUpgradeNumber() throws Exception {
281: Registry reg = Util.createRegistry();
282: Updater updater = reg.getUpdater();
283: updater.setComponentUpgradeNumber("SunSequencingEngine",
284: BigInteger.ONE);
285: }
286:
287: /**
288: * Test setting a component configuration attribute
289: */
290: public void testSetComponentAttributes() throws Exception {
291: Registry reg = Util.createRegistry();
292: Updater updater = reg.getUpdater();
293:
294: // Setup
295: ComponentInfo comp = createTestComponent();
296: updater.addServer("another-server");
297: updater.addComponent("another-server", comp);
298:
299: java.util.Properties props = new java.util.Properties();
300: props.put("p1", "v1");
301: props.put("p2", "v2");
302: updater.setComponentAttributes(comp.getName(),
303: "another-server", props);
304:
305: // cleanup
306: updater.removeComponent("another-server", comp.getName());
307: updater.removeServer("another-server");
308:
309: }
310:
311: /**
312: * Test adding application variables
313: */
314: public void testAddComponentApplicationVariables() throws Exception {
315: Registry reg = Util.createRegistry();
316: Updater updater = reg.getUpdater();
317:
318: // Setup
319: ComponentInfo comp = createTestComponent();
320: updater.addServer("another-server");
321: updater.addComponent("another-server", comp);
322:
323: com.sun.jbi.management.ComponentInfo.Variable[] vars = new com.sun.jbi.management.ComponentInfo.Variable[4];
324:
325: vars[0] = new com.sun.jbi.management.ComponentInfo.Variable(
326: "name", "Peter", "STRING");
327: vars[1] = new com.sun.jbi.management.ComponentInfo.Variable(
328: "ID", "007", "NUMBER");
329: vars[2] = new com.sun.jbi.management.ComponentInfo.Variable(
330: "password", "56757X15", "PASSWORD");
331: vars[3] = new com.sun.jbi.management.ComponentInfo.Variable(
332: "isEmployee", "true", "BOOLEAN");
333:
334: updater.addComponentApplicationVariables(comp.getName(),
335: "another-server", vars);
336:
337: // Verify
338: RegistryDocument regDoc = new RegistryDocument(getRegistryDom());
339: com.sun.jbi.management.ComponentInfo.Variable[] retrivedVars = regDoc
340: .getComponentApplicationVariables("another-server",
341: true, comp.getName());
342:
343: assertEquals(4, retrivedVars.length);
344:
345: // cleanup
346: updater.removeComponent("another-server", comp.getName());
347: updater.removeServer("another-server");
348:
349: }
350:
351: /**
352: * Test updating application variables
353: */
354: public void testUpdateComponentApplicationVariables()
355: throws Exception {
356: Registry reg = Util.createRegistry();
357: Updater updater = reg.getUpdater();
358:
359: // Setup
360: ComponentInfo comp = createTestComponent();
361: updater.addServer("another-server");
362: updater.addComponent("another-server", comp);
363:
364: com.sun.jbi.management.ComponentInfo.Variable[] vars = new com.sun.jbi.management.ComponentInfo.Variable[4];
365:
366: vars[0] = new com.sun.jbi.management.ComponentInfo.Variable(
367: "name", "Peter", "STRING");
368: vars[1] = new com.sun.jbi.management.ComponentInfo.Variable(
369: "ID", "007", "NUMBER");
370: vars[2] = new com.sun.jbi.management.ComponentInfo.Variable(
371: "password", "56757X15", "PASSWORD");
372: vars[3] = new com.sun.jbi.management.ComponentInfo.Variable(
373: "isEmployee", "true", "BOOLEAN");
374:
375: updater.addComponentApplicationVariables(comp.getName(),
376: "another-server", vars);
377:
378: com.sun.jbi.management.ComponentInfo.Variable[] updates = new com.sun.jbi.management.ComponentInfo.Variable[2];
379:
380: updates[0] = new com.sun.jbi.management.ComponentInfo.Variable(
381: "name", "Parker", "STRING");
382: updates[1] = new com.sun.jbi.management.ComponentInfo.Variable(
383: "phone", "89786", "NUMBER");
384:
385: updater.updateComponentApplicationVariables(comp.getName(),
386: "another-server", updates);
387:
388: // Verify
389: RegistryDocument regDoc = new RegistryDocument(getRegistryDom());
390: com.sun.jbi.management.ComponentInfo.Variable[] retrivedVars = regDoc
391: .getComponentApplicationVariables("another-server",
392: true, comp.getName());
393:
394: boolean updated = false;
395: for (com.sun.jbi.management.ComponentInfo.Variable var : retrivedVars) {
396: if (var.getName().equals("name")) {
397: assertTrue(var.getValue().equals("Parker"));
398: updated = true;
399: }
400: }
401:
402: assertTrue(updated);
403:
404: // cleanup
405: updater.removeComponent("another-server", comp.getName());
406: updater.removeServer("another-server");
407:
408: }
409:
410: /**
411: * Test updating application variables
412: */
413: public void testDeleteComponentApplicationVariables()
414: throws Exception {
415: Registry reg = Util.createRegistry();
416: Updater updater = reg.getUpdater();
417:
418: // Setup
419: ComponentInfo comp = createTestComponent();
420: updater.addServer("another-server");
421: updater.addComponent("another-server", comp);
422:
423: com.sun.jbi.management.ComponentInfo.Variable[] vars = new com.sun.jbi.management.ComponentInfo.Variable[4];
424:
425: vars[0] = new com.sun.jbi.management.ComponentInfo.Variable(
426: "name", "Peter", "STRING");
427: vars[1] = new com.sun.jbi.management.ComponentInfo.Variable(
428: "ID", "007", "NUMBER");
429: vars[2] = new com.sun.jbi.management.ComponentInfo.Variable(
430: "password", "56757X15", "PASSWORD");
431: vars[3] = new com.sun.jbi.management.ComponentInfo.Variable(
432: "isEmployee", "true", "BOOLEAN");
433:
434: updater.addComponentApplicationVariables(comp.getName(),
435: "another-server", vars);
436:
437: String[] varNames = new String[] { "name", "ID", "password" };
438:
439: updater.deleteComponentApplicationVariables(comp.getName(),
440: "another-server", varNames);
441:
442: // Verify
443: RegistryDocument regDoc = new RegistryDocument(getRegistryDom());
444: com.sun.jbi.management.ComponentInfo.Variable[] retrivedVars = regDoc
445: .getComponentApplicationVariables("another-server",
446: true, comp.getName());
447:
448: assertEquals(1, retrivedVars.length);
449: assertTrue(vars[3].equals(retrivedVars[0]));
450:
451: // cleanup
452: updater.removeComponent("another-server", comp.getName());
453: updater.removeServer("another-server");
454:
455: }
456:
457: /**
458: * Test adding application configuration
459: */
460: public void testAddComponentApplicationConfiguration()
461: throws Exception {
462: Registry reg = Util.createRegistry();
463: Updater updater = reg.getUpdater();
464:
465: // Setup
466: ComponentInfo comp = createTestComponent();
467: updater.addServer("another-server");
468: updater.addComponent("another-server", comp);
469:
470: Properties config1 = new Properties();
471: config1.setProperty("configurationName", "myName");
472: config1.setProperty("prop1", "value1");
473: config1.setProperty("prop2", "value2");
474:
475: updater.addComponentApplicationConfiguration(comp.getName(),
476: "another-server", config1);
477:
478: // Verify
479: RegistryDocument regDoc = new RegistryDocument(getRegistryDom());
480: Map<String, Properties> configs = regDoc
481: .getComponentApplicationConfiguration("another-server",
482: true, comp.getName());
483:
484: assertEquals(1, configs.size());
485:
486: // cleanup
487: updater.removeComponent("another-server", comp.getName());
488: updater.removeServer("another-server");
489: }
490:
491: /**
492: * Test adding application configuration
493: */
494: public void testUpdateComponentApplicationConfiguration()
495: throws Exception {
496: Registry reg = Util.createRegistry();
497: Updater updater = reg.getUpdater();
498:
499: // Setup
500: ComponentInfo comp = createTestComponent();
501: updater.addServer("another-server");
502: updater.addComponent("another-server", comp);
503:
504: Properties config1 = new Properties();
505: config1.setProperty("configurationName", "myName");
506: config1.setProperty("prop1", "value1");
507: config1.setProperty("prop2", "value2");
508:
509: updater.addComponentApplicationConfiguration(comp.getName(),
510: "another-server", config1);
511: config1.setProperty("prop1", "updatedValue1");
512:
513: // Update
514: updater.addComponentApplicationConfiguration(comp.getName(),
515: "another-server", config1);
516:
517: // Verify
518: RegistryDocument regDoc = new RegistryDocument(getRegistryDom());
519: Map<String, Properties> configs = regDoc
520: .getComponentApplicationConfiguration("another-server",
521: true, comp.getName());
522:
523: assertEquals(1, configs.size());
524:
525: Properties updatedProps = configs.get("myName");
526: assertEquals(updatedProps.getProperty("prop1"), "updatedValue1");
527:
528: // cleanup
529: updater.removeComponent("another-server", comp.getName());
530: updater.removeServer("another-server");
531: }
532:
533: /**
534: * Test delete application configuration
535: */
536: public void testDeleteComponentApplicationConfiguration()
537: throws Exception {
538: Registry reg = Util.createRegistry();
539: Updater updater = reg.getUpdater();
540:
541: // Setup
542: ComponentInfo comp = createTestComponent();
543: updater.addServer("another-server");
544: updater.addComponent("another-server", comp);
545:
546: Properties config1 = new Properties();
547: config1.setProperty("configurationName", "myName");
548: config1.setProperty("prop1", "value1");
549: config1.setProperty("prop2", "value2");
550:
551: updater.addComponentApplicationConfiguration(comp.getName(),
552: "another-server", config1);
553: config1.setProperty("prop1", "updatedValue1");
554:
555: // Delete
556: updater.deleteComponentApplicationConfiguration(comp.getName(),
557: "another-server", "myName");
558:
559: // Verify
560: RegistryDocument regDoc = new RegistryDocument(getRegistryDom());
561: Map<String, Properties> configs = regDoc
562: .getComponentApplicationConfiguration("another-server",
563: true, comp.getName());
564:
565: assertEquals(0, configs.size());
566:
567: // cleanup
568: updater.removeComponent("another-server", comp.getName());
569: updater.removeServer("another-server");
570: }
571:
572: /**
573: * Test adding application configuration with missing "configurationName"
574: */
575: public void testAddComponentApplicationConfigurationMissingName()
576: throws Exception {
577: Registry reg = Util.createRegistry();
578: Updater updater = reg.getUpdater();
579:
580: // Setup
581: ComponentInfo comp = createTestComponent();
582: updater.addServer("another-server");
583: updater.addComponent("another-server", comp);
584:
585: Properties config1 = new Properties();
586: config1.setProperty("prop1", "value1");
587: config1.setProperty("prop2", "value2");
588:
589: try {
590: updater.addComponentApplicationConfiguration(
591: comp.getName(), "another-server", config1);
592: fail("RegistryException not thrown for missing configurationName key");
593: } catch (RegistryException ex) {
594: assertTrue(true);
595: }
596:
597: // Verify config not added
598: RegistryDocument regDoc = new RegistryDocument(getRegistryDom());
599: Map<String, Properties> configs = regDoc
600: .getComponentApplicationConfiguration("another-server",
601: true, comp.getName());
602:
603: assertEquals(0, configs.size());
604:
605: // cleanup
606: updater.removeComponent("another-server", comp.getName());
607: updater.removeServer("another-server");
608: }
609:
610: /*----------------------------------------------------------------------------------*\
611: * Private Helpers *
612: \*----------------------------------------------------------------------------------*/
613:
614: private ComponentInfo createTestComponent() {
615: ComponentInfoImpl component = new ComponentInfoImpl();
616:
617: component.setName("SunBPELEngine");
618: component.setWorkspaceRoot("C:/foo");
619: component.setInstallRoot("C:/foo");
620: component.setComponentClassName("SunBPELEngine.class");
621: component.setBootstrapClassLoaderSelfFirst(true);
622: component.setClassLoaderSelfFirst(true);
623: component.setProperty("time", "seconds");
624: component.setProperty("threads", "10");
625: component.setStatus(ComponentState.STOPPED);
626: component.setTimestamp(1000);
627: component.setUpgradeNumber(1);
628: component.setComponentType(ComponentType.ENGINE);
629: component.addServiceUnitInfo(createTestServiceUnit("SU1"));
630:
631: return component;
632: }
633:
634: private ServiceUnitInfo createTestServiceUnit(String name) {
635: ServiceUnitInfoImpl suinfo = new ServiceUnitInfoImpl();
636: suinfo.setName(name);
637: suinfo.setServiceAssemblyName("CompositeApplication");
638: suinfo.setState(ServiceUnitState.STOPPED);
639:
640: return suinfo;
641: }
642:
643: private ComponentInfo createTestSharedLibrary() {
644: ComponentInfoImpl sl = new ComponentInfoImpl();
645:
646: sl.setName("SunBPELEngineLibrary");
647: sl.setClassLoaderSelfFirst(true);
648: sl.setInstallRoot("D:/tmp");
649: return sl;
650: }
651:
652: private org.w3c.dom.Document getRegistryDom() throws Exception {
653: DocumentBuilderFactory dbf = DocumentBuilderFactory
654: .newInstance();
655: DocumentBuilder db = dbf.newDocumentBuilder();
656: org.w3c.dom.Document document = db.parse(mRegFile);
657: return document;
658: }
659: }
|