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.scenarios;
24  
25  import junit.framework.Test;
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  
29  public class HelloWorldScenario extends TestCase {
30      public HelloWorldScenario(String name) {
31          super(name);
32      }
33  
34      public static Test suite() {
35          TestSuite suite = new TestSuite(HelloWorldScenario.class);
36          return suite;
37      }
38  
39      public static void main(String[] args) {
40          HelloWorldScenario scenario = new HelloWorldScenario("HelloWorld");
41          scenario.test();
42      }
43  
44      public void test() {
45          HelloWorld helloWorld = new HelloWorld();
46          helloWorld.hello();
47      }
48  }
49  
50  class HelloWorld {
51      Foo foo;
52  
53      public HelloWorld() {
54          this.foo = new Foo(this, 2);
55      }
56  
57      public void hello() {
58          print("Hello, World!");
59          Thread thread1 = new Thread(new ThreadOne());
60          thread1.start();
61          Thread thread2 = new Thread(new ThreadTwo());
62          thread2.start();
63          try {
64              thread1.join();
65              thread2.join();
66          } catch (InterruptedException e) {
67          }
68          ExceptionTest exceptionTest = new ExceptionTest();
69          exceptionTest.test();
70      }
71  
72      public void print(String s) {
73          try {
74              int x = 1 + 2;
75              for (int i = 0; i < 3; i++) {
76                  foo.foo();
77              }
78              System.out.println(s + x);
79          } catch (Exception e) {
80              System.out.println(e);
81          }
82          bar();
83      }
84  
85      public void bar() {
86          System.out.println("So, there");
87      }
88  
89      private static class ThreadOne implements Runnable {
90          public void run() {
91              for (int i = 0; i < 10; i++) {
92                  t1();
93              }
94          }
95  
96          private void t1() {
97              System.out.println("t1");
98              Thread.yield();
99          }
100     }
101 
102     private static class ThreadTwo implements Runnable {
103         public void run() {
104             for (int i = 0; i < 10; i++) {
105                 t2();
106             }
107         }
108 
109         private void t2() {
110             System.out.println("t2");
111             Thread.yield();
112         }
113     }
114 
115     private static class ExceptionTest {
116         public void test() {
117             ExceptionTest2 exceptionTest2 = new ExceptionTest2();
118             try {
119                 exceptionTest2.test();
120             } catch (Exception e) {
121             }
122             print();
123         }
124 
125         public void print() {
126             System.out.println("Exception test ready");
127         }
128     }
129 
130     private static class ExceptionTest2 {
131         public void test() {
132             ExceptionTest3 exceptionTest3 = new ExceptionTest3();
133             exceptionTest3.test();
134         }
135     }
136 
137     private static class ExceptionTest3 {
138         public void test() {
139             throw new RuntimeException("foo");
140         }
141     }
142 }