OSDN Git Service

ee85ea5da419ed9577a60cf672a633dccbeba85e
[redminele/redminele.git] / ruby / lib / ruby / gems / 1.8 / gems / eventmachine-0.12.10-x86-mswin32-60 / java / src / com / rubyeventmachine / tests / ApplicationTest.java
1 /**
2  * $Id$
3  * 
4  * Author:: Francis Cianfrocca (gmail: blackhedd)
5  * Homepage::  http://rubyeventmachine.com
6  * Date:: 15 Jul 2007
7  * 
8  * See EventMachine and EventMachine::Connection for documentation and
9  * usage examples.
10  * 
11  *
12  *----------------------------------------------------------------------------
13  *
14  * Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
15  * Gmail: blackhedd
16  * 
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of either: 1) the GNU General Public License
19  * as published by the Free Software Foundation; either version 2 of the
20  * License, or (at your option) any later version; or 2) Ruby's License.
21  * 
22  * See the file COPYING for complete licensing information.
23  *
24  *---------------------------------------------------------------------------
25  *
26  * 
27  */
28
29
30
31 package com.rubyeventmachine.tests;
32
33
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.junit.Assert;
40 import java.net.*;
41 import java.io.*;
42 import java.nio.*;
43
44 import com.rubyeventmachine.*;
45 import com.rubyeventmachine.application.*;
46
47 public class ApplicationTest {
48
49         @BeforeClass
50         public static void setUpBeforeClass() throws Exception {
51         }
52
53         @AfterClass
54         public static void tearDownAfterClass() throws Exception {
55         }
56
57         @Before
58         public void setUp() throws Exception {
59         }
60
61         @After
62         public void tearDown() throws Exception {
63         }
64
65         @Test
66         public void testRunnableArgument() {
67                 final Application a = new Application();
68                 a.run (new Runnable() {
69                         public void run() {
70                                 a.stop();
71                         }
72                 });
73         }
74         
75
76         
77         class F implements ConnectionFactory {
78                 public Connection connection() {
79                         return new Connection() {
80                                 public void receiveData (ByteBuffer bb) {
81                                         application.stop();
82                                 }
83                         };
84                 }
85                 
86         };
87         
88         @Test
89         public void testTcpServer() throws EmReactorException {
90                 final Application a = new Application();
91                 final SocketAddress saddr = new InetSocketAddress ("127.0.0.1", 9008);
92                 a.run (new Runnable() {
93                         public void run() {
94                                 try {
95                                         a.startServer (saddr, new F());
96                                 } catch (EmReactorException e) { Assert.fail(); }
97                                 new Thread() {
98                                         public void run() {
99                                                 try {
100                                                         Socket s = new Socket ("127.0.0.1", 9008);
101                                                         s.getOutputStream().write(new String ("boo").getBytes());
102                                                 } catch (UnknownHostException e) {
103                                                 } catch (IOException e) {}
104                                         }
105                                 }.start();
106                         }
107                 });
108         }
109 }