001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
027: */
028:
029: package org.netbeans.lib.uihandlerserver;
030:
031: import java.io.BufferedInputStream;
032: import java.io.File;
033: import java.io.FileInputStream;
034: import java.io.FileNotFoundException;
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.util.logging.Handler;
038: import java.util.logging.Level;
039: import java.util.logging.LogRecord;
040: import java.util.logging.Logger;
041: import java.util.zip.GZIPInputStream;
042: import org.netbeans.junit.NbTestCase;
043: import org.netbeans.lib.uihandler.LogRecords;
044: import org.netbeans.lib.uihandler.TestHandler;
045:
046: /**
047: *
048: * @author Jaroslav Tulach
049: */
050: public class ReadBigDataTest extends NbTestCase {
051: private Logger LOG;
052:
053: public ReadBigDataTest(String testName) {
054: super (testName);
055: }
056:
057: protected Level logLevel() {
058: return Level.FINEST;
059: }
060:
061: protected void setUp() throws Exception {
062: LOG = Logger.getLogger("TEST-" + getName());
063: }
064:
065: protected void tearDown() throws Exception {
066: }
067:
068: public void testAntonsOutOfMemExc() throws Exception {
069: String what = "antons.gz";
070:
071: InputStream is = new GZIPInputStream(getClass()
072: .getResourceAsStream(what));
073:
074: class H extends Handler {
075: int cnt;
076: LogRecord first;
077:
078: public void publish(LogRecord record) {
079: if (cnt == 0) {
080: first = record;
081: }
082: cnt++;
083: if (record.getParameters() != null
084: && record.getParameters().length > 1500) {
085: fail("Too many parameters: "
086: + record.getParameters().length);
087: }
088: }
089:
090: public void flush() {
091: }
092:
093: public void close() throws SecurityException {
094: }
095: }
096:
097: H h = new H();
098: is = new GZIPInputStream(getClass().getResourceAsStream(what));
099: LogRecords.scan(is, h);
100: is.close();
101:
102: if (h.cnt != 322) {
103: fail("Invalid number of records: " + h.cnt);
104: }
105: }
106:
107: public void testWriteAndRead() throws Exception {
108: File dir = new File(new File(System.getProperty("user.dir")),
109: "ui");
110: if (!dir.exists()) {
111: return;
112: }
113:
114: File[] arr = dir.listFiles();
115: if (arr == null) {
116: return;
117: }
118:
119: int[] cnts = new int[arr.length];
120: int err1 = readAsAStream(cnts, arr, 0);
121: int err2 = readAsSAX(cnts, 0, arr);
122:
123: assertEquals("No errors: " + err1 + " and no " + err2, 0, err1
124: + err2);
125: }
126:
127: private int readAsSAX(final int[] cnts, int err, final File[] arr)
128: throws IOException, FileNotFoundException {
129: class H extends Handler {
130: int cnt;
131:
132: public void publish(LogRecord record) {
133: cnt++;
134: }
135:
136: public void flush() {
137: }
138:
139: public void close() throws SecurityException {
140: }
141: }
142:
143: int i = -1;
144: for (File f : arr) {
145: LOG.log(Level.WARNING, "scanning {0}", f.getPath());
146: i++;
147: InputStream is = new BufferedInputStream(
148: new FileInputStream(f));
149: H h = new H();
150: try {
151: LogRecords.scan(is, h);
152: } catch (IOException ex) {
153: LOG.log(Level.WARNING, null, ex);
154: err++;
155: continue;
156: } finally {
157: is.close();
158: }
159:
160: assertEquals("The same amount for " + f, h.cnt, cnts[i]);
161: }
162: return err;
163: }
164:
165: private int readAsAStream(final int[] cnts, final File[] arr,
166: int err) throws IOException, FileNotFoundException {
167: int i = -1;
168: for (File f : arr) {
169: LOG.log(Level.WARNING, "reading {0}", f.getPath());
170: i++;
171: InputStream is = new BufferedInputStream(
172: new FileInputStream(f));
173: int cnt = 0;
174: TestHandler records = new TestHandler(is);
175: try {
176: while (records.read() != null) {
177: cnt++;
178: }
179: } catch (Exception ex) {
180: LOG.log(Level.WARNING, null, ex);
181: err++;
182: continue;
183: } finally {
184: cnts[i] = cnt;
185: is.close();
186: }
187: is.close();
188: }
189: return err;
190: }
191: }
|