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