1 19 20 package org.netbeans.modules.uihandler; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.DataInputStream ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.net.URL ; 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 import java.util.Collections ; 32 import java.util.List ; 33 import java.util.logging.Handler ; 34 import java.util.logging.Level ; 35 import java.util.logging.LogRecord ; 36 import java.util.logging.Logger ; 37 import javax.xml.parsers.DocumentBuilder ; 38 import javax.xml.parsers.DocumentBuilderFactory ; 39 import org.netbeans.junit.NbTestCase; 40 import org.netbeans.lib.uihandler.LogRecords; 41 import org.netbeans.lib.uihandler.MultiPartHandler; 42 import org.w3c.dom.Document ; 43 44 48 public class UploadLogsTest extends NbTestCase { 49 private Logger LOG; 50 51 52 public UploadLogsTest(String s) { 53 super(s); 54 } 55 56 protected void setUp() throws Exception { 57 LOG = Logger.getLogger("test." + getName()); 58 59 clearWorkDir(); 60 } 61 62 protected Level logLevel() { 63 return Level.INFO; 64 } 65 66 public void testSendsCorrectlyEncoded() throws Exception { 67 68 for (int times = 0; times < 10; times++) { 69 LOG.log(Level.INFO, "Running for {0} times", times); 70 List <LogRecord > recs = new ArrayList <LogRecord >(); 71 recs.add(new LogRecord (Level.WARNING, "MSG_MISTAKE")); 72 MemoryURL.registerURL("memory://upload", "Ok"); 73 URL redir = Installer.uploadLogs(new URL ("memory://upload"), "myId", Collections.<String ,String >emptyMap(), recs); 74 75 final byte[] content = MemoryURL.getOutputForURL("memory://upload"); 76 77 String lineDelim = System.getProperty("line.separator"); 78 assertNotNull("Some delim is there", lineDelim); 79 int head = new String (content, "utf-8").indexOf(lineDelim + lineDelim); 80 if (head == -1) { 81 fail("There should be an empty line:\n" + new String (content, "utf-8")); 82 } 83 84 class RFImpl implements MultiPartHandler.RequestFacade, MultiPartHandler.InputFacade { 85 private ByteArrayInputStream is = new ByteArrayInputStream (content); 86 87 public int getContentLength() { 88 return content.length; 89 } 90 91 public String getContentType() { 92 return MemoryURL.getRequestParameter("memory://upload", "Content-Type"); 93 } 94 95 public MultiPartHandler.InputFacade getInput() throws IOException { 96 return this; 97 } 98 99 public int readLine(byte[] arr, int off, int len) throws IOException { 100 int cnt = 0; 101 for (; cnt < len; ) { 102 int ch = is.read(); 103 if (ch == -1) { 104 return ch; 105 } 106 arr[off + cnt] = (byte)ch; 107 cnt++; 108 if (ch == '\n') { 109 break; 110 } 111 } 112 return cnt; 113 } 114 115 public InputStream getInputStream() { 116 return is; 117 } 118 } 119 RFImpl request = new RFImpl(); 120 121 File dir = new File (getWorkDir(), "ui"); 122 dir.mkdirs(); 123 124 MultiPartHandler handler = new MultiPartHandler(request, dir.getPath()); 125 handler.parseMultipartUpload(); 126 127 File [] files = dir.listFiles(); 128 assertEquals(times + "th file created", times + 1, files.length); 129 if (!files[times].getName().startsWith("myId")) { 130 fail("Expected was 'myId':" + files[times].getName()); 131 } 132 LOG.info("Got these files: " + Arrays.asList(files)); 133 Arrays.sort(files); 134 LOG.info("Sorted to files: " + Arrays.asList(files)); 135 136 137 assertEquals("Handler keeps name of the right file", files[times], handler.getFile("logs")); 138 139 DataInputStream is = new DataInputStream (new FileInputStream (files[0])); 140 class H extends Handler { 141 public LogRecord nr; 142 143 public void publish(LogRecord arg0) { 144 assertNull("First call", nr); 145 nr = arg0; 146 } 147 148 public void flush() { 149 } 150 151 public void close() throws SecurityException { 152 } 153 } 154 155 H h2 = new H(); 156 LogRecords.scan(is, h2); 157 is.close(); 158 LogRecord rec = h2.nr; 159 160 assertEquals("Same msg", recs.get(0).getMessage(), rec.getMessage()); 161 162 163 DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 164 Document dom = b.parse(files[0]); 165 166 assertNotNull("Parsed", dom); 167 } 168 } 169 170 } 171 172 173 | Popular Tags |