View Javadoc

1   // Copyright (C) 2004 - 2009 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.common;
23  
24  import junit.framework.TestCase;
25  
26  
27  /**
28   * Unit tests for {@link AbstractTestSemantics}.
29   *
30   * @author Philip Aston
31   */
32  public class TestAbstractTestSemantics extends TestCase {
33  
34    public static class ConcreteTest extends AbstractTestSemantics {
35      private final int m_number;
36      private final String m_description;
37  
38      public ConcreteTest(int number, String description) {
39        m_number = number;
40        m_description = description;
41      }
42  
43      public int getNumber() {
44        return m_number;
45      }
46  
47      public String getDescription() {
48        return m_description;
49      }
50    }
51  
52    public void testEquals() throws Exception {
53      final Test t1 = new ConcreteTest(10, "foo");
54      final Test t2 = new ConcreteTest(10, "bah");
55      final Test t3 = new ConcreteTest(1, "blah");
56      final Test t4 = new Test() {
57          public int getNumber() { return 1; }
58          public String getDescription() { return null; }
59          public final int compareTo(Test o) { return 0; }
60        };
61  
62      assertEquals(t3, t3);
63      assertEquals(t1, t2);
64      assertEquals(t2, t1);
65      assertTrue(!t1.equals(t3));
66      assertTrue(!t1.equals(new Integer(10)));
67      assertEquals(t3, t4);
68      assertTrue(!t2.equals(t4));
69    }
70  
71    public void testHashCode() throws Exception {
72      final Test t1 = new ConcreteTest(10, null);
73      final Test t2 = new ConcreteTest(10, "bah");
74      final Test t3 = new ConcreteTest(1, "blah");
75  
76      assertEquals(t3.hashCode(), t3.hashCode());
77      assertEquals(t1.hashCode(), t2.hashCode());
78      assertTrue(t1.hashCode() != t3.hashCode());
79    }
80  
81    public void testToString() {
82      final Test t1 = new ConcreteTest(10, null);
83      final Test t2 = new ConcreteTest(99, "bah");
84  
85      assertTrue(t1.toString().indexOf("10") >= 0);
86      assertTrue(t2.toString().indexOf("99") >= 0);
87      assertTrue(t2.toString().indexOf("bah") >= 0);
88    }
89  }