KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > URLMapper50852Test


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
20 package org.openide.filesystems;
21
22
23 import org.netbeans.junit.*;
24 import org.openide.util.Lookup;
25
26 import java.net.URL JavaDoc;
27 import java.io.File JavaDoc;
28
29 /**
30  * Simulates issue 50852.
31  *
32  * @author Radek Matous
33  */

34 public class URLMapper50852Test extends NbTestCase {
35     private static URL JavaDoc testURL = null;
36     private static final MyThread resultsComputingThread = new MyThread();
37     private static final MyThread secondThread = new MyThread();
38     static MyURLMapper MAPPER_INSTANCE = null;
39
40
41     public URLMapper50852Test(String JavaDoc name) {
42         super(name);
43     }
44
45     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
46         junit.textui.TestRunner.run(new NbTestSuite(URLMapper50852Test.class));
47     }
48
49     /**
50      * Setups variables.
51      */

52     protected void setUp() throws Exception JavaDoc {
53         File JavaDoc workdir = getWorkDir();
54         testURL = workdir.toURI().toURL();
55         System.setProperty("org.openide.util.Lookup", "org.openide.filesystems.URLMapper50852Test$Lkp");
56         MAPPER_INSTANCE = new MyURLMapper ();
57         Lkp lkp = (Lkp)org.openide.util.Lookup.getDefault();
58         lkp.getInstanceContent().add(MAPPER_INSTANCE);
59     }
60
61     public void testURLMapper50852 () throws Exception JavaDoc {
62         resultsComputingThread.start();
63         Thread.sleep(1000);
64         secondThread.start();
65         Thread.sleep(1000);
66         
67         for (int i = 0; i < 5; i++) {
68             if (!resultsComputingThread.isFinished() && secondThread.isFinished() ) {
69                 break;
70             }
71             Thread.sleep(1000);
72         }
73         assertFalse (resultsComputingThread.isFinished());
74         assertTrue ("Even if a thread is blocked in the computation, another one can proceed", secondThread.isFinished());
75         assertTrue ("and successfully call into the mapper", MAPPER_INSTANCE.called);
76         synchronized (testURL) {
77             testURL.notifyAll();
78         }
79         
80     }
81
82
83     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
84         private org.openide.util.lookup.InstanceContent ic;
85         public Lkp() {
86             this(new org.openide.util.lookup.InstanceContent());
87         }
88
89         private Lkp(org.openide.util.lookup.InstanceContent ic) {
90             super(ic);
91             this.ic = ic;
92         }
93
94         org.openide.util.lookup.InstanceContent getInstanceContent () {
95             return ic;
96         }
97         
98         protected void beforeLookup(Template template) {
99             super.beforeLookup(template);
100
101             synchronized (testURL) {
102                 if (Thread.currentThread() == resultsComputingThread) {
103                     try {
104                         testURL.wait();
105                     } catch (InterruptedException JavaDoc e) {
106                         fail ();
107                     }
108                 }
109             }
110         }
111
112     } // end of Lkp
113

114     public static final class MyURLMapper extends URLMapper {
115         private boolean called = false;
116         
117         
118         public URL JavaDoc getURL(FileObject fo, int type) {
119             called = true;
120             return null;
121         }
122
123         public FileObject[] getFileObjects(URL JavaDoc url) {
124             called = true;
125             return new FileObject[0];
126         }
127
128         public String JavaDoc findMIMEType(FileObject fo) {
129             called = true;
130             return null;
131         }
132
133         boolean isCalled() {
134             return called;
135         }
136     }
137
138     private static class MyThread extends Thread JavaDoc {
139         private boolean finished = false;
140         
141         public void run() {
142             super.run();
143             URLMapper.findFileObject(testURL);
144             finished = true;
145         }
146
147         boolean isFinished() {
148             return finished;
149         }
150     }
151 }
152   
153   
154   
Popular Tags