1 17 18 package org.netbeans.server.uihandler; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.sql.DriverManager ; 23 import java.util.Enumeration ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.logging.Level ; 27 import java.util.logging.LogRecord ; 28 import javax.servlet.Servlet ; 29 import javax.servlet.ServletConfig ; 30 import javax.servlet.ServletContext ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.ServletRequest ; 33 import javax.servlet.ServletResponse ; 34 import javax.servlet.http.HttpSession ; 35 import javax.servlet.jsp.JspWriter ; 36 import javax.servlet.jsp.PageContext ; 37 import javax.servlet.jsp.el.ExpressionEvaluator ; 38 import javax.servlet.jsp.el.VariableResolver ; 39 import org.netbeans.junit.MockServices; 40 import org.netbeans.junit.NbTestCase; 41 42 46 public class StatisticsTest extends NbTestCase { 47 private LogsManager result; 48 private File logs; 49 50 51 public StatisticsTest(String testName) { 52 super(testName); 53 } 54 55 @Override 56 protected Level logLevel() { 57 return Level.FINE; 58 } 59 60 private File logs() throws IOException { 61 File f = new File (getWorkDir(), "logs"); 62 f.mkdirs(); 63 return f; 64 } 65 66 67 protected void setUp() throws Exception { 68 clearWorkDir(); 69 70 MockServices.setServices(CntngStat.class); 71 File log = LogsManagerTest.extractResourceAs(logs(), "1.log", "log444"); 72 73 result = LogsManager.createManager(logs()); 74 } 75 76 protected void tearDown() throws Exception { 77 } 78 79 public void testHowManyLogsInALog() throws Exception { 80 PageContext cnt = new PgCnt(); 81 result.preparePageContext(cnt, "log444"); 82 83 assertTrue(CntngStat.finishSessionUploadCalled); 84 assertEquals("log444", CntngStat.finishSessionUploadId); 85 assertEquals("first session", 0, CntngStat.finishSessionUploadSession); 86 assertEquals("this was not initial parsing", false, CntngStat.finishSessionUploadInitialParse); 87 CntngStat.finishSessionUploadCalled = false; 88 89 Object obj = cnt.getAttribute("globalCnts"); 90 assertNotNull("Data are there", obj); 91 assertEquals(Integer .class, obj.getClass()); 92 assertEquals(18, obj); 93 94 obj = cnt.getAttribute("lastCnts"); 95 assertNotNull("Last Data are there", obj); 96 assertEquals(Integer .class, obj.getClass()); 97 assertEquals(18, obj); 98 99 obj = cnt.getAttribute("userCnts"); 100 assertNotNull("User Data are there", obj); 101 assertEquals(Integer .class, obj.getClass()); 102 assertEquals(18, obj); 103 104 File newLog = LogsManagerTest.extractResourceAs(logs(), "1.log", "log444.1"); 105 106 result.addLog(newLog); 107 108 result.preparePageContext(cnt, "log444"); 109 110 assertTrue(CntngStat.finishSessionUploadCalled); 111 assertEquals("log444", CntngStat.finishSessionUploadId); 112 assertEquals("2nd session", 1, CntngStat.finishSessionUploadSession); 113 assertEquals("this was initial parsing", true, CntngStat.finishSessionUploadInitialParse); 114 CntngStat.finishSessionUploadCalled = false; 115 116 obj = cnt.getAttribute("globalCnts"); 117 assertNotNull("Data are there", obj); 118 assertEquals(Integer .class, obj.getClass()); 119 assertEquals(36, obj); 120 121 obj = cnt.getAttribute("lastCnts"); 122 assertNotNull("Last Data are there", obj); 123 assertEquals(Integer .class, obj.getClass()); 124 assertEquals(18, obj); 125 126 obj = cnt.getAttribute("userCnts"); 127 assertNotNull("User Data are there", obj); 128 assertEquals(Integer .class, obj.getClass()); 129 assertEquals(36, obj); 130 131 132 File newUser = LogsManagerTest.extractResourceAs(logs(), "1.log", "log337"); 133 134 result.addLog(newUser); 135 136 result.preparePageContext(cnt, "log337"); 137 138 assertTrue(CntngStat.finishSessionUploadCalled); 139 assertEquals("log337", CntngStat.finishSessionUploadId); 140 assertEquals("1st session", 0, CntngStat.finishSessionUploadSession); 141 assertEquals("this was initial parsing", true, CntngStat.finishSessionUploadInitialParse); 142 CntngStat.finishSessionUploadCalled = false; 143 144 145 obj = cnt.getAttribute("globalCnts"); 146 assertNotNull("Data are there", obj); 147 assertEquals(Integer .class, obj.getClass()); 148 assertEquals(54, obj); 149 150 obj = cnt.getAttribute("lastCnts"); 151 assertNotNull("Last Data are there", obj); 152 assertEquals(Integer .class, obj.getClass()); 153 assertEquals(18, obj); 154 155 obj = cnt.getAttribute("userCnts"); 156 assertNotNull("User Data are there", obj); 157 assertEquals(Integer .class, obj.getClass()); 158 assertEquals(18, obj); 159 } 160 161 public static final class CntngStat extends Statistics<Integer > { 162 private static boolean finishSessionUploadCalled; 163 private static boolean finishSessionUploadInitialParse; 164 private static String finishSessionUploadId; 165 private static int finishSessionUploadSession; 166 167 168 public CntngStat() { 169 super("Cnts"); 170 } 171 172 protected Integer newData() { 173 return 0; 174 } 175 176 protected Integer process(LogRecord rec) { 177 return 1; 178 } 179 180 protected Integer join(Integer one, Integer two) { 181 return one + two; 182 } 183 184 protected Integer finishSessionUpload( 185 String userId, int sessionNumber, 186 boolean initialParse, Integer d 187 ) { 188 assertFalse("Not yet called", finishSessionUploadCalled); 189 finishSessionUploadCalled = true; 190 finishSessionUploadId = userId; 191 finishSessionUploadSession = sessionNumber; 192 finishSessionUploadInitialParse = initialParse; 193 return d; 194 } 195 } 197 static final class PgCnt extends PageContext { 198 private Map <String ,Object > attr = new HashMap <String , Object >(); 199 200 201 public void initialize(Servlet arg0, ServletRequest arg1, 202 ServletResponse arg2, String arg3, boolean arg4, 203 int arg5, boolean arg6) throws IOException , 204 IllegalStateException , 205 IllegalArgumentException { 206 throw new UnsupportedOperationException ("Not supported yet."); 207 } 208 209 public void release() { 210 throw new UnsupportedOperationException ("Not supported yet."); 211 } 212 213 public HttpSession getSession() { 214 throw new UnsupportedOperationException ("Not supported yet."); 215 } 216 217 public Object getPage() { 218 throw new UnsupportedOperationException ("Not supported yet."); 219 } 220 221 public ServletRequest getRequest() { 222 throw new UnsupportedOperationException ("Not supported yet."); 223 } 224 225 public ServletResponse getResponse() { 226 throw new UnsupportedOperationException ("Not supported yet."); 227 } 228 229 public Exception getException() { 230 throw new UnsupportedOperationException ("Not supported yet."); 231 } 232 233 public ServletConfig getServletConfig() { 234 throw new UnsupportedOperationException ("Not supported yet."); 235 } 236 237 public ServletContext getServletContext() { 238 throw new UnsupportedOperationException ("Not supported yet."); 239 } 240 241 public void forward(String arg0) throws ServletException , IOException { 242 throw new UnsupportedOperationException ("Not supported yet."); 243 } 244 245 public void include(String arg0) throws ServletException , IOException { 246 throw new UnsupportedOperationException ("Not supported yet."); 247 } 248 249 public void include(String arg0, boolean arg1) throws ServletException , 250 IOException { 251 throw new UnsupportedOperationException ("Not supported yet."); 252 } 253 254 public void handlePageException(Exception arg0) throws ServletException , 255 IOException { 256 throw new UnsupportedOperationException ("Not supported yet."); 257 } 258 259 public void handlePageException(Throwable arg0) throws ServletException , 260 IOException { 261 throw new UnsupportedOperationException ("Not supported yet."); 262 } 263 264 public void setAttribute(String arg0, Object arg1) { 265 attr.put(arg0, arg1); 266 } 267 268 public void setAttribute(String arg0, Object arg1, int arg2) { 269 throw new UnsupportedOperationException ("Not supported yet."); 270 } 271 272 public Object getAttribute(String arg0) { 273 return attr.get(arg0); 274 } 275 276 public Object getAttribute(String arg0, int arg1) { 277 throw new UnsupportedOperationException ("Not supported yet."); 278 } 279 280 public Object findAttribute(String arg0) { 281 throw new UnsupportedOperationException ("Not supported yet."); 282 } 283 284 public void removeAttribute(String arg0) { 285 throw new UnsupportedOperationException ("Not supported yet."); 286 } 287 288 public void removeAttribute(String arg0, int arg1) { 289 throw new UnsupportedOperationException ("Not supported yet."); 290 } 291 292 public int getAttributesScope(String arg0) { 293 throw new UnsupportedOperationException ("Not supported yet."); 294 } 295 296 public Enumeration getAttributeNamesInScope(int arg0) { 297 throw new UnsupportedOperationException ("Not supported yet."); 298 } 299 300 public JspWriter getOut() { 301 throw new UnsupportedOperationException ("Not supported yet."); 302 } 303 304 public ExpressionEvaluator getExpressionEvaluator() { 305 throw new UnsupportedOperationException ("Not supported yet."); 306 } 307 308 public VariableResolver getVariableResolver() { 309 throw new UnsupportedOperationException ("Not supported yet."); 310 } 311 } 312 } 313 | Popular Tags |