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: * @(#)TestRegistryImpl.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.ComponentQuery;
032: import com.sun.jbi.ServiceAssemblyQuery;
033: import com.sun.jbi.management.registry.GenericQuery;
034: import com.sun.jbi.management.registry.Registry;
035: import com.sun.jbi.management.registry.RegistryBuilder;
036: import com.sun.jbi.management.registry.RegistryException;
037: import com.sun.jbi.management.repository.Repository;
038: import com.sun.jbi.management.repository.ArchiveType;
039:
040: import com.sun.jbi.management.system.Util;
041: import com.sun.jbi.management.system.ScaffoldEnvironmentContext;
042:
043: import java.io.File;
044: import java.util.List;
045:
046: public class TestRegistryImpl extends junit.framework.TestCase {
047: /**
048: * The sample Configuration Directory.
049: */
050: private String mConfigDir = null;
051: private File mRegFile;
052: private File mRegSaveFile;
053: private File mRegBackupFile;
054: String mRegFilePath;
055: String mRegSaveFilePath;
056: String mRegCorruptFilePath;
057: String mRegBackupFilePath;
058: String mRegErrorFolderPath;
059: String mRegGoodFilePath;
060: String mCompPath;
061: String mSAPath;
062:
063: private static final String COMPONENT_NAME = "SunSequencingEngine";
064:
065: private static final String SA_NAME = "CompositeApplication";
066:
067: public TestRegistryImpl(String aTestName) {
068: super (aTestName);
069:
070: String srcroot = System.getProperty("junit.srcroot");
071: String manage = "/runtime/manage"; // open-esb build
072: mConfigDir = srcroot + manage + "/bld/test-classes/testdata/";
073:
074: java.io.File f = new java.io.File(srcroot + manage);
075: if (!f.exists()) {
076: manage = "/shasta/manage"; // mainline/whitney build
077: mConfigDir = srcroot + manage + "/bld/regress/testdata/";
078: }
079:
080: mRegFilePath = mConfigDir + File.separator + "jbi-registry.xml";
081: mRegGoodFilePath = mConfigDir + File.separator
082: + "jbi-registry-good.xml";
083: mRegSaveFilePath = mConfigDir + File.separator
084: + "jbi-registry-save.xml";
085: mRegCorruptFilePath = mConfigDir + File.separator
086: + "jbi-registry-corrupt.xml";
087: mRegBackupFilePath = mConfigDir + File.separator
088: + "jbi-registry-backup.xml";
089: mRegErrorFolderPath = mConfigDir + File.separator + "error";
090: mCompPath = mConfigDir + File.separator + "component.zip";
091: mSAPath = mConfigDir + File.separator + "service-assembly.zip";
092:
093: mRegFile = new File(mRegFilePath);
094: mRegSaveFile = new File(mRegSaveFilePath);
095: mRegBackupFile = new File(mRegBackupFilePath);
096:
097: }
098:
099: public void setUp() throws Exception {
100: super .setUp();
101:
102: if (mRegSaveFile.exists()) {
103: mRegSaveFile.delete();
104: }
105: Util.fileCopy(mRegGoodFilePath, mRegSaveFilePath);
106: }
107:
108: public void tearDown() throws Exception {
109: // -- restore registry.xml
110: RegistryBuilder.destroyRegistry();
111: Util.fileCopy(mRegSaveFilePath, mRegFilePath);
112: }
113:
114: /**
115: * Test Registry Creation, when the registry.xml and registry-backup.xml have
116: * not been created. i.e. build the registry from scratch.
117: *
118: * @throws Exception if an unexpected error occurs
119: */
120: public void testRegistryCreation() throws Exception {
121: if (mRegFile.exists()) {
122: mRegFile.delete();
123: }
124:
125: if (mRegBackupFile.exists()) {
126: mRegBackupFile.delete();
127: }
128: Registry reg = Util.createRegistry(true);
129:
130: assertNotNull(reg);
131: }
132:
133: /**
134: * The repository is empty so all the registered entities in the registry
135: * are expected to be cleaned up. This tests the cleanup logic when the registry
136: * has registered entities which are missing in the repository.
137: */
138: public void testInitializingRegistryWithRegistryCleanup()
139: throws Exception {
140: // -- This should cause the registry to be cleaned up, since the repository
141: // -- is empty.
142: Registry reg = Util.createRegistry(false);
143:
144: GenericQuery genQuery = reg.getGenericQuery();
145:
146: List<String> sas = genQuery.getRegisteredServiceAssemblies();
147: List<String> components = genQuery.getRegisteredComponents();
148: List<String> sls = genQuery.getRegisteredSharedLibraries();
149:
150: assertTrue(sas.isEmpty());
151: assertTrue(components.isEmpty());
152: assertTrue(sls.isEmpty());
153: }
154:
155: /**
156: * The repository in this case will have only one component. When the registry is
157: * initialized all the registered entities in the registry
158: * are expected to be cleaned up except the SunSequencingEngine component.
159: * This tests the cleanup logic when the registry has registered entities
160: * most of which are missing in the repository, except one component.
161: */
162: public void testInitializingRegistryWithPartialRegistryCleanup()
163: throws Exception {
164: Repository repos = Util.createManagementContext()
165: .getRepository();
166: repos.addArchive(ArchiveType.COMPONENT, mCompPath);
167:
168: // -- This should cause the registry to be cleaned up, since the repository
169: // -- is empty.
170: Registry reg = Util.createRegistry(false);
171:
172: GenericQuery genQuery = reg.getGenericQuery();
173:
174: List<String> sas = genQuery.getRegisteredServiceAssemblies();
175: List<String> components = genQuery.getRegisteredComponents();
176: List<String> sls = genQuery.getRegisteredSharedLibraries();
177:
178: // -- Verify : Registry should only have the SunSequencingEngine
179: assertTrue(sas.isEmpty());
180: assertFalse(components.isEmpty());
181: assertTrue(components.contains(COMPONENT_NAME));
182: assertTrue(sls.isEmpty());
183:
184: String[] targets = new String[] { "server", "clusterA" };
185: // -- The component should not have any service units, since the SA is cleaned up
186: for (String target : targets) {
187: ComponentQuery compQuery = reg.getComponentQuery(target);
188: List sus = compQuery.getComponentInfo(COMPONENT_NAME)
189: .getServiceUnitList();
190: assertTrue(sus.isEmpty());
191: }
192:
193: // -- The service assembly ref from the instance should also be cleaned up
194: for (String target : targets) {
195: ServiceAssemblyQuery saQuery = reg
196: .getServiceAssemblyQuery(target);
197: List<String> serverSas = saQuery.getServiceAssemblies();
198: assertTrue(serverSas.isEmpty());
199: }
200:
201: repos.purge();
202: }
203:
204: /**
205: * The repository in this case will have a service assemmbly which is deployed to
206: * various instances in the registry. The target components are missing in the repos.
207: * When the registry is initialized the orphaned service assembly should be removed
208: * from all the instances. The repository should still have the assembly.
209: *
210: * This tests the cleanup logic when the registry has orphan service assemblies in
211: * the server/cluster refs.
212: */
213: public void testInitializingRegistryWithOrphanedServiceAssembly()
214: throws Exception {
215: Repository repos = Util.createManagementContext()
216: .getRepository();
217: repos.addArchive(ArchiveType.SERVICE_ASSEMBLY, mSAPath);
218:
219: // -- This should cause the components from the registry to be cleaned up
220: Registry reg = Util.createRegistry(false);
221:
222: GenericQuery genQuery = reg.getGenericQuery();
223:
224: List<String> sas = genQuery.getRegisteredServiceAssemblies();
225: List<String> components = genQuery.getRegisteredComponents();
226: List<String> sls = genQuery.getRegisteredSharedLibraries();
227:
228: // -- Verify : Registry should only have the CompositApplication
229: assertFalse(sas.isEmpty());
230: assertTrue(sas.contains(SA_NAME));
231: assertTrue(components.isEmpty());
232: assertTrue(sls.isEmpty());
233:
234: // -- The orphaned service assembly should be removed from all instances
235: String[] targets = new String[] { "server", "clusterA" };
236: for (String target : targets) {
237: ServiceAssemblyQuery saQuery = reg
238: .getServiceAssemblyQuery(target);
239: List<String> serverSas = saQuery.getServiceAssemblies();
240: assertTrue(serverSas.isEmpty());
241: }
242:
243: repos.purge();
244: }
245:
246: /**
247: * This tests the case when a service assembly is there in the repository,
248: * but not the registry. The repository should be cleanedup.
249: */
250: public void testInitializingRegistryWithRepositoryCleanup()
251: throws Exception {
252:
253: Repository repos = Util.createManagementContext()
254: .getRepository();
255: repos.addArchive(ArchiveType.SERVICE_ASSEMBLY, mSAPath);
256:
257: // Verify : service assembly added properly
258: List<String> sas = repos
259: .getArchiveEntityNames(ArchiveType.SERVICE_ASSEMBLY);
260: assertFalse(sas.isEmpty());
261: assertTrue(sas.contains(SA_NAME));
262:
263: // -- Create an empty registry
264: if (mRegFile.exists()) {
265: mRegFile.delete();
266: }
267:
268: if (mRegBackupFile.exists()) {
269: mRegBackupFile.delete();
270: }
271: Registry reg = Util.createRegistry(false);
272:
273: // -- Verify : Repository should be clean
274: sas = repos.getArchiveEntityNames(ArchiveType.SERVICE_ASSEMBLY);
275: List<String> components = repos
276: .getArchiveEntityNames(ArchiveType.COMPONENT);
277: List<String> sls = repos
278: .getArchiveEntityNames(ArchiveType.SHARED_LIBRARY);
279: assertTrue(sas.isEmpty());
280: assertTrue(components.isEmpty());
281: assertTrue(sls.isEmpty());
282:
283: repos.purge();
284: }
285:
286: /**
287: * Test reading from a bad registry and a missing-backup. The expected
288: * result is a failure to initialize the registry.
289: */
290: public void testReadingCorruptRegistry() throws Exception {
291:
292: if (mRegFile.exists()) {
293: mRegFile.delete();
294: }
295: Util.fileCopy(mRegCorruptFilePath, mRegFilePath);
296:
297: try {
298: Registry reg = Util.createRegistry(true);
299: fail();
300: } catch (RegistryException rex) {
301: File errorFolder = new File(mRegErrorFolderPath);
302: assertTrue(errorFolder.exists());
303: assertTrue(errorFolder.list().length != 0);
304: }
305: }
306:
307: /**
308: * Test reading from a missing registry file when a backup file exists.
309: * Expected result is a successful to initialization of the registry.
310: */
311: public void testInitializingRegistryFromBackup() throws Exception {
312: Util.fileCopy(mRegGoodFilePath, mRegBackupFilePath);
313: Registry reg = Util.createRegistry(true);
314: assertNotNull(reg);
315: }
316:
317: /**
318: * Test reading from a missing registry file when a backup file exists, but is
319: * corrupted. Expected result is a failure to initialize the registry.
320: */
321: public void testInitializingRegistryFromBadBackup()
322: throws Exception {
323: if (mRegFile.exists()) {
324: mRegFile.delete();
325: }
326: Util.fileCopy(mRegCorruptFilePath, mRegBackupFilePath);
327:
328: try {
329: Registry reg = Util.createRegistry(true);
330: fail(" A Registry Exception was expected.");
331: } catch (RegistryException rex) {
332: System.out.println(rex.getMessage());
333: }
334: }
335:
336: }
|