1 17 18 package org.netbeans.lib.uihandlerserver; 19 20 import java.io.BufferedInputStream ; 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.util.logging.Handler ; 27 import java.util.logging.Level ; 28 import java.util.logging.LogRecord ; 29 import java.util.logging.Logger ; 30 import java.util.zip.GZIPInputStream ; 31 import org.netbeans.junit.NbTestCase; 32 import org.netbeans.lib.uihandler.LogRecords; 33 34 38 public class ReadBigDataTest extends NbTestCase { 39 private Logger LOG; 40 41 public ReadBigDataTest(String testName) { 42 super(testName); 43 } 44 45 protected Level logLevel() { 46 return Level.FINEST; 47 } 48 49 protected void setUp() throws Exception { 50 LOG = Logger.getLogger("TEST-" + getName()); 51 } 52 53 protected void tearDown() throws Exception { 54 } 55 56 public void testAntonsOutOfMemExc() throws Exception { 57 String what = "antons.gz"; 58 59 InputStream is = new GZIPInputStream (getClass().getResourceAsStream(what)); 60 61 class H extends Handler { 62 int cnt; 63 LogRecord first; 64 65 public void publish(LogRecord 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 { 79 } 80 } 81 82 H h = new H(); 83 is = new GZIPInputStream (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 { 93 File dir = new File (new File (System.getProperty("user.dir")), "ui"); 94 if (!dir.exists()) { 95 return; 96 } 97 98 File [] 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 [] arr) throws IOException , FileNotFoundException { 111 class H extends Handler { 112 int cnt; 113 114 public void publish(LogRecord record) { 115 cnt++; 116 } 117 118 public void flush() { 119 } 120 121 public void close() throws SecurityException { 122 } 123 } 124 125 int i = -1; 126 for (File f : arr) { 127 LOG.log(Level.WARNING, "scanning {0}", f.getPath()); 128 i++; 129 InputStream is = new BufferedInputStream (new FileInputStream (f)); 130 H h = new H(); 131 try { 132 LogRecords.scan(is, h); 133 } catch (IOException 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 [] arr, int err) throws IOException , FileNotFoundException { 147 int i = -1; 148 for (File f : arr) { 149 LOG.log(Level.WARNING, "reading {0}", f.getPath()); 150 i++; 151 InputStream is = new BufferedInputStream (new FileInputStream (f)); 152 int cnt = 0; 153 try { 154 while (LogRecords.read(is) != null) { 155 cnt++; 156 } 157 } catch (IOException 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 |