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.config.builders;
8   
9   import org.mule.util.FilenameUtils;
10  
11  import java.io.File;
12  
13  import javax.servlet.ServletContext;
14  
15  import org.junit.After;
16  import org.junit.Before;
17  import org.junit.Test;
18  import org.springframework.web.context.WebApplicationContext;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  import static org.mockito.Mockito.mock;
23  import static org.mockito.Mockito.verify;
24  import static org.mockito.Mockito.when;
25  
26  public class MuleXmlBuilderContextListenerTestCase
27  {
28      private MuleXmlBuilderContextListener listener;
29      private ServletContext context;
30  
31      @Before
32      public void setUp() throws Exception
33      {
34          listener = new MuleXmlBuilderContextListener();
35          context = mock(ServletContext.class);
36      }
37  
38      @After
39      public void tearDown() throws Exception
40      {
41          listener.muleContext.stop();
42      }
43  
44      @Test
45      public void noMuleAppProperties()
46      {
47          when(context.getInitParameter(MuleXmlBuilderContextListener.INIT_PARAMETER_MULE_CONFIG))
48              .thenReturn("mule-config.xml");
49          when(context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
50              .thenReturn(null);
51          when(context.getAttribute(MuleXmlBuilderContextListener.ATTR_JAVAX_SERVLET_CONTEXT_TEMPDIR))
52              .thenReturn(new File(".mule/testWeb"));
53  
54          listener.initialize(context);
55  
56          verify(context).getInitParameter(MuleXmlBuilderContextListener.INIT_PARAMETER_MULE_CONFIG);
57          verify(context).getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
58          
59          assertEquals("./.mule/testWeb", listener.muleContext.getConfiguration().getWorkingDirectory());
60      }
61  
62      @Test
63      public void withImplicitMuleAppProperties()
64      {
65          when(context.getInitParameter(MuleXmlBuilderContextListener.INIT_PARAMETER_MULE_CONFIG))
66              .thenReturn("org/mule/config/builders/mule-config.xml");
67          when(context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
68              .thenReturn(null);
69          when(context.getAttribute(MuleXmlBuilderContextListener.ATTR_JAVAX_SERVLET_CONTEXT_TEMPDIR))
70                  .thenReturn(new File(".mule/testWeb"));
71  
72          listener.initialize(context);
73  
74          verify(context).getInitParameter(MuleXmlBuilderContextListener.INIT_PARAMETER_MULE_CONFIG);
75          verify(context).getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
76  
77          // TODO don't like this convention, the whole mule-app.properties WAR support in Mule 3 is redundant
78          // and should go away
79          assertWorkingDirectoryEndsWith("target/.appTmp/testWeb");
80      }
81  
82      @Test
83      public void withExplicitMuleAppProperties()
84      {
85          when(context.getInitParameter(MuleXmlBuilderContextListener.INIT_PARAMETER_MULE_CONFIG))
86              .thenReturn("org/mule/config/builders/mule-config.xml");
87          when(context.getInitParameter(MuleXmlBuilderContextListener.INIT_PARAMETER_MULE_APP_CONFIG))
88          .thenReturn("org/mule/config/builders/mule-app-ppp.properties");
89          when(context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
90              .thenReturn(null);
91          when(context.getAttribute(MuleXmlBuilderContextListener.ATTR_JAVAX_SERVLET_CONTEXT_TEMPDIR))
92                  .thenReturn(new File(".mule/testWeb"));
93  
94          listener.initialize(context);
95  
96          verify(context).getInitParameter(MuleXmlBuilderContextListener.INIT_PARAMETER_MULE_CONFIG);
97          verify(context).getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
98  
99          // TODO don't like this convention, the whole mule-app.properties WAR support in Mule 3 is redundant
100         // and should go away
101         assertWorkingDirectoryEndsWith("target/.appTmp2/testWeb");
102     }
103 
104     private void assertWorkingDirectoryEndsWith(String expected)
105     {
106         // handle Windows filenames, just in case
107         String workingDirectory = listener.muleContext.getConfiguration().getWorkingDirectory().replace('\\', '/');
108         workingDirectory = FilenameUtils.separatorsToUnix(workingDirectory);
109         assertTrue(workingDirectory.endsWith(expected));
110     }
111 }