1   /*
2    * Copyright (c) 2003-2008, by Henrik Arro and Contributors
3    *
4    * This file is part of JSeq, a tool to automatically create
5    * sequence diagrams by tracing program execution.
6    *
7    * See <http://jseq.sourceforge.net> for more information.
8    *
9    * JSeq is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of
12   * the License, or (at your option) any later version.
13   *
14   * JSeq is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17   * GNU Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public License
20   * along with JSeq. If not, see <http://www.gnu.org/licenses/>.
21   */
22  
23  package th.co.edge.jseq;
24  
25  import junit.framework.Test;
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  
29  public class ActivationListTest extends TestCase {
30      public ActivationListTest(String name) {
31          super(name);
32      }
33  
34      public static Test suite() {
35          TestSuite suite = new TestSuite(ActivationListTest.class);
36          return suite;
37      }
38  
39      //
40      // Test methods
41      //
42  
43      public void testFilter() {
44          ActivationList list = buildActivationList();
45          ActivationList filteredList = list.filter(new ClassExclusionFilter(
46                  "Bar.*"));
47          assertEquals(1, filteredList.size());
48          Activation root = filteredList.get(0);
49          assertEquals(3, root.getNumCalls());
50          Activation fooInit = root.getNestedActivations().get(0);
51          assertEquals(0, fooInit.getNumCalls());
52      }
53  
54      public void testFind() {
55          ActivationList list = buildActivationList();
56          ActivationList filteredList = list.find(new MethodFilter("Foo.<init>"));
57          assertEquals(1, filteredList.size());
58          Activation root = filteredList.get(0);
59          assertEquals(6, root.getNumCalls());
60          Activation fooInit = root.getNestedActivations().get(0);
61          assertEquals(0, fooInit.getNumCalls());
62      }
63  
64      public void testCopy() {
65          ActivationList list = buildActivationList();
66          ActivationList copy = list.copy();
67          assertEquals(list, copy);
68      }
69  
70      public void testCollapseRepetitions() {
71          ActivationList list = buildActivationList();
72          // Bar.<init> + 5 calls to Bar.frotz = 6 calls
73          int numCallsByFooInit = list.get(0).getNestedActivations().get(0)
74                  .getNumCalls();
75          assertEquals(6, numCallsByFooInit);
76          ActivationList noRepetitions = list.collapseRepetitions();
77          // Bar.<init> + 1 call to Bar.frotz (x5) = 2 calls
78          numCallsByFooInit = noRepetitions.get(0).getNestedActivations().get(0)
79                  .getNumCalls();
80          assertEquals(2, numCallsByFooInit);
81      }
82  
83      //
84      // Utiltity methods
85      //
86  
87      /**
88       * Creates a test activation list.
89       * 
90       * The activation list looks as follows:
91       * 
92       * <pre><code>
93       * [Scenarios.testWithdrawal
94       *     Foo.&lt;init&gt;
95       *         Bar.&lt;init&gt;
96       *         Bar.frotz
97       *         Bar.frotz
98       *         Bar.frotz
99       *         Bar.frotz
100      *         Bar.frotz
101      *     Foo.bar
102      *     Foo.baz
103      * ]
104      * </code></pre>
105      */
106     public static ActivationList buildActivationList() {
107         Activation root = new Activation(null, "Scenarios", new TestMethodImpl(
108                 "testWithdrawal"), -1);
109         Activation fooInit = new Activation(root, "Foo", new TestMethodImpl(
110                 "<init>", true), -1);
111         new Activation(fooInit, "Bar", new TestMethodImpl("<init>", true), -1);
112         new Activation(fooInit, "Bar", new TestMethodImpl("frotz"), -1);
113         new Activation(fooInit, "Bar", new TestMethodImpl("frotz"), -1);
114         new Activation(fooInit, "Bar", new TestMethodImpl("frotz"), -1);
115         new Activation(fooInit, "Bar", new TestMethodImpl("frotz"), -1);
116         new Activation(fooInit, "Bar", new TestMethodImpl("frotz"), -1);
117 
118         new Activation(root, "Foo", new TestMethodImpl("bar"), -1);
119         new Activation(root, "Foo", new TestMethodImpl("baz"), -1);
120         ActivationList list = new ActivationList();
121         list.add(root);
122         return list;
123     }
124 }