1
+ package xdvrx1_serverProject ;
2
+
3
+ import java .io .*;
4
+ import java .net .*;
5
+
6
+ import org .junit .*;
7
+
8
+ //streams are expressed referring to files
9
+ //but since we want to test the functionality,
10
+ //and since streams are treated almost the same,
11
+ //we use streams using files
12
+ public class ReadInputStreamTest {
13
+
14
+ File rootDirectory = new File ("." );
15
+ File tempFile ;
16
+ String defaultPage ;
17
+ Socket connection ;
18
+
19
+ InputStream is ;
20
+ OutputStream out ;
21
+
22
+ FileInputStream fin ;
23
+ BufferedInputStream bis ;
24
+
25
+ Reader in ;
26
+ Writer out2 ;
27
+
28
+ ReadInputStream readInputStream ;
29
+
30
+ @ Before
31
+ public void setUp () throws Exception {
32
+ tempFile = File .createTempFile ("tempFileX" , ".txt" );
33
+
34
+ out = new FileOutputStream (tempFile );
35
+ out = new BufferedOutputStream (out );
36
+
37
+ out2 = new OutputStreamWriter (out , "US-ASCII" );
38
+
39
+ out2 .append ('a' );
40
+ out2 .append ('b' );
41
+ out2 .append ('c' );
42
+ out2 .append ('\r' );
43
+ out2 .append ('\n' );
44
+ out2 .append (' ' );
45
+
46
+ out2 .flush ();
47
+
48
+ is = new FileInputStream (tempFile );
49
+ bis = new BufferedInputStream (is );
50
+
51
+ in = new InputStreamReader (bis , "US-ASCII" );
52
+
53
+ readInputStream = new ReadInputStream ();
54
+
55
+ defaultPage = "index.html" ;
56
+ connection = new Socket ();
57
+
58
+ }
59
+
60
+ @ Test
61
+ public void testMethodShouldReturnNotNull () {
62
+ //test the method whether its returning
63
+ //an object
64
+ Assert .assertNotNull (readInputStream .
65
+ readUserRequest (bis , in , connection ));
66
+ }
67
+
68
+ @ Test
69
+ public void testMethodShouldReturnAnObject () {
70
+
71
+ //a simple expectation from the encoded data
72
+ StringBuffer expectedResult = new StringBuffer ();
73
+ expectedResult .append ('a' );
74
+ expectedResult .append ('b' );
75
+ expectedResult .append ('c' );
76
+ expectedResult .append ('\r' );
77
+ expectedResult .append ('\n' );
78
+ expectedResult .append (' ' );
79
+
80
+ Assert .assertEquals (expectedResult .toString (),
81
+ readInputStream .
82
+ readUserRequest (bis , in , connection ).toString ());
83
+
84
+ }
85
+
86
+ @ Test (expected = IOException .class )
87
+ public void testMethodShouldThrowException () throws IOException {
88
+
89
+ //when we close the input stream, it should
90
+ //throw an exception
91
+ in .close ();
92
+ StringBuffer bufferResult = readInputStream .readUserRequest (bis , in , connection );
93
+ //from the method, it will go directly to else
94
+ //and will return null, thereby indicating that there
95
+ //is an exception
96
+
97
+ if (bufferResult == null )
98
+ //in order for this test to pass
99
+ //it should deliberately throw the exception
100
+ throw new IOException ("IOException" );
101
+
102
+ }
103
+
104
+ @ After
105
+ public void tearDown () throws Exception {
106
+
107
+ }
108
+
109
+ }
0 commit comments