1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.scxml;
17  
18  import java.util.Map;
19  import java.util.HashMap;
20  
21  import junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  import junit.textui.TestRunner;
25  
26  /***
27   * Unit tests {@link org.apache.commons.scxml.TriggerEvent}.
28   */
29  public class TriggerEventTest extends TestCase {
30      /***
31       * Construct a new instance of TriggerEventTest with the specified name
32       */
33      public TriggerEventTest(String name) {
34          super(name);
35      }
36  
37      public static Test suite() {
38          TestSuite suite = new TestSuite(TriggerEventTest.class);
39          suite.setName("TriggerEvent Tests");
40          return suite;
41      }
42  
43      // Test data
44      private Map payloadData;
45      private Object payload1, payload2;
46      private TriggerEvent te1, te2, te3, te4, te5, te6, te7;
47  
48      /***
49       * Set up instance variables required by this test case.
50       */
51      public void setUp() {
52          payloadData = new HashMap();
53          payloadData.put("property1", "value1");
54          payload1 = payloadData;
55          payload2 = new Object();
56          te1 = new TriggerEvent("name1", TriggerEvent.CHANGE_EVENT, payload1);
57          te2 = new TriggerEvent("name1", TriggerEvent.CHANGE_EVENT, payload1);
58          te3 = new TriggerEvent("name2", TriggerEvent.CALL_EVENT, payload2);
59          te4 = new TriggerEvent("name2", TriggerEvent.CALL_EVENT, payload2);
60          te5 = new TriggerEvent("name3", TriggerEvent.SIGNAL_EVENT);
61          te6 = new TriggerEvent("name3", TriggerEvent.SIGNAL_EVENT);
62          te7 = new TriggerEvent("name3", TriggerEvent.TIME_EVENT);
63      }
64  
65      /***
66       * Tear down instance variables required by this test case.
67       */
68      public void tearDown() {
69          payloadData.clear();
70          payloadData = null;
71          payload1 = payload2 = null;
72          te1 = te2 = te3 = te4 = te5 = te6 = te7 = null;
73      }
74  
75      /***
76       * Test the implementation
77       */
78      public void testTriggerEventGetters() {
79          assertEquals(te1.getName(), "name1");
80          assertEquals(te2.getType(), 2);
81          assertNull(te7.getPayload());
82      }
83  
84      public void testTriggerEventEquals() {
85          assertTrue(te1.equals(te2));
86          assertTrue(te3.equals(te4));
87          assertTrue(te5.equals(te6));
88          assertFalse(te1.equals(te4));
89          assertFalse(te7.equals(null));
90      }
91  
92      public void testTriggerEventToString() {
93          assertEquals("TriggerEvent{name=name3,type=4}", te7.toString());
94          assertEquals("TriggerEvent{name=name1,type=2,payload="
95              + "{property1=value1}}", te2.toString());
96      }
97  
98      public void testTriggerEventHashCode() {
99          assertEquals("TriggerEvent{name=name3,type=4}".hashCode(),
100             te7.hashCode());
101         assertEquals("TriggerEvent{name=name3,type=3}".hashCode(),
102             te5.hashCode());
103     }
104 
105     public static void main(String args[]) {
106         TestRunner.run(suite());
107     }
108 }
109