001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.vmd.api.model;
042:
043: import junit.framework.Test;
044: import junit.framework.TestCase;
045: import junit.framework.TestSuite;
046: import org.netbeans.modules.vmd.api.model.common.TypesSupport;
047:
048: import org.netbeans.modules.vmd.api.model.descriptors.FirstCD;
049: import org.netbeans.modules.vmd.api.model.utils.ModelTestUtil;
050:
051: import static org.netbeans.modules.vmd.api.model.utils.TestTypes.*;
052:
053: /**
054: *
055: * @author Karol Harezlak
056: */
057: public class DestructiveTest extends TestCase {
058:
059: private final DesignDocument document = ModelTestUtil
060: .createTestDesignDocument();
061: private final Thread read = new Thread(new Read());
062: private final Thread writeContinuously = new Thread(
063: new WriteContinuously());
064: private final Thread writeShortly = new Thread(
065: new WriteContinuously());
066:
067: public DestructiveTest(String testName) {
068: super (testName);
069: }
070:
071: protected void setUp() throws Exception {
072: }
073:
074: protected void tearDown() throws Exception {
075: }
076:
077: public static Test suite() {
078: TestSuite suite = new TestSuite(DestructiveTest.class);
079: //TestSuite suite = new TestSuite();
080: //suite.addTest(new DestructiveTest("testForceReadAccesWhileWriteing"));
081:
082: return suite;
083: }
084:
085: public void testLotComponents() {
086: System.out
087: .println("This test tries to create 1000 document components so please be patience"
088: + // NOI18N
089: " and check output for more results"); // NOI18N
090: System.out
091: .println("It shouldn't take longer then 10s to 1 min"); // NOI18N
092:
093: document.getTransactionManager().writeAccess(new Runnable() {
094: int i = 0;
095:
096: public void run() {
097: do {
098: document.createComponent(FirstCD.TYPEID_CLASS);
099: i++;
100: } while (i < 1000);
101: }
102: });
103: document.getTransactionManager().writeAccess(new Runnable() {
104: int i = 0;
105:
106: public void run() {
107: do {
108: document.getComponentByUID(i).writeProperty(
109: FirstCD.PROPERTY_TEST,
110: TypesSupport.createStringValue(String
111: .valueOf(i)));
112: i++;
113: } while (i < 1000);
114: }
115: });
116: }
117:
118: public void testForceReadAccesWhileWriteContinuously() {
119: System.out
120: .println("This test force to obtain read access while component is lock because of writing "); // NOI18N
121: writeContinuously.start();
122: try {
123: writeContinuously.join();
124: } catch (InterruptedException ex) {
125: ex.printStackTrace();
126: }
127: }
128:
129: public void testForceReadAccesWhileWriteShortly() {
130: System.out
131: .println("This test force to obtain read access while component is lock because of writing "); // NOI18N
132: writeShortly.start();
133: try {
134: writeShortly.join();
135: } catch (InterruptedException ex) {
136: ex.printStackTrace();
137: }
138: }
139:
140: private class Read implements Runnable {
141: public void run() {
142: int i = 0;
143: System.out
144: .println("Start trying obtain read access to the document"); // NOI18N
145: do {
146: i++;
147: System.out
148: .println("Trying obtaian read access to the document "
149: + i); // NOI18N
150: DestructiveTest.this .document.getTransactionManager()
151: .readAccess(new Runnable() {
152: public void run() {
153: if (DestructiveTest.this .document
154: .getTransactionManager()
155: .isWriteAccess()) {
156: System.out
157: .println("!!!Read access obtained while isWriteAccess() == TRUE!!!"); // NOI18N
158: fail(); // If test fail in this line it means that it was able obtain read access while writing lock was active!
159: } else {
160: System.out
161: .println("Read access obtain when document isWriteAccess()= FALSE"); // NOI18N
162: }
163: }
164: });
165: } while (writeContinuously.isAlive()
166: || writeShortly.isAlive());
167: System.out
168: .println("Stop trying obtaian read access to the document"); // NOI18N
169: }
170: }
171:
172: private class WriteContinuously implements Runnable {
173: public void run() {
174: read.start();
175: DestructiveTest.this .document.getTransactionManager()
176: .writeAccess(new Runnable() {
177: int i = 0;
178:
179: public void run() {
180: System.out
181: .println("Start writing to the document"); // NOI18N
182: do {
183: i++;
184: DestructiveTest.this .document
185: .createComponent(FirstCD.TYPEID_CLASS);
186: } while (i < 1000);
187: System.out
188: .println("Stop writing to the document"); // NOI18N
189: }
190: });
191: }
192: }
193:
194: private class WriteShort implements Runnable {
195: public void run() {
196: int i = 0;
197: read.start();
198: do {
199: DestructiveTest.this .document.getTransactionManager()
200: .writeAccess(new Runnable() {
201: public void run() {
202: System.out
203: .println("Start writing to the document"); // NOI18N
204: DestructiveTest.this .document
205: .createComponent(FirstCD.TYPEID_CLASS);
206: System.out
207: .println("Stop writing to the document"); // NOI18N
208: }
209: });
210: i++;
211: } while (i < 1000);
212: }
213: }
214: }
|