View Javadoc

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