View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.portal.service.util;
35  
36  import java.io.FileInputStream;
37  import java.io.IOException;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import org.springframework.mock.web.MockHttpServletRequest;
42  
43  import fr.paris.lutece.test.LuteceTestCase;
44  import fr.paris.lutece.util.ReferenceList;
45  import org.junit.Rule;
46  import org.junit.Test;
47  import org.junit.rules.ExpectedException;
48  
49  /**
50   * AppPathService Test Class
51   */
52  public class AppPathServiceTest extends LuteceTestCase
53  {
54      // TODO D�comenter les virtuals host dans config.properties...
55      private static final String PROPERTY_VIRTUAL_HOST_KEY_PARAMETER = "virtualHostKey.parameterName";
56      private static final String PROPERTY_BASE_URL = "lutece.base.url";
57      private static final String FRAGMENT_END_PATH_XSL = "/WEB-INF/xsl/";
58      private static final String FRAGMENT_END_PATH_CONF = "/WEB-INF/conf/";
59  
60      /**
61       * Test of getPath method, of class fr.paris.lutece.portal.service.util.AppPathService.
62       */
63      public void testGetPath( )
64      {
65          System.out.println( "getPath" );
66  
67          String strKey = "path.stylesheet";
68          String expResult = FRAGMENT_END_PATH_XSL;
69          String result = AppPathService.getPath( strKey );
70          assertNotNull( expResult );
71          assertTrue( result.endsWith( expResult ) );
72          System.out.println( result );
73      }
74  
75      /**
76       * Test of getWebAppPath method, of class fr.paris.lutece.portal.service.util.AppPathService. FIXME : uncomment this method when a better way to find real
77       * app path is found.
78       */
79  
80      /*
81       * public void testGetWebAppPath( ) { System.out.println( "getWebAppPath" );
82       * 
83       * String expResult = WEBAPP_PATH; String result = AppPathService.getWebAppPath( ); assertNotNull( result ); }
84       */
85  
86      /**
87       * Test of getResourceAsStream method, of class fr.paris.lutece.portal.service.util.AppPathService.
88       */
89      public void testGetResourceAsStream( ) throws IOException
90      {
91          System.out.println( "getResourceAsStream" );
92  
93          String strPath = FRAGMENT_END_PATH_CONF;
94          String strFilename = "lutece.properties";
95  
96          FileInputStream fis = AppPathService.getResourceAsStream( strPath, strFilename );
97          assertNotNull( fis );
98  
99          // Don't forget to close the file input stream
100         if ( fis != null )
101         {
102             fis.close( );
103         }
104     }
105 
106     /**
107      * Test of getAbsolutePathFromRelativePath method, of class fr.paris.lutece.portal.service.util.AppPathService. FIXME : uncomment this method when a better
108      * way to find real app path is found.
109      */
110 
111     /*
112      * public void testGetAbsolutePathFromRelativePath( ) { System.out.println( "getAbsolutePathFromRelativePath" );
113      * 
114      * String strDirectory = FRAGMENT_END_PATH_TEMPLATES;
115      * 
116      * String expResult = strDirectory; String result = AppPathService.getAbsolutePathFromRelativePath( strDirectory ); assertNotNull( result ); assertTrue(
117      * result.endsWith( expResult ) ); }
118      */
119 
120     /**
121      * Test of getBaseUrl method, of class fr.paris.lutece.portal.service.util.AppPathService.
122      */
123     public void testGetBaseUrl( )
124     {
125         System.out.println( "getBaseUrl" );
126 
127         HttpServletRequest request = null;
128 
129         // Test case where base url is defined in the properties. Can't test dynamic base Url.
130         String expResult = AppPropertiesService.getProperty( PROPERTY_BASE_URL );
131 
132         if ( expResult != null )
133         {
134             expResult += "/";
135 
136             String result = AppPathService.getBaseUrl( request );
137             assertEquals( expResult, result );
138         }
139     }
140 
141     /**
142      * Test of getAvailableVirtualHosts method, of class fr.paris.lutece.portal.service.util.AppPathService.
143      */
144     public void testGetAvailableVirtualHosts( )
145     {
146         System.out.println( "getAvailableVirtualHosts" );
147 
148         ReferenceList result = AppPathService.getAvailableVirtualHosts( );
149 
150         if ( result != null )
151         {
152             assertTrue( result.size( ) == 2 );
153         }
154     }
155 
156     /**
157      * Test of getVirtualHostKey method, of class fr.paris.lutece.portal.service.util.AppPathService.
158      */
159     public void testGetVirtualHostKey( )
160     {
161         System.out.println( "getVirtualHostKey" );
162 
163         MockHttpServletRequest request = new MockHttpServletRequest( );
164         String strParameter = AppPropertiesService.getProperty( PROPERTY_VIRTUAL_HOST_KEY_PARAMETER );
165         ReferenceList listKeys = AppPathService.getAvailableVirtualHosts( );
166 
167         if ( listKeys != null )
168         {
169             String strKey = listKeys.get( 0 ).getCode( );
170             request.addParameter( strParameter, strKey );
171 
172             String expResult = strKey;
173             String result = AppPathService.getVirtualHostKey( request );
174             assertEquals( expResult, result );
175         }
176     }
177 }