KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.BufferedOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.concurrent.ExecutionException JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import org.netbeans.junit.Log;
33 import org.netbeans.junit.NbTestCase;
34 import org.netbeans.lib.uihandler.InputGesture;
35 import org.netbeans.server.uihandler.StatisticsTest.PgCnt;
36
37 /**
38  *
39  * @author Jaroslav Tulach
40  */

41 public class LogsManagerTest extends NbTestCase {
42     private LogsManager result;
43     private File JavaDoc logs;
44     
45     
46     public LogsManagerTest(String JavaDoc testName) {
47         super(testName);
48     }
49     
50     @Override JavaDoc
51     protected Level JavaDoc logLevel() {
52         return Level.FINE;
53     }
54     
55     private File JavaDoc logs() throws IOException JavaDoc {
56         File JavaDoc f = new File JavaDoc(getWorkDir(), "logs");
57         f.mkdirs();
58         return f;
59     }
60     
61     
62     protected void setUp() throws Exception JavaDoc {
63         clearWorkDir();
64
65         File JavaDoc toDir = logs();
66         File JavaDoc log = extractResourceAs(toDir, "1.log", "log444");
67         File JavaDoc log2 = extractResourceAs(toDir, "2.log", "log4442");
68         
69         result = LogsManager.createManager(logs());
70     }
71     
72     protected void tearDown() throws Exception JavaDoc {
73     }
74     
75     public void testGetDefaultWhenNoPropsAreSet() throws Exception JavaDoc {
76         CharSequence JavaDoc log = Log.enable("org.netbeans.server.uihandler", Level.WARNING);
77         LogsManager l = LogsManager.getDefault();
78         assertNotNull("Always returns something even no props are set", l);
79         if (log.toString().indexOf("env") == -1) {
80             fail("There should be a warning about env variables:\n" + log);
81         }
82         
83         assertEquals("-1 indicates problems", -1, l.getNumberOfLogs());
84     }
85     
86     
87     public void testGetDefault() throws Exception JavaDoc {
88         doCountsTest(0);
89     }
90     public void testUserInc() throws Exception JavaDoc {
91         doCountsTest(1);
92     }
93     public void testUserSessionChange() throws Exception JavaDoc {
94         doCountsTest(2);
95     }
96     
97     private void doCountsTest(int type) throws Exception JavaDoc {
98         assertNotNull(result);
99         
100         
101         Map JavaDoc<InputGesture,Integer JavaDoc> cnts = null;
102         switch (type) {
103             case 0: cnts = getGlobalCounts(result); break;
104             case 1: cnts = getUserCounts(result, "log444"); break;
105             case 2: cnts = Collections.emptyMap();
106         }
107         
108         assertNotNull(cnts);
109         
110         for (Map.Entry JavaDoc<InputGesture,Integer JavaDoc> e : cnts.entrySet()) {
111             if (e.getValue() <= 0) {
112                 fail("Too less of " + e.getKey() + " = " + e.getValue());
113             }
114         }
115         int numb = result.getNumberOfLogs();
116         
117         File JavaDoc log = extractResourceAs(logs(), "1.log", "log444.1");
118         result.addLog(log);
119         
120         Map JavaDoc<InputGesture,Integer JavaDoc> newCnts = null;
121         switch (type) {
122             case 0: newCnts = getGlobalCounts(result); break;
123             case 1: newCnts = getUserCounts(result, "log444"); break;
124             case 2: newCnts = getLastSessionCounts(result, "log444"); break;
125         }
126         assertEquals("One log added", numb + 1, result.getNumberOfLogs());
127         
128         for (Map.Entry JavaDoc<InputGesture,Integer JavaDoc> e : cnts.entrySet()) {
129             Integer JavaDoc newValue = newCnts.get(e.getKey());
130             if (e.getValue() >= newValue) {
131                 fail("Too small increment of of " + e.getKey() + " = " + e.getValue() + " and " + newValue);
132             }
133         }
134     }
135     
136     static File JavaDoc extractResource(File JavaDoc toDir, String JavaDoc res) throws IOException JavaDoc {
137         return extractResourceAs(toDir, res, res);
138     }
139     
140     static File JavaDoc extractResourceAs(File JavaDoc toDir, String JavaDoc res, String JavaDoc outFile) throws IOException JavaDoc {
141         InputStream JavaDoc is = LogsManagerTest.class.getResourceAsStream(res);
142         File JavaDoc out = new File JavaDoc(toDir, outFile);
143         OutputStream JavaDoc os = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(out));
144         byte[] arr = new byte[4096];
145         for (;;) {
146             int len = is.read(arr);
147             if (-1 == len) break;
148             os.write(arr, 0, len);
149         }
150         os.close();
151         is.close();
152         return out;
153     }
154     
155     @SuppressWarnings JavaDoc("unchecked")
156     private static Map JavaDoc<InputGesture,Integer JavaDoc> getGlobalCounts(LogsManager result) throws InterruptedException JavaDoc, ExecutionException JavaDoc {
157         PgCnt page = new PgCnt();
158         result.preparePageContext(page, null);
159         return (Map JavaDoc<InputGesture, Integer JavaDoc>) page.getAttribute("globalGestures");
160     }
161     @SuppressWarnings JavaDoc("unchecked")
162     private static Map JavaDoc<InputGesture,Integer JavaDoc> getUserCounts(LogsManager result, String JavaDoc id) throws InterruptedException JavaDoc, ExecutionException JavaDoc {
163         PgCnt page = new PgCnt();
164         result.preparePageContext(page, id);
165         return (Map JavaDoc<InputGesture, Integer JavaDoc>) page.getAttribute("userGestures");
166     }
167     @SuppressWarnings JavaDoc("unchecked")
168     private static Map JavaDoc<InputGesture,Integer JavaDoc> getLastSessionCounts(LogsManager result, String JavaDoc id) throws InterruptedException JavaDoc, ExecutionException JavaDoc {
169         PgCnt page = new PgCnt();
170         result.preparePageContext(page, id);
171         return (Map JavaDoc<InputGesture, Integer JavaDoc>) page.getAttribute("lastGestures");
172     }
173 }
174
Popular Tags