SimpleSingletonTest xref

    1   /*
    2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    3    *
    4    * This software is open source.
    5    * See the bottom of this file for the licence.
    6    */
    7
    8   package org.dom4j.util;
    9
    10  import junit.framework.Test;
    11  import junit.framework.TestSuite;
    12  import junit.framework.TestCase;
    13
    14  import java.util.HashMap;
    15  import java.util.Map;
    16
    17  /***
    18   * PerThreadSingleton Tester.
    19   *
    20   * @author ddlucas
    21   * @since
    22   *
    23   * <pre>
    24   * 01 / 05 / 2005
    25   * </pre>
    26   *
    27   * @version 1.0
    28   */
    29  public class SimpleSingletonTest extends TestCase {
    30      public SimpleSingletonTest(String name) {
    31          super(name);
    32      }
    33
    34      private static SingletonStrategy singleton;
    35
    36      private static Object reference;
    37
    38      public void setUp() throws Exception {
    39          super.setUp();
    40          if (singleton == null) {
    41              singleton = new PerThreadSingleton();
    42              singleton.setSingletonClassName(HashMap.class.getName());
    43          }
    44      }
    45
    46      public void tearDown() throws Exception {
    47          super.tearDown();
    48      }
    49
    50      public void testFirstInstance() throws Exception {
    51          Map map = (Map) singleton.instance();
    52          String expected = null;
    53          String actual = (String) map.get("Test");
    54          assertEquals("testInstance", expected, actual);
    55
    56          expected = "new value";
    57          map.put("Test", expected);
    58
    59          map = (Map) singleton.instance();
    60          reference = map;
    61          actual = (String) map.get("Test");
    62          assertEquals("testFirstInstance", expected, actual);
    63      }
    64
    65      public void testSecondInstance() throws Exception {
    66          Map map = (Map) singleton.instance();
    67          assertEquals("testSecondInstance reference", reference, map);
    68          String actual = (String) map.get("Test");
    69          String expected = "new value";
    70          assertEquals("testInstance", expected, actual);
    71      }
    72
    73      public static Test suite() {
    74          TestSuite suite = new TestSuite();
    75          suite.addTest(TestSuite.createTest(SimpleSingletonTest.class,
    76                  "testFirstInstance"));
    77          suite.addTest(TestSuite.createTest(SimpleSingletonTest.class,
    78                  "testSecondInstance"));
    79          return suite;
    80      }
    81  }
    82
    83  /*
    84   * Redistribution and use of this software and associated documentation
    85   * ("Software"), with or without modification, are permitted provided that the
    86   * following conditions are met:
    87   *
    88   * 1. Redistributions of source code must retain copyright statements and
    89   * notices. Redistributions must also contain a copy of this document.
    90   *
    91   * 2. Redistributions in binary form must reproduce the above copyright notice,
    92   * this list of conditions and the following disclaimer in the documentation
    93   * and/or other materials provided with the distribution.
    94   *
    95   * 3. The name "DOM4J" must not be used to endorse or promote products derived
    96   * from this Software without prior written permission of MetaStuff, Ltd. For
    97   * written permission, please contact dom4j-info@metastuff.com.
    98   *
    99   * 4. Products derived from this Software may not be called "DOM4J" nor may
    100  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    101  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    102  *
    103  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    104  *
    105  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    106  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    107  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    108  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    109  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    110  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    111  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    112  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    113  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    114  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    115  * POSSIBILITY OF SUCH DAMAGE.
    116  *
    117  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    118  */
    119