1   /*
2    * Copyright (c) 2003-2008 Henrik Arro
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.sdedit;
24  
25  import java.io.StringWriter;
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  import th.co.edge.jseq.ActivationList;
30  import th.co.edge.jseq.ActivationListTest;
31  import junit.framework.TestCase;
32  
33  public class SdeditTextDiagramTest extends TestCase {
34      public void testWriteDiagram() throws Exception {
35          ActivationList rootActivations = ActivationListTest.buildActivationList();
36          SdeditTextDiagram diagram = new SdeditTextDiagram(rootActivations);
37          StringWriter writer = new StringWriter();
38          diagram.writeDiagram(writer);
39          String sdedit = writer.toString();
40          assertEquals(1, numOccurrences("Scenarios", sdedit));
41          assertEquals(1, numOccurrences("Foo", sdedit));
42          assertEquals(1, numOccurrences("Bar", sdedit));
43          assertEquals(1, numOccurrences("\\.testWithdrawal", sdedit));
44          assertEquals(2, numOccurrences("\\.<init>", sdedit));
45          assertEquals(5, numOccurrences("\\.frotz", sdedit));
46          assertEquals(1, numOccurrences("\\.bar", sdedit));
47          assertEquals(1, numOccurrences("\\.baz", sdedit));
48      }
49      
50      private int numOccurrences(String regex, String s) {
51          Pattern pattern = Pattern.compile(regex);
52          Matcher matcher = pattern.matcher(s);
53          int numOccurrences = 0;
54          int start = 0;
55          while (matcher.find(start)) {
56              numOccurrences++;
57              start = matcher.start() + 1;
58          }
59          return numOccurrences;
60      }
61  }