KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > grlea > log > test > TestOfPropertiesLocationOptions


1 package org.grlea.log.test;
2
3 // $Id: TestOfPropertiesLocationOptions.java,v 1.2 2006/07/13 12:44:57 grlea Exp $
4
// Copyright (c) 2004-2006 Graham Lea. All rights reserved.
5

6 // Licensed under the Apache License, Version 2.0 (the "License");
7
// you may not use this file except in compliance with the License.
8
// You may obtain a copy of the License at
9
//
10
// http://www.apache.org/licenses/LICENSE-2.0
11
//
12
// Unless required by applicable law or agreed to in writing, software
13
// distributed under the License is distributed on an "AS IS" BASIS,
14
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
// See the License for the specific language governing permissions and
16
// limitations under the License.
17

18 import org.grlea.log.SimpleLogger;
19
20 import junit.framework.TestCase;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 /**
27  * <p></p>
28  *
29  * @author Graham Lea
30  * @version $Revision: 1.2 $
31  */

32 public class
33 TestOfPropertiesLocationOptions
34 extends TestCase
35 {
36    private static final File JavaDoc workingDirectory = new File JavaDoc("propertiesLocationTests");
37
38    private static final File JavaDoc absoluteFileDir = new File JavaDoc(System.getProperty("java.io.tmpdir"));
39    private static final File JavaDoc absoluteFile = new File JavaDoc(absoluteFileDir, "absoluteSimpleLog.properties");
40
41    private static final String JavaDoc RELATIVE_FILE_DIR_NAME = "config";
42    private static final File JavaDoc relativeFileDir = new File JavaDoc(workingDirectory, RELATIVE_FILE_DIR_NAME);
43    private static final String JavaDoc RELATIVE_PROPERTIES = "relativeSimpleLog.properties";
44    private static final File JavaDoc relativeFile = new File JavaDoc(relativeFileDir, RELATIVE_PROPERTIES);
45
46    private static final File JavaDoc classpathDir = new File JavaDoc(workingDirectory, "classpath");
47    private static final File JavaDoc defaultFile = new File JavaDoc(classpathDir, "simplelog.properties");
48
49    private static final String JavaDoc IN_CLASSPATH_PATH = "inClasspathTest";
50    private static final File JavaDoc inClasspathFileDir = new File JavaDoc(classpathDir, IN_CLASSPATH_PATH);
51    private static final String JavaDoc IN_CLASSPATH_PROPERTIES = "inClasspathSimpleLog.properties";
52    private static final File JavaDoc inClasspathFile = new File JavaDoc(inClasspathFileDir,
53                                                         IN_CLASSPATH_PROPERTIES);
54
55    private static final File JavaDoc outputDir = new File JavaDoc(workingDirectory, "log");
56    private static final File JavaDoc outputFile = new File JavaDoc(outputDir,
57                                                    TestOfPropertiesLocationOptions.class.getName() +
58                                                    ".log");
59
60    public
61    TestOfPropertiesLocationOptions()
62    {}
63
64    protected void
65    setUp()
66    throws Exception JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc logfileLocationPropertyValue)
178    throws Exception JavaDoc
179    {
180       String JavaDoc javaHomeString = System.getProperty("java.home");
181       File JavaDoc javaHome = new File JavaDoc(javaHomeString);
182       File JavaDoc javaBin = new File JavaDoc(javaHome, "bin");
183       File JavaDoc javaExe = new File JavaDoc(javaBin, "java");
184       if (!javaExe.exists())
185          javaExe = new File JavaDoc(javaBin, "java.exe");
186       if (!javaExe.exists())
187          fail("Failed to find java executable in " + javaBin.getAbsolutePath());
188
189       String JavaDoc classpath = System.getProperty("java.class.path");
190       classpath = classpathDir.getAbsolutePath() + File.pathSeparator + classpath;
191
192       String JavaDoc[] commandLine =
193          (logfileLocationPropertyValue == null) ?
194             new String JavaDoc[] {javaExe.getAbsolutePath(), "-cp", classpath, LogGenerator.class.getName()} :
195             new String JavaDoc[] {javaExe.getAbsolutePath(), "-cp", classpath,
196                           "-Dsimplelog.configuration=" + logfileLocationPropertyValue,
197                           LogGenerator.class.getName()};
198
199 // for (int i = 0; i < commandLine.length; i++)
200
// System.out.println("\t" + commandLine[i]);
201

202       Process JavaDoc process = Runtime.getRuntime().exec(commandLine, new String JavaDoc[0], workingDirectory);
203       process.waitFor();
204    }
205
206    private void
207    writeProperties(int defaultLevel, File JavaDoc file)
208    throws Exception JavaDoc
209    {
210       Properties JavaDoc properties = new Properties JavaDoc();
211       properties.setProperty("simplelog.defaultLevel", String.valueOf(defaultLevel));
212       properties.setProperty("simplelog.logFile", outputFile.getAbsolutePath());
213       properties.setProperty("simplelog.logFile.append", "false");
214       FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(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 JavaDoc[] args)
232       {
233 // JOptionPane.showMessageDialog(null, "LogGenerator running!");
234
log.ludicrous("Ludicrous");
235          System.exit(0);
236       }
237    }
238 }
Popular Tags