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.module.launcher;
8   
9   
10  
11  import org.mule.module.launcher.descriptor.ApplicationDescriptor;
12  import org.mule.tck.junit4.AbstractMuleTestCase;
13  
14  import java.io.File;
15  import java.io.FileOutputStream;
16  import java.io.InputStream;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.junit.Test;
21  import org.mule.util.IOUtils;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNull;
25  
26  /**
27   * Test the overriding of app properties by system properties
28   */
29  public class PropertyOverridesTestCase extends AbstractMuleTestCase
30  {
31      private Map<String, String> existingProperties = new HashMap<String, String>();
32  
33      private void setSystemProperties()
34      {
35          setSystemProperty("texas", "province");
36          setSystemProperty("-Operu", "nation");
37          setSystemProperty("-Dtexas.capital", "houston");
38          setSystemProperty("-O-Dperu.capital", "bogota");
39          setSystemProperty("-Omule", "wayCool");
40          setSystemProperty("-Omule.mmc", "evenCooler");
41      }
42  
43      @Test
44      public void testOverrides() throws Exception
45      {
46          File tempProps = File.createTempFile("property", "overrides");
47          InputStream input = getClass().getClassLoader().getResourceAsStream("overridden.properties");
48          FileOutputStream output = new FileOutputStream(tempProps);
49          IOUtils.copy(input, output);
50          input.close();
51          output.close();
52          ApplicationDescriptor descriptor = new ApplicationDescriptor();
53          DefaultAppBloodhound dab = new DefaultAppBloodhound();
54          dab.setApplicationProperties(descriptor, tempProps);
55          Map<String, String>appProps = descriptor.getAppProperties();
56          assertEquals("state", appProps.get("texas"));
57          assertEquals("country", appProps.get("peru"));
58          assertEquals("austin", appProps.get("texas.capital"));
59          assertEquals("4", appProps.get("peru.capital.numberOfletters"));
60          assertEquals("esb", appProps.get("mule"));
61          assertEquals("ipaas", appProps.get("mule.ion"));
62  
63          try
64          {
65              setSystemProperties();
66              descriptor = new ApplicationDescriptor();
67              dab.setApplicationProperties(descriptor, tempProps);
68              appProps = descriptor.getAppProperties();
69              assertEquals("state", appProps.get("texas"));
70              assertEquals("nation", appProps.get("peru"));
71              assertEquals("austin", appProps.get("texas.capital"));
72              assertEquals("4", appProps.get("peru.capital.numberOfletters"));
73              assertEquals("wayCool", appProps.get("mule"));
74              assertEquals("ipaas", appProps.get("mule.ion"));
75              assertEquals("evenCooler", appProps.get("mule.mmc"));
76  
77              descriptor = new ApplicationDescriptor();
78              dab.setApplicationProperties(descriptor, new File("nonexistent.nonexistent"));
79              appProps = descriptor.getAppProperties();
80              assertNull(appProps.get("texas"));
81              assertEquals("nation", appProps.get("peru"));
82              assertNull(appProps.get("texas.capital"));
83              assertNull(appProps.get("peru.capital.numberOfletters"));
84              assertEquals("wayCool", appProps.get("mule"));
85              assertNull(appProps.get("mule.ion"));
86              assertEquals("evenCooler", appProps.get("mule.mmc"));
87          }
88          finally
89          {
90              resetSystemProperties();
91          }
92      }
93  
94      private void resetSystemProperties()
95      {
96          for (Map.Entry<String, String> entry: existingProperties.entrySet())
97          {
98              String key = entry.getKey();
99              String value = entry.getValue();
100             if (value == null)
101             {
102                 System.getProperties().remove(key);
103             }
104             else
105             {
106                 System.setProperty(key, entry.getValue());
107             }
108         }
109     }
110 
111     private void setSystemProperty(String key, String value)
112     {
113         String previous = System.setProperty(key, value);
114         existingProperties.put(key, previous);
115     }
116 }