KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > TopLoggingOwnConfigTest


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 package org.netbeans.core.startup;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.FileWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.PrintStream JavaDoc;
28 import java.util.logging.Handler JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.LogManager JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import java.util.logging.StreamHandler JavaDoc;
33 import java.util.logging.XMLFormatter JavaDoc;
34 import java.util.regex.Matcher JavaDoc;
35 import java.util.regex.Pattern JavaDoc;
36 import org.netbeans.junit.NbTestCase;
37
38
39 /**
40  * Checks the behaviour of NetBeans logging support.
41  */

42 public class TopLoggingOwnConfigTest extends NbTestCase {
43     private File JavaDoc log;
44     
45     public TopLoggingOwnConfigTest(String JavaDoc testName) {
46         super(testName);
47     }
48
49     protected void setUp() throws Exception JavaDoc {
50         clearWorkDir();
51
52         System.setProperty("netbeans.user", getWorkDirPath());
53
54         log = new File JavaDoc(getWorkDir(), "own.log");
55         File JavaDoc cfg = new File JavaDoc(getWorkDir(), "cfg");
56
57         FileWriter JavaDoc w = new FileWriter JavaDoc(cfg);
58         w.write("handlers=java.util.logging.FileHandler\n");
59         w.write(".level=100\n");
60         w.write("java.util.logging.FileHandler.pattern=" + log.toString().replace('\\', '/') +"\n");
61         w.close();
62
63
64         System.setProperty("java.util.logging.config.file", cfg.getPath());
65
66         // initialize logging
67
TopLogging.initialize();
68     }
69
70     protected void tearDown() throws Exception JavaDoc {
71     }
72
73
74     public void testLogOneLine() throws Exception JavaDoc {
75         Logger.getLogger(TopLoggingTest.class.getName()).log(Level.FINER, "First visible message");
76
77         String JavaDoc content = readLog();
78         if (content.indexOf("<!DOCTYPE") == -1) {
79             fail("Content must be XML based: " + content);
80         }
81
82         if (content.indexOf("First vis") == -1) {
83             fail("It must contain our log message: " + content);
84         }
85     }
86
87     private String JavaDoc readLog() throws IOException JavaDoc {
88         LogManager.getLogManager().reset();
89
90         assertTrue("Log file exists: " + log, log.canRead());
91
92         FileInputStream JavaDoc is = new FileInputStream JavaDoc(log);
93
94         byte[] arr = new byte[(int)log.length()];
95         int r = is.read(arr);
96         assertEquals("all read", arr.length, r);
97         is.close();
98
99         return new String JavaDoc(arr);
100     }
101 }
102
Popular Tags