KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > jackpotrules > ErrorManagerTest


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.modules.apisupport.jackpotrules;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.regex.Matcher JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28
29 import org.netbeans.junit.NbTestCase;
30
31 /**
32  *
33  * @author Jaroslav Tulach
34  */

35 public final class ErrorManagerTest extends NbTestCase {
36
37     private URL JavaDoc script;
38
39
40     public ErrorManagerTest(String JavaDoc s) {
41         super(s);
42     }
43
44     protected Level JavaDoc logLevel() {
45         return Level.INFO;
46     }
47
48     protected void setUp() throws Exception JavaDoc {
49         clearWorkDir();
50         script = ErrorManagerTest.class.getResource("/org/netbeans/modules/apisupport/jackpotrules/annotate-to-initcause.rules");
51         assertNotNull(script);
52     }
53
54     public void testRenameNotifyToPst() throws Exception JavaDoc {
55         File JavaDoc f = JackpotUtils.extractString(new File JavaDoc(getWorkDir(), "A.java"),
56             "package test;\n" +
57             "class A {\n" +
58             " public static void main(String[] args) {\n" +
59             " Exception e = new Exception();\n" +
60             " org.openide.ErrorManager.getDefault().notify(e);\n" +
61             " }\n" +
62             "}\n"
63         );
64
65         JackpotUtils.apply(getWorkDir(), script);
66
67         String JavaDoc txt = JackpotUtils.readFile(f, false);
68
69         if (txt.indexOf("ErrorManager") >= 0) {
70             fail("No ErrorManager shall be there:\n" + txt);
71         }
72     }
73
74     public void testErrMgrAnnoMsg() throws Exception JavaDoc {
75         File JavaDoc f = JackpotUtils.extractString(new File JavaDoc(getWorkDir(), "A.java"),
76             "package test;\n" +
77             "import javax.swing.JButton;" +
78             "class A {\n" +
79             " public static void main(String[] args) {\n" +
80             " NullPointerException npe = new NullPointerException();\n" +
81             " org.openide.ErrorManager.getDefault().annotate(" +
82             " npe, \"localizedText\");" +
83             " }\n" +
84             "}\n"
85         );
86
87         JackpotUtils.apply(getWorkDir(), script);
88
89         String JavaDoc txt = JackpotUtils.readFile(f, false);
90
91         if (txt.indexOf("ErrorManager") >= 0) {
92             fail("No ErrorManager shall be there:\n" + txt);
93         }
94
95         if (txt.indexOf("attachLocalizedMessage") < 0) {
96             fail("There should be a localized message usage:\n" + txt);
97         }
98     }
99     public void testErrMgrUnknown() throws Exception JavaDoc {
100         File JavaDoc f = JackpotUtils.extractString(new File JavaDoc(getWorkDir(), "A.java"),
101             "package test;\n" +
102             "import javax.swing.JButton;" +
103             "class A {\n" +
104             " public static void main(String[] args) {\n" +
105             " NullPointerException npe = new NullPointerException();\n" +
106             " org.openide.ErrorManager.getDefault().annotate(" +
107             " npe, org.openide.ErrorManager.UNKNOWN," +
108             " \"msg\", null, null, null);" +
109             " }\n" +
110             "}\n"
111         );
112
113         JackpotUtils.apply(getWorkDir(), script);
114
115         String JavaDoc txt = JackpotUtils.readFile(f, false);
116
117         if (txt.indexOf("ErrorManager") >= 0) {
118             fail("No ErrorManager shall be there:\n" + txt);
119         }
120     }
121
122     public void testErrMgrException() throws Exception JavaDoc {
123         File JavaDoc f = JackpotUtils.extractString(new File JavaDoc(getWorkDir(), "A.java"),
124             "package test;\n" +
125             "import javax.swing.JButton;" +
126             "class A {\n" +
127             " public static void main(String[] args) {\n" +
128             " JButton r = new JButton();\n" +
129             " String icon = null;\n" +
130             " NullPointerException npe = new NullPointerException();\n" +
131             " org.openide.ErrorManager.getDefault().annotate(" +
132             " npe, org.openide.ErrorManager.EXCEPTION," +
133             " \"Probably an ImageIcon with a null source image: \" + icon + \" - \" + //NOI18N\n" +
134             " r.getText(), null, null, null\n" +
135             "); //NOI18N\n" +
136             " }\n" +
137             "}\n"
138         );
139
140         JackpotUtils.apply(getWorkDir(), script);
141
142         String JavaDoc txt = JackpotUtils.readFile(f, false);
143
144         if (txt.indexOf("ErrorManager") >= 0) {
145             fail("No ErrorManager shall be there:\n" + txt);
146         }
147     }
148     public void testConvertToWarning() throws Exception JavaDoc {
149         File JavaDoc f = JackpotUtils.extractString(new File JavaDoc(getWorkDir(), "A.java"),
150             "package test;\n" +
151             "class A {\n" +
152             " public static void main(String[] args) {\n" +
153             " NullPointerException e = new NullPointerException();\n" +
154             " org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.EXCEPTION, e);" +
155             " }\n" +
156             "}\n"
157         );
158
159         JackpotUtils.apply(getWorkDir(), script);
160
161         String JavaDoc txt = JackpotUtils.readFile(f, false);
162
163         if (txt.indexOf("ErrorManager") >= 0) {
164             fail("No ErrorManager shall be there:\n" + txt);
165         }
166         if (!txt.replace('\n', ' ').matches(".*Logger.getLogger *\\( *\"global *\"\\).log *\\(Level.WARNING, *null, *e *\\).*")) {
167             fail("Logger.global shall be there:\n" + txt);
168         }
169     }
170
171     public void testConvertToLogging() throws Exception JavaDoc {
172         File JavaDoc f = JackpotUtils.extractString(new File JavaDoc(getWorkDir(), "A.java"),
173             "package test;\n" +
174             "class A {\n" +
175             " public static void main(String[] args) {\n" +
176             " NullPointerException e = new NullPointerException();\n" +
177             " org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);" +
178             " }\n" +
179             "}\n"
180         );
181
182         JackpotUtils.apply(getWorkDir(), script);
183
184         String JavaDoc txt = JackpotUtils.readFile(f, false);
185
186         if (txt.indexOf("ErrorManager") >= 0) {
187             fail("No ErrorManager shall be there:\n" + txt);
188         }
189         if (!txt.replace('\n', ' ').matches(".*Logger.getLogger *\\( *\"global\" *\\).log *\\(Level.INFO, *null, *e *\\).*")) {
190             fail("Logger.global shall be there:\n" + txt);
191         }
192     }
193
194     public void testGetRidOfCopyAnnotation() throws Exception JavaDoc {
195         File JavaDoc f = JackpotUtils.extractString(new File JavaDoc(getWorkDir(), "A.java"),
196             "package test;\n" +
197             "class A {\n" +
198             " public static void main(String[] args) {\n" +
199             " NullPointerException npe = new NullPointerException();\n" +
200             " java.io.IOException io = null;\n" +
201             " org.openide.ErrorManager.getDefault().copyAnnotation(npe, io);" +
202             " }\n" +
203             "}\n"
204         );
205
206         JackpotUtils.apply(getWorkDir(), script);
207
208         String JavaDoc txt = JackpotUtils.readFile(f, false);
209
210         if (txt.indexOf("ErrorManager") >= 0) {
211             fail("No ErrorManager shall be there:\n" + txt);
212         }
213
214         if (!txt.replace("\n", " ").matches(".*npe.initCause *\\( *io *\\).*")) {
215             fail("initCause shall be used:\n" + txt);
216         }
217     }
218
219     public void testNonLocalizedAnnotation() throws Exception JavaDoc {
220         File JavaDoc f = JackpotUtils.extractString(new File JavaDoc(getWorkDir(), "A.java"),
221             "package test;\n" +
222             "class A {\n" +
223             " public static void main(String[] args) {\n" +
224             " NullPointerException npe = new NullPointerException();\n" +
225             " String msg = \"nonlocmsg\";\n" +
226             " org.openide.ErrorManager.getDefault().annotate(npe, org.openide.ErrorManager.UNKNOWN, null, msg, null, null);" +
227             " }\n" +
228             "}\n"
229         );
230
231         JackpotUtils.apply(getWorkDir(), script);
232
233         String JavaDoc txt = JackpotUtils.readFile(f, false);
234
235         if (txt.indexOf("ErrorManager") >= 0) {
236             fail("No ErrorManager shall be there:\n" + txt);
237         }
238
239         if (!txt.replace("\n", " ").matches(".*Exceptions.attachLocalizedMessage.*")) {
240             fail("initCause shall be used:\n" + txt);
241         }
242     }
243
244
245     public void testFriendContractWithOwnLevel() throws Exception JavaDoc {
246         File JavaDoc f = JackpotUtils.extractString(new File JavaDoc(getWorkDir(), "A.java"),
247             "package test;\n" +
248             "class A {\n" +
249             " public static void main(String[] args) {\n" +
250             " NullPointerException npe = new NullPointerException();\n" +
251             " String msg = \"nonlocmsg\";\n" +
252             " org.openide.ErrorManager.getDefault().annotate(npe, " +
253             " org.openide.ErrorManager.USER, msg == null ? \"\" : npe.getMessage()," +
254             " msg, npe, new java.util.Date()); //NOI18N\n" +
255             " }\n" +
256             "}\n"
257         );
258
259         JackpotUtils.apply(getWorkDir(), script);
260
261         String JavaDoc txt = JackpotUtils.readFile(f, false);
262
263         if (txt.indexOf("ErrorManager") >= 0) {
264             fail("No ErrorManager shall be there:\n" + txt);
265         }
266
267         if (!txt.replace("\n", " ").matches(".*UIException.annotateUser *\\( *npe.*")) {
268             fail("UIException shall be used:\n" + txt);
269         }
270     }
271
272     public void testWarningGoesToLogging() throws Exception JavaDoc {
273         File JavaDoc f = JackpotUtils.extractString(new File JavaDoc(getWorkDir(), "A.java"),
274             "package test;\n" +
275             "class A {\n" +
276             " public static void main(String[] args) {\n" +
277             " NullPointerException npe = new NullPointerException();\n" +
278             " org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, npe);" +
279             " }\n" +
280             "}\n"
281         );
282
283         JackpotUtils.apply(getWorkDir(), script);
284
285         String JavaDoc txt = JackpotUtils.readFile(f, false);
286
287         if (txt.indexOf("ErrorManager") >= 0) {
288             fail("No ErrorManager shall be there:\n" + txt);
289         }
290
291         if (!txt.replace('\n', ' ').matches(".*Logger.getLogger *\\( *\"global\" *\\).log *\\(Level.WARNING, *null, *npe *\\).*")) {
292             fail("Logger.global shall be there:\n" + txt);
293         }
294     }
295
296     public void testAnnotateWithInfo() throws Exception JavaDoc {
297         File JavaDoc f = JackpotUtils.extractString(new File JavaDoc(getWorkDir(), "A.java"),
298             "package test;\n" +
299             "class A {\n" +
300             " public static void main(String[] args) {\n" +
301             " NullPointerException cnfe = new NullPointerException();\n" +
302             " String msg = \"ahoj\";\n" +
303             " org.openide.ErrorManager.getDefault().annotate(cnfe, org.openide.ErrorManager.INFORMATIONAL, msg, null, null, null);" +
304             " }\n" +
305             "}\n"
306         );
307
308         JackpotUtils.apply(getWorkDir(), script);
309
310         String JavaDoc txt = JackpotUtils.readFile(f, false);
311
312         if (txt.indexOf("ErrorManager") >= 0) {
313             fail("No ErrorManager shall be there:\n" + txt);
314         }
315
316         if (!txt.replace('\n', ' ').matches(".*Exceptions.attachMessage *\\(*cnfe, *msg\\).*")) {
317             fail("Exceptions.attachMessage shall be there:\n" + txt);
318         }
319
320     }
321
322     public void testRenamesEditor() throws Exception JavaDoc {
323         File JavaDoc f = JackpotUtils.extractString(new File JavaDoc(getWorkDir(), "A.java"),
324             "package test;\n" +
325             "class A {\n" +
326             " public static void main(String[] args) {\n" +
327             " try {" +
328             " System.exit(0);\n" +
329             " } catch( IllegalArgumentException iaE ) {" +
330             " org.openide.ErrorManager.getDefault().annotate(iaE, org.openide.ErrorManager.USER, null, iaE.getLocalizedMessage(), null, null);\n" +
331             " throw iaE;\n" +
332             " }" +
333             " }\n" +
334             "}\n"
335         );
336
337         JackpotUtils.apply(getWorkDir(), script);
338
339         String JavaDoc txt = JackpotUtils.readFile(f, false);
340
341         if (txt.indexOf("ErrorManager") >= 0) {
342             fail("No ErrorManager shall be there:\n" + txt);
343         }
344
345         if (!txt.replace('\n', ' ').matches(".*UIException.*")) {
346             fail("UIException shall be there:\n" + txt);
347         }
348         if (txt.replace('\n', ' ').indexOf("$") >= 0) {
349             fail("NO $ shall be there:\n" + txt);
350         }
351     }
352     
353     
354     public void testUsingAttachMessageAndThenInitCauseIsNotGoodIdea() throws Exception JavaDoc {
355         doUsingAttachMessageAndThenInitCauseIsNotGoodIdea("attachMessage");
356     }
357     
358     public void testUsingAttachLocMessageAndThenInitCauseIsNotGoodIdea() throws Exception JavaDoc {
359         doUsingAttachMessageAndThenInitCauseIsNotGoodIdea("attachLocalizedMessage");
360     }
361     
362     private void doUsingAttachMessageAndThenInitCauseIsNotGoodIdea(String JavaDoc method) throws Exception JavaDoc {
363         File JavaDoc f = JackpotUtils.extractString(new File JavaDoc(getWorkDir(), "A.java"),
364             "package test;\n" +
365             "import java.io.IOException;\n" +
366             "class A {\n" +
367             " public static void main(String[] args) throws IOException {\n" +
368             " try {" +
369             " System.exit(0);\n" +
370             " } catch( IllegalArgumentException iaE ) {" +
371             " IOException io = new IOException();" +
372             " org.openide.util.Exceptions." + method + "(io, \"msg\");\n" +
373             " iaE.printStackTrace();\n" +
374             " io.initCause(iaE);\n" +
375             " throw io;\n" +
376             " }" +
377             " }\n" +
378             "}\n"
379         );
380
381         JackpotUtils.apply(getWorkDir(), script);
382
383         String JavaDoc txt = JackpotUtils.readFile(f, false);
384
385         Matcher JavaDoc m = Pattern.compile("initCause.*" + method, Pattern.DOTALL).matcher(txt);
386         if (!m.find()) {
387             fail("First of all we should use initCause and then attachMessage:\n" + txt);
388         }
389     }
390     
391 }
392
Popular Tags