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