001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.environment.commandline.test;
018:
019: import org.apache.avalon.framework.logger.ConsoleLogger;
020: import org.apache.avalon.framework.logger.Logger;
021:
022: import org.apache.cocoon.environment.commandline.CommandLineContext;
023:
024: import junit.framework.TestCase;
025: import junit.swingui.TestRunner;
026:
027: import java.io.File;
028: import java.net.URL;
029:
030: /**
031: * A simple test case for CommandLineContext.
032: *
033: * @author <a href="mailto:berni_huber@a1.net">Bernhard Huber</a>
034: * @version CVS $Id: CommandLineContextTestCase.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036: public final class CommandLineContextTestCase extends TestCase {
037:
038: private String commandLineContextDir;
039: private CommandLineContext commandLineContext;
040:
041: /**
042: * Constructor for the CommandLineContextTestCase object
043: */
044: public CommandLineContextTestCase() {
045: this ("CommandLineContextTestCase");
046: }
047:
048: /**
049: * Constructor for the CommandLineContextTestCase object
050: */
051: public CommandLineContextTestCase(String name) {
052: super (name);
053: }
054:
055: /**
056: * The main program for the CommandLineContextTestCase class
057: *
058: * @param args The command line arguments
059: */
060: public static void main(final String[] args) throws Exception {
061: final String[] testCaseName = { CommandLineContextTestCase.class
062: .getName() };
063: TestRunner.main(testCaseName);
064: }
065:
066: /**
067: * The JUnit setup method
068: */
069: public void setUp() throws Exception {
070: super .setUp();
071: commandLineContextDir = System.getProperty("java.io.tmpdir",
072: "/tmp");
073: new File(commandLineContextDir, "foo" + File.separator + "bar")
074: .mkdirs();
075:
076: String level = System.getProperty("junit.test.loglevel", ""
077: + ConsoleLogger.LEVEL_DEBUG);
078: Logger logger = new ConsoleLogger(Integer.parseInt(level));
079:
080: commandLineContext = new CommandLineContext(
081: commandLineContextDir);
082: commandLineContext.enableLogging(logger);
083: }
084:
085: /**
086: * The teardown method for JUnit
087: */
088: public void tearDown() throws Exception {
089: super .tearDown();
090: new File(commandLineContextDir, "foo" + File.separator + "bar")
091: .delete();
092: new File(commandLineContextDir, "foo").delete();
093: }
094:
095: /**
096: * A unit test for <code>getResource()</code>
097: */
098: public void testGetResource() throws Exception {
099: Object[] test_values = {
100: new String[] { "", commandLineContextDir },
101: new String[] { "/", commandLineContextDir },
102: new String[] { "foo",
103: commandLineContextDir + File.separator + "foo" },
104: new String[] {
105: "foo/bar",
106: commandLineContextDir + File.separator
107: + "foo/bar" },
108: new String[] { "foo/bar/nonexistent", null } };
109: for (int i = 0; i < test_values.length; i++) {
110: String tests[] = (String[]) test_values[i];
111: String test = tests[0];
112: URL result = commandLineContext.getResource(test);
113: URL expected = null;
114: if (result != null) {
115: expected = new File(tests[1]).toURL();
116: }
117: String message = "Test " + "'" + test + "'";
118: assertEquals(message, expected, result);
119: }
120: }
121:
122: /**
123: * A unit test for <code>getRealPath()</code>
124: */
125: public void testGetRealPath() throws Exception {
126: Object[] test_values = { new String[] { "", "" },
127: new String[] { "/", "/" },
128: new String[] { "foo", "foo" },
129: new String[] { "foo/bar", "foo/bar" } };
130: for (int i = 0; i < test_values.length; i++) {
131: String tests[] = (String[]) test_values[i];
132: String test = tests[0];
133: File expected_file = new File(commandLineContextDir,
134: tests[1]);
135: String expected = expected_file.getAbsolutePath();
136:
137: String result = commandLineContext.getRealPath(test);
138: String message = "Test " + "'" + test + "'";
139: assertEquals(message, expected, result);
140: }
141: }
142:
143: /**
144: * A unit test for <code>getAttribute</code>,
145: * <code>setAttribute</code>, and <code>removeAttribute()</code>
146: */
147: public void testAttributes() throws Exception {
148: Object[] test_values = { new String[] { "a", "b" },
149: new String[] { "foo", "foo" },
150: new String[] { "foo/bar", "foo/bar" } };
151: for (int i = 0; i < test_values.length; i++) {
152: String tests[] = (String[]) test_values[i];
153: String name = tests[0];
154: String expected = tests[1];
155:
156: commandLineContext.setAttribute(name, expected);
157:
158: String result = (String) commandLineContext
159: .getAttribute(name);
160: assertEquals("Test " + "'" + "n" + "'", expected, result);
161:
162: commandLineContext.removeAttribute(name);
163: result = (String) commandLineContext.getAttribute(name);
164: assertEquals("Test " + "'" + "<null>" + "'", null, result);
165: }
166: }
167: }
|