KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jackpot > test > TestUtilities


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.netbeans.jackpot.test;
21
22 import org.netbeans.modules.java.source.engine.BuildErrorsException;
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.EOFException JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.OutputStreamWriter JavaDoc;
32 import java.io.Writer JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.util.zip.GZIPInputStream JavaDoc;
35 import org.netbeans.api.java.source.query.ResultTableModel;
36 import org.netbeans.api.java.source.transform.Transformer;
37 import org.netbeans.modules.java.source.engine.DefaultApplicationContext;
38 import org.netbeans.modules.java.source.engine.EngineFactory;
39 import org.netbeans.modules.java.source.engine.JackpotEngine;
40
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.filesystems.Repository;
44
45 /**
46  * Utilities to aid unit testing Jackpot rule files, Query and Transformer classes.
47  *
48  * @author Jaroslav Tulach
49  * @author Tom Ball
50  */

51 public final class TestUtilities {
52     
53     // do not instantiate
54
private TestUtilities() {}
55     
56     /**
57      * Tests whether a transformation makes an expected result.
58      *
59      * @param from the source text to be transformed.
60      * @param result the expected text after transformation.
61      * @param rules one or more rules that define the transformation to use.
62      * @throws TransformationException if the transformed text doesn't match the result.
63      */

64      public static void assertTransform(String JavaDoc from, String JavaDoc result, String JavaDoc rules) throws TransformationException, Exception JavaDoc {
65         File JavaDoc src = copyStringToFile(getClassName(from), from);
66         File JavaDoc rulesFile = copyStringToFile("test.rules", rules);
67         apply(tempDirectory, rulesFile.getPath(), true, true, false);
68
69         String JavaDoc txt = copyFileToString(src);
70         if (!txt.equals(result))
71             throw new TransformationException("expected: \"" + result + "\" got: \"" + txt + "\"");
72     }
73     
74     /**
75      * Tests whether a transformation makes an expected result.
76      *
77      * @param from the source text to be transformed.
78      * @param result the expected text after transformation.
79      * @param clazz the transformation class to use.
80      * @throws TransformationException if the transformed text doesn't match the result.
81      */

82      public static void assertTransform(String JavaDoc from, String JavaDoc result, Class JavaDoc clazz) throws TransformationException, Exception JavaDoc {
83         File JavaDoc src = copyStringToFile(getClassName(from), from);
84         String JavaDoc className = clazz.getName();
85         apply(tempDirectory, className, false, true, false);
86
87         String JavaDoc txt = copyFileToString(src);
88         if (!txt.equals(result))
89             throw new TransformationException("expected: \"" + result + "\" got: \"" + txt + "\"");
90     }
91      
92      private static String JavaDoc getClassName(String JavaDoc src) {
93          return null; // FIXME
94
}
95
96     /**
97      * Applies a rule file to a directory of source files.
98      *
99      * @param dir the directory containing the source files to be modified.
100      * @param rules the rule file to apply to the source files.
101      * @return the number of matches found
102      * @throws BuildErrorsException
103      * If any errors are found when building the source files.
104      */

105     public static int applyRules(File JavaDoc dir, URL JavaDoc rules)
106             throws BuildErrorsException, Exception JavaDoc {
107         return applyRules(dir, rules, false);
108     }
109
110     /**
111      * Applies a rule file to a directory of source files.
112      *
113      * @param dir the directory containing the source files to be modified.
114      * @param rules the rule file to apply to the source files.
115      * @param allowErrors true if the rules should still be applied if there are build errors.
116      * @return the number of matches found
117      * @throws BuildErrorsException
118      * If any errors are found when building the source files.
119      */

120     public static int applyRules(File JavaDoc dir, URL JavaDoc rules, boolean allowErrors)
121             throws BuildErrorsException, Exception JavaDoc {
122         File JavaDoc rulesFile = copyResourceToFile(rules);
123         return apply(dir, rulesFile.getPath(), true, true, allowErrors).getResultCount();
124     }
125
126     /**
127      * Applies a Query class to a directory of source files.
128      *
129      * @param dir the directory containing the source files to be modified.
130      * @param query the rules to apply to the source files.
131      * @return the matches found
132      * @throws BuildErrorsException
133      * If any errors are found when building the source files.
134      * @throws ModificationException
135      * If any changes were made to the source files after applying the
136      * Query class.
137      */

138     public static ResultTableModel applyQuery(File JavaDoc dir, String JavaDoc queryName)
139             throws BuildErrorsException, Exception JavaDoc {
140         return apply(dir, queryName, false, true, false);
141     }
142
143     /**
144      * Applies a Transformer class to a directory of source files.
145      *
146      * @param dir the directory containing the source files to be modified.
147      * @param transformer the rules to apply to the source files.
148      * @return the number of matches found
149      * @throws BuildErrorsException
150      * If any errors are found when building the source files.
151      */

152     public static int applyTransformer(File JavaDoc dir, String JavaDoc transformerName)
153             throws BuildErrorsException, Exception JavaDoc {
154         return apply(dir, transformerName, false, true, false).getResultCount();
155     }
156
157     private static ResultTableModel apply(File JavaDoc dir, String JavaDoc cmd, boolean isScript, boolean makesChanges, boolean allowErrors)
158             throws BuildErrorsException, Exception JavaDoc {
159         DefaultApplicationContext context = new DefaultApplicationContext();
160         JackpotEngine eng = EngineFactory.createEngine(context);
161
162         int errors = eng.initialize(dir.getPath(), System.getProperty("java.class.path"), "1.5");
163         if (errors > 0 && !allowErrors)
164             throw new BuildErrorsException(Integer.toString(errors) + " build errors"); // TODO: get log from engine
165

166         ResultTableModel result = isScript ?
167             eng.runScript("q", "t", cmd) : eng.runCommand("q", "t", cmd);
168         if (makesChanges && !eng.commit())
169             throw new AssertionError JavaDoc("commit canceled"); // shouldn't happen with new UI
170

171         return result;
172     }
173     
174     /**
175      * Returns a string which contains the contents of a file.
176      *
177      * @param f the file to be read
178      * @return the contents of the file(s).
179      */

180     public final static String JavaDoc copyFileToString (java.io.File JavaDoc f) throws java.io.IOException JavaDoc {
181         int s = (int)f.length ();
182         byte[] data = new byte[s];
183         int len = new FileInputStream JavaDoc (f).read (data);
184         if (len != s)
185             throw new EOFException JavaDoc("truncated file");
186         return new String JavaDoc (data);
187     }
188     
189     /**
190      * Returns a string which contains the contents of a GZIP compressed file.
191      *
192      * @param f the file to be read
193      * @return the contents of the file(s).
194      */

195     public final static String JavaDoc copyGZipFileToString (java.io.File JavaDoc f) throws java.io.IOException JavaDoc {
196         GZIPInputStream JavaDoc is = new GZIPInputStream JavaDoc(new FileInputStream JavaDoc(f));
197         byte[] arr = new byte[256 * 256];
198         int first = 0;
199         for(;;) {
200             int len = is.read(arr, first, arr.length - first);
201             if (first + len < arr.length) {
202                 return new String JavaDoc(arr, 0, first + len);
203             }
204         }
205     }
206     
207     /**
208      * Copies a string to a specified file.
209      *
210      * @param f the file to use.
211      * @param content the contents of the returned file.
212      * @return the created file
213      */

214     public final static File JavaDoc copyStringToFile (File JavaDoc f, String JavaDoc content) throws Exception JavaDoc {
215         FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
216         InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(content.getBytes("UTF-8"));
217         FileUtil.copy(is, os);
218         os.close ();
219             
220         return f;
221     }
222     
223     private final static File JavaDoc copyStringToFile(String JavaDoc filename, String JavaDoc res) throws Exception JavaDoc {
224         File JavaDoc f = new File JavaDoc(tempDirectory, filename);
225         f.deleteOnExit ();
226         return copyStringToFile(f, res);
227     }
228
229     private final static File JavaDoc copyResourceToFile(URL JavaDoc u) throws Exception JavaDoc {
230         File JavaDoc f = File.createTempFile("res", ".xml");
231         f.deleteOnExit ();
232        
233         FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
234         InputStream JavaDoc is = u.openStream();
235         FileUtil.copy(is, os);
236         os.close ();
237             
238         return f;
239     }
240
241     private static File JavaDoc tempDirectory;
242     {
243         try {
244             File JavaDoc f = File.createTempFile("foo", "bar");
245             tempDirectory = f.getParentFile();
246         } catch (IOException JavaDoc e) {
247             tempDirectory = new File JavaDoc("/tmp");
248         }
249     }
250 }
Popular Tags