1 17 18 package org.apache.tools.ant.types; 19 20 import org.apache.tools.ant.Project; 21 import org.apache.tools.ant.util.JavaEnvUtils; 22 23 import junit.framework.TestCase; 24 import junit.framework.AssertionFailedError; 25 26 import java.io.File ; 27 28 32 public class CommandlineJavaTest extends TestCase { 33 34 public CommandlineJavaTest(String name) { 35 super(name); 36 } 37 38 private Project project; 39 40 public void setUp() { 41 project = new Project(); 42 project.setBasedir("."); 43 project.setProperty("build.sysclasspath", "ignore"); 44 } 45 46 public void testGetCommandline() throws CloneNotSupportedException { 47 CommandlineJava c = new CommandlineJava(); 48 c.createArgument().setValue("org.apache.tools.ant.CommandlineJavaTest"); 49 c.setClassname("junit.textui.TestRunner"); 50 c.createVmArgument().setValue("-Djava.compiler=NONE"); 51 String [] s = c.getCommandline(); 52 assertEquals("no classpath", 4, s.length); 53 61 assertEquals("no classpath", "-Djava.compiler=NONE", s[1]); 62 assertEquals("no classpath", "junit.textui.TestRunner", s[2]); 63 assertEquals("no classpath", 64 "org.apache.tools.ant.CommandlineJavaTest", s[3]); 65 try { 66 CommandlineJava c2 = (CommandlineJava) c.clone(); 67 } catch (NullPointerException ex) { 68 fail("cloning should work without classpath specified"); 69 } 70 71 c.createClasspath(project).setLocation(project.resolveFile("build.xml")); 72 c.createClasspath(project).setLocation(project.resolveFile( 73 System.getProperty("ant.home")+"/lib/ant.jar")); 74 s = c.getCommandline(); 75 assertEquals("with classpath", 6, s.length); 76 assertEquals("with classpath", "-Djava.compiler=NONE", s[1]); 78 assertEquals("with classpath", "-classpath", s[2]); 79 assertTrue("build.xml contained", 80 s[3].indexOf("build.xml"+java.io.File.pathSeparator) >= 0); 81 assertTrue("ant.jar contained", s[3].endsWith("ant.jar")); 82 assertEquals("with classpath", "junit.textui.TestRunner", s[4]); 83 assertEquals("with classpath", 84 "org.apache.tools.ant.CommandlineJavaTest", s[5]); 85 } 86 87 public void testJarOption() throws Exception { 88 CommandlineJava c = new CommandlineJava(); 89 c.createArgument().setValue("arg1"); 90 c.setJar("myfile.jar"); 91 c.createVmArgument().setValue("-classic"); 92 c.createVmArgument().setValue("-Dx=y"); 93 String [] s = c.getCommandline(); 94 assertEquals("-classic", s[1]); 95 assertEquals("-Dx=y", s[2]); 96 assertEquals("-jar", s[3]); 97 assertEquals("myfile.jar", s[4]); 98 assertEquals("arg1", s[5]); 99 } 100 101 public void testSysproperties() { 102 String currentClasspath = System.getProperty("java.class.path"); 103 assertNotNull(currentClasspath); 104 assertNull(System.getProperty("key")); 105 CommandlineJava c = new CommandlineJava(); 106 Environment.Variable v = new Environment.Variable(); 107 v.setKey("key"); 108 v.setValue("value"); 109 c.addSysproperty(v); 110 111 project.setProperty("key2", "value2"); 112 PropertySet ps = new PropertySet(); 113 ps.setProject(project); 114 ps.appendName("key2"); 115 c.addSyspropertyset(ps); 116 117 try { 118 c.setSystemProperties(); 119 String newClasspath = System.getProperty("java.class.path"); 120 assertNotNull(newClasspath); 121 assertEquals(currentClasspath, newClasspath); 122 assertNotNull(System.getProperty("key")); 123 assertEquals("value", System.getProperty("key")); 124 assertTrue(System.getProperties().containsKey("java.class.path")); 125 assertNotNull(System.getProperty("key2")); 126 assertEquals("value2", System.getProperty("key2")); 127 } finally { 128 c.restoreSystemProperties(); 129 } 130 assertNull(System.getProperty("key")); 131 assertNull(System.getProperty("key2")); 132 } 133 134 public void testAssertions() throws CloneNotSupportedException { 135 if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2) 136 || JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) { 137 return; 138 } 139 140 CommandlineJava c = new CommandlineJava(); 141 c.createArgument().setValue("org.apache.tools.ant.CommandlineJavaTest"); 142 c.setClassname("junit.textui.TestRunner"); 143 c.createVmArgument().setValue("-Djava.compiler=NONE"); 144 Assertions a = new Assertions(); 145 a.setProject(project); 146 Assertions.EnabledAssertion ea = new Assertions.EnabledAssertion(); 147 ea.setClass("junit.textui.TestRunner"); 148 a.addEnable(ea); 149 c.setAssertions(a); 150 151 String [] expected = new String [] { 152 null, 153 "-Djava.compiler=NONE", 154 "-ea:junit.textui.TestRunner", 155 "junit.textui.TestRunner", 156 "org.apache.tools.ant.CommandlineJavaTest", 157 }; 158 159 for (int i = 0; i < 3; i++) { 161 String [] s = c.getCommandline(); 162 assertEquals(expected.length, s.length); 163 for (int j = 1; j < expected.length; j++) { 164 assertEquals(expected[j], s[j]); 165 } 166 } 167 CommandlineJava c2 = (CommandlineJava) c.clone(); 168 String [] s = c2.getCommandline(); 169 assertEquals(expected.length, s.length); 170 for (int j = 1; j < expected.length; j++) { 171 assertEquals(expected[j], s[j]); 172 } 173 } 174 175 } 176 | Popular Tags |