1 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 ; 27 import java.net.URL ; 28 29 35 public final class CommandLineContextTestCase extends TestCase { 36 37 private String commandLineContextDir; 38 private CommandLineContext commandLineContext; 39 40 41 44 public CommandLineContextTestCase() { 45 this("CommandLineContextTestCase"); 46 } 47 48 51 public CommandLineContextTestCase(String name) { 52 super(name); 53 } 54 55 60 public static void main(final String [] args) throws Exception { 61 final String [] testCaseName = {CommandLineContextTestCase.class.getName()}; 62 TestRunner.main(testCaseName); 63 } 64 65 66 69 public void setUp() throws Exception { 70 super.setUp(); 71 commandLineContextDir = System.getProperty("java.io.tmpdir", "/tmp"); 72 new File (commandLineContextDir, "foo" + File.separator + "bar").mkdirs(); 73 74 String 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 84 public void tearDown() throws Exception { 85 super.tearDown(); 86 new File (commandLineContextDir, "foo" + File.separator + "bar").delete(); 87 new File (commandLineContextDir, "foo").delete(); 88 } 89 90 93 public void testGetResource() throws Exception { 94 Object [] test_values = { 95 new String []{"", commandLineContextDir}, 96 new String []{"/", commandLineContextDir}, 97 new String []{"foo", commandLineContextDir + File.separator + "foo"}, 98 new String []{"foo/bar", commandLineContextDir + File.separator + "foo/bar"}, 99 new String []{"foo/bar/nonexistent", null} 100 }; 101 for (int i = 0; i < test_values.length; i++) { 102 String tests[] = (String []) test_values[i]; 103 String test = tests[0]; 104 URL result = commandLineContext.getResource(test); 105 URL expected = null; 106 if (result != null) { 107 expected = new File (tests[1]).toURL(); 108 } 109 String message = "Test " + "'" + test + "'"; 110 assertEquals(message, expected, result); 111 } 112 } 113 114 117 public void testGetRealPath() throws Exception { 118 Object [] test_values = { 119 new String []{"", ""}, 120 new String []{"/", "/"}, 121 new String []{"foo", "foo"}, 122 new String []{"foo/bar", "foo/bar"} 123 }; 124 for (int i = 0; i < test_values.length; i++) { 125 String tests[] = (String []) test_values[i]; 126 String test = tests[0]; 127 File expected_file = new File (commandLineContextDir, tests[1]); 128 String expected = expected_file.getAbsolutePath(); 129 130 String result = commandLineContext.getRealPath(test); 131 String message = "Test " + 132 "'" + test + "'"; 133 assertEquals(message, expected, result); 134 } 135 } 136 137 141 public void testAttributes() throws Exception { 142 Object [] test_values = { 143 new String []{"a", "b"}, 144 new String []{"foo", "foo"}, 145 new String []{"foo/bar", "foo/bar"} 146 }; 147 for (int i = 0; i < test_values.length; i++) { 148 String tests[] = (String []) test_values[i]; 149 String name = tests[0]; 150 String expected = tests[1]; 151 152 commandLineContext.setAttribute(name, expected); 153 154 String result = (String ) commandLineContext.getAttribute(name); 155 assertEquals("Test " + "'" + "n" + "'", expected, result); 156 157 commandLineContext.removeAttribute(name); 158 result = (String ) commandLineContext.getAttribute(name); 159 assertEquals("Test " + "'" + "<null>" + "'", null, result); 160 } 161 } 162 } 163 | Popular Tags |