KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > uihandlerserver > LogRecordsTest


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  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
16  */

17
18 package org.netbeans.lib.uihandlerserver;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.DataInputStream JavaDoc;
23 import java.io.DataOutputStream 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.InputStream JavaDoc;
29 import java.io.UnsupportedEncodingException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.Random JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34 import java.util.logging.Handler JavaDoc;
35 import java.util.logging.Level JavaDoc;
36 import java.util.logging.LogRecord JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38 import junit.framework.Test;
39 import org.netbeans.junit.Log;
40 import org.netbeans.junit.NbTestCase;
41 import org.netbeans.junit.NbTestSuite;
42 import org.netbeans.lib.uihandler.LogRecords;
43
44 /**
45  *
46  * @author Jaroslav Tulach
47  */

48 public class LogRecordsTest extends NbTestCase {
49     private Logger JavaDoc LOG;
50     
51     public LogRecordsTest(String JavaDoc testName) {
52         super(testName);
53     }
54     
55     
56     public static Test suite() {
57         return new NbTestSuite(LogRecordsTest.class);
58 // return new LogRecordsTest("testReadException");
59
}
60     
61     protected Level JavaDoc logLevel() {
62         return Level.FINEST;
63     }
64
65     protected void setUp() throws Exception JavaDoc {
66         LOG = Logger.getLogger("TEST-" + getName());
67     }
68
69     protected void tearDown() throws Exception JavaDoc {
70     }
71
72     public void testParamsGetCleared() throws Exception JavaDoc {
73         String JavaDoc r =
74             "<record>" +
75             "<date>2006-11-17T10:16:14</date>" +
76             "<millis>1163729774285</millis>" +
77             "<sequence>20</sequence>" +
78             "<level>INFO</level>" +
79             "<thread>12</thread>" +
80             "<message>MSG</message>" +
81             "<key>MSG</key>" +
82             "<catalog>a.bundle.somewhere</catalog>" +
83             "<param>1</param>" +
84             "</record>" +
85             
86             "<record>" +
87             "<date>2006-11-17T10:16:14</date>" +
88             "<millis>1163729774285</millis>" +
89             "<sequence>20</sequence>" +
90             "<level>INFO</level>" +
91             "<thread>12</thread>" +
92             "<message>MSG</message>" +
93             "<key>MSG</key>" +
94             "<catalog>a.bundle.somewhere</catalog>" +
95             "<param>2</param>" +
96             "</record>";
97         
98         class H extends Handler JavaDoc {
99             int cnt;
100             
101             public void publish(LogRecord JavaDoc arg0) {
102                 cnt++;
103                     assertNotNull("We have params " + cnt, arg0.getParameters());
104                 assertEquals("One argument for " + cnt + "th record", 1, arg0.getParameters().length);
105             }
106
107             public void flush() {
108             }
109
110             public void close() throws SecurityException JavaDoc {
111             }
112         }
113         H h = new H();
114         
115         LogRecords.scan(new ByteArrayInputStream JavaDoc(r.getBytes()), h);
116
117         assertEquals("Two records", 2, h.cnt);
118     }
119     
120     public void testWriteAndRead() throws Exception JavaDoc {
121         doWriteAndReadTest(System.currentTimeMillis());
122     }
123     
124     public void testNewFailureOn1165572711706() throws Exception JavaDoc {
125         doWriteAndReadTest(1165572711706L);
126     }
127     
128     public void testFailureOn1159804485342() throws Exception JavaDoc {
129         doWriteAndReadTest(1159804485342L);
130     }
131     
132     public void testMakeSureItIsReadable() throws Exception JavaDoc {
133         InputStream JavaDoc is = getClass().getResourceAsStream("NB1216449736.xml");
134         int cnt = 0;
135         for (;;) {
136             LOG.log(Level.INFO, "Reading {0}th record", cnt);
137             LogRecord JavaDoc r = LogRecords.read(is);
138             if (r == null) {
139                 break;
140             }
141             LOG.log(Level.INFO, "Read {0}th record", cnt);
142             cnt++;
143         }
144         is.close();
145     }
146     
147     public void testReadException() throws Exception JavaDoc {
148         String JavaDoc what = "NB1216449736.xml";
149         
150         InputStream JavaDoc is = getClass().getResourceAsStream(what);
151         int cnt = 0;
152         
153         class H extends Handler JavaDoc {
154             int cnt;
155             LogRecord JavaDoc first;
156             
157             public void publish(LogRecord JavaDoc record) {
158                 if (cnt == 0) {
159                     first = record;
160                 }
161                 cnt++;
162             }
163
164             public void flush() {
165             }
166
167             public void close() throws SecurityException JavaDoc {
168             }
169         }
170         
171         LogRecord JavaDoc first = null;
172         for (;;) {
173             LOG.log(Level.INFO, "Reading {0}th record", cnt);
174             LogRecord JavaDoc r = LogRecords.read(is);
175             if (r == null) {
176                 break;
177             }
178             if (first == null) {
179                 first = r;
180             }
181             LOG.log(Level.INFO, "Read {0}th record", cnt);
182             cnt++;
183         }
184         is.close();
185         
186         H h = new H();
187         is = getClass().getResourceAsStream(what);
188         LogRecords.scan(is, h);
189         is.close();
190         
191         assertNotNull(first);
192         assertNotNull(h.first);
193         assertEquals("Same message", first.getMessage(), h.first.getMessage());
194         assertNotNull("exception from read", first.getThrown());
195         assertNotNull("exception from scan", h.first.getThrown());
196         assertEquals("Same exception message", first.getThrown().getMessage(), h.first.getThrown().getMessage());
197         
198         StackTraceElement JavaDoc[] arr1 = first.getThrown().getStackTrace();
199         StackTraceElement JavaDoc[] arr2 = h.first.getThrown().getStackTrace();
200         
201         assertEquals("Same length", arr1.length, arr2.length);
202
203         for (int i = 0; i < arr1.length; i++) {
204             if (!arr1[i].equals(arr2[i])) {
205                 fail(i + " th stack differ: " + arr1[i] + " != " + arr2[i] + "\nline: " + arr1[i].getLineNumber() + " and " + arr2[i].getLineNumber());
206             }
207         }
208         
209         
210         assertEquals("The same amount of records", cnt, h.cnt);
211         
212     }
213     
214     public void testCanReadEmpty() throws Exception JavaDoc {
215         InputStream JavaDoc is = getClass().getResourceAsStream("Empty.xml");
216         int cnt = 0;
217         for (;;) {
218             LOG.log(Level.INFO, "Reading {0}th record", cnt);
219             LogRecord JavaDoc r = LogRecords.read(is);
220             if (r == null) {
221                 break;
222             }
223             LOG.log(Level.INFO, "Read {0}th record", cnt);
224             cnt++;
225         }
226         is.close();
227         
228         assertEquals("No records", 0, cnt);
229     }
230     public void testMakeSureItIsScannable() throws Exception JavaDoc {
231         InputStream JavaDoc is = getClass().getResourceAsStream("NB1216449736.xml");
232         int cnt = 0;
233         
234         class H extends Handler JavaDoc {
235             int cnt;
236             
237             public void publish(LogRecord JavaDoc record) {
238                 cnt++;
239             }
240
241             public void flush() {
242             }
243
244             public void close() throws SecurityException JavaDoc {
245             }
246         }
247         
248         for (;;) {
249             LOG.log(Level.INFO, "Reading {0}th record", cnt);
250             LogRecord JavaDoc r = LogRecords.read(is);
251             if (r == null) {
252                 break;
253             }
254             LOG.log(Level.INFO, "Read {0}th record", cnt);
255             cnt++;
256         }
257         is.close();
258         
259         H h = new H();
260         is = getClass().getResourceAsStream("NB1216449736.xml");
261         LogRecords.scan(is, h);
262         is.close();
263         
264         assertEquals("The same amount of records", cnt, h.cnt);
265     }
266     public void testBadUserIsBad() throws Exception JavaDoc {
267         String JavaDoc what = "baduser.xml";
268         InputStream JavaDoc is = getClass().getResourceAsStream(what);
269         int cnt = 0;
270         
271         class H extends Handler JavaDoc {
272             int cnt;
273             
274             public void publish(LogRecord JavaDoc record) {
275                 cnt++;
276             }
277
278             public void flush() {
279             }
280
281             public void close() throws SecurityException JavaDoc {
282             }
283         }
284         
285         for (;;) {
286             LOG.log(Level.INFO, "Reading {0}th record", cnt);
287             LogRecord JavaDoc r = LogRecords.read(is);
288             if (r == null) {
289                 break;
290             }
291             LOG.log(Level.INFO, "Read {0}th record", cnt);
292             cnt++;
293         }
294         is.close();
295         
296         H h = new H();
297         is = getClass().getResourceAsStream(what);
298         LogRecords.scan(is, h);
299         is.close();
300         
301         assertEquals("The same amount of records", cnt, h.cnt);
302     }
303     
304     
305
306     public void testDoesNotAskForWrongBunles() throws Exception JavaDoc {
307         LogRecord JavaDoc rec = new LogRecord JavaDoc(Level.FINER, "UI_ACTION_BUTTON_PRESS"); // NOI18N
308
rec.setParameters(new Object JavaDoc[] { "0", "1" });
309         rec.setResourceBundle(ResourceBundle.getBundle(LogRecordsTest.class.getPackage().getName() + ".Props"));
310         
311         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
312         LogRecords.write(os, rec);
313         os.close();
314         
315         
316         class H extends Handler JavaDoc {
317             int cnt;
318             
319             public void publish(LogRecord JavaDoc arg0) {
320                 cnt++;
321                 assertNotNull("We have params " + cnt, arg0.getParameters());
322                 assertEquals("Two argument for " + cnt + "th record", 2, arg0.getParameters().length);
323             }
324
325             public void flush() {
326             }
327
328             public void close() throws SecurityException JavaDoc {
329             }
330         }
331         H h = new H();
332         
333         CharSequence JavaDoc log = Log.enable("", Level.INFO);
334         LogRecords.scan(new ByteArrayInputStream JavaDoc(os.toByteArray()), h);
335
336         assertEquals("One record", 1, h.cnt);
337         
338         if (log.toString().indexOf("Cannot find resource") < 0) {
339             fail(log.toString());
340         }
341     }
342     
343     public void testScanEmpty91974() throws Exception JavaDoc {
344         String JavaDoc what = "uigestures-iz91974.xml";
345         InputStream JavaDoc is = getClass().getResourceAsStream(what);
346         int cnt = 0;
347         
348         class H extends Handler JavaDoc {
349             int cnt;
350             
351             public void publish(LogRecord JavaDoc record) {
352                 cnt++;
353             }
354
355             public void flush() {
356             }
357
358             public void close() throws SecurityException JavaDoc {
359             }
360         }
361         
362         for (;;) {
363             LOG.log(Level.INFO, "Reading {0}th record", cnt);
364             LogRecord JavaDoc r = LogRecords.read(is);
365             if (r == null) {
366                 break;
367             }
368             LOG.log(Level.INFO, "Read {0}th record", cnt);
369             cnt++;
370         }
371         is.close();
372         
373         H h = new H();
374         is = getClass().getResourceAsStream(what);
375         LogRecords.scan(is, h);
376         is.close();
377         
378         assertEquals("The same amount of records", cnt, h.cnt);
379     }
380     public void testNotFinishedFiles() throws Exception JavaDoc {
381         String JavaDoc what = "eof.xml";
382         InputStream JavaDoc is = getClass().getResourceAsStream(what);
383         int cnt = 0;
384         
385         class H extends Handler JavaDoc {
386             int cnt;
387             
388             public void publish(LogRecord JavaDoc record) {
389                 cnt++;
390             }
391
392             public void flush() {
393             }
394
395             public void close() throws SecurityException JavaDoc {
396             }
397         }
398         
399         for (;;) {
400             LOG.log(Level.INFO, "Reading {0}th record", cnt);
401             LogRecord JavaDoc r;
402             try {
403                 r = LogRecords.read(is);
404             } catch (EOFException JavaDoc ex) {
405                 assertNull("Next read is null", LogRecords.read(is));
406                 break;
407             }
408             if (r == null) {
409                 break;
410             }
411             LOG.log(Level.INFO, "Read {0}th record", cnt);
412             cnt++;
413         }
414         is.close();
415         
416         H h = new H();
417         is = getClass().getResourceAsStream(what);
418         LogRecords.scan(is, h);
419         is.close();
420         
421         assertEquals("The same amount of records", cnt, h.cnt);
422     }
423     public void testScanFileThatClaimsTohaveWrongUTF8Char() throws Exception JavaDoc {
424         InputStream JavaDoc is = getClass().getResourceAsStream("wrongutfchar.xml");
425         int cnt = 0;
426         
427         class H extends Handler JavaDoc {
428             int cnt;
429             
430             public void publish(LogRecord JavaDoc record) {
431                 cnt++;
432             }
433
434             public void flush() {
435             }
436
437             public void close() throws SecurityException JavaDoc {
438             }
439         }
440         
441         for (;;) {
442             LOG.log(Level.INFO, "Reading {0}th record", cnt);
443             LogRecord JavaDoc r = LogRecords.read(is);
444             if (r == null) {
445                 break;
446             }
447             LOG.log(Level.INFO, "Read {0}th record", cnt);
448             cnt++;
449         }
450         is.close();
451         
452         H h = new H();
453         is = getClass().getResourceAsStream("wrongutfchar.xml");
454         LogRecords.scan(is, h);
455         is.close();
456         
457         assertEquals("The same amount of records", cnt, h.cnt);
458     }
459     
460     private void doWriteAndReadTest(long seed) throws Exception JavaDoc {
461         Logger.getAnonymousLogger().info("seed is: " + seed);
462         
463         File JavaDoc file = new File JavaDoc(getWorkDir(), "feed.txt");
464         Random JavaDoc r = new Random JavaDoc(seed);
465         DataOutputStream JavaDoc out = new DataOutputStream JavaDoc(new FileOutputStream JavaDoc(file));
466         
467         
468         int cnt = r.nextInt(500);
469         final LogRecord JavaDoc[] arr = new LogRecord JavaDoc[cnt];
470         for (int i = 0; i < cnt; i++) {
471             LogRecord JavaDoc rec = generateLogRecord(r);
472             arr[i] = rec;
473             LogRecords.write(out, rec);
474         }
475         out.close();
476         
477
478         {
479             DataInputStream JavaDoc in = new DataInputStream JavaDoc(new FileInputStream JavaDoc(file));
480             for (int i = 0; i < cnt; i++) {
481                 LogRecord JavaDoc rec = LogRecords.read(in);
482                 assertLog(i + "-th record is the same", rec, arr[i]);
483             }
484             in.close();
485         }
486         
487         class H extends Handler JavaDoc {
488             int cnt;
489             
490             public void publish(LogRecord JavaDoc rec) {
491                 try {
492                     assertLog(cnt + "-th record is the same", rec, arr[cnt]);
493                 } catch(Exception JavaDoc ex) {
494                     throw (RuntimeException JavaDoc)new RuntimeException JavaDoc().initCause(ex);
495                 }
496                 cnt++;
497             }
498
499             public void flush() {
500                 assertEquals("All read", cnt, arr.length);
501                 cnt = -1;
502             }
503
504             public void close() throws SecurityException JavaDoc {
505             }
506         }
507         
508         H h = new H();
509         {
510             LOG.info("Scanning " + file);
511             DataInputStream JavaDoc in = new DataInputStream JavaDoc(new FileInputStream JavaDoc(file));
512             LogRecords.scan(in, h);
513             in.close();
514         }
515         assertEquals("Cleared", -1, h.cnt);
516     }
517     
518     private LogRecord JavaDoc generateLogRecord(Random JavaDoc r) throws UnsupportedEncodingException JavaDoc {
519         LogRecord JavaDoc rec = new LogRecord JavaDoc(randomLevel(r), randomString(r));
520         return rec;
521     }
522
523     private void assertLog(String JavaDoc string, LogRecord JavaDoc r1, LogRecord JavaDoc r2) throws Exception JavaDoc {
524         if (r1 == null && r2 != null) {
525             fail("r1: null r2 not: " + r(r2));
526         }
527         if (r1 != null && r2 == null) {
528             fail("r2: null r1 not: " + r(r2));
529         }
530         
531         for (Method JavaDoc m : LogRecord JavaDoc.class.getMethods()) {
532             if (m.getName().startsWith("get") && m.getParameterTypes().length == 0) {
533                 Object JavaDoc o1 = m.invoke(r1);
534                 Object JavaDoc o2 = m.invoke(r2);
535                 
536                 if (o1 == null && o2 == null) {
537                     continue;
538                 }
539                 if (o1 == null || o2 == null || !o1.equals(o2)) {
540                     assertEquals(
541                         "Logs differ in result of " + m.getName() + "\nrec1: " + r(r1) + "\nrec2: " + r(r2),
542                         o1, o2
543                     );
544                 }
545             }
546         }
547     }
548     
549     private static String JavaDoc r(LogRecord JavaDoc r) {
550         return r.getMessage();
551     }
552
553     private static Level JavaDoc randomLevel(Random JavaDoc r) {
554         int lev = r.nextInt(1100);
555         if (lev >= Level.SEVERE.intValue()) return Level.SEVERE;
556         if (lev >= Level.WARNING.intValue()) return Level.WARNING;
557         if (lev >= Level.INFO.intValue()) return Level.INFO;
558         if (lev >= Level.CONFIG.intValue()) return Level.CONFIG;
559         if (lev >= Level.FINE.intValue()) return Level.FINE;
560         if (lev >= Level.FINER.intValue()) return Level.FINER;
561         if (lev >= Level.FINEST.intValue()) return Level.FINEST;
562         return Level.OFF;
563     }
564
565     private static String JavaDoc randomString(Random JavaDoc r) throws UnsupportedEncodingException JavaDoc {
566         int len = r.nextInt(50);
567         byte[] arr = new byte[len];
568         for (int i = 0; i < arr.length; i++) {
569             int ch = r.nextInt(256);
570             if (ch < 32) {
571                 ch = 32;
572             }
573             if (ch > 'z') {
574                 ch = 'z';
575             }
576             arr[i] = (byte)ch;
577         }
578         return new String JavaDoc(new String JavaDoc(arr, "utf-8").getBytes(), "utf-8");
579     }
580 }
581
Popular Tags