View Javadoc

1   /*
2    * $Id: MuleConfigurationTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.setClusterId("MY_CLUSTER");
64          config.setDomainId("MY_DOMAIN");
65          config.setCacheMessageAsBytes(false);
66          config.setCacheMessageOriginalPayload(false);
67          config.setEnableStreaming(false);
68          ThreadSafeAccess.AccessControl.setAssertMessageAccess(false);
69          config.setAutoWrapMessageAwareTransform(false);
70          
71          MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
72          contextBuilder.setMuleConfiguration(config);
73          muleContext = new DefaultMuleContextFactory().createMuleContext(contextBuilder);
74          
75          muleContext.start();
76          
77          verifyConfiguration();
78      }
79  
80      /** Test for MULE-3092 */
81      public void testConfigureWithSystemProperties() throws Exception
82      {
83          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "encoding", "UTF-16");
84          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "endpoints.synchronous", "true");
85          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "systemModelType", "direct");
86          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "timeout.synchronous", "30000");
87          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "timeout.transaction", "60000");
88          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "remoteSync", "true");
89          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "workingDirectory", workingDirectory);
90          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "clientMode", "true");
91          
92          // this is just to make the test work for now. Since the initialization of the threadsafe
93          // check behaviour in ThreadSafeAccess.AccessControl has already happened at this point in
94          // time (we touched ThreadSafeAccess.AccessControl in setUp) setting the system property 
95          // won't have any effect here
96          // System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "disable.threadsafemessages", "true");
97          ThreadSafeAccess.AccessControl.setFailOnMessageScribbling(false);
98          
99          System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "serverId", "MY_SERVER");
100         System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "clusterId", "MY_CLUSTER");
101         System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "domainId", "MY_DOMAIN");
102         System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "message.cacheBytes", "false");
103         System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "message.cacheOriginal", "false");
104         System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "streaming.enable", "false");
105         System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "message.assertAccess", "false");
106         System.setProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "transform.autoWrap", "false");
107         
108         muleContext = new DefaultMuleContextFactory().createMuleContext();
109         muleContext.start();
110 
111         verifyConfiguration();
112 
113         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "encoding");
114         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "endpoints.synchronous");
115         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "systemModelType");
116         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "timeout.synchronous");
117         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "timeout.transaction");
118         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "remoteSync");
119         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "workingDirectory");
120         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "clientMode");
121         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "disable.threadsafemessages");
122         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "serverId");
123         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "clusterId");
124         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "domainId");
125         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "message.cacheBytes");
126         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "message.cacheOriginal");
127         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "streaming.enable");
128         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "message.assertAccess");
129         System.clearProperty(MuleProperties.SYSTEM_PROPERTY_PREFIX + "transform.autoWrap");
130     }
131 
132     /** Test for MULE-3110 */
133     public void testConfigureAfterInitFails() throws Exception
134     {
135         muleContext = new DefaultMuleContextFactory().createMuleContext();        
136 
137         DefaultMuleConfiguration mutableConfig = ((DefaultMuleConfiguration) muleContext.getConfiguration());
138         
139         // These are OK to change after init but before start
140         mutableConfig.setDefaultSynchronousEndpoints(true);
141         mutableConfig.setSystemModelType("direct");
142         mutableConfig.setDefaultResponseTimeout(30000);
143         mutableConfig.setDefaultTransactionTimeout(60000);
144         mutableConfig.setClientMode(true);
145 
146         // These are not OK to change after init
147         mutableConfig.setDefaultEncoding("UTF-16");
148         mutableConfig.setWorkingDirectory(workingDirectory);
149         mutableConfig.setId("MY_SERVER");
150         mutableConfig.setClusterId("MY_CLUSTER");
151         mutableConfig.setDomainId("MY_DOMAIN");
152 
153         MuleConfiguration config = muleContext.getConfiguration();
154 
155         // These are OK to change after init but before start
156         assertEquals("direct", config.getSystemModelType());
157         assertEquals(30000, config.getDefaultResponseTimeout());
158         assertEquals(60000, config.getDefaultTransactionTimeout());
159         assertTrue(config.isClientMode());
160         
161         // These are not OK to change after init
162         assertFalse("UTF-16".equals(config.getDefaultEncoding()));
163         assertFalse(workingDirectory.equals(config.getWorkingDirectory()));
164         assertFalse("MY_SERVER".equals(config.getId()));
165         assertFalse("MY_CLUSTER".equals(config.getClusterId()));
166         assertFalse("MY_DOMAIN".equals(config.getDomainId()));
167     }
168 
169     /** Test for MULE-3110 */
170     public void testConfigureAfterStartFails() throws Exception
171     {
172         muleContext = new DefaultMuleContextFactory().createMuleContext();        
173         muleContext.start();
174 
175         DefaultMuleConfiguration mutableConfig = ((DefaultMuleConfiguration) muleContext.getConfiguration());
176         mutableConfig.setDefaultSynchronousEndpoints(true);
177         mutableConfig.setSystemModelType("direct");
178         mutableConfig.setDefaultResponseTimeout(30000);
179         mutableConfig.setDefaultTransactionTimeout(60000);
180         mutableConfig.setClientMode(true);
181 
182         MuleConfiguration config = muleContext.getConfiguration();
183         assertFalse("direct".equals(config.getSystemModelType()));
184         assertFalse(30000 == config.getDefaultResponseTimeout());
185         assertFalse(60000 == config.getDefaultTransactionTimeout());
186         assertFalse(config.isClientMode());
187     }
188 
189     protected void verifyConfiguration()
190     {
191         MuleConfiguration config = muleContext.getConfiguration();
192         assertEquals("UTF-16", config.getDefaultEncoding());
193         assertEquals("direct", config.getSystemModelType());
194         assertEquals(30000, config.getDefaultResponseTimeout());
195         assertEquals(60000, config.getDefaultTransactionTimeout());
196         // on windows this ends up with a c:/ in it
197         assertTrue(config.getWorkingDirectory().indexOf(workingDirectory) != -1);
198         assertTrue(config.isClientMode());
199         assertFalse(ThreadSafeAccess.AccessControl.isFailOnMessageScribbling());
200         assertEquals("MY_SERVER", config.getId());
201         assertEquals("MY_CLUSTER", config.getClusterId());
202         assertEquals("MY_DOMAIN", config.getDomainId());
203         assertFalse(config.isCacheMessageAsBytes());
204         assertFalse(config.isCacheMessageOriginalPayload());
205         assertFalse(config.isEnableStreaming());
206         assertFalse(ThreadSafeAccess.AccessControl.isAssertMessageAccess());
207         assertFalse(config.isAutoWrapMessageAwareTransform());
208     }
209 }
210 
211