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: * @(#)TestParallelAccess.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.ComponentType;
032: import com.sun.jbi.management.registry.Registry;
033: import com.sun.jbi.management.system.Util;
034:
035: import java.io.File;
036:
037: public class TestParallelAccess extends junit.framework.TestCase {
038:
039: /**
040: * Test Component Name.
041: */
042: private String mTstComp = null;
043: private String mConfigDir = null;
044: private File mRegFile;
045: private File mRegBkupFile;
046: String mRegFilePath;
047: String mRegBkupFilePath;
048:
049: /**
050: * Number of tests.
051: */
052: private static final int NUM_THREADS = 10;
053:
054: /**
055: * The Registry.
056: */
057: private Registry mRegistry = null;
058:
059: public TestParallelAccess(String aTestName) throws Exception {
060: super (aTestName);
061: String srcroot = System.getProperty("junit.srcroot");
062: String manage = "/runtime/manage"; // open-esb build
063: mConfigDir = srcroot + manage + "/bld/test-classes/testdata/";
064:
065: java.io.File f = new java.io.File(srcroot + manage);
066: if (!f.exists()) {
067: manage = "/shasta/manage"; // mainline/whitney build
068: mConfigDir = srcroot + manage + "/bld/regress/testdata/";
069: }
070:
071: mRegFilePath = mConfigDir + File.separator + "jbi-registry.xml";
072: mRegBkupFilePath = mConfigDir + File.separator
073: + "jbi-registry-backup.xml";
074: mTstComp = "FileBinding";
075:
076: mRegFile = new File(mRegFilePath);
077: mRegBkupFile = new File(mRegBkupFilePath);
078:
079: }
080:
081: public void setUp() throws Exception {
082: super .setUp();
083:
084: if (mRegFile.exists()) {
085: mRegFile.delete();
086: }
087: if (mRegBkupFile.exists()) {
088: mRegBkupFile.delete();
089: }
090:
091: com.sun.jbi.management.registry.RegistryBuilder
092: .destroyRegistry();
093: mRegistry = Util.createRegistry(true);
094: mRegistry.getUpdater().addServer("another-server");
095: }
096:
097: public void tearDown() throws Exception {
098: if (mRegFile.exists()) {
099: mRegFile.delete();
100: }
101: if (mRegBkupFile.exists()) {
102: mRegBkupFile.delete();
103: }
104:
105: com.sun.jbi.management.registry.RegistryBuilder
106: .destroyRegistry();
107: }
108:
109: /**
110: * Test adding components to the Registry in parallel.
111: *
112: * @throws Exception if an unexpected error occurs, test fails.
113: */
114: public void testParallelComponentAddition() throws Exception {
115: Thread[] threads = new Thread[NUM_THREADS];
116:
117: // -- Create the Threads
118: for (int i = 0; i < NUM_THREADS; i++) {
119: ComponentRunnable cr = new ComponentRunnable(mRegistry,
120: mTstComp + i, true);
121: threads[i] = new Thread(cr);
122: }
123:
124: // -- Start the Threads
125: for (int i = 0; i < NUM_THREADS; i++) {
126: threads[i].start();
127: }
128:
129: // -- Wait for the Threads
130: for (int i = 0; i < NUM_THREADS; i++) {
131: threads[i].join();
132:
133: }
134:
135: // -- Verify Result
136: java.util.List<String> comps = mRegistry.getComponentQuery(
137: "domain").getComponentIds(ComponentType.ALL);
138: assertTrue(comps.size() == NUM_THREADS);
139: java.util.List<String> instComps = mRegistry.getComponentQuery(
140: "another-server").getComponentIds(ComponentType.ALL);
141: assertTrue(instComps.size() == NUM_THREADS);
142: }
143: }
|