1 package org.grlea.log.test; 2 3 6 18 import org.grlea.log.SimpleLogger; 19 20 import junit.framework.TestCase; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.util.Properties ; 25 26 32 public class 33 TestOfPropertiesLocationOptions 34 extends TestCase 35 { 36 private static final File workingDirectory = new File ("propertiesLocationTests"); 37 38 private static final File absoluteFileDir = new File (System.getProperty("java.io.tmpdir")); 39 private static final File absoluteFile = new File (absoluteFileDir, "absoluteSimpleLog.properties"); 40 41 private static final String RELATIVE_FILE_DIR_NAME = "config"; 42 private static final File relativeFileDir = new File (workingDirectory, RELATIVE_FILE_DIR_NAME); 43 private static final String RELATIVE_PROPERTIES = "relativeSimpleLog.properties"; 44 private static final File relativeFile = new File (relativeFileDir, RELATIVE_PROPERTIES); 45 46 private static final File classpathDir = new File (workingDirectory, "classpath"); 47 private static final File defaultFile = new File (classpathDir, "simplelog.properties"); 48 49 private static final String IN_CLASSPATH_PATH = "inClasspathTest"; 50 private static final File inClasspathFileDir = new File (classpathDir, IN_CLASSPATH_PATH); 51 private static final String IN_CLASSPATH_PROPERTIES = "inClasspathSimpleLog.properties"; 52 private static final File inClasspathFile = new File (inClasspathFileDir, 53 IN_CLASSPATH_PROPERTIES); 54 55 private static final File outputDir = new File (workingDirectory, "log"); 56 private static final File outputFile = new File (outputDir, 57 TestOfPropertiesLocationOptions.class.getName() + 58 ".log"); 59 60 public 61 TestOfPropertiesLocationOptions() 62 {} 63 64 protected void 65 setUp() 66 throws Exception 67 { 68 if (!absoluteFileDir.exists()) 69 absoluteFileDir.mkdirs(); 70 71 if (!relativeFileDir.exists()) 72 relativeFileDir.mkdirs(); 73 74 if (!classpathDir.exists()) 75 classpathDir.mkdirs(); 76 77 if (!inClasspathFileDir.exists()) 78 inClasspathFileDir.mkdirs(); 79 80 if (!outputDir.exists()) 81 outputDir.mkdirs(); 82 } 83 84 protected void 85 tearDown() 86 throws Exception 87 { 88 if (absoluteFileDir.exists()) 89 absoluteFileDir.delete(); 90 91 if (relativeFileDir.exists()) 92 relativeFileDir.delete(); 93 94 if (classpathDir.exists()) 95 classpathDir.delete(); 96 97 if (outputDir.exists()) 98 outputDir.delete(); 99 } 100 101 public void 102 testZeroOutput() 103 throws Exception 104 { 105 writeProperties(0, absoluteFile); 106 writeProperties(0, relativeFile); 107 writeProperties(0, defaultFile); 108 writeProperties(0, inClasspathFile); 109 110 runLogGenerator(null); 111 112 long logFileSize = outputFile.length(); 113 assertEquals("logFileSize > 0", false, logFileSize > 0); 114 } 115 116 public void 117 testDefaultLocation() 118 throws Exception 119 { 120 writeProperties(0, absoluteFile); 121 writeProperties(0, relativeFile); 122 writeProperties(7, defaultFile); 123 writeProperties(0, inClasspathFile); 124 125 runLogGenerator(null); 126 127 long logFileSize = outputFile.length(); 128 assertEquals("logFileSize > 0", true, logFileSize > 0); 129 } 130 131 public void 132 testAbsolutePropetiesLocation() 133 throws Exception 134 { 135 writeProperties(7, absoluteFile); 136 writeProperties(0, relativeFile); 137 writeProperties(0, defaultFile); 138 writeProperties(0, inClasspathFile); 139 140 runLogGenerator("file:" + absoluteFile.getAbsolutePath()); 141 142 long logFileSize = outputFile.length(); 143 assertEquals("logFileSize > 0", true, logFileSize > 0); 144 } 145 146 public void 147 testRelativePropetiesLocation() 148 throws Exception 149 { 150 writeProperties(0, absoluteFile); 151 writeProperties(7, relativeFile); 152 writeProperties(0, defaultFile); 153 writeProperties(0, inClasspathFile); 154 155 runLogGenerator("file:" + RELATIVE_FILE_DIR_NAME + File.separator + RELATIVE_PROPERTIES); 156 157 long logFileSize = outputFile.length(); 158 assertEquals("logFileSize > 0", true, logFileSize > 0); 159 } 160 161 public void 162 testInClasspathPropetiesLocation() 163 throws Exception 164 { 165 writeProperties(0, absoluteFile); 166 writeProperties(0, relativeFile); 167 writeProperties(0, defaultFile); 168 writeProperties(7, inClasspathFile); 169 170 runLogGenerator("classpath:" + IN_CLASSPATH_PATH + File.separator + IN_CLASSPATH_PROPERTIES); 171 172 long logFileSize = outputFile.length(); 173 assertEquals("logFileSize > 0", true, logFileSize > 0); 174 } 175 176 private void 177 runLogGenerator(String logfileLocationPropertyValue) 178 throws Exception 179 { 180 String javaHomeString = System.getProperty("java.home"); 181 File javaHome = new File (javaHomeString); 182 File javaBin = new File (javaHome, "bin"); 183 File javaExe = new File (javaBin, "java"); 184 if (!javaExe.exists()) 185 javaExe = new File (javaBin, "java.exe"); 186 if (!javaExe.exists()) 187 fail("Failed to find java executable in " + javaBin.getAbsolutePath()); 188 189 String classpath = System.getProperty("java.class.path"); 190 classpath = classpathDir.getAbsolutePath() + File.pathSeparator + classpath; 191 192 String [] commandLine = 193 (logfileLocationPropertyValue == null) ? 194 new String [] {javaExe.getAbsolutePath(), "-cp", classpath, LogGenerator.class.getName()} : 195 new String [] {javaExe.getAbsolutePath(), "-cp", classpath, 196 "-Dsimplelog.configuration=" + logfileLocationPropertyValue, 197 LogGenerator.class.getName()}; 198 199 202 Process process = Runtime.getRuntime().exec(commandLine, new String [0], workingDirectory); 203 process.waitFor(); 204 } 205 206 private void 207 writeProperties(int defaultLevel, File file) 208 throws Exception 209 { 210 Properties properties = new Properties (); 211 properties.setProperty("simplelog.defaultLevel", String.valueOf(defaultLevel)); 212 properties.setProperty("simplelog.logFile", outputFile.getAbsolutePath()); 213 properties.setProperty("simplelog.logFile.append", "false"); 214 FileOutputStream out = new FileOutputStream (file); 215 try 216 { 217 properties.store(out, ""); 218 } 219 finally 220 { 221 out.close(); 222 } 223 } 224 225 public static final class 226 LogGenerator 227 { 228 private static final SimpleLogger log = new SimpleLogger(LogGenerator.class); 229 230 public static void 231 main(String [] args) 232 { 233 log.ludicrous("Ludicrous"); 235 System.exit(0); 236 } 237 } 238 } | Popular Tags |