KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > server > uihandler > ExceptionsTest


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.server.uihandler;
21
22 import org.netbeans.server.uihandler.Exceptions;
23 import java.io.File JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.LogRecord JavaDoc;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.server.uihandler.StatisticsTest.PgCnt;
32 import org.openide.util.NbBundle;
33
34 /**
35  *
36  * @author jindra
37  */

38 public class ExceptionsTest extends NbTestCase {
39     private LogsManager logsManager;
40     private File JavaDoc data;
41     
42     public ExceptionsTest(String JavaDoc testName) {
43         super(testName);
44     }
45
46     protected Level JavaDoc logLevel() {
47         return Level.INFO;
48     }
49     
50     public void testGetLastSessionIssueId() throws Exception JavaDoc{
51         clearWorkDir();
52         DbInsertionTest.setUpTestingDB(getWorkDirPath());
53         data = new File JavaDoc(getWorkDir(), "data");
54         data.mkdirs();
55         logsManager = LogsManager.createManager(data);
56         
57         PgCnt page = new PgCnt();
58         int issueId;
59         Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc> mapa;
60         logsManager.preparePageContext(page, null);
61         mapa = (Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc>) page.getAttribute("lastExceptions");
62         assertNull(mapa);
63         logsManager.preparePageContext(page, "log4442");
64         mapa = (Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc>) page.getAttribute("lastExceptions");
65         assertNull("MAPA SHOULD BE NULL FOR NO SESSIONS", mapa);
66         
67         File JavaDoc log = LogsManagerTest.extractResourceAs(data, "2.log", "log4442.1");
68         logsManager.addLog(log);
69         
70         logsManager.preparePageContext(page, "log4442");
71         mapa = (Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc>) page.getAttribute("lastExceptions");
72         assertNotNull("MAPA SHOULD BE NOT NULL", mapa);
73         assertNotNull("ONE SESSION IS ALREADY LOGGED", mapa.get(2));
74         issueId = Integer.parseInt(mapa.get(2).getMessage());
75         assertEquals("THIS ISSUE SHOULD HAVE NO DUPLICATES", -1, issueId);
76         
77         log = LogsManagerTest.extractResourceAs(data, "2.log", "log4443.1");
78         // wait for database to insert LOG 1
79
Thread.currentThread().sleep(1000);
80         logsManager.addLog(log); // ADD THE SAME ISSUE
81

82         // wait for database to insert LOG 2
83
Thread.currentThread().sleep(1000);
84         logsManager.preparePageContext(page, "log4443");
85         mapa = (Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc>) page.getAttribute("lastExceptions");
86         assertNotNull("MAPA SHOULD BE NOT NULL", mapa);
87         assertNotNull("ONE SESSION IS ALREADY LOGGED", mapa.get(2));
88         issueId = Integer.parseInt(mapa.get(2).getMessage());
89         assertEquals("THIS ISSUE SHOULD BE DUPLICATE OF ISSUE 1", 1, issueId);
90         
91         log = LogsManagerTest.extractResourceAs(data, "1.log", "log4442.2");
92         logsManager.addLog(log);
93         logsManager.preparePageContext(page, "log4442");
94         // wait for database to insert LOG 2
95
Thread.currentThread().sleep(1000);
96         mapa = (Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc>) page.getAttribute("lastExceptions");
97         assertNotNull("MAPA SHOULD BE NOT NULL", mapa);
98         assertNotNull("MORE SESSIONS ARE ALREADY LOGGED", mapa.get(2));
99         issueId = Integer.parseInt(mapa.get(2).getMessage());
100         assertEquals("THIS IS NOT AN ERROR REPORT", -2, issueId);
101         
102         /** test issue #95540 */
103         log = LogsManagerTest.extractResourceAs(data, "3.log", "log4445.1");
104         logsManager.addLog(log);
105         logsManager.preparePageContext(page, "log4445");
106         // wait for database to insert LOG 3
107
Thread.currentThread().sleep(1000);
108         mapa = (Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc>) page.getAttribute("lastExceptions");
109         assertNotNull("MAPA SHOULD BE NOT NULL", mapa);
110         assertNotNull("ONE SESSION IS ALREADY LOGGED", mapa.get(2));
111         issueId = Integer.parseInt(mapa.get(2).getMessage());
112         assertEquals("THIS SHOULD BE INSERTED AS A NEW ISSUE", -1, issueId);
113         
114         Statement JavaDoc statement = Utils.getConnection().createStatement();
115         ResultSet JavaDoc result = statement.executeQuery("SELECT * FROM ISSUE");
116         assertTrue("THERE MUST BE TWO ISSUES", result.next());
117         assertTrue("THERE MUST BE TWO ISSUES", result.next());
118         assertFalse("THERE MUST BE TWO ISSUES", result.next());
119         result.close();
120         statement.close();
121         
122         Utils.closeConnection();
123     }
124     
125     public void testProcess() {
126         Exceptions exceptions = new Exceptions();
127         LogRecord JavaDoc log = getDataLog();
128         LogRecord JavaDoc logThrown = getThrownLog();
129         Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc> mapa = exceptions.process(log);
130         assertEquals("PROCESS DATA", log, mapa.get(0));
131         mapa = exceptions.process(logThrown);
132         assertEquals("PROCESS EXCEPTION", logThrown, mapa.get(1));
133     }
134     
135     public void testJoin() {
136         Exceptions exceptions = new Exceptions();
137         Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc> empty = exceptions.newData();
138         Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc> data = exceptions.process(getDataLog());
139         Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc> thr = exceptions.process(getThrownLog());
140         LogRecord JavaDoc log2 = getThrownLog();
141         log2.setThrown(new UnknownError JavaDoc("ERRRROR"));
142         Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc> thr2 = exceptions.process(log2);
143         Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc> result;
144         result = exceptions.join(empty, data);
145         assertEquals("JOIN DATA", data, result);
146         result = exceptions.join(empty, thr);
147         assertEquals("JOIN THROWN", thr, result);
148         result = exceptions.join(data, thr);
149         assertEquals("JOIN BOTH", data.get(0), result.get(0));
150         assertEquals("JOIN BOTH", thr.get(1), result.get(1));
151         Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc> result2;
152         result2 = exceptions.join(result, empty);
153         assertEquals("HOLD DATA", result, result2);
154         result2 = exceptions.join(thr2, result);
155         assertEquals("GET LAST EXCEPTION", result, result2);
156         result2 = exceptions.join(result, thr2);
157         if (result.equals(result2)) throw new AssertionError JavaDoc("EXCEPTION SHOULD BE REVRITEN BE THE LAST ONE");
158         assertEquals("DATA SHOULD STAY THE SAME", result.get(0), result2.get(0));
159         assertNotNull(result.get(0));
160         assertEquals("EXCEPTION SHOULD BE REVRITEN BE THE LAST ONE", thr2.get(1), result2.get(1));
161         assertNotNull(result.get(1));
162     }
163     
164     public void testFinishSessionUpload() throws Exception JavaDoc {
165         clearWorkDir();
166         DbInsertionTest.setUpTestingDB(getWorkDirPath());
167         Statement JavaDoc statement = Utils.getConnection().createStatement();
168         ResultSet JavaDoc re = statement.executeQuery("SELECT * FROM ISSUE");
169         while (re.next()) System.out.println(re.getString(1));
170         String JavaDoc userId = "log4445";
171         Exceptions exceptions = new Exceptions();
172         Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc> data = exceptions.process(getDataLog());
173         Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc> thr = exceptions.process(getThrownLog());
174         Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc> both = exceptions.newData();
175         both = exceptions.join(both, thr);
176         both = exceptions.join(data, both);
177         Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc> result = exceptions.finishSessionUpload(userId, 1, false, data);
178         checkResult("NOT INITIAL UPLOAD",result, -2);//not INITIAL
179
result = exceptions.finishSessionUpload(userId, 1, true, thr);
180         checkResult("NO METADATA IN REPORT", result, -2);//not META DATA
181
result = exceptions.finishSessionUpload(userId, 1, true, data);
182         checkResult("NO THROWN IN REPORT", result, -2);//not THROWN
183
result = exceptions.finishSessionUpload(userId, 1, true, both);
184         checkResult("THERE SHOULD BE NO DUPLICATE OF THIS ISSUE", result, -1);//not DUPLICATE
185
// Thread.sleep(1000);
186
// result = exceptions.finishSessionUpload(userId, 1, true, both);// INSERT ONCE MORE
187
// checkResult("THIS ISSUE SHOULD BE A DUPLICATE OF #1", result, 1);//duplicate of issue no 1
188
}
189     
190     private void checkResult(String JavaDoc str, Map JavaDoc<Integer JavaDoc, LogRecord JavaDoc> result, int i){
191         assertNotNull(result);
192         LogRecord JavaDoc rec = result.get(2);
193         assertNotNull("RECORD SHOULD BE NEVER NULL", rec);
194         assertEquals(str, i, Integer.parseInt(rec.getMessage()));
195     }
196     
197     private LogRecord JavaDoc getDataLog(){
198         LogRecord JavaDoc rec = new LogRecord JavaDoc(Level.CONFIG, Exceptions.USER_CONFIGURATION);
199         ArrayList JavaDoc<String JavaDoc> params = new ArrayList JavaDoc<String JavaDoc>(6);
200         params.add("Linux");
201         params.add("HOT SPOT");
202         params.add("IDE DEV");
203         params.add("USER NAME");
204         params.add("SUMMARY");
205         params.add("COMMENT");
206         rec.setResourceBundle(NbBundle.getBundle(ExceptionsTest.class));
207         rec.setParameters(params.toArray());
208         rec.setResourceBundleName("MUJ BUNDLE");
209         return rec;
210     }
211     
212     private LogRecord JavaDoc getThrownLog(){
213         Throwable JavaDoc thrown = new NullPointerException JavaDoc("TESTING EXCEPTION");
214         LogRecord JavaDoc logThrown = new LogRecord JavaDoc(Level.SEVERE, "CHYBA");
215         logThrown.setThrown(thrown);
216         return logThrown;
217     }
218 }
219
Popular Tags