View Javadoc

    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 org.apache.log4j.helpers.OptionConverter;
    24  import org.apache.log4j.Level;
    25  import org.apache.log4j.xml.XLevel;
    26
    27  import junit.framework.TestCase;
    28  import junit.framework.TestSuite;
    29  import junit.framework.Test;
    30  import java.util.Properties;
    31
    32  /***
    33     Test variable substitution code.
    34     @author Ceki G&uuml;lc&uuml;
    35
    36     @since 1.0
    37  */
    38  public class OptionConverterTestCase extends TestCase {
    39
    40    Properties props;
    41
    42    public OptionConverterTestCase(String name) {
    43      super(name);
    44    }
    45
    46    public
    47    void setUp() {
    48      props = new Properties();
    49      props.put("TOTO", "wonderful");
    50      props.put("key1", "value1");
    51      props.put("key2", "value2");
    52      System.setProperties(props);
    53
    54
    55    }
    56
    57    public
    58    void tearDown() {
    59      props = null;
    60    }
    61
    62    public
    63    void varSubstTest1() {
    64      String r;
    65
    66      r = OptionConverter.substVars("hello world.", null);
    67      assertEquals("hello world.", r);
    68
    69      r = OptionConverter.substVars("hello ${TOTO} world.", null);
    70
    71      assertEquals("hello wonderful world.", r);
    72    }
    73
    74
    75    public
    76    void varSubstTest2() {
    77      String r;
    78
    79      r = OptionConverter.substVars("Test2 ${key1} mid ${key2} end.", null);
    80      assertEquals("Test2 value1 mid value2 end.", r);
    81    }
    82
    83    public
    84    void varSubstTest3() {
    85      String r;
    86
    87      r = OptionConverter.substVars(
    88  				     "Test3 ${unset} mid ${key1} end.", null);
    89      assertEquals("Test3  mid value1 end.", r);
    90    }
    91
    92    public
    93    void varSubstTest4() {
    94      String res;
    95      String val = "Test4 ${incomplete ";
    96      try {
    97        res = OptionConverter.substVars(val, null);
    98      }
    99      catch(IllegalArgumentException e) {
    100       String errorMsg = e.getMessage();
    101       //System.out.println('['+errorMsg+']');
    102       assertEquals('"'+val
    103 		   + "\" has no closing brace. Opening brace at position 6.",
    104 		   errorMsg);
    105     }
    106   }
    107
    108   public
    109   void varSubstTest5() {
    110     Properties props = new Properties();
    111     props.put("p1", "x1");
    112     props.put("p2", "${p1}");
    113     String res = OptionConverter.substVars("${p2}", props);
    114     System.out.println("Result is ["+res+"].");
    115     assertEquals(res, "x1");
    116   }
    117
    118   public
    119   void toLevelTest1() {
    120     String val = "INFO";
    121     Level p = OptionConverter.toLevel(val, null);
    122     assertEquals(p, Level.INFO);
    123   }
    124
    125   public
    126   void toLevelTest2() {
    127     String val = "INFO#org.apache.log4j.xml.XLevel";
    128     Level p = OptionConverter.toLevel(val, null);
    129     assertEquals(p, Level.INFO);
    130   }
    131
    132   public
    133   void toLevelTest3() {
    134     String val = "TRACE#org.apache.log4j.xml.XLevel";
    135     Level p = OptionConverter.toLevel(val, null);
    136     assertEquals(p, XLevel.TRACE);
    137   }
    138
    139   public
    140   void toLevelTest4() {
    141     String val = "TR#org.apache.log4j.xml.XLevel";
    142     Level p = OptionConverter.toLevel(val, null);
    143     assertEquals(p, null);
    144   }
    145
    146   public
    147   void toLevelTest5() {
    148     String val = "INFO#org.apache.log4j.xml.TOTO";
    149     Level p = OptionConverter.toLevel(val, null);
    150     assertEquals(p, null);
    151   }
    152
    153
    154   public
    155   static
    156   Test suite() {
    157     TestSuite suite = new TestSuite();
    158     suite.addTest(new OptionConverterTestCase("varSubstTest5"));
    159     suite.addTest(new OptionConverterTestCase("varSubstTest1"));
    160     suite.addTest(new OptionConverterTestCase("varSubstTest2"));
    161     suite.addTest(new OptionConverterTestCase("varSubstTest3"));
    162     suite.addTest(new OptionConverterTestCase("varSubstTest4"));
    163
    164
    165     suite.addTest(new OptionConverterTestCase("toLevelTest1"));
    166     suite.addTest(new OptionConverterTestCase("toLevelTest2"));
    167     suite.addTest(new OptionConverterTestCase("toLevelTest3"));
    168     suite.addTest(new OptionConverterTestCase("toLevelTest4"));
    169     suite.addTest(new OptionConverterTestCase("toLevelTest5"));
    170     return suite;
    171   }
    172
    173 }