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  //
    19  // Log4j uses the JUnit framework for internal unit testing. JUnit
    20  // available from
    21  //
    22  //     http://www.junit.org
    23
    24
    25  package org.apache.log4j.or;
    26
    27  import org.apache.log4j.spi.LoggingEvent;
    28  import org.apache.log4j.Category;
    29  import org.apache.log4j.Priority;
    30
    31  import org.apache.log4j.or.ObjectRenderer;
    32  import org.apache.log4j.or.RendererMap;
    33
    34  import junit.framework.TestCase;
    35  import junit.framework.TestSuite;
    36  import junit.framework.TestFailure;
    37  import junit.framework.Test;
    38
    39  import java.io.Serializable;
    40
    41
    42  /***
    43     Unit test the {@link ObjectRenderer}.
    44     @author Ceki G&uuml;lc&uuml;
    45     @since 1.0 */
    46  public class ORTestCase extends TestCase {
    47
    48    static UTObjectRenderer aor;
    49    static UTObjectRenderer bor;
    50    static UTObjectRenderer xor;
    51    static UTObjectRenderer yor;
    52
    53    static UTObjectRenderer oor;
    54    static UTObjectRenderer nor;
    55    static UTObjectRenderer ior;
    56    static UTObjectRenderer cor;
    57    static UTObjectRenderer sor;
    58
    59
    60
    61    public ORTestCase(String name) {
    62      super(name);
    63    }
    64
    65
    66    public
    67    void setUp() {
    68      aor = new UTObjectRenderer("A");
    69      bor = new UTObjectRenderer("B");
    70      xor = new UTObjectRenderer("X");
    71      yor = new UTObjectRenderer("Y");
    72
    73      oor = new UTObjectRenderer("Object");
    74      nor = new UTObjectRenderer("Number");
    75      ior = new UTObjectRenderer("Integer");
    76      cor = new UTObjectRenderer("Comparable");
    77      sor = new UTObjectRenderer("Serializable");
    78    }
    79
    80    // Add: no renderer
    81    // Expect: defaultRenderer
    82    public
    83    void test1() {
    84      RendererMap map = new RendererMap();
    85      ObjectRenderer dr = map.getDefaultRenderer();
    86      ObjectRenderer r = map.get(Integer.class);
    87      assertEquals(r, dr);
    88    }
    89
    90    // Add: Integer renderer
    91    // Expect: Integer renderer
    92    public
    93    void test2() {
    94      RendererMap map = new RendererMap();
    95      map.put(Integer.class, ior);
    96      ObjectRenderer r = map.get(Integer.class);
    97      assertEquals(r, ior);
    98
    99    }
    100
    101   // Add: Number renderer
    102   // Expect: Number
    103   public
    104   void test3() {
    105     RendererMap map = new RendererMap();
    106     map.put(Number.class, ior);
    107     ObjectRenderer r = map.get(Integer.class);
    108     assertEquals(r, ior);
    109   }
    110
    111   // Add: Object renderer
    112   // Result: Object
    113   public
    114   void test4() {
    115     RendererMap map = new RendererMap();
    116     map.put(Object.class, oor);
    117     ObjectRenderer r = map.get(Integer.class);
    118     assertEquals(r, oor);
    119   }
    120
    121   // Add: Object, Number, Integer
    122   // Expect: Integer
    123   public
    124   void test5() {
    125     RendererMap map = new RendererMap();
    126
    127     map.put(Object.class, oor);
    128     map.put(Number.class, nor);
    129     map.put(Integer.class, ior);
    130
    131     ObjectRenderer r = map.get(Integer.class);
    132     assertEquals(r, ior);
    133   }
    134
    135   // Add: Object, Number
    136   // Expect: Number
    137   public
    138   void test6() {
    139     RendererMap map = new RendererMap();
    140
    141     map.put(Object.class, oor);
    142     map.put(Number.class, nor);
    143
    144     ObjectRenderer r = map.get(Integer.class);
    145     assertEquals(r, nor);
    146   }
    147
    148   // Add: Comparable
    149   // Expect: Comparable
    150   public
    151   void test7() throws Exception {
    152     RendererMap map = new RendererMap();
    153     Class comparable = null;
    154     try {
    155         comparable = getClass().forName("java.lang.Comparable");
    156     } catch(Exception ex) {
    157         //  likely JDK 1.1
    158         return;
    159     }
    160     map.put(comparable, cor);
    161     ObjectRenderer r = map.get(Integer.class);
    162     assertEquals(r, cor);
    163   }
    164
    165
    166   // Add: Serializable
    167   // Expect: Serializablee
    168   public
    169   void test8() {
    170     RendererMap map = new RendererMap();
    171     map.put(Serializable.class, sor);
    172     ObjectRenderer r = map.get(Integer.class);
    173     assertEquals(r, sor);
    174   }
    175
    176   // Add: Y
    177   // Expect: Y
    178   public
    179   void test9() {
    180     RendererMap map = new RendererMap();
    181     map.put(Y.class, yor);
    182     ObjectRenderer r = map.get(B.class);
    183     assertEquals(r, yor);
    184   }
    185
    186   // Add: X
    187   // Expect: X
    188   public
    189   void test10() {
    190     RendererMap map = new RendererMap();
    191     map.put(X.class, xor);
    192     ObjectRenderer r = map.get(B.class);
    193     assertEquals(r, xor);
    194   }
    195
    196
    197
    198
    199   public
    200   static
    201   Test suite() {
    202     TestSuite suite = new TestSuite();
    203     suite.addTest(new ORTestCase("test1"));
    204     suite.addTest(new ORTestCase("test2"));
    205     suite.addTest(new ORTestCase("test3"));
    206     suite.addTest(new ORTestCase("test4"));
    207     suite.addTest(new ORTestCase("test5"));
    208     suite.addTest(new ORTestCase("test6"));
    209     suite.addTest(new ORTestCase("test7"));
    210     suite.addTest(new ORTestCase("test8"));
    211     suite.addTest(new ORTestCase("test9"));
    212     suite.addTest(new ORTestCase("test10"));
    213     return suite;
    214   }
    215
    216
    217
    218 }
    219
    220 class UTObjectRenderer implements ObjectRenderer {
    221
    222   String name;
    223
    224   UTObjectRenderer(String name) {
    225     this.name = name;
    226   }
    227
    228   public
    229   String doRender(Object o) {
    230     return name;
    231   }
    232
    233   public
    234   String toString() {
    235     return("UTObjectRenderer: "+name);
    236   }
    237 }
    238
    239
    240 interface X  {
    241 }
    242
    243 interface Y extends X {
    244 }
    245
    246
    247 class A implements Y  {
    248 }
    249
    250 class B extends A  {
    251 }