KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > admin > StandardAdminHandlerTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.lib.cvsclient.admin;
20
21 import junit.framework.*;
22 import java.io.*;
23 import java.util.*;
24 import org.netbeans.lib.cvsclient.TestKit;
25 import org.netbeans.lib.cvsclient.command.*;
26 import org.netbeans.lib.cvsclient.file.FileUtils;
27
28 /**
29  *
30  * @author Petr Kuzel
31  */

32 public class StandardAdminHandlerTest extends TestCase {
33
34     public StandardAdminHandlerTest(String JavaDoc testName) {
35         super(testName);
36     }
37
38     protected void setUp() throws Exception JavaDoc {
39     }
40
41     protected void tearDown() throws Exception JavaDoc {
42     }
43
44     public static Test suite() {
45         TestSuite suite = new TestSuite(StandardAdminHandlerTest.class);
46         
47         return suite;
48     }
49
50     int test70625ThreadDone;
51     
52     /**
53      * Simulates 70625 issue.
54      * It can randomly pass for incorrect code but never fails for correct code.
55      */

56     public void test70625() throws Exception JavaDoc {
57         try {
58             System.err.println("test70625()");
59
60             File tmpDir = TestKit.createTmpFolder("test_StandardAdminHandlerTest");
61
62             final File file = new File(tmpDir, "test70625");
63             final Entry entry = new Entry();
64             entry.setName("test70625");
65
66             final StandardAdminHandler instance = new StandardAdminHandler();
67
68             // create CVS structure that simulates rename in progress
69
new File(tmpDir, "CVS").mkdirs();
70             new File(tmpDir, "CVS/Entries.Backup").createNewFile();
71
72             SyncTwo lock = new SyncTwo();
73             instance.t9yBeforeRenameSync(lock);
74
75             final Runnable JavaDoc r = new Runnable JavaDoc() {
76                 public void run() {
77                     try {
78                         instance.setEntry(file, entry);
79                     } catch (IOException ex) {
80                         System.err.println("Ignoring " + ex.getMessage() + " ...");
81                     } finally {
82                         test70625ThreadDone ++;
83                         synchronized(StandardAdminHandlerTest.this) {
84                             StandardAdminHandlerTest.this.notifyAll();
85                         }
86                     }
87                 }
88             };
89
90             final Runnable JavaDoc r2 = new Runnable JavaDoc() {
91                 public void run() {
92                     try {
93                         instance.getEntry(file);
94                     } catch (IOException ex) {
95                         System.err.println("Ignoring " + ex.getMessage() + " ...");
96                     } finally {
97                         test70625ThreadDone ++;
98                         synchronized(StandardAdminHandlerTest.this) {
99                             StandardAdminHandlerTest.this.notifyAll();
100                         }
101                     }
102                 }
103             };
104
105             new Thread JavaDoc(r, "Update").start();
106             new Thread JavaDoc(r2, "Annotator").start();
107             
108             synchronized(this) {
109                 while (test70625ThreadDone < 2) {
110                     wait();
111                 }
112             }
113
114             if (new File(tmpDir, "CVS/Entries").exists() == false) {
115                 fail("CVS/Entries gone.");
116             }
117         } catch (Exception JavaDoc ex) {
118             ex.printStackTrace();
119             throw ex;
120         }
121     }
122
123     private class SyncTwo implements Runnable JavaDoc {
124         
125         int counter;
126         
127         public synchronized void run() {
128             counter++;
129             Thread.dumpStack();
130             while (counter < 2) {
131                 try {
132                     System.err.println(Thread.currentThread().getName() + " is waiting for sibling...");
133                     wait(100); // correct code will always wait
134
return;
135                 } catch (InterruptedException JavaDoc ex) {
136                     ex.printStackTrace();
137                 }
138             }
139             notifyAll();
140         }
141     }
142     
143 }
144
Popular Tags