KickJava   Java API By Example, From Geeks To Geeks.

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

47 public final class TestUtilities {
48     
49     // do not instantiate
50
private TestUtilities() {}
51     
52 // /**
53
// * Tests whether a transformation makes an expected result.
54
// *
55
// * @param from the source text to be transformed.
56
// * @param result the expected text after transformation.
57
// * @param rules one or more rules that define the transformation to use.
58
// * @throws TransformationException if the transformed text doesn't match the result.
59
// */
60
// public static void assertTransform(String from, String result, String rules) throws TransformationException, Exception {
61
// File src = copyStringToFile(getClassName(from), from);
62
// File rulesFile = copyStringToFile("test.rules", rules);
63
// apply(tempDirectory, rulesFile.getPath(), true, true, false);
64
//
65
// String txt = copyFileToString(src);
66
// if (!txt.equals(result))
67
// throw new TransformationException("expected: \"" + result + "\" got: \"" + txt + "\"");
68
// }
69
//
70
// /**
71
// * Tests whether a transformation makes an expected result.
72
// *
73
// * @param from the source text to be transformed.
74
// * @param result the expected text after transformation.
75
// * @param clazz the transformation class to use.
76
// * @throws TransformationException if the transformed text doesn't match the result.
77
// */
78
// public static void assertTransform(String from, String result, Class clazz) throws TransformationException, Exception {
79
// File src = copyStringToFile(getClassName(from), from);
80
// String className = clazz.getName();
81
// apply(tempDirectory, className, false, true, false);
82
//
83
// String txt = copyFileToString(src);
84
// if (!txt.equals(result))
85
// throw new TransformationException("expected: \"" + result + "\" got: \"" + txt + "\"");
86
// }
87
//
88
// private static String getClassName(String src) {
89
// return null; // FIXME
90
// }
91
//
92
// /**
93
// * Applies a rule file to a directory of source files.
94
// *
95
// * @param dir the directory containing the source files to be modified.
96
// * @param rules the rule file to apply to the source files.
97
// * @return the number of matches found
98
// * @throws BuildErrorsException
99
// * If any errors are found when building the source files.
100
// */
101
// public static int applyRules(File dir, URL rules)
102
// throws BuildErrorsException, Exception {
103
// return applyRules(dir, rules, false);
104
//// }
105
//
106
// /**
107
// * Applies a rule file to a directory of source files.
108
// *
109
// * @param dir the directory containing the source files to be modified.
110
// * @param rules the rule file to apply to the source files.
111
// * @param allowErrors true if the rules should still be applied if there are build errors.
112
// * @return the number of matches found
113
// * @throws BuildErrorsException
114
// * If any errors are found when building the source files.
115
// */
116
// public static int applyRules(File dir, URL rules, boolean allowErrors)
117
// throws BuildErrorsException, Exception {
118
// File rulesFile = copyResourceToFile(rules);
119
// return apply(dir, rulesFile.getPath(), true, true, allowErrors).getResultCount();
120
// }
121
//
122
// /**
123
// * Applies a Query class to a directory of source files.
124
// *
125
// * @param dir the directory containing the source files to be modified.
126
// * @param query the rules to apply to the source files.
127
// * @return the matches found
128
// * @throws BuildErrorsException
129
// * If any errors are found when building the source files.
130
// * @throws ModificationException
131
// * If any changes were made to the source files after applying the
132
// * Query class.
133
// */
134
// public static ResultTableModel applyQuery(File dir, String queryName)
135
// throws BuildErrorsException, Exception {
136
// return apply(dir, queryName, false, true, false);
137
// }
138
//
139
// /**
140
// * Applies a Transformer class to a directory of source files.
141
// *
142
// * @param dir the directory containing the source files to be modified.
143
// * @param transformer the rules to apply to the source files.
144
// * @return the number of matches found
145
// * @throws BuildErrorsException
146
// * If any errors are found when building the source files.
147
// */
148
// public static int applyTransformer(File dir, String transformerName)
149
// throws BuildErrorsException, Exception {
150
// return apply(dir, transformerName, false, true, false).getResultCount();
151
// }
152
//
153
// private static ResultTableModel apply(File dir, String cmd, boolean isScript, boolean makesChanges, boolean allowErrors)
154
// throws BuildErrorsException, Exception {
155
// DefaultApplicationContext context = new DefaultApplicationContext();
156
// JackpotEngine eng = EngineFactory.createEngine(context);
157
//
158
// int errors = eng.initialize(dir.getPath(), System.getProperty("java.class.path"), "1.5");
159
// if (errors > 0 && !allowErrors)
160
// throw new BuildErrorsException(Integer.toString(errors) + " build errors"); // TODO: get log from engine
161
//
162
// ResultTableModel result = isScript ?
163
// eng.runScript("q", "t", cmd) : eng.runCommand("q", "t", cmd);
164
// if (makesChanges && !eng.commit())
165
// throw new AssertionError("commit canceled"); // shouldn't happen with new UI
166
//
167
// return result;
168
// }
169
//
170
/**
171      * Returns a string which contains the contents of a file.
172      *
173      * @param f the file to be read
174      * @return the contents of the file(s).
175      */

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

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

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