View Javadoc

1   /*
2    * $Id: MuleConfigurationTestCase.java 22396 2011-07-12 21:26:04Z 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  
11  package org.mule.test.config;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.ThreadSafeAccess;
15  import org.mule.api.config.MuleConfiguration;
16  import org.mule.api.config.MuleProperties;
17  import org.mule.api.context.MuleContextBuilder;
18  import org.mule.config.DefaultMuleConfiguration;
19  import org.mule.context.DefaultMuleContextBuilder;
20  import org.mule.context.DefaultMuleContextFactory;
21  
22  import junit.framework.TestCase;
23  
24  public class MuleConfigurationTestCase extends TestCase
25  {
26      
27      private boolean failOnMessageScribbling;
28      protected String workingDirectory = "target";
29          
30      @Override
31      protected void setUp() throws Exception
32      {
33          super.setUp();
34          
35          // fiddling with ThreadSafeAccess must not have side effects on later tests. Store
36          // the current state here and restore it in tearDown
37          failOnMessageScribbling = ThreadSafeAccess.AccessControl.isFailOnMessageScribbling();
38      }
39  
40      @Override
41      protected void tearDown() throws Exception
42      {
43          muleContext.dispose();
44          muleContext = null;
45          ThreadSafeAccess.AccessControl.setFailOnMessageScribbling(failOnMessageScribbling);
46      } 
47      
48      private MuleContext muleContext;
49      
50      /** Test for MULE-3092 */
51      public void testConfigureProgramatically() throws Exception
52      {
53          DefaultMuleConfiguration config = new DefaultMuleConfiguration();
54          config.setDefaultEncoding("UTF-16");
55          config.setDefaultSynchronousEndpoints(true);
56          config.setSystemModelType("direct");
57          config.setDefaultResponseTimeout(30000);
58          config.setDefaultTransactionTimeout(60000);
59          config.setWorkingDirectory(workingDirectory);
60          config.setClientMode(true);
61          ThreadSafeAccess.AccessControl.setFailOnMessageScribbling(false);
62          config.setId("MY_SERVER");
63          config.setDomainId("MY_DOMAIN");
64          config.setCacheMessageAsBytes(false);
65          config.setCacheMessageOriginalPayload(false);
66          config.setEnableStreaming(false);
67          ThreadSafeAccess.AccessControl.setAssertMessageAccess(false);
68          config.setAutoWrapMessageAwareTransform(false);
69          
70          MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
71          contextBuilder.setMuleConfiguration(config);
72          muleContext = new DefaultMuleContextFactory().createMuleContext(contextBuilder);
73          
74          muleContext.start();
75          
76          verifyConfiguration();
77      }
78  
79      /** Test for MULE-3092 */
80      public void testConfigureWithSystemProperties() throws Exception
81      {
82          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "encoding", "UTF-16");
83          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "endpoints.synchronous", "true");
84          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "systemModelType", "direct");
85          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "timeout.synchronous", "30000");
86          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "timeout.transaction", "60000");
87          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "remoteSync", "true");
88          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "workingDirectory", workingDirectory);
89          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "clientMode", "true");
90          
91          // this is just to make the test work for now. Since the initialization of the threadsafe
92          // check behaviour in ThreadSafeAccess.AccessControl has already happened at this point in
93          // time (we touched ThreadSafeAccess.AccessControl in setUp) setting the system property 
94          // won't have any effect here
95          // System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "disable.threadsafemessages", "true");
96          ThreadSafeAccess.AccessControl.setFailOnMessageScribbling(false);
97          
98          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "serverId", "MY_SERVER");
99          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "domainId", "MY_DOMAIN");
100         System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "message.cacheBytes", "false");
101         System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "message.cacheOriginal", "false");
102         System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "streaming.enable", "false");
103         System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "message.assertAccess", "false");
104         System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "transform.autoWrap", "false");
105         
106         muleContext = new DefaultMuleContextFactory().createMuleContext();
107         muleContext.start();
108 
109         verifyConfiguration();
110 
111         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "encoding");
112         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "endpoints.synchronous");
113         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "systemModelType");
114         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "timeout.synchronous");
115         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "timeout.transaction");
116         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "remoteSync");
117         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "workingDirectory");
118         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "clientMode");
119         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "disable.threadsafemessages");
120         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "serverId");
121         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "domainId");
122         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "message.cacheBytes");
123         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "message.cacheOriginal");
124         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "streaming.enable");
125         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "message.assertAccess");
126         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "transform.autoWrap");
127     }
128 
129     /** Test for MULE-3110 */
130     public void testConfigureAfterInitFails() throws Exception
131     {
132         muleContext = new DefaultMuleContextFactory().createMuleContext();        
133 
134         DefaultMuleConfiguration mutableConfig = ((DefaultMuleConfiguration) muleContext.getConfiguration());
135         
136         // These are OK to change after init but before start
137         mutableConfig.setDefaultSynchronousEndpoints(true);
138         mutableConfig.setSystemModelType("direct");
139         mutableConfig.setDefaultResponseTimeout(30000);
140         mutableConfig.setDefaultTransactionTimeout(60000);
141         mutableConfig.setClientMode(true);
142 
143         // These are not OK to change after init
144         mutableConfig.setDefaultEncoding("UTF-16");
145         mutableConfig.setWorkingDirectory(workingDirectory);
146         mutableConfig.setId("MY_SERVER");
147         mutableConfig.setDomainId("MY_DOMAIN");
148 
149         MuleConfiguration config = muleContext.getConfiguration();
150 
151         // These are OK to change after init but before start
152         assertEquals("direct", config.getSystemModelType());
153         assertEquals(30000, config.getDefaultResponseTimeout());
154         assertEquals(60000, config.getDefaultTransactionTimeout());
155         assertTrue(config.isClientMode());
156         
157         // These are not OK to change after init
158         assertFalse("UTF-16".equals(config.getDefaultEncoding()));
159         assertFalse(workingDirectory.equals(config.getWorkingDirectory()));
160         assertFalse("MY_SERVER".equals(config.getId()));
161         assertFalse("MY_DOMAIN".equals(config.getDomainId()));
162     }
163 
164     /** Test for MULE-3110 */
165     public void testConfigureAfterStartFails() throws Exception
166     {
167         muleContext = new DefaultMuleContextFactory().createMuleContext();        
168         muleContext.start();
169 
170         DefaultMuleConfiguration mutableConfig = ((DefaultMuleConfiguration) muleContext.getConfiguration());
171         mutableConfig.setDefaultSynchronousEndpoints(true);
172         mutableConfig.setSystemModelType("direct");
173         mutableConfig.setDefaultResponseTimeout(30000);
174         mutableConfig.setDefaultTransactionTimeout(60000);
175         mutableConfig.setClientMode(true);
176 
177         MuleConfiguration config = muleContext.getConfiguration();
178         assertFalse("direct".equals(config.getSystemModelType()));
179         assertFalse(30000 == config.getDefaultResponseTimeout());
180         assertFalse(60000 == config.getDefaultTransactionTimeout());
181         assertFalse(config.isClientMode());
182     }
183 
184     protected void verifyConfiguration()
185     {
186         MuleConfiguration config = muleContext.getConfiguration();
187         assertEquals("UTF-16", config.getDefaultEncoding());
188         assertEquals("direct", config.getSystemModelType());
189         assertEquals(30000, config.getDefaultResponseTimeout());
190         assertEquals(60000, config.getDefaultTransactionTimeout());
191         // on windows this ends up with a c:/ in it
192         assertTrue(config.getWorkingDirectory().indexOf(workingDirectory) != -1);
193         assertTrue(config.isClientMode());
194         assertFalse(ThreadSafeAccess.AccessControl.isFailOnMessageScribbling());
195         assertEquals("MY_SERVER", config.getId());
196         assertEquals("MY_DOMAIN", config.getDomainId());
197         assertFalse(config.isCacheMessageAsBytes());
198         assertFalse(config.isCacheMessageOriginalPayload());
199         assertFalse(config.isEnableStreaming());
200         assertFalse(ThreadSafeAccess.AccessControl.isAssertMessageAccess());
201         assertFalse(config.isAutoWrapMessageAwareTransform());
202     }
203 }
204 
205