PageRenderTime 34ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/docs/enum/test/com/plotnix/enum/EnumTestCase.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 180 lines | 68 code | 30 blank | 82 comment | 2 complexity | 8516eb7a974b0d8dee6890b9681baddb MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * $Header$
  3. * $Revision$
  4. * $Date$
  5. *
  6. * ====================================================================
  7. * The PLOTNIX Software License, Version 1.0
  8. *
  9. *
  10. * Copyright (c) 2001 The PLOTNIX Software Foundation. All rights
  11. * reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in
  22. * the documentation and/or other materials provided with the
  23. * distribution.
  24. *
  25. * 3. The end-user documentation included with the redistribution,
  26. * if any, must include the following acknowledgment:
  27. * "This product includes software developed by the
  28. * PLOTNIX, Inc (http://www.plotnix.com/)."
  29. * Alternately, this acknowledgment may appear in the software itself,
  30. * if and wherever such third-party acknowledgments normally appear.
  31. *
  32. * 4. The name "PLOTNIX" must not be used to endorse or promote
  33. * products derived from this software without prior written
  34. * permission. For written permission, please contact dmitri@plotnix.com.
  35. *
  36. * 5. Products derived from this software may not be called "PLOTNIX",
  37. * nor may "PLOTNIX" appear in their name, without prior written
  38. * permission of the PLOTNIX, Inc.
  39. *
  40. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  41. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  42. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  43. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  46. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  47. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  48. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  49. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  50. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * For more information on PLOTNIX, Inc, please see <http://www.plotnix.com/>.
  55. */
  56. package com.plotnix.enum;
  57. import java.util.*;
  58. import junit.framework.Test;
  59. import junit.framework.TestCase;
  60. import junit.framework.TestSuite;
  61. import java.io.*;
  62. /**
  63. *
  64. * @author Dmitri Plotnikov
  65. * @version $Revision$ $Date$
  66. */
  67. public class EnumTestCase extends TestCase {
  68. /**
  69. * Exercises this test case only
  70. */
  71. public static void main(String args[]) {
  72. junit.textui.TestRunner.run(suite());
  73. }
  74. // ---------------------------------------------------------- Constructors
  75. /**
  76. * Construct a new instance of this test case.
  77. *
  78. * @param name Name of the test case
  79. */
  80. public EnumTestCase(String name)
  81. {
  82. super(name);
  83. }
  84. // -------------------------------------------------- Overall Test Methods
  85. /**
  86. * Set up instance variables required by this test case.
  87. */
  88. public void setUp()
  89. {
  90. }
  91. /**
  92. * Return the tests included in this test suite.
  93. */
  94. public static Test suite()
  95. {
  96. return (new TestSuite(EnumTestCase.class));
  97. }
  98. /**
  99. * Tear down instance variables required by this test case.
  100. */
  101. public void tearDown()
  102. {
  103. }
  104. // ------------------------------------------------ Individual Test Methods
  105. /**
  106. */
  107. public void testEnum() throws Exception {
  108. Color blue = Color.BLUE;
  109. assertEquals("blue.stringValue", "BLUE", blue.stringValue());
  110. assertEquals("blue.intValue", 2, blue.intValue());
  111. assertEquals("blue.toString", "Blue", blue.toString());
  112. Enum array[] = Enum.enum(Color.class);
  113. assertNotNull("enum(class) != null", array);
  114. assertEquals("enum(class).length", 3, array.length);
  115. assertEquals("enum(class)[0]", Color.RED, array[0]);
  116. assertEquals("enum(class)[1]", Color.GREEN, array[1]);
  117. assertEquals("enum(class)[2]", Color.BLUE, array[2]);
  118. Color sameColor = (Color)Enum.enum(Color.class, "BLUE");
  119. assertEquals("enum(class, id)", blue, sameColor);
  120. ComparisonResult less = ComparisonResult.LESS_THAN;
  121. assertEquals("less.stringValue", "LESS_THAN", less.stringValue());
  122. assertEquals("less.intValue", -1, less.intValue());
  123. ComparisonResult sameResult = (ComparisonResult)Enum.enum(ComparisonResult.class, -1);
  124. assert("enum(class, int)", sameResult == less);
  125. Flower rose = Flower.ROSE;
  126. assertEquals("rose.stringValue", "rose", rose.stringValue());
  127. assertEquals("rose.intValue", 0, rose.intValue());
  128. assertEquals("rose.toString", "Rosa", rose.toString());
  129. assertEquals("rose.toString(italian)", "una rosa", rose.toString(Locale.ITALY));
  130. assertEquals("daisy.toString(italian)", "una margherita", Flower.enum("daisy").toString(Locale.ITALY));
  131. assertEquals("EMPLOYED.stringValue", "EMPLOYED", Enum.enum(Employment.class, Employment.EMPLOYED).stringValue());
  132. assertEquals("EMPLOYED.intValue", Employment.EMPLOYED, Enum.enum(Employment.class, Employment.EMPLOYED).intValue());
  133. Gender male = Gender.MALE;
  134. assertEquals("male.stringValue", "M", male.stringValue());
  135. assertEquals("mail.intValue", 1, male.intValue());
  136. assertEquals("mail.toString", "Male", male.toString());
  137. Color newBlue = (Color)reserialize(blue);
  138. assert("Reserialization", newBlue == blue);
  139. }
  140. private Object reserialize(Object object) throws Exception {
  141. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  142. ObjectOutputStream oos = new ObjectOutputStream(baos);
  143. oos.writeObject(object);
  144. byte[] array = baos.toByteArray();
  145. oos.close();
  146. ByteArrayInputStream bais = new ByteArrayInputStream(array);
  147. ObjectInputStream ois = new ObjectInputStream(bais);
  148. return ois.readObject();
  149. }
  150. }