KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > highlights > HighlighterImplTest


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.modules.editor.highlights;
20
21 import java.beans.PropertyVetoException JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.ref.Reference JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import javax.swing.JEditorPane JavaDoc;
27 import javax.swing.text.Document JavaDoc;
28 import javax.swing.text.JTextComponent JavaDoc;
29 import javax.swing.text.PlainDocument JavaDoc;
30 import org.netbeans.editor.BaseTextUI;
31 import org.netbeans.junit.NbTestCase;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.filesystems.LocalFileSystem;
35 import org.openide.filesystems.Repository;
36 import org.openide.loaders.DataObject;
37
38 /**
39  *
40  * @author Jan Lahoda
41  */

42 public class HighlighterImplTest extends NbTestCase {
43     
44     public HighlighterImplTest(String JavaDoc testName) {
45         super(testName);
46     }
47
48     protected void setUp() throws Exception JavaDoc {
49     }
50
51     public void testIsReclaimable() throws Exception JavaDoc {
52         FileObject dir = makeScratchDir(this);
53         FileObject file = dir.createData("test.txt");
54         JTextComponent JavaDoc c = new JEditorPane JavaDoc();
55         
56         HighlighterImpl.getDefault().assureRegistered(c);
57         
58         Document JavaDoc d = new PlainDocument JavaDoc();
59         
60         d.putProperty(Document.StreamDescriptionProperty, DataObject.find(file));
61         c.setDocument(d);
62         
63         Reference JavaDoc fileR = new WeakReference JavaDoc(file);
64         Reference JavaDoc cR = new WeakReference JavaDoc(c);
65         Reference JavaDoc docR = new WeakReference JavaDoc(d);
66         
67         dir = null;
68         file = null;
69         c = null;
70         d = null;
71         
72         assertGC("", cR);
73         assertGC("", docR);
74         
75         //it is necessary to touch the HighlighterImpl.comp2FO map to actually free the FileObject:
76
HighlighterImpl.getDefault().assureRegistered(new JEditorPane JavaDoc());
77         
78         assertGC("", fileR);
79     }
80     
81     public boolean runInEQ() {
82         return true;
83     }
84     /**Copied from org.netbeans.api.project.
85      * Create a scratch directory for tests.
86      * Will be in /tmp or whatever, and will be empty.
87      * If you just need a java.io.File use clearWorkDir + getWorkDir.
88      */

89     public static FileObject makeScratchDir(NbTestCase test) throws IOException JavaDoc {
90         test.clearWorkDir();
91         File JavaDoc root = test.getWorkDir();
92         assert root.isDirectory() && root.list().length == 0;
93         FileObject fo = FileUtil.toFileObject(root);
94         if (fo != null) {
95             // Presumably using masterfs.
96
return fo;
97         } else {
98             // For the benefit of those not using masterfs.
99
LocalFileSystem lfs = new LocalFileSystem();
100             try {
101                 lfs.setRootDirectory(root);
102             } catch (PropertyVetoException JavaDoc e) {
103                 assert false : e;
104             }
105             Repository.getDefault().addFileSystem(lfs);
106             return lfs.getRoot();
107         }
108     }
109     
110 }
111
Popular Tags