KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > commandline > test > CommandLineContextTestCase


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

16 package org.apache.cocoon.environment.commandline.test;
17
18 import org.apache.avalon.framework.logger.ConsoleLogger;
19 import org.apache.avalon.framework.logger.Logger;
20
21 import org.apache.cocoon.environment.commandline.CommandLineContext;
22
23 import junit.framework.TestCase;
24 import junit.swingui.TestRunner;
25
26 import java.io.File JavaDoc;
27 import java.net.URL JavaDoc;
28
29 /**
30  * A simple test case for CommandLineContext.
31  *
32  * @author <a HREF="mailto:berni_huber@a1.net">Bernhard Huber</a>
33  * @version CVS $Id: CommandLineContextTestCase.java 124189 2005-01-05 08:50:15Z antonio $
34  */

35 public final class CommandLineContextTestCase extends TestCase {
36
37     private String JavaDoc commandLineContextDir;
38     private CommandLineContext commandLineContext;
39
40
41     /**
42      * Constructor for the CommandLineContextTestCase object
43      */

44     public CommandLineContextTestCase() {
45         this("CommandLineContextTestCase");
46     }
47
48     /**
49      * Constructor for the CommandLineContextTestCase object
50      */

51     public CommandLineContextTestCase(String JavaDoc name) {
52         super(name);
53     }
54
55     /**
56      * The main program for the CommandLineContextTestCase class
57      *
58      * @param args The command line arguments
59      */

60     public static void main(final String JavaDoc[] args) throws Exception JavaDoc {
61         final String JavaDoc[] testCaseName = {CommandLineContextTestCase.class.getName()};
62         TestRunner.main(testCaseName);
63     }
64
65
66     /**
67      * The JUnit setup method
68      */

69     public void setUp() throws Exception JavaDoc {
70         super.setUp();
71         commandLineContextDir = System.getProperty("java.io.tmpdir", "/tmp");
72         new File JavaDoc(commandLineContextDir, "foo" + File.separator + "bar").mkdirs();
73
74         String JavaDoc level = System.getProperty("junit.test.loglevel", "" + ConsoleLogger.LEVEL_DEBUG);
75         Logger logger = new ConsoleLogger(Integer.parseInt(level));
76
77         commandLineContext = new CommandLineContext(commandLineContextDir);
78         commandLineContext.enableLogging(logger);
79     }
80
81     /**
82      * The teardown method for JUnit
83      */

84     public void tearDown() throws Exception JavaDoc {
85         super.tearDown();
86         new File JavaDoc(commandLineContextDir, "foo" + File.separator + "bar").delete();
87         new File JavaDoc(commandLineContextDir, "foo").delete();
88     }
89
90     /**
91      * A unit test for <code>getResource()</code>
92      */

93     public void testGetResource() throws Exception JavaDoc {
94         Object JavaDoc[] test_values = {
95                 new String JavaDoc[]{"", commandLineContextDir},
96                 new String JavaDoc[]{"/", commandLineContextDir},
97                 new String JavaDoc[]{"foo", commandLineContextDir + File.separator + "foo"},
98                 new String JavaDoc[]{"foo/bar", commandLineContextDir + File.separator + "foo/bar"},
99                 new String JavaDoc[]{"foo/bar/nonexistent", null}
100                 };
101         for (int i = 0; i < test_values.length; i++) {
102             String JavaDoc tests[] = (String JavaDoc[]) test_values[i];
103             String JavaDoc test = tests[0];
104             URL JavaDoc result = commandLineContext.getResource(test);
105             URL JavaDoc expected = null;
106             if (result != null) {
107                 expected = new File JavaDoc(tests[1]).toURL();
108             }
109             String JavaDoc message = "Test " + "'" + test + "'";
110             assertEquals(message, expected, result);
111         }
112     }
113
114     /**
115      * A unit test for <code>getRealPath()</code>
116      */

117     public void testGetRealPath() throws Exception JavaDoc {
118         Object JavaDoc[] test_values = {
119                 new String JavaDoc[]{"", ""},
120                 new String JavaDoc[]{"/", "/"},
121                 new String JavaDoc[]{"foo", "foo"},
122                 new String JavaDoc[]{"foo/bar", "foo/bar"}
123                 };
124         for (int i = 0; i < test_values.length; i++) {
125             String JavaDoc tests[] = (String JavaDoc[]) test_values[i];
126             String JavaDoc test = tests[0];
127             File JavaDoc expected_file = new File JavaDoc(commandLineContextDir, tests[1]);
128             String JavaDoc expected = expected_file.getAbsolutePath();
129
130             String JavaDoc result = commandLineContext.getRealPath(test);
131             String JavaDoc message = "Test " +
132                     "'" + test + "'";
133             assertEquals(message, expected, result);
134         }
135     }
136
137     /**
138      * A unit test for <code>getAttribute</code>,
139      * <code>setAttribute</code>, and <code>removeAttribute()</code>
140      */

141     public void testAttributes() throws Exception JavaDoc {
142         Object JavaDoc[] test_values = {
143                 new String JavaDoc[]{"a", "b"},
144                 new String JavaDoc[]{"foo", "foo"},
145                 new String JavaDoc[]{"foo/bar", "foo/bar"}
146                 };
147         for (int i = 0; i < test_values.length; i++) {
148             String JavaDoc tests[] = (String JavaDoc[]) test_values[i];
149             String JavaDoc name = tests[0];
150             String JavaDoc expected = tests[1];
151
152             commandLineContext.setAttribute(name, expected);
153
154             String JavaDoc result = (String JavaDoc) commandLineContext.getAttribute(name);
155             assertEquals("Test " + "'" + "n" + "'", expected, result);
156
157             commandLineContext.removeAttribute(name);
158             result = (String JavaDoc) commandLineContext.getAttribute(name);
159             assertEquals("Test " + "'" + "<null>" + "'", null, result);
160         }
161     }
162 }
163
Popular Tags