KickJava   Java API By Example, From Geeks To Geeks.

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


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

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