1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.mpt;
21  
22  import java.io.IOException;
23  import java.io.OutputStreamWriter;
24  import java.io.Writer;
25  import java.net.Socket;
26  
27  import javax.net.SocketFactory;
28  
29  import junit.framework.TestCase;
30  
31  public class TestDiscardProtocol extends TestCase {
32  
33      private final class InputLater implements Runnable {
34          private Exception e;
35          
36          public void run() {
37              try  {
38                  Thread.sleep(1000);
39                  input();
40              } catch (Exception e) {
41                  this.e = e;
42              }
43          }
44          
45          public void assertExecutedSuccessfully() throws Exception {
46              if (e != null) {
47                  e.printStackTrace();
48                  throw e;
49              }
50          }
51      }
52  
53      private static final String INPUT = "One, two, three - Testing";
54  
55      private static final int PORT = 10001;
56      
57      private DiscardProtocol protocol;
58      private Socket socket;
59  
60      private DiscardProtocol.Record record;
61      
62      @Override
63      protected void setUp() throws Exception {
64          super.setUp();
65          protocol = new DiscardProtocol(PORT);
66          protocol.start();
67          socket = SocketFactory.getDefault().createSocket("127.0.0.1", PORT);
68          record = protocol.recordNext();
69      }
70  
71      @Override
72      protected void tearDown() throws Exception {
73          protocol.stop();
74          super.tearDown();
75      }
76      
77      public void testRecord() throws Exception {
78          assertTrue(socket.isConnected());
79          input();
80          String output = record.complete();
81          assertEquals(INPUT, output);
82      }
83  
84      private void input() throws IOException {
85          Writer out = new OutputStreamWriter(socket.getOutputStream());
86          out.append(INPUT);
87          out.close();
88          socket.close();
89      }
90      
91      public void testComplete() throws Exception {
92          InputLater inputLater = new InputLater();
93          Thread thread = new Thread(inputLater);
94          thread.start();
95          String output = record.complete();
96          assertEquals(INPUT, output);
97          inputLater.assertExecutedSuccessfully();
98      }
99  }