KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > uihandler > UploadLogsTest


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  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.uihandler;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.DataInputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.logging.Handler JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.LogRecord JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37 import javax.xml.parsers.DocumentBuilder JavaDoc;
38 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
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 JavaDoc;
43
44 /**
45  *
46  * @author Jaroslav Tulach
47  */

48 public class UploadLogsTest extends NbTestCase {
49     private Logger JavaDoc LOG;
50     
51
52     public UploadLogsTest(String JavaDoc s) {
53         super(s);
54     }
55     
56     protected void setUp() throws Exception JavaDoc {
57         LOG = Logger.getLogger("test." + getName());
58         
59         clearWorkDir();
60     }
61     
62     protected Level JavaDoc logLevel() {
63         return Level.INFO;
64     }
65     
66     public void testSendsCorrectlyEncoded() throws Exception JavaDoc {
67         
68         for (int times = 0; times < 10; times++) {
69             LOG.log(Level.INFO, "Running for {0} times", times);
70             List JavaDoc<LogRecord JavaDoc> recs = new ArrayList JavaDoc<LogRecord JavaDoc>();
71             recs.add(new LogRecord JavaDoc(Level.WARNING, "MSG_MISTAKE"));
72             MemoryURL.registerURL("memory://upload", "Ok");
73             URL JavaDoc redir = Installer.uploadLogs(new URL JavaDoc("memory://upload"), "myId", Collections.<String JavaDoc,String JavaDoc>emptyMap(), recs);
74
75             final byte[] content = MemoryURL.getOutputForURL("memory://upload");
76
77             String JavaDoc lineDelim = System.getProperty("line.separator");
78             assertNotNull("Some delim is there", lineDelim);
79             int head = new String JavaDoc(content, "utf-8").indexOf(lineDelim + lineDelim);
80             if (head == -1) {
81                 fail("There should be an empty line:\n" + new String JavaDoc(content, "utf-8"));
82             }
83
84             class RFImpl implements MultiPartHandler.RequestFacade, MultiPartHandler.InputFacade {
85                 private ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(content);
86
87                 public int getContentLength() {
88                     return content.length;
89                 }
90
91                 public String JavaDoc getContentType() {
92                     return MemoryURL.getRequestParameter("memory://upload", "Content-Type");
93                 }
94
95                 public MultiPartHandler.InputFacade getInput() throws IOException JavaDoc {
96                     return this;
97                 }
98
99                 public int readLine(byte[] arr, int off, int len) throws IOException JavaDoc {
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 JavaDoc getInputStream() {
116                     return is;
117                 }
118             }
119             RFImpl request = new RFImpl();
120
121             File JavaDoc dir = new File JavaDoc(getWorkDir(), "ui");
122             dir.mkdirs();
123
124             MultiPartHandler handler = new MultiPartHandler(request, dir.getPath());
125             handler.parseMultipartUpload();
126
127             File JavaDoc[] 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 JavaDoc is = new DataInputStream JavaDoc(new FileInputStream JavaDoc(files[0]));
140             class H extends Handler JavaDoc {
141                 public LogRecord JavaDoc nr;
142                 
143                 public void publish(LogRecord JavaDoc arg0) {
144                     assertNull("First call", nr);
145                     nr = arg0;
146                 }
147
148                 public void flush() {
149                 }
150
151                 public void close() throws SecurityException JavaDoc {
152                 }
153             }
154             
155             H h2 = new H();
156             LogRecords.scan(is, h2);
157             is.close();
158             LogRecord JavaDoc rec = h2.nr;
159
160             assertEquals("Same msg", recs.get(0).getMessage(), rec.getMessage());
161
162
163             DocumentBuilder JavaDoc b = DocumentBuilderFactory.newInstance().newDocumentBuilder();
164             Document JavaDoc dom = b.parse(files[0]);
165
166             assertNotNull("Parsed", dom);
167         }
168     }
169
170 }
171
172
173
Popular Tags