PageRenderTime 103ms CodeModel.GetById 61ms app.highlight 11ms RepoModel.GetById 29ms 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
  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 */
 56package com.plotnix.enum;
 57
 58import java.util.*;
 59
 60import junit.framework.Test;
 61import junit.framework.TestCase;
 62import junit.framework.TestSuite;
 63
 64import java.io.*;
 65
 66/**
 67 *
 68 * @author Dmitri Plotnikov
 69 * @version $Revision$ $Date$
 70 */
 71public class EnumTestCase extends TestCase {
 72    /**
 73     * Exercises this test case only
 74     */
 75    public static void main(String args[]) {
 76        junit.textui.TestRunner.run(suite());
 77    }
 78
 79
 80    // ---------------------------------------------------------- Constructors
 81
 82    /**
 83     * Construct a new instance of this test case.
 84     *
 85     * @param name Name of the test case
 86     */
 87    public EnumTestCase(String name)
 88    {
 89        super(name);
 90    }
 91
 92
 93    // -------------------------------------------------- Overall Test Methods
 94
 95
 96    /**
 97     * Set up instance variables required by this test case.
 98     */
 99    public void setUp()
100    {
101    }
102
103
104    /**
105     * Return the tests included in this test suite.
106     */
107    public static Test suite()
108    {
109        return (new TestSuite(EnumTestCase.class));
110    }
111
112    /**
113     * Tear down instance variables required by this test case.
114     */
115    public void tearDown()
116    {
117    }
118
119
120    // ------------------------------------------------ Individual Test Methods
121
122    /**
123     */
124    public void testEnum() throws Exception {
125        Color blue = Color.BLUE;
126        assertEquals("blue.stringValue", "BLUE", blue.stringValue());
127        assertEquals("blue.intValue", 2, blue.intValue());
128        assertEquals("blue.toString", "Blue", blue.toString());
129
130        Enum array[] = Enum.enum(Color.class);
131        assertNotNull("enum(class) != null", array);
132        assertEquals("enum(class).length", 3, array.length);
133        assertEquals("enum(class)[0]", Color.RED, array[0]);
134        assertEquals("enum(class)[1]", Color.GREEN, array[1]);
135        assertEquals("enum(class)[2]", Color.BLUE, array[2]);
136
137        Color sameColor = (Color)Enum.enum(Color.class, "BLUE");
138        assertEquals("enum(class, id)", blue, sameColor);
139
140        ComparisonResult less = ComparisonResult.LESS_THAN;
141        assertEquals("less.stringValue", "LESS_THAN", less.stringValue());
142        assertEquals("less.intValue", -1, less.intValue());
143
144        ComparisonResult sameResult = (ComparisonResult)Enum.enum(ComparisonResult.class, -1);
145        assert("enum(class, int)", sameResult == less);
146
147        Flower rose = Flower.ROSE;
148        assertEquals("rose.stringValue", "rose", rose.stringValue());
149        assertEquals("rose.intValue", 0, rose.intValue());
150        assertEquals("rose.toString", "Rosa", rose.toString());
151        assertEquals("rose.toString(italian)", "una rosa", rose.toString(Locale.ITALY));
152        assertEquals("daisy.toString(italian)", "una margherita", Flower.enum("daisy").toString(Locale.ITALY));
153
154        assertEquals("EMPLOYED.stringValue", "EMPLOYED", Enum.enum(Employment.class, Employment.EMPLOYED).stringValue());
155        assertEquals("EMPLOYED.intValue", Employment.EMPLOYED, Enum.enum(Employment.class, Employment.EMPLOYED).intValue());
156
157        Gender male = Gender.MALE;
158        assertEquals("male.stringValue", "M", male.stringValue());
159        assertEquals("mail.intValue", 1, male.intValue());
160        assertEquals("mail.toString", "Male", male.toString());
161
162        Color newBlue = (Color)reserialize(blue);
163        assert("Reserialization", newBlue == blue);
164    }
165
166    private Object reserialize(Object object) throws Exception {
167        ByteArrayOutputStream baos = new ByteArrayOutputStream();
168        ObjectOutputStream oos = new ObjectOutputStream(baos);
169        oos.writeObject(object);
170
171        byte[] array = baos.toByteArray();
172
173        oos.close();
174
175        ByteArrayInputStream bais = new ByteArrayInputStream(array);
176
177        ObjectInputStream ois = new ObjectInputStream(bais);
178        return ois.readObject();
179    }
180}