1 4 package org.oddjob.values.types; 5 6 import java.io.ByteArrayInputStream ; 7 import java.io.File ; 8 import java.io.IOException ; 9 import java.util.Iterator ; 10 import java.util.Map ; 11 12 import junit.framework.TestCase; 13 14 import org.apache.commons.beanutils.DynaBean; 15 import org.apache.log4j.Logger; 16 import org.oddjob.Oddjob; 17 import org.oddjob.arooa.reflect.BeanUtilsHelper; 18 import org.oddjob.arooa.reflect.IntrospectionHelper; 19 20 23 public class EnvironmentTest extends TestCase { 24 private static final Logger logger = Logger.getLogger(EnvironmentTest.class); 25 26 public static String pathFrom(Map map) { 27 for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) { 28 Map.Entry entry = (Map.Entry ) it.next(); 29 String key = ((String ) entry.getKey()).toUpperCase(); 30 if ("PATH".equals(key)) { 31 return (String ) entry.getValue(); 32 } 33 } 34 return null; 35 } 36 37 public static String envCommand() { 38 String envCommand = System.getProperty("oddjob.envcommand"); 39 if (envCommand != null) { 40 return envCommand; 41 } 42 String os = System.getProperty("os.name").toLowerCase(); 43 if (os.matches(".*windows.*")) { 44 return "cmd /c set"; 45 } 46 return "set"; 47 } 48 49 public void test1() { 50 EnvironmentType et = new EnvironmentType(); 51 et.setCommand(envCommand()); 52 assertNotNull(pathFrom(et)); 53 } 54 55 public void test2() { 56 String xml= 57 "<oddjob><variables id='v'>" + 58 "<environment command='cmd /c set' name='JAVA_HOME'/>" + 59 "</variables></oddjob>"; 60 61 Oddjob oj = new Oddjob(); 62 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 63 oj.run(); 64 DynaBean b = (DynaBean) oj.lookup("v"); 65 String result = (String ) IntrospectionHelper.valueFor( 66 BeanUtilsHelper.getProperty(b, "JAVA_HOME")); 67 logger.debug(result); 68 assertEquals( 69 new File (System.getProperty("java.home")), 70 new File (new File (result), "jre")); 71 } 72 73 public void testProcess() throws IOException { 74 String sep = System.getProperty("line.separator"); 75 String x = "fruit=apple" + sep 76 + "veg=big" + sep + "carrot" + sep 77 + "empty=" + sep 78 + "colours=first=red:second=blue"; 79 80 Map results = new EnvironmentType().process( 81 new ByteArrayInputStream (x.getBytes())); 82 83 logger.debug(MapType.propertiesFrom(results)); 84 85 assertEquals("apple", results.get("fruit")); 86 assertEquals("big" + sep + "carrot", results.get("veg")); 87 assertEquals("", results.get("empty")); 88 assertEquals("first=red:second=blue", results.get("colours")); 89 90 results = new EnvironmentType().process( 92 new ByteArrayInputStream ("".getBytes())); 93 assertEquals(0, results.size()); 94 } 95 96 } 97 | Popular Tags |