PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/apache-log4j-1.2.17/tests/src/java/org/apache/log4j/LevelTest.java

#
Java | 267 lines | 119 code | 34 blank | 114 comment | 2 complexity | 41080b3ab0c136ccc902776c1d33ff6b 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. package org.apache.log4j;
  18. import junit.framework.TestCase;
  19. import org.apache.log4j.util.SerializationTestHelper;
  20. import java.util.Locale;
  21. /**
  22. * Tests of Level.
  23. *
  24. * @author Curt Arnold
  25. * @since 1.2.12
  26. */
  27. public class LevelTest extends TestCase {
  28. /**
  29. * Constructs new instance of test.
  30. * @param name test name.
  31. */
  32. public LevelTest(final String name) {
  33. super(name);
  34. }
  35. /**
  36. * Serialize Level.INFO and check against witness.
  37. * @throws Exception if exception during test.
  38. *
  39. */
  40. public void testSerializeINFO() throws Exception {
  41. int[] skip = new int[] { };
  42. SerializationTestHelper.assertSerializationEquals(
  43. "witness/serialization/info.bin", Level.INFO, skip, Integer.MAX_VALUE);
  44. }
  45. /**
  46. * Deserialize witness and see if resolved to Level.INFO.
  47. * @throws Exception if exception during test.
  48. */
  49. public void testDeserializeINFO() throws Exception {
  50. Object obj =
  51. SerializationTestHelper.deserializeStream(
  52. "witness/serialization/info.bin");
  53. assertTrue(obj instanceof Level);
  54. Level info = (Level) obj;
  55. assertEquals("INFO", info.toString());
  56. //
  57. // JDK 1.1 doesn't support readResolve necessary for the assertion
  58. if (!System.getProperty("java.version").startsWith("1.1.")) {
  59. assertTrue(obj == Level.INFO);
  60. }
  61. }
  62. /**
  63. * Tests that a custom level can be serialized and deserialized
  64. * and is not resolved to a stock level.
  65. *
  66. * @throws Exception if exception during test.
  67. */
  68. public void testCustomLevelSerialization() throws Exception {
  69. CustomLevel custom = new CustomLevel();
  70. Object obj = SerializationTestHelper.serializeClone(custom);
  71. assertTrue(obj instanceof CustomLevel);
  72. CustomLevel clone = (CustomLevel) obj;
  73. assertEquals(Level.INFO.level, clone.level);
  74. assertEquals(Level.INFO.levelStr, clone.levelStr);
  75. assertEquals(Level.INFO.syslogEquivalent, clone.syslogEquivalent);
  76. }
  77. /**
  78. * Custom level to check that custom levels are
  79. * serializable, but not resolved to a plain Level.
  80. */
  81. private static class CustomLevel extends Level {
  82. private static final long serialVersionUID = 1L;
  83. /**
  84. * Create an instance of CustomLevel.
  85. */
  86. public CustomLevel() {
  87. super(
  88. Level.INFO.level, Level.INFO.levelStr, Level.INFO.syslogEquivalent);
  89. }
  90. }
  91. /**
  92. * Tests Level.TRACE_INT.
  93. */
  94. public void testTraceInt() {
  95. assertEquals(5000, Level.TRACE_INT);
  96. }
  97. /**
  98. * Tests Level.TRACE.
  99. */
  100. public void testTrace() {
  101. assertEquals("TRACE", Level.TRACE.toString());
  102. assertEquals(5000, Level.TRACE.toInt());
  103. assertEquals(7, Level.TRACE.getSyslogEquivalent());
  104. }
  105. /**
  106. * Tests Level.toLevel(Level.TRACE_INT).
  107. */
  108. public void testIntToTrace() {
  109. Level trace = Level.toLevel(5000);
  110. assertEquals("TRACE", trace.toString());
  111. }
  112. /**
  113. * Tests Level.toLevel("TRACE");
  114. */
  115. public void testStringToTrace() {
  116. Level trace = Level.toLevel("TRACE");
  117. assertEquals("TRACE", trace.toString());
  118. }
  119. /**
  120. * Tests that Level extends Priority.
  121. */
  122. public void testLevelExtendsPriority() {
  123. assertTrue(Priority.class.isAssignableFrom(Level.class));
  124. }
  125. /**
  126. * Tests Level.OFF.
  127. */
  128. public void testOFF() {
  129. assertTrue(Level.OFF instanceof Level);
  130. }
  131. /**
  132. * Tests Level.FATAL.
  133. */
  134. public void testFATAL() {
  135. assertTrue(Level.FATAL instanceof Level);
  136. }
  137. /**
  138. * Tests Level.ERROR.
  139. */
  140. public void testERROR() {
  141. assertTrue(Level.ERROR instanceof Level);
  142. }
  143. /**
  144. * Tests Level.WARN.
  145. */
  146. public void testWARN() {
  147. assertTrue(Level.WARN instanceof Level);
  148. }
  149. /**
  150. * Tests Level.INFO.
  151. */
  152. public void testINFO() {
  153. assertTrue(Level.INFO instanceof Level);
  154. }
  155. /**
  156. * Tests Level.DEBUG.
  157. */
  158. public void testDEBUG() {
  159. assertTrue(Level.DEBUG instanceof Level);
  160. }
  161. /**
  162. * Tests Level.TRACE.
  163. */
  164. public void testTRACE() {
  165. assertTrue(Level.TRACE instanceof Level);
  166. }
  167. /**
  168. * Tests Level.ALL.
  169. */
  170. public void testALL() {
  171. assertTrue(Level.ALL instanceof Level);
  172. }
  173. /**
  174. * Tests Level.serialVersionUID.
  175. */
  176. public void testSerialVersionUID() {
  177. assertEquals(3491141966387921974L, Level.serialVersionUID);
  178. }
  179. /**
  180. * Tests Level.toLevel(Level.All_INT).
  181. */
  182. public void testIntToAll() {
  183. Level level = Level.toLevel(Level.ALL_INT);
  184. assertEquals("ALL", level.toString());
  185. }
  186. /**
  187. * Tests Level.toLevel(Level.FATAL_INT).
  188. */
  189. public void testIntToFatal() {
  190. Level level = Level.toLevel(Level.FATAL_INT);
  191. assertEquals("FATAL", level.toString());
  192. }
  193. /**
  194. * Tests Level.toLevel(Level.OFF_INT).
  195. */
  196. public void testIntToOff() {
  197. Level level = Level.toLevel(Level.OFF_INT);
  198. assertEquals("OFF", level.toString());
  199. }
  200. /**
  201. * Tests Level.toLevel(17, Level.FATAL).
  202. */
  203. public void testToLevelUnrecognizedInt() {
  204. Level level = Level.toLevel(17, Level.FATAL);
  205. assertEquals("FATAL", level.toString());
  206. }
  207. /**
  208. * Tests Level.toLevel(null, Level.FATAL).
  209. */
  210. public void testToLevelNull() {
  211. Level level = Level.toLevel(null, Level.FATAL);
  212. assertEquals("FATAL", level.toString());
  213. }
  214. /**
  215. * Test that dotless lower I + "nfo" is recognized as INFO.
  216. */
  217. public void testDotlessLowerI() {
  218. Level level = Level.toLevel("\u0131nfo");
  219. assertEquals("INFO", level.toString());
  220. }
  221. /**
  222. * Test that dotted lower I + "nfo" is recognized as INFO
  223. * even in Turkish locale.
  224. */
  225. public void testDottedLowerI() {
  226. Locale defaultLocale = Locale.getDefault();
  227. Locale turkey = new Locale("tr", "TR");
  228. Locale.setDefault(turkey);
  229. Level level = Level.toLevel("info");
  230. Locale.setDefault(defaultLocale);
  231. assertEquals("INFO", level.toString());
  232. }
  233. }