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