View Javadoc
1   /*
2    * Copyright (c) 2002-2025, 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.util.env;
35  
36  import java.io.File;
37  import java.net.URISyntaxException;
38  import java.net.URL;
39  import java.nio.file.Paths;
40  import java.util.HashMap;
41  import java.util.Map;
42  import org.junit.Test;
43  import static org.junit.Assert.*;
44  
45  /**
46   * EnvUtilTest
47   */
48  public class EnvUtilTest
49  {
50      private static final String ENV_LUTECE_DB_HOST_VAR = "LUTECE_HOST";
51      private static final String ENV_LUTECE_DB_HOST_VALUE = "mysql";
52      private static final String ENV_LUTECE_DB_USER_VAR = "LUTECE_DB_USER";
53      private static final String ENV_LUTECE_DB_USER_VALUE = "lutece_user";
54      private static final String ENV_LUTECE_DB_NAME_VAR = "LUTECE_DATABASE";
55      private static final String ENV_LUTECE_DB_NAME_VALUE = "lutece";
56      private static final String ENV_LUTECE_DB_PWD_FILE_VAR = "LUTECE_DB_PWD_FILE";
57      private static final String ENV_LUTECE_DB_PWD_FILE_VALUE = "./fr/paris/lutece/util/env/password.txt";
58      private static final String PASSWORD = "${LUTECE_DB_PWD_FILE}";
59      private static final String PASSWORD_EXPECTED = "change me";
60      private static final String URL = "jdbc:mysql://${LUTECE_HOST}/${LUTECE_DATABASE}?autoReconnect=true&useUnicode=yes&characterEncoding=utf8";
61      private static final String URL_EXPECTED = "jdbc:mysql://mysql/lutece?autoReconnect=true&useUnicode=yes&characterEncoding=utf8";
62  
63      /**
64       * Test of evaluate method, of class EnvUtil.
65       * 
66       * @throws java.net.URISyntaxException
67       */
68      @Test
69      public void testEvaluate( ) throws URISyntaxException
70      {
71          System.out.println( "testEvaluate" );
72  
73          Map<String, String> mapEnv = new HashMap<>( );
74          mapEnv.put( ENV_LUTECE_DB_USER_VAR, ENV_LUTECE_DB_USER_VALUE );
75          mapEnv.put( ENV_LUTECE_DB_NAME_VAR, ENV_LUTECE_DB_NAME_VALUE );
76          mapEnv.put( ENV_LUTECE_DB_HOST_VAR, ENV_LUTECE_DB_HOST_VALUE );
77          URL url = getClass( ).getClassLoader( ).getResource( ENV_LUTECE_DB_PWD_FILE_VALUE );
78          File file = Paths.get( url.toURI( ) ).toFile( );
79          mapEnv.put( ENV_LUTECE_DB_PWD_FILE_VAR, file.getAbsolutePath( ) );
80  
81          // Try a real env variable
82          String strSource = "${JAVA_HOME}";
83          String result = EnvUtil.evaluate( strSource );
84          System.out.println( strSource + ":" + result );
85  
86          EnvUtil.setMockMapEnv( mapEnv );
87          strSource = "${" + ENV_LUTECE_DB_USER_VAR + "}";
88          result = EnvUtil.evaluate( strSource );
89          System.out.println( strSource + ":" + result );
90          assertEquals( ENV_LUTECE_DB_USER_VALUE, result );
91          strSource = "${" + EnvUtil.PREFIX_ENV + ENV_LUTECE_DB_USER_VAR + "}";
92          result = EnvUtil.evaluate( strSource, EnvUtil.PREFIX_ENV );
93          System.out.println( strSource + ":" + result );
94          assertEquals( ENV_LUTECE_DB_USER_VALUE, result );
95          result = EnvUtil.evaluate( ENV_LUTECE_DB_USER_VAR );
96          assertEquals( ENV_LUTECE_DB_USER_VAR, result );
97          result = EnvUtil.evaluate( URL );
98          assertEquals( URL_EXPECTED, result );
99          result = EnvUtil.evaluate( PASSWORD );
100         System.out.println( result );
101         assertEquals( PASSWORD_EXPECTED, result );
102 
103         System.out.println( EnvUtil.evaluate( null ) );
104         System.out.println( EnvUtil.evaluate( "${DUMMY}" ) );
105 
106     }
107 
108 }