1 22 package org.jboss.test.util.test; 23 24 import java.io.File ; 25 import java.util.Properties ; 26 27 import org.jboss.test.JBossTestCase; 28 import org.jboss.util.StringPropertyReplacer; 29 30 38 public class StringPropertyReplacerUnitTestCase extends JBossTestCase 39 { 40 public StringPropertyReplacerUnitTestCase(String name) 41 { 42 super(name); 43 } 44 45 49 public void testPropReplacement() 50 throws Exception 51 { 52 getLog().debug("+++ testPropReplacement"); 53 String xref = "${x}"; 54 String xval = StringPropertyReplacer.replaceProperties(xref); 55 assertTrue("xval == xref", xval.equals(xref)); 56 57 System.setProperty("x", "testPropReplacement"); 58 xval = StringPropertyReplacer.replaceProperties(xref); 59 assertTrue("xval == 'xval'", xval.equals("testPropReplacement")); 60 } 61 62 66 public void testNonSystemPropReplacement() 67 throws Exception 68 { 69 getLog().debug("+++ testNonSystemPropReplacement"); 70 String xref = "${xx}"; 71 72 Properties props = new Properties (); 73 props.setProperty("xx", "testNonSystemPropReplacement"); 74 String xval = StringPropertyReplacer.replaceProperties(xref, props); 75 assertTrue("xval == 'xval'", xval.equals("testNonSystemPropReplacement")); 76 } 77 78 82 public void testDefaultValueSystemPropReplacement() 83 throws Exception 84 { 85 getLog().debug("+++ testDefaultValueSystemPropReplacement"); 86 87 String xref = "${xxx:d}"; 88 String xdef = "d"; 89 String xval = StringPropertyReplacer.replaceProperties(xref); 90 assertTrue("xval == xdef", xval.equals(xdef)); 91 92 System.setProperty("xxx", "testPropReplacement"); 93 xval = StringPropertyReplacer.replaceProperties(xref); 94 assertTrue("xval == 'xval'", xval.equals("testPropReplacement")); 95 } 96 97 98 102 public void testDefaultValueNonSystemPropReplacement() 103 throws Exception 104 { 105 getLog().debug("+++ testDefaultValueNonSystemPropReplacement"); 106 107 Properties props = new Properties (); 108 109 String xref = "${xxx:d}"; 110 String xdef = "d"; 111 String xval = StringPropertyReplacer.replaceProperties(xref, props); 112 assertTrue("xval == xdef", xval.equals(xdef)); 113 114 props.setProperty("xxx", "testNonSystemPropReplacement"); 115 xval = StringPropertyReplacer.replaceProperties(xref, props); 116 assertTrue("xval == 'xval'", xval.equals("testNonSystemPropReplacement")); 117 } 118 119 123 public void testSecondarySystemPropReplacement() 124 throws Exception 125 { 126 getLog().debug("+++ testSecondarySystemPropReplacement"); 127 128 String xref = "${x1,x2}"; 129 String xval = StringPropertyReplacer.replaceProperties(xref); 130 assertTrue("xval == 'xref'", xval.equals(xref)); 131 132 System.setProperty("x2", "secondaryPropReplacement"); 133 xval = StringPropertyReplacer.replaceProperties(xref); 134 assertTrue("xval == 'xval'", xval.equals("secondaryPropReplacement")); 135 136 System.setProperty("x1", "primaryPropReplacement"); 137 xval = StringPropertyReplacer.replaceProperties(xref); 138 assertTrue("xval == 'xval'", xval.equals("primaryPropReplacement")); 139 } 140 141 145 public void testSecondaryNonSystemPropReplacementWithDefault() 146 throws Exception 147 { 148 getLog().debug("+++ testSecondaryNonSystemPropReplacementWithDefault"); 149 150 Properties props = new Properties (); 151 152 String xref = "${x1,x2:d}"; 153 String xdef = "d"; 154 String xval = StringPropertyReplacer.replaceProperties(xref, props); 155 assertTrue("xval == 'xdef'", xval.equals(xdef)); 156 157 props.setProperty("x2", "secondaryPropReplacement"); 158 xval = StringPropertyReplacer.replaceProperties(xref, props); 159 assertTrue("xval == 'xval'", xval.equals("secondaryPropReplacement")); 160 161 props.setProperty("x1", "primaryPropReplacement"); 162 xval = StringPropertyReplacer.replaceProperties(xref, props); 163 assertTrue("xval == 'xval'", xval.equals("primaryPropReplacement")); 164 } 165 166 170 public void testPathologicalNonSystemPropReplacement() 171 throws Exception 172 { 173 getLog().debug("+++ testPathologicalNonSystemPropReplacement"); 174 175 Properties props = new Properties (); 176 177 String xref = "${x1,x2:d}"; 178 props.setProperty("x1,x2:d", "pathologicalPropReplacement"); 179 String xval = StringPropertyReplacer.replaceProperties(xref, props); 180 assertTrue("xval == 'xval'", xval.equals("pathologicalPropReplacement")); 181 } 182 183 187 public void testPathologicalMissingSecondaryProperty() 188 throws Exception 189 { 190 getLog().debug("+++ testPathologicalMissingSecondaryProperty"); 191 192 Properties props = new Properties (); 193 194 String xref = "${x1,}"; 195 String xval = StringPropertyReplacer.replaceProperties(xref, props); 196 assertTrue("xval == 'xref'", xval.equals(xref)); 197 198 props.setProperty("x1", "primaryPropReplacement"); 199 xval = StringPropertyReplacer.replaceProperties(xref, props); 200 assertTrue("xval == 'xval'", xval.equals("primaryPropReplacement")); 201 } 202 203 207 public void testPathologicalMissingSecondaryPropertyWithDefault() 208 throws Exception 209 { 210 getLog().debug("+++ testPathologicalMissingSecondaryPropertyWithDefault"); 211 212 Properties props = new Properties (); 213 214 String xref = "${x1,:d}"; 215 String xdef = "d"; 216 String xval = StringPropertyReplacer.replaceProperties(xref, props); 217 assertTrue("xval == 'xdef'", xval.equals(xdef)); 218 219 props.setProperty("x1", "primaryPropReplacement"); 220 xval = StringPropertyReplacer.replaceProperties(xref, props); 221 assertTrue("xval == 'xval'", xval.equals("primaryPropReplacement")); 222 } 223 224 228 public void testPathologicalMissingPrimaryProperty() 229 throws Exception 230 { 231 getLog().debug("+++ testPathologicalMissingPrimaryProperty"); 232 233 Properties props = new Properties (); 234 235 String xref = "${,x2}"; 236 String xval = StringPropertyReplacer.replaceProperties(xref, props); 237 assertTrue("xval == 'xref'", xval.equals(xref)); 238 239 props.setProperty("x2", "secondaryPropReplacement"); 240 xval = StringPropertyReplacer.replaceProperties(xref, props); 241 assertTrue("xval == 'xval'", xval.equals("secondaryPropReplacement")); 242 } 243 244 248 public void testPathologicalMissingPrimaryPropertyWithDefault() 249 throws Exception 250 { 251 getLog().debug("+++ testPathologicalMissingPrimaryPropertyWithDefault"); 252 253 Properties props = new Properties (); 254 255 String xref = "${,x2:d}"; 256 String xdef = "d"; 257 String xval = StringPropertyReplacer.replaceProperties(xref, props); 258 assertTrue("xval == 'xdef'", xval.equals(xdef)); 259 260 props.setProperty("x2", "secondaryPropReplacement"); 261 xval = StringPropertyReplacer.replaceProperties(xref, props); 262 assertTrue("xval == 'xval'", xval.equals("secondaryPropReplacement")); 263 } 264 265 270 public void testEmptyDefaultNonSystemPropReplacement() 271 throws Exception 272 { 273 getLog().debug("+++ testEmptyDefaultNonSystemPropReplacement"); 274 275 Properties props = new Properties (); 276 277 String xref="${x1:}"; 278 String xval = StringPropertyReplacer.replaceProperties(xref, props); 279 assertTrue("xval == ''", xval.equals("")); 280 281 props.setProperty("x1", "primaryPropReplacement"); 282 xval = StringPropertyReplacer.replaceProperties(xref, props); 283 assertTrue("xval == 'xval'", xval.equals("primaryPropReplacement")); 284 } 285 286 290 public void testFilePropReplacement() 291 throws Exception 292 { 293 getLog().debug("+++ testFilePropReplacement"); 294 String pathSeparatorRef = "${:}"; 295 String pathSeparator = StringPropertyReplacer.replaceProperties(pathSeparatorRef); 296 String separatorRef = "${/}"; 297 String separator = StringPropertyReplacer.replaceProperties(separatorRef); 298 getLog().debug("File.pathSeparator='"+File.pathSeparator+"'"); 299 getLog().debug("File.separator='"+File.separator+"'"); 300 assertTrue("${:}("+pathSeparator+") == File.pathSeparator", pathSeparator.equals(File.pathSeparator)); 301 assertTrue("${/}("+separator+") == File.separator", separator.equals(File.separator)); 302 } 303 304 } 305 | Popular Tags |