View Javadoc

1   /*
2    * $Id: SystemUtilsTestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
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.junit4.AbstractMuleTestCase;
14  
15  import java.util.Collections;
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertSame;
25  
26  public class SystemUtilsTestCase extends AbstractMuleTestCase
27  {
28  
29      @Test
30      public void testEnvironment() throws Exception
31      {
32          Map env = SystemUtils.getenv();
33          assertNotNull(env);
34          assertFalse(env.isEmpty());
35          assertSame(env, SystemUtils.getenv());
36  
37          String envVarToTest = (SystemUtils.IS_OS_WINDOWS ? "Path" : "PATH");
38          // This is a hack to catch Cygwin environments; it won't work in cases where
39          // the user has a different term from /etc/termcaps
40          if (SystemUtils.IS_OS_WINDOWS)
41          {
42              String term = (String) env.get("TERM");
43              if (term != null && term.contains("cygwin"))
44              {
45                  envVarToTest = "PATH";
46              }
47          }
48  
49          assertNotNull(env.get(envVarToTest));
50      }
51  
52      @Test
53      public void testParsePropertyDefinitions()
54      {
55          Map expected = Collections.EMPTY_MAP;
56          String input;
57  
58          assertEquals(expected, SystemUtils.parsePropertyDefinitions(null));
59          assertEquals(expected, SystemUtils.parsePropertyDefinitions(""));
60          assertEquals(expected, SystemUtils.parsePropertyDefinitions(" "));
61          assertEquals(expected, SystemUtils.parsePropertyDefinitions("foo"));
62          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-D"));
63          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-D="));
64  
65          expected = Collections.singletonMap("-D", "true");
66          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-D-D"));
67  
68          expected = Collections.singletonMap("-D-D", "true");
69          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-D-D-D"));
70  
71          expected = Collections.singletonMap("-D-D-D", "true");
72          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-D-D-D-D"));
73  
74          assertEquals(Collections.EMPTY_MAP, SystemUtils.parsePropertyDefinitions("-D=noKey"));
75          assertEquals(Collections.EMPTY_MAP, SystemUtils.parsePropertyDefinitions("=-D"));
76          assertEquals(Collections.EMPTY_MAP, SystemUtils.parsePropertyDefinitions("foo =foo foo"));
77  
78          expected = Collections.singletonMap("k", "true");
79          assertEquals(expected, SystemUtils.parsePropertyDefinitions(" -Dk "));
80  
81          expected = Collections.singletonMap("key", "true");
82          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-Dkey"));
83  
84          expected = Collections.singletonMap("k", "v");
85          assertEquals(expected, SystemUtils.parsePropertyDefinitions(" -Dk=v "));
86  
87          expected = Collections.singletonMap("key", "value");
88          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-Dkey=value"));
89  
90          expected = Collections.singletonMap("key", "quoted");
91          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-Dkey=\"quoted\""));
92  
93          expected = MapUtils.mapWithKeysAndValues(HashMap.class, new String[]{"key", "foo"}, new String[]{
94              "-Dvalue", "bar"});
95          assertEquals(expected, SystemUtils.parsePropertyDefinitions("-Dkey=-Dvalue -Dfoo=bar"));
96  
97          assertEquals(Collections.EMPTY_MAP, SystemUtils.parsePropertyDefinitions("-D=-Dfoo-D== =foo"));
98  
99          expected = Collections.singletonMap("key", "split value");
100         assertEquals(expected, SystemUtils.parsePropertyDefinitions("-Dkey=\"split value\""));
101 
102         expected = MapUtils.mapWithKeysAndValues(HashMap.class, new String[]{"key1", "key2"}, new String[]{
103             "split one", "split two"});
104         input = "-Dkey1=\"split one\" -Dkey2=\"split two\" ";
105         assertEquals(expected, SystemUtils.parsePropertyDefinitions(input));
106 
107         expected = Collections.singletonMap("key", "open end");
108         input = "-Dkey=\"open end";
109         assertEquals(expected, SystemUtils.parsePropertyDefinitions(input));
110 
111         expected = MapUtils.mapWithKeysAndValues(HashMap.class, new String[]{"keyOnly", "mule.foo",
112             "mule.bar"}, new String[]{"true", "xfoo", "xbar"});
113         input = "  standalone key=value -D -D= -DkeyOnly -D=noKey -Dmule.foo=xfoo -Dmule.bar=xbar ";
114         assertEquals(expected, SystemUtils.parsePropertyDefinitions(input));
115     }
116 
117 }