View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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 fr.paris.lutece.test.LuteceTestCase;
37  import fr.paris.lutece.test.MokeHttpServletRequest;
38  import fr.paris.lutece.util.ReferenceList;
39  
40  import java.io.FileInputStream;
41  import java.io.IOException;
42  
43  import javax.servlet.http.HttpServletRequest;
44  
45  
46  /**
47   * AppPathService Test Class
48   */
49  public class AppPathServiceTest extends LuteceTestCase
50  {
51      //TODO D�comenter les virtuals host dans config.properties...
52      private static final String PROPERTY_VIRTUAL_HOST_KEY_PARAMETER = "virtualHostKey.parameterName";
53      private static final String PROPERTY_BASE_URL = "lutece.base.url";
54      private static final String FRAGMENT_END_PATH_XSL = "/WEB-INF/xsl/";
55      private static final String FRAGMENT_END_PATH_CONF = "/WEB-INF/conf/";
56      private static final String FRAGMENT_END_PATH_TEMPLATES = "/WEB-INF/templates";
57  
58      //TODO WebApp Path en dur...
59      //private static final String WEBAPP_PATH = "C:/Java/projets/lutece/core/target/lutece/";
60  
61      /**
62       * Test of getPath method, of class fr.paris.lutece.portal.service.util.AppPathService.
63       */
64      public void testGetPath(  )
65      {
66          System.out.println( "getPath" );
67  
68          String strKey = "path.stylesheet";
69          String expResult = FRAGMENT_END_PATH_XSL;
70          String result = AppPathService.getPath( strKey );
71          assertNotNull( expResult );
72          assertTrue( result.endsWith( expResult ) );
73  
74          strKey = "dummy";
75  
76          String strException = null;
77  
78          try
79          {
80              AppPathService.getPath( strKey );
81          }
82          catch ( AppException e )
83          {
84              strException = e.getMessage(  );
85          }
86  
87          assertNotNull( strException );
88      }
89  
90      /**
91       * Test of getWebAppPath method, of class fr.paris.lutece.portal.service.util.AppPathService.
92       * FIXME : uncomment this method when a better way to find real app path is found.
93       */
94  
95      /*public void testGetWebAppPath(  )
96      {
97          System.out.println( "getWebAppPath" );
98      
99          String expResult = WEBAPP_PATH;
100         String result = AppPathService.getWebAppPath(  );
101         assertNotNull( result );
102     }*/
103 
104     /**
105      * Test of getResourceAsStream method, of class fr.paris.lutece.portal.service.util.AppPathService.
106      */
107     public void testGetResourceAsStream(  ) throws IOException
108     {
109         System.out.println( "getResourceAsStream" );
110 
111         String strPath = FRAGMENT_END_PATH_CONF;
112         String strFilename = "lutece.properties";
113 
114         FileInputStream fis = AppPathService.getResourceAsStream( strPath, strFilename );
115         assertNotNull( fis );
116 
117         // Don't forget to close the file input stream
118         if ( fis != null )
119         {
120             fis.close(  );
121         }
122     }
123 
124     /**
125      * Test of getAbsolutePathFromRelativePath method, of class fr.paris.lutece.portal.service.util.AppPathService.
126      * FIXME : uncomment this method when a better way to find real app path is found.
127      */
128 
129     /*public void testGetAbsolutePathFromRelativePath(  )
130     {
131         System.out.println( "getAbsolutePathFromRelativePath" );
132     
133         String strDirectory = FRAGMENT_END_PATH_TEMPLATES;
134     
135         String expResult = strDirectory;
136         String result = AppPathService.getAbsolutePathFromRelativePath( strDirectory );
137         assertNotNull( result );
138         assertTrue( result.endsWith( expResult ) );
139     }*/
140 
141     /**
142      * Test of getBaseUrl method, of class fr.paris.lutece.portal.service.util.AppPathService.
143      */
144     public void testGetBaseUrl(  )
145     {
146         System.out.println( "getBaseUrl" );
147 
148         HttpServletRequest request = null;
149 
150         // Test case where base url is defined in the properties. Can't test dynamic base Url.
151         String expResult = AppPropertiesService.getProperty( PROPERTY_BASE_URL );
152 
153         if ( expResult != null )
154         {
155             expResult += "/";
156 
157             String result = AppPathService.getBaseUrl( request );
158             assertEquals( expResult, result );
159         }
160     }
161 
162     /**
163      * Test of getAvailableVirtualHosts method, of class fr.paris.lutece.portal.service.util.AppPathService.
164      */
165     public void testGetAvailableVirtualHosts(  )
166     {
167         System.out.println( "getAvailableVirtualHosts" );
168 
169         ReferenceList result = AppPathService.getAvailableVirtualHosts(  );
170 
171         if ( result != null )
172         {
173             assertTrue( result.size(  ) == 2 );
174         }
175     }
176 
177     /**
178      * Test of getVirtualHostKey method, of class fr.paris.lutece.portal.service.util.AppPathService.
179      */
180     public void testGetVirtualHostKey(  )
181     {
182         System.out.println( "getVirtualHostKey" );
183 
184         MokeHttpServletRequest request = new MokeHttpServletRequest(  );
185         String strParameter = AppPropertiesService.getProperty( PROPERTY_VIRTUAL_HOST_KEY_PARAMETER );
186         ReferenceList listKeys = AppPathService.getAvailableVirtualHosts(  );
187 
188         if ( listKeys != null )
189         {
190             String strKey = listKeys.get( 0 ).getCode(  );
191             request.addMokeParameters( strParameter, strKey );
192 
193             String expResult = strKey;
194             String result = AppPathService.getVirtualHostKey( request );
195             assertEquals( expResult, result );
196         }
197     }
198 }