KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > CLILoggerTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.cli.framework;
25
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.LogRecord JavaDoc;
31 import junit.framework.*;
32 /**
33  *
34  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
35  * @version $Revision: 1.4 $
36  */

37
38 public class CLILoggerTest extends TestCase {
39     private static final String JavaDoc ls = System.getProperty("line.separator");
40     private static final String JavaDoc m = "a message";
41     private static final CLILogger log = CLILogger.getInstance();
42
43     public void testPrintExceptionStackTrace(){
44         log.setOutputLevel(Level.FINEST);
45         final Throwable JavaDoc t = new Throwable JavaDoc(m);
46         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
47         final PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
48         t.printStackTrace(pw);
49         log.printExceptionStackTrace(t);
50         assertEquals(sw.toString() + ls, out.toString());
51         assertEquals("", err.toString());
52     }
53     
54         
55     public void testPrintDebugMessage() {
56         log.setOutputLevel(Level.FINEST);
57         log.printDebugMessage(m);
58         assertEquals("", err.toString());
59         assertEquals(m + ls, out.toString());
60     }
61
62     public void testPrintWarning(){
63         log.setOutputLevel(Level.FINEST);
64         log.printWarning(m);
65         assertEquals(m + ls, out.toString());
66         assertEquals("", err.toString());
67     }
68         
69     public void testPrintDetailMessage(){
70         log.setOutputLevel(Level.FINEST);
71         log.printDetailMessage(m);
72         assertEquals(m + ls, out.toString());
73         assertEquals("", err.toString());
74     }
75     
76         
77     public void testLevelsBlockOutput(){
78         log.setOutputLevel(Level.SEVERE);
79         log.printMessage(m);
80         assertEquals("", out.toString());
81         assertEquals("", err.toString());
82         log.setOutputLevel(Level.INFO);
83         log.printMessage(m);
84         assertEquals(m + ls, out.toString());
85         assertEquals("", err.toString());
86     }
87         
88     public void testErrorLogging(){
89         log.printError(m);
90         assertEquals(m + ls, err.toString());
91         assertEquals("", out.toString());
92     }
93         
94     public void testBasicLogging(){
95         log.printMessage(m);
96         assertEquals(m + ls, out.toString());
97         assertEquals("", err.toString());
98     }
99     public void testSetLevelDoesntWorkUnderDebug(){
100         assertEquals(Level.INFO, log.getOutputLevel());
101         System.setProperty("Debug", "on");
102         log.setOutputLevel(Level.SEVERE);
103         System.getProperties().remove("Debug");
104         assertTrue(Level.SEVERE != log.getOutputLevel());
105         assertEquals(Level.INFO, log.getOutputLevel());
106     }
107     
108     public void testLevelGetSet() {
109         assertEquals(Level.INFO, log.getOutputLevel());
110         log.setOutputLevel(Level.SEVERE);
111         assertEquals(Level.SEVERE, log.getOutputLevel());
112         
113     }
114     
115         public CLILoggerTest(String JavaDoc name){
116         super(name);
117     }
118         ByteArrayOutputStream JavaDoc err;
119         ByteArrayOutputStream JavaDoc out;
120         InputsAndOutputs io;
121
122     protected void setUp() {
123         log.setOutputLevel(Level.INFO);
124         err = new ByteArrayOutputStream JavaDoc();
125         out = new ByteArrayOutputStream JavaDoc();
126         io = InputsAndOutputs.getInstance();
127         io.setErrorOutput(err);
128         io.setUserOutput(out);
129     }
130
131
132
133     protected void tearDown() {
134     }
135
136     private void nyi(){
137         fail("Not Yet Implemented");
138     }
139
140     public static void main(String JavaDoc args[]){
141         if (args.length == 0){
142             junit.textui.TestRunner.run(CLILoggerTest.class);
143         } else {
144             junit.textui.TestRunner.run(makeSuite(args));
145         }
146     }
147     private static TestSuite makeSuite(String JavaDoc args[]){
148         final TestSuite ts = new TestSuite();
149         for (int i = 0; i < args.length; i++){
150             ts.addTest(new CLILoggerTest(args[i]));
151         }
152         return ts;
153     }
154 }
155
Popular Tags