View Javadoc

1   /*
2    * $Id: SystemUtilsTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util;
12  
13  import org.mule.tck.AbstractMuleTestCase;
14  
15  import java.util.Collections;
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  public class SystemUtilsTestCase extends AbstractMuleTestCase
20  {
21  
22      public void testEnvironment() throws Exception
23      {
24          Map env = SystemUtils.getenv();
25          assertNotNull(env);
26          assertFalse(env.isEmpty());
27          assertSame(env, SystemUtils.getenv());
28  
29          String envVarToTest = (SystemUtils.IS_OS_WINDOWS ? "Path" : "PATH");
30          // This is a hack to catch Cygwin environments; it won't work in cases where
31          // the user has a different term from /etc/termcaps
32          if (SystemUtils.IS_OS_WINDOWS)
33          {
34              String term = (String) env.get("TERM");
35              if (term != null && term.contains("cygwin"))
36              {
37                  envVarToTest = "PATH";
38              }
39          }
40  
41          assertNotNull(env.get(envVarToTest));
42      }
43  
44      public void testParsePropertyDefinitions()
45      {
46          Map expected = Collections.EMPTY_MAP;
47          String input;
48  
49          assertEquals(expected, SystemUtils.parsePropertyDefinitions(null));
50          assertEquals(expected, SystemUtils.parsePropertyDefinitions(""));
51          assertEquals(expected, SystemUtils.parsePropertyDefinitions(" "));
52          assertEquals(expected, SystemUtils.parsePropertyDefinitions("foo"));
53          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-D"));
54          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-D="));
55  
56          expected = Collections.singletonMap("-D", "true");
57          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-D-D"));
58  
59          expected = Collections.singletonMap("-D-D", "true");
60          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-D-D-D"));
61  
62          expected = Collections.singletonMap("-D-D-D", "true");
63          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-D-D-D-D"));
64  
65          assertEquals(Collections.EMPTY_MAP, SystemUtils.parsePropertyDefinitions("-D=noKey"));
66          assertEquals(Collections.EMPTY_MAP, SystemUtils.parsePropertyDefinitions("=-D"));
67          assertEquals(Collections.EMPTY_MAP, SystemUtils.parsePropertyDefinitions("foo =foo foo"));
68  
69          expected = Collections.singletonMap("k", "true");
70          assertEquals(expected, SystemUtils.parsePropertyDefinitions(" -Dk "));
71  
72          expected = Collections.singletonMap("key", "true");
73          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-Dkey"));
74  
75          expected = Collections.singletonMap("k", "v");
76          assertEquals(expected, SystemUtils.parsePropertyDefinitions(" -Dk=v "));
77  
78          expected = Collections.singletonMap("key", "value");
79          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-Dkey=value"));
80  
81          expected = Collections.singletonMap("key", "quoted");
82          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-Dkey=\"quoted\""));
83  
84          expected = MapUtils.mapWithKeysAndValues(HashMap.class, new String[]{"key", "foo"}, new String[]{
85              "-Dvalue", "bar"});
86          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-Dkey=-Dvalue -Dfoo=bar"));
87  
88          assertEquals(Collections.EMPTY_MAP, SystemUtils.parsePropertyDefinitions("-D=-Dfoo-D== =foo"));
89  
90          expected = Collections.singletonMap("key", "split value");
91          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-Dkey=\"split value\""));
92  
93          expected = MapUtils.mapWithKeysAndValues(HashMap.class, new String[]{"key1", "key2"}, new String[]{
94              "split one", "split two"});
95          input = "-Dkey1=\"split one\" -Dkey2=\"split two\" ";
96          assertEquals(expected, SystemUtils.parsePropertyDefinitions(input));
97  
98          expected = Collections.singletonMap("key", "open end");
99          input = "-Dkey=\"open end";
100         assertEquals(expected, SystemUtils.parsePropertyDefinitions(input));
101 
102         expected = MapUtils.mapWithKeysAndValues(HashMap.class, new String[]{"keyOnly", "mule.foo",
103             "mule.bar"}, new String[]{"true", "xfoo", "xbar"});
104         input = "  standalone key=value -D -D= -DkeyOnly -D=noKey -Dmule.foo=xfoo -Dmule.bar=xbar ";
105         assertEquals(expected, SystemUtils.parsePropertyDefinitions(input));
106     }
107 
108 }