1 23 24 package com.sun.enterprise.cli.framework; 25 26 import java.io.ByteArrayOutputStream ; 27 import java.io.PrintWriter ; 28 import java.io.StringWriter ; 29 import java.util.logging.Level ; 30 import java.util.logging.LogRecord ; 31 import junit.framework.*; 32 37 38 public class CLILoggerTest extends TestCase { 39 private static final String ls = System.getProperty("line.separator"); 40 private static final String m = "a message"; 41 private static final CLILogger log = CLILogger.getInstance(); 42 43 public void testPrintExceptionStackTrace(){ 44 log.setOutputLevel(Level.FINEST); 45 final Throwable t = new Throwable (m); 46 final StringWriter sw = new StringWriter (); 47 final PrintWriter pw = new PrintWriter (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 name){ 116 super(name); 117 } 118 ByteArrayOutputStream err; 119 ByteArrayOutputStream out; 120 InputsAndOutputs io; 121 122 protected void setUp() { 123 log.setOutputLevel(Level.INFO); 124 err = new ByteArrayOutputStream (); 125 out = new ByteArrayOutputStream (); 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 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 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 |