KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > semantic > TestBase


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.java.editor.semantic;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.TreeSet JavaDoc;
35 import java.util.concurrent.CountDownLatch JavaDoc;
36 import java.util.regex.Pattern JavaDoc;
37 import javax.swing.event.ChangeListener JavaDoc;
38 import javax.swing.text.Document JavaDoc;
39 import junit.framework.Test;
40 import junit.framework.TestSuite;
41 import org.netbeans.api.java.classpath.ClassPath;
42 import org.netbeans.api.java.lexer.JavaTokenId;
43 import org.netbeans.api.java.queries.SourceForBinaryQuery;
44 import org.netbeans.api.java.source.CancellableTask;
45 import org.netbeans.api.java.source.CompilationController;
46 import org.netbeans.api.java.source.JavaSource;
47 import org.netbeans.api.java.source.JavaSource.Phase;
48 import org.netbeans.api.java.source.SourceUtilsTestUtil;
49 import org.netbeans.api.lexer.Language;
50 import org.netbeans.junit.NbTestCase;
51 import org.netbeans.modules.editor.highlights.HighlightComparator;
52 import org.netbeans.modules.editor.highlights.spi.Highlight;
53 import org.netbeans.modules.java.JavaDataLoader;
54 import org.netbeans.spi.java.classpath.ClassPathProvider;
55 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
56 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
57 import org.openide.cookies.EditorCookie;
58 import org.openide.filesystems.FileObject;
59 import org.openide.filesystems.FileUtil;
60 import org.openide.loaders.DataObject;
61
62 /**
63  *
64  * @author Jan Lahoda
65  */

66 public abstract class TestBase extends NbTestCase {
67     
68     private static final boolean SHOW_GUI_DIFF = false;
69     
70     /**
71      * Creates a new instance of TestBase
72      */

73     public TestBase(String JavaDoc name) {
74         super(name);
75     }
76     
77     private FileObject testSourceFO;
78     private URL JavaDoc testBuildDir;
79     
80     protected final void copyToWorkDir(File JavaDoc resource, File JavaDoc toFile) throws IOException JavaDoc {
81         //TODO: finally:
82
InputStream JavaDoc is = new FileInputStream JavaDoc(resource);
83         OutputStream JavaDoc outs = new FileOutputStream JavaDoc(toFile);
84         
85         int read;
86         
87         while ((read = is.read()) != (-1)) {
88             outs.write(read);
89         }
90         
91         outs.close();
92         
93         is.close();
94     }
95     
96     protected void performTest(String JavaDoc fileName, final Performer performer) throws Exception JavaDoc {
97         performTest(fileName, performer, false);
98     }
99     
100     protected void performTest(String JavaDoc fileName, final Performer performer, boolean doCompileRecursively) throws Exception JavaDoc {
101         SourceUtilsTestUtil.prepareTest(new String JavaDoc[] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object JavaDoc[0]);
102         
103     FileObject scratch = SourceUtilsTestUtil.makeScratchDir(this);
104     FileObject cache = scratch.createFolder("cache");
105     
106         File JavaDoc wd = getWorkDir();
107         File JavaDoc testSource = new File JavaDoc(wd, "test/" + fileName + ".java");
108         
109         testSource.getParentFile().mkdirs();
110         
111         File JavaDoc dataFolder = new File JavaDoc(getDataDir(), "org/netbeans/modules/java/editor/semantic/data/");
112         
113         for (File JavaDoc f : dataFolder.listFiles()) {
114             copyToWorkDir(f, new File JavaDoc(wd, "test/" + f.getName()));
115         }
116         
117         testSourceFO = FileUtil.toFileObject(testSource);
118
119         assertNotNull(testSourceFO);
120         
121         File JavaDoc testBuildTo = new File JavaDoc(wd, "test-build");
122         
123         testBuildTo.mkdirs();
124
125         SourceUtilsTestUtil.prepareTest(FileUtil.toFileObject(dataFolder), FileUtil.toFileObject(testBuildTo), cache);
126         
127         if (doCompileRecursively) {
128             SourceUtilsTestUtil.compileRecursively(FileUtil.toFileObject(dataFolder));
129         }
130
131         final Document JavaDoc doc = getDocument(testSourceFO);
132         final Set JavaDoc<Highlight> highlights = new TreeSet JavaDoc<Highlight>(new HighlightComparator());
133         
134         JavaSource source = JavaSource.forFileObject(testSourceFO);
135         
136         assertNotNull(source);
137         
138     final CountDownLatch JavaDoc l = new CountDownLatch JavaDoc(1);
139     
140         source.runUserActionTask(new CancellableTask<CompilationController>() {
141             
142             public void cancel() {
143             }
144             
145             public void run(CompilationController parameter) {
146                 try {
147                     parameter.toPhase(Phase.UP_TO_DATE);
148                     highlights.addAll(performer.compute(parameter, doc));
149                 } catch (IOException JavaDoc e) {
150                     e.printStackTrace();
151                 } finally {
152             l.countDown();
153         }
154             }
155             
156         }, true);
157     
158         l.await();
159                 
160         File JavaDoc output = new File JavaDoc(getWorkDir(), getName() + ".out");
161         Writer JavaDoc out = new FileWriter JavaDoc(output);
162         
163         for (Highlight h : highlights) {
164             if (h instanceof HighlightImpl) {
165                 out.write(((HighlightImpl) h).getHighlightTestData());
166             } else {
167                 out.write("Unsupported highlight type: " + h.getClass());
168             }
169             
170             out.write("\n");
171         }
172         
173         out.close();
174                 
175         boolean wasException = true;
176         
177         try {
178             File JavaDoc goldenFile = getGoldenFile();
179             File JavaDoc diffFile = new File JavaDoc(getWorkDir(), getName() + ".diff");
180             
181             assertFile(output, goldenFile, diffFile);
182             wasException = false;
183         } finally {
184             if (wasException && SHOW_GUI_DIFF) {
185                 try {
186                     String JavaDoc name = getClass().getName();
187                     
188                     name = name.substring(name.lastIndexOf('.') + 1);
189                     
190                     ShowGoldenFiles.run(name, getName(), fileName);
191                     
192                 } catch (Exception JavaDoc e) {
193                     e.printStackTrace();
194                 }
195             }
196         }
197     }
198     
199     public static interface Performer {
200         
201         public Collection JavaDoc<Highlight> compute(CompilationController parameter, Document JavaDoc doc);
202         
203     }
204     
205     protected final Document JavaDoc getDocument(FileObject file) throws IOException JavaDoc {
206         DataObject od = DataObject.find(file);
207         EditorCookie ec = (EditorCookie) od.getCookie(EditorCookie.class);
208         
209         if (ec != null) {
210             Document JavaDoc doc = ec.openDocument();
211             
212             doc.putProperty(Language.class, JavaTokenId.language());
213             
214             return doc;
215         } else {
216             return null;
217         }
218     }
219
220 }
221
Popular Tags