1 17 18 package org.apache.tools.ant.taskdefs.optional; 19 20 import org.apache.tools.ant.BuildFileTest; 21 22 import java.io.IOException ; 23 import java.io.File ; 24 import java.io.InputStream ; 25 import java.io.BufferedInputStream ; 26 import java.io.FileInputStream ; 27 import java.io.FileReader ; 28 import java.io.BufferedReader ; 29 import java.util.Properties ; 30 31 37 public class EchoPropertiesTest extends BuildFileTest { 38 39 private final static String TASKDEFS_DIR = "src/etc/testcases/taskdefs/optional/"; 40 private static final String GOOD_OUTFILE = "test.properties"; 41 private static final String GOOD_OUTFILE_XML = "test.xml"; 42 private static final String PREFIX_OUTFILE = "test-prefix.properties"; 43 private static final String TEST_VALUE = "isSet"; 44 private static final String BAD_OUTFILE = "."; 45 46 public EchoPropertiesTest(String name) { 47 super(name); 48 } 49 50 51 public void setUp() { 52 configureProject(TASKDEFS_DIR + "echoproperties.xml"); 53 project.setProperty( "test.property", TEST_VALUE ); 54 } 55 56 57 public void tearDown() { 58 executeTarget("cleanup"); 59 } 60 61 62 public void testEchoToLog() { 63 expectLogContaining("testEchoToLog", "test.property="+TEST_VALUE); 64 } 65 66 67 public void testReadBadFile() { 68 expectBuildExceptionContaining( "testReadBadFile", 69 "srcfile is a directory", "srcfile is a directory!" ); 70 } 71 72 73 public void testReadBadFileFail() { 74 expectBuildExceptionContaining( "testReadBadFile", 75 "srcfile is a directory", "srcfile is a directory!" ); 76 } 77 78 79 public void testReadBadFileNoFail() { 80 expectLog( "testReadBadFileNoFail", "srcfile is a directory!" ); 81 } 82 83 84 public void testEchoToBadFile() { 85 expectBuildExceptionContaining( "testEchoToBadFile", 86 "destfile is a directory", "destfile is a directory!" ); 87 } 88 89 90 public void testEchoToBadFileFail() { 91 expectBuildExceptionContaining( "testEchoToBadFileFail", 92 "destfile is a directory", "destfile is a directory!" ); 93 } 94 95 96 public void testEchoToBadFileNoFail() { 97 expectLog( "testEchoToBadFileNoFail", "destfile is a directory!"); 98 } 99 100 101 public void testEchoToGoodFile() throws Exception { 102 executeTarget( "testEchoToGoodFile" ); 103 assertGoodFile(); 104 } 105 106 107 public void testEchoToGoodFileXml() throws Exception { 108 executeTarget( "testEchoToGoodFileXml" ); 109 110 File f = createRelativeFile( GOOD_OUTFILE_XML ); 112 FileReader fr = new FileReader ( f ); 113 try { 114 BufferedReader br = new BufferedReader ( fr ); 115 String read = null; 116 while ( (read = br.readLine()) != null) { 117 if (read.indexOf("<property name=\"test.property\" value=\""+TEST_VALUE+"\"></property>") >= 0) { 118 return; 120 } 121 } 122 fail( "did not encounter set property in generated file." ); 123 } finally { 124 try { 125 fr.close(); 126 } catch(IOException e) {} 127 } 128 } 129 130 131 public void testEchoToGoodFileFail() throws Exception { 132 executeTarget( "testEchoToGoodFileFail" ); 133 assertGoodFile(); 134 } 135 136 137 public void testEchoToGoodFileNoFail() throws Exception { 138 executeTarget( "testEchoToGoodFileNoFail" ); 139 assertGoodFile(); 140 } 141 142 143 public void testEchoPrefix() throws Exception { 144 testEchoPrefixVarious("testEchoPrefix"); 145 } 146 147 public void testEchoPrefixAsPropertyset() throws Exception { 148 testEchoPrefixVarious("testEchoPrefixAsPropertyset"); 149 } 150 151 public void testEchoPrefixAsNegatedPropertyset() throws Exception { 152 testEchoPrefixVarious("testEchoPrefixAsNegatedPropertyset"); 153 } 154 155 public void testEchoPrefixAsDoublyNegatedPropertyset() throws Exception { 156 testEchoPrefixVarious("testEchoPrefixAsDoublyNegatedPropertyset"); 157 } 158 159 private void testEchoPrefixVarious(String target) throws Exception { 160 executeTarget(target); 161 Properties props = loadPropFile(PREFIX_OUTFILE); 162 assertEquals("prefix didn't include 'a.set' property", 163 "true", props.getProperty("a.set")); 164 assertNull("prefix failed to filter out property 'b.set'", 165 props.getProperty("b.set")); 166 } 167 168 protected Properties loadPropFile(String relativeFilename) 169 throws IOException { 170 File f = createRelativeFile( relativeFilename ); 171 Properties props=new Properties (); 172 InputStream in=null; 173 try { 174 in=new BufferedInputStream (new FileInputStream (f)); 175 props.load(in); 176 } finally { 177 if(in!=null) { 178 try { in.close(); } catch(IOException e) {} 179 } 180 } 181 return props; 182 } 183 184 protected void assertGoodFile() throws Exception { 185 File f = createRelativeFile( GOOD_OUTFILE ); 186 assertTrue( 187 "Did not create "+f.getAbsolutePath(), 188 f.exists() ); 189 Properties props=loadPropFile(GOOD_OUTFILE); 190 props.list(System.out); 191 assertEquals("test property not found ", 192 TEST_VALUE, props.getProperty("test.property")); 193 212 } 213 214 215 protected String toAbsolute( String filename ) { 216 return createRelativeFile( filename ).getAbsolutePath(); 217 } 218 219 220 protected File createRelativeFile( String filename ) { 221 if (filename.equals( "." )) { 222 return getProjectDir(); 223 } 224 return new File ( getProjectDir(), filename ); 226 } 227 } 228 229 | Popular Tags |