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