1 4 5 package org.objectweb.util.monolog; 6 7 import org.objectweb.util.monolog.api.BasicLevel; 8 import org.objectweb.util.monolog.api.Handler; 9 import org.objectweb.util.monolog.api.TopicalLogger; 10 import org.objectweb.util.monolog.api.MonologFactory; 11 12 import java.util.Properties ; 13 14 20 public class TestConfigurability extends TestHelper { 21 22 27 public static void main(String args[]) { 28 if (args.length < 2) { 29 System.out.println("Syntax error !"); 30 System.out.println( 31 "java TestConfigurability <logger factory class name> <mode> " 32 + "[<config file name> [ true | false ]]"); 33 System.exit(1); 34 } 35 int size = args.length; 36 String [] params = new String [size]; 37 String [] methods = new String [size]; 38 params[0] = args[1]; 39 methods[0] = "setConfigMode"; 40 if (args.length > 2) { 41 params[1] = args[2]; 42 methods[1] = "setConfigFile"; 43 } 44 if (args.length > 3) { 45 params[2] = args[3]; 46 methods[2] = "setUseClassPath"; 47 } 48 params[params.length - 1] = ""; 49 methods[methods.length - 1] = "init"; 50 TestHelper.run(TestConfigurability.class, methods, params, args[0]); 51 } 52 53 56 public static TestSuite getTestSuite(String lfcn, String mode, 57 String configFile, String useCP) { 58 int size = 1 + (mode != null ? 1 : 0) + (configFile != null ? 1 : 0) 59 + (useCP != null ? 1 : 0); 60 61 String [] params = new String [size]; 62 String [] methods = new String [size]; 63 params[0] = mode; 64 methods[0] = "setConfigMode"; 65 if (configFile != null) { 66 params[1] = configFile; 67 methods[1] = "setConfigFile"; 68 } 69 if (useCP != null) { 70 params[2] = useCP; 71 methods[2] = "setUseClassPath"; 72 } 73 params[params.length - 1] = ""; 74 methods[methods.length - 1] = "init"; 75 76 return TestHelper.getTestSuite(TestLogger.class, methods, params, lfcn); 77 } 78 79 String mode = null; 80 String configFileName = null; 81 String useClassPath = null; 82 83 public void setConfigMode(String configMode) { 84 mode = configMode; 85 } 86 87 public void setConfigFile(String configFile) { 88 configFileName = configFile; 89 } 90 91 public void setUseClassPath(String useclasspath) { 92 useClassPath = useclasspath; 93 } 94 95 98 public void init(String unused) { 99 100 Properties prop = null; 101 102 if (mode != null && !mode.equalsIgnoreCase("null")) { 103 prop = new Properties (); 104 prop.put(MonologFactory.LOG_CONFIGURATION_TYPE, mode); 105 if (configFileName != null) { 106 prop.put(MonologFactory.LOG_CONFIGURATION_FILE, configFileName); 107 } 108 109 if (useClassPath != null) { 110 prop.put(MonologFactory.LOG_CONFIGURATION_FILE_USE_CLASSPATH, 111 useClassPath); 112 } 113 debug("Test the configurability with in " + mode + " mode" 114 + (useClassPath != null ? 115 ", use of the classpath: " + useClassPath 116 : "")); 117 } 118 try { 119 ((MonologFactory) lf).configure(prop); 120 } 121 catch (Exception e) { 122 fail("Impossible to configure in " + mode + " mode: " 123 + e.getMessage()); 124 } 125 } 126 127 public void testSimple() { 128 quietRootLogger(); 129 TopicalLogger l = (TopicalLogger) 130 lf.getLogger("test.configurability.simple"); 131 Handler hc = 132 hf.createHandler("myhandler_configurability", "file"); 133 hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, "test.log"); 134 hc.setAttribute(Handler.PATTERN_ATTRIBUTE, "%m%n"); 135 hc.setAttribute("activation", lf); 136 try { 137 l.addHandler(hc); 138 } 139 catch (Exception e) { 140 fail(e.getMessage()); 141 } 142 l.setIntLevel(BasicLevel.DEBUG); 143 String str = "configurability mode " + mode + " " + useClassPath; 144 l.log(BasicLevel.DEBUG, str); 145 String [] found = getLastLines("test.log", 1); 146 assertNotNull("TestHelper error", found); 147 assertNotNull("TestHelper error", found[0]); 148 assertTrue("no log in collocated Handler", found[0].endsWith(str)); 149 } 150 } 151 | Popular Tags |