KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedInputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.util.logging.Handler JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.LogRecord JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import java.util.zip.GZIPInputStream JavaDoc;
31 import org.netbeans.junit.NbTestCase;
32 import org.netbeans.lib.uihandler.LogRecords;
33
34 /**
35  *
36  * @author Jaroslav Tulach
37  */

38 public class ReadBigDataTest extends NbTestCase {
39     private Logger JavaDoc LOG;
40     
41     public ReadBigDataTest(String JavaDoc testName) {
42         super(testName);
43     }
44     
45     protected Level JavaDoc logLevel() {
46         return Level.FINEST;
47     }
48
49     protected void setUp() throws Exception JavaDoc {
50         LOG = Logger.getLogger("TEST-" + getName());
51     }
52
53     protected void tearDown() throws Exception JavaDoc {
54     }
55
56     public void testAntonsOutOfMemExc() throws Exception JavaDoc {
57         String JavaDoc what = "antons.gz";
58         
59         InputStream JavaDoc is = new GZIPInputStream JavaDoc(getClass().getResourceAsStream(what));
60         
61         class H extends Handler JavaDoc {
62             int cnt;
63             LogRecord JavaDoc first;
64             
65             public void publish(LogRecord JavaDoc record) {
66                 if (cnt == 0) {
67                     first = record;
68                 }
69                 cnt++;
70                 if (record.getParameters() != null && record.getParameters().length > 500) {
71                     fail("Too many parameters: " + record.getParameters().length);
72                 }
73             }
74
75             public void flush() {
76             }
77
78             public void close() throws SecurityException JavaDoc {
79             }
80         }
81         
82         H h = new H();
83         is = new GZIPInputStream JavaDoc(getClass().getResourceAsStream(what));
84         LogRecords.scan(is, h);
85         is.close();
86         
87         if (h.cnt != 322) {
88             fail("Invalid number of records: " + h.cnt);
89         }
90     }
91
92     public void testWriteAndRead() throws Exception JavaDoc {
93         File JavaDoc dir = new File JavaDoc(new File JavaDoc(System.getProperty("user.dir")), "ui");
94         if (!dir.exists()) {
95             return;
96         }
97
98         File JavaDoc[] arr = dir.listFiles();
99         if (arr == null) {
100             return;
101         }
102         
103         int[] cnts = new int[arr.length];
104         int err1 = readAsAStream(cnts, arr, 0);
105         int err2 = readAsSAX(cnts, 0, arr);
106         
107         assertEquals("No errors: " + err1 + " and no " + err2, 0, err1 + err2);
108     }
109
110     private int readAsSAX(final int[] cnts, int err, final File JavaDoc[] arr) throws IOException JavaDoc, FileNotFoundException JavaDoc {
111         class H extends Handler JavaDoc {
112             int cnt;
113
114             public void publish(LogRecord JavaDoc record) {
115                 cnt++;
116             }
117
118             public void flush() {
119             }
120
121             public void close() throws SecurityException JavaDoc {
122             }
123         }
124         
125         int i = -1;
126         for (File JavaDoc f : arr) {
127             LOG.log(Level.WARNING, "scanning {0}", f.getPath());
128             i++;
129             InputStream JavaDoc is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(f));
130             H h = new H();
131             try {
132                 LogRecords.scan(is, h);
133             } catch (IOException JavaDoc ex) {
134                 LOG.log(Level.WARNING, null, ex);
135                 err++;
136                 continue;
137             } finally {
138                 is.close();
139             }
140             
141             assertEquals("The same amount for " + f, h.cnt, cnts[i]);
142         }
143         return err;
144     }
145
146     private int readAsAStream(final int[] cnts, final File JavaDoc[] arr, int err) throws IOException JavaDoc, FileNotFoundException JavaDoc {
147         int i = -1;
148         for (File JavaDoc f : arr) {
149             LOG.log(Level.WARNING, "reading {0}", f.getPath());
150             i++;
151             InputStream JavaDoc is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(f));
152             int cnt = 0;
153             try {
154                 while (LogRecords.read(is) != null) {
155                     cnt++;
156                 }
157             } catch (IOException JavaDoc ex) {
158                 LOG.log(Level.WARNING, null, ex);
159                 err++;
160                 continue;
161             } finally {
162                 cnts[i] = cnt;
163                 is.close();
164             }
165             is.close();
166         }
167         return err;
168     }
169 }
170
Popular Tags