PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/apache-log4j-1.2.17/tests/src/java/org/apache/log4j/or/ORTestCase.java

#
Java | 245 lines | 148 code | 51 blank | 46 comment | 0 complexity | c2fb4679712035b988fd001b448df769 MD5 | raw file
Possible License(s): Apache-2.0
  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. // available from
  20. //
  21. // http://www.junit.org
  22. package org.apache.log4j.or;
  23. import junit.framework.TestCase;
  24. import junit.framework.TestSuite;
  25. import junit.framework.Test;
  26. import java.io.Serializable;
  27. /**
  28. Unit test the {@link ObjectRenderer}.
  29. @author Ceki Gülcü
  30. @since 1.0 */
  31. public class ORTestCase extends TestCase {
  32. static UTObjectRenderer aor;
  33. static UTObjectRenderer bor;
  34. static UTObjectRenderer xor;
  35. static UTObjectRenderer yor;
  36. static UTObjectRenderer oor;
  37. static UTObjectRenderer nor;
  38. static UTObjectRenderer ior;
  39. static UTObjectRenderer cor;
  40. static UTObjectRenderer sor;
  41. public ORTestCase(String name) {
  42. super(name);
  43. }
  44. public
  45. void setUp() {
  46. aor = new UTObjectRenderer("A");
  47. bor = new UTObjectRenderer("B");
  48. xor = new UTObjectRenderer("X");
  49. yor = new UTObjectRenderer("Y");
  50. oor = new UTObjectRenderer("Object");
  51. nor = new UTObjectRenderer("Number");
  52. ior = new UTObjectRenderer("Integer");
  53. cor = new UTObjectRenderer("Comparable");
  54. sor = new UTObjectRenderer("Serializable");
  55. }
  56. // Add: no renderer
  57. // Expect: defaultRenderer
  58. public
  59. void test1() {
  60. RendererMap map = new RendererMap();
  61. ObjectRenderer dr = map.getDefaultRenderer();
  62. ObjectRenderer r = map.get(Integer.class);
  63. assertEquals(r, dr);
  64. }
  65. // Add: Integer renderer
  66. // Expect: Integer renderer
  67. public
  68. void test2() {
  69. RendererMap map = new RendererMap();
  70. map.put(Integer.class, ior);
  71. ObjectRenderer r = map.get(Integer.class);
  72. assertEquals(r, ior);
  73. }
  74. // Add: Number renderer
  75. // Expect: Number
  76. public
  77. void test3() {
  78. RendererMap map = new RendererMap();
  79. map.put(Number.class, ior);
  80. ObjectRenderer r = map.get(Integer.class);
  81. assertEquals(r, ior);
  82. }
  83. // Add: Object renderer
  84. // Result: Object
  85. public
  86. void test4() {
  87. RendererMap map = new RendererMap();
  88. map.put(Object.class, oor);
  89. ObjectRenderer r = map.get(Integer.class);
  90. assertEquals(r, oor);
  91. }
  92. // Add: Object, Number, Integer
  93. // Expect: Integer
  94. public
  95. void test5() {
  96. RendererMap map = new RendererMap();
  97. map.put(Object.class, oor);
  98. map.put(Number.class, nor);
  99. map.put(Integer.class, ior);
  100. ObjectRenderer r = map.get(Integer.class);
  101. assertEquals(r, ior);
  102. }
  103. // Add: Object, Number
  104. // Expect: Number
  105. public
  106. void test6() {
  107. RendererMap map = new RendererMap();
  108. map.put(Object.class, oor);
  109. map.put(Number.class, nor);
  110. ObjectRenderer r = map.get(Integer.class);
  111. assertEquals(r, nor);
  112. }
  113. // Add: Comparable
  114. // Expect: Comparable
  115. public
  116. void test7() throws Exception {
  117. RendererMap map = new RendererMap();
  118. Class comparable = null;
  119. try {
  120. comparable = Class.forName("java.lang.Comparable");
  121. } catch(Exception ex) {
  122. // likely JDK 1.1
  123. return;
  124. }
  125. map.put(comparable, cor);
  126. ObjectRenderer r = map.get(Integer.class);
  127. assertEquals(r, cor);
  128. }
  129. // Add: Serializable
  130. // Expect: Serializablee
  131. public
  132. void test8() {
  133. RendererMap map = new RendererMap();
  134. map.put(Serializable.class, sor);
  135. ObjectRenderer r = map.get(Integer.class);
  136. assertEquals(r, sor);
  137. }
  138. // Add: Y
  139. // Expect: Y
  140. public
  141. void test9() {
  142. RendererMap map = new RendererMap();
  143. map.put(Y.class, yor);
  144. ObjectRenderer r = map.get(B.class);
  145. assertEquals(r, yor);
  146. }
  147. // Add: X
  148. // Expect: X
  149. public
  150. void test10() {
  151. RendererMap map = new RendererMap();
  152. map.put(X.class, xor);
  153. ObjectRenderer r = map.get(B.class);
  154. assertEquals(r, xor);
  155. }
  156. public
  157. static
  158. Test suite() {
  159. TestSuite suite = new TestSuite();
  160. suite.addTest(new ORTestCase("test1"));
  161. suite.addTest(new ORTestCase("test2"));
  162. suite.addTest(new ORTestCase("test3"));
  163. suite.addTest(new ORTestCase("test4"));
  164. suite.addTest(new ORTestCase("test5"));
  165. suite.addTest(new ORTestCase("test6"));
  166. suite.addTest(new ORTestCase("test7"));
  167. suite.addTest(new ORTestCase("test8"));
  168. suite.addTest(new ORTestCase("test9"));
  169. suite.addTest(new ORTestCase("test10"));
  170. return suite;
  171. }
  172. }
  173. class UTObjectRenderer implements ObjectRenderer {
  174. String name;
  175. UTObjectRenderer(String name) {
  176. this.name = name;
  177. }
  178. public
  179. String doRender(Object o) {
  180. return name;
  181. }
  182. public
  183. String toString() {
  184. return("UTObjectRenderer: "+name);
  185. }
  186. }
  187. interface X {
  188. }
  189. interface Y extends X {
  190. }
  191. class A implements Y {
  192. }
  193. class B extends A {
  194. }