View Javadoc

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