OptionConverterTestCase xref

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  // Log4j uses the JUnit framework for internal unit testing. JUnit
    19  // is available from "http://www.junit.org".
    20
    21  package org.apache.log4j.helpers;
    22
    23  import java.io.File;
    24  import java.io.FileInputStream;
    25  import java.io.IOException;
    26  import java.util.Properties;
    27
    28  import junit.framework.Test;
    29  import junit.framework.TestCase;
    30  import junit.framework.TestSuite;
    31
    32  import org.apache.log4j.Level;
    33  import org.apache.log4j.LogManager;
    34  import org.apache.log4j.PropertyConfiguratorTest;
    35  import org.apache.log4j.xml.XLevel;
    36
    37  /**
    38     Test variable substitution code.
    39     @author Ceki G&uuml;lc&uuml;
    40
    41     @since 1.0
    42  */
    43  public class OptionConverterTestCase extends TestCase {
    44
    45    Properties props;
    46
    47    public OptionConverterTestCase(String name) {
    48      super(name);
    49    }
    50
    51    public
    52    void setUp() {
    53      props = new Properties();
    54      props.put("TOTO", "wonderful");
    55      props.put("key1", "value1");
    56      props.put("key2", "value2");
    57      // Log4J will NPE without this:
    58      props.put("line.separator", System.getProperty("line.separator"));
    59      // Log4J will throw an Error without this:
    60      props.put("java.home", System.getProperty("java.home"));
    61      System.setProperties(props);
    62
    63
    64    }
    65
    66    public
    67    void tearDown() {
    68      props = null;
    69      LogManager.resetConfiguration();
    70    }
    71
    72    public
    73    void varSubstTest1() {
    74      String r;
    75
    76      r = OptionConverter.substVars("hello world.", null);
    77      assertEquals("hello world.", r);
    78
    79      r = OptionConverter.substVars("hello ${TOTO} world.", null);
    80
    81      assertEquals("hello wonderful world.", r);
    82    }
    83
    84
    85    public
    86    void varSubstTest2() {
    87      String r;
    88
    89      r = OptionConverter.substVars("Test2 ${key1} mid ${key2} end.", null);
    90      assertEquals("Test2 value1 mid value2 end.", r);
    91    }
    92
    93    public
    94    void varSubstTest3() {
    95      String r;
    96
    97      r = OptionConverter.substVars(
    98  				     "Test3 ${unset} mid ${key1} end.", null);
    99      assertEquals("Test3  mid value1 end.", r);
    100   }
    101
    102   public
    103   void varSubstTest4() {
    104     String val = "Test4 ${incomplete ";
    105     try {
    106       OptionConverter.substVars(val, null);
    107     }
    108     catch(IllegalArgumentException e) {
    109       String errorMsg = e.getMessage();
    110       //System.out.println('['+errorMsg+']');
    111       assertEquals('"'+val
    112 		   + "\" has no closing brace. Opening brace at position 6.",
    113 		   errorMsg);
    114     }
    115   }
    116
    117   public
    118   void varSubstTest5() {
    119     Properties props = new Properties();
    120     props.put("p1", "x1");
    121     props.put("p2", "${p1}");
    122     String res = OptionConverter.substVars("${p2}", props);
    123     System.out.println("Result is ["+res+"].");
    124     assertEquals(res, "x1");
    125   }
    126
    127   /**
    128    * Tests configuring Log4J from an InputStream.
    129    *
    130    * @since 1.2.17
    131    */
    132     public void testInputStream() throws IOException {
    133         File file = new File("input/filter1.properties");
    134         assertTrue(file.exists());
    135         FileInputStream inputStream = new FileInputStream(file);
    136         try {
    137             OptionConverter.selectAndConfigure(inputStream, null, LogManager.getLoggerRepository());
    138         } finally {
    139             inputStream.close();
    140         }
    141         new PropertyConfiguratorTest(this.getClass().getName()).validateNested();
    142     }
    143
    144   public
    145   void toLevelTest1() {
    146     String val = "INFO";
    147     Level p = OptionConverter.toLevel(val, null);
    148     assertEquals(p, Level.INFO);
    149   }
    150
    151   public
    152   void toLevelTest2() {
    153     String val = "INFO#org.apache.log4j.xml.XLevel";
    154     Level p = OptionConverter.toLevel(val, null);
    155     assertEquals(p, Level.INFO);
    156   }
    157
    158   public
    159   void toLevelTest3() {
    160     String val = "TRACE#org.apache.log4j.xml.XLevel";
    161     Level p = OptionConverter.toLevel(val, null);
    162     assertEquals(p, XLevel.TRACE);
    163   }
    164
    165   public
    166   void toLevelTest4() {
    167     String val = "TR#org.apache.log4j.xml.XLevel";
    168     Level p = OptionConverter.toLevel(val, null);
    169     assertEquals(p, null);
    170   }
    171
    172   public
    173   void toLevelTest5() {
    174     String val = "INFO#org.apache.log4j.xml.TOTO";
    175     Level p = OptionConverter.toLevel(val, null);
    176     assertEquals(p, null);
    177   }
    178
    179   public
    180   static
    181   Test suite() {
    182     TestSuite suite = new TestSuite();
    183     suite.addTest(new OptionConverterTestCase("varSubstTest5"));
    184     suite.addTest(new OptionConverterTestCase("varSubstTest1"));
    185     suite.addTest(new OptionConverterTestCase("varSubstTest2"));
    186     suite.addTest(new OptionConverterTestCase("varSubstTest3"));
    187     suite.addTest(new OptionConverterTestCase("varSubstTest4"));
    188
    189     suite.addTest(new OptionConverterTestCase("testInputStream"));
    190
    191     suite.addTest(new OptionConverterTestCase("toLevelTest1"));
    192     suite.addTest(new OptionConverterTestCase("toLevelTest2"));
    193     suite.addTest(new OptionConverterTestCase("toLevelTest3"));
    194     suite.addTest(new OptionConverterTestCase("toLevelTest4"));
    195     suite.addTest(new OptionConverterTestCase("toLevelTest5"));
    196     return suite;
    197   }
    198
    199 }