Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / util / SplitOutputStreamTest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.util;
19
20 import gov.nasa.jpf.util.test.TestJPF;
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.io.PipedInputStream;
24 import java.io.PipedOutputStream;
25 import java.util.Arrays;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30
31 public class SplitOutputStreamTest extends TestJPF {
32
33   private PipedInputStream m_sinks[];
34   private SplitOutputStream m_fixture;
35
36   @Before
37   public void before() throws IOException {
38     PipedOutputStream output[];
39     int i;
40
41     m_sinks = new PipedInputStream[2];
42     output = new PipedOutputStream[2];
43
44     for (i = m_sinks.length; --i >= 0;) {
45       m_sinks[i] = new PipedInputStream();
46       output[i] = new PipedOutputStream(m_sinks[i]);
47     }
48
49     m_fixture = new SplitOutputStream(output);
50
51     Arrays.fill(output, null);   // Force SplitOutputStream to make a copy of output
52   }
53
54   @After
55   public void after() throws IOException {
56     int i, length, offset, delta;
57     byte expect[], actual[];
58
59     m_fixture.flush();
60     m_fixture.close();
61
62     expect = new byte[128];
63     actual = new byte[128];
64
65     while (true) {
66       length = m_sinks[0].read(expect);
67
68       if (length < 0) {
69         break;
70       }
71
72       for (i = m_sinks.length; --i > 0;) {
73         assertTrue(m_sinks[i].available() >= length);
74
75         for (offset = 0; offset < length; offset += delta) {
76           delta = m_sinks[i].read(actual, offset, length - offset);
77
78           assertTrue(delta >= 0);
79         }
80
81         assertArrayEquals(actual, expect);
82       }
83     }
84
85     for (i = m_sinks.length; --i > 0;) {
86       assertEquals(-1, m_sinks[i].read());
87     }
88   }
89
90   @Test(expected = NullPointerException.class)
91   public void passNullPointerToConstructor() throws IOException {
92     OutputStream sinks[];
93
94     sinks = null;
95
96     new SplitOutputStream(sinks);
97   }
98
99   @Test(expected = IllegalArgumentException.class)
100   public void passNoArgsToConstructor() throws IOException {
101     new SplitOutputStream();
102   }
103
104   @Test(expected = NullPointerException.class)
105   public void passNullArgsToConstructor() throws IOException {
106     OutputStream sink;
107
108     sink = null;
109
110     new SplitOutputStream(sink);
111   }
112
113   @Test
114   public void writeByte() throws IOException {
115     m_fixture.write(123);
116   }
117
118   @Test
119   public void modifyAfterConstructor() {
120
121   }
122
123   @Test(expected = NullPointerException.class)
124   public void writeNullBuffer() throws IOException {
125     m_fixture.write(null, 0, 1);
126   }
127
128   @Test(expected = IndexOutOfBoundsException.class)
129   public void writeIndexNegOne() throws IOException {
130     m_fixture.write(new byte[0], -1, 0);
131   }
132
133   @Test(expected = IndexOutOfBoundsException.class)
134   public void writeLengthNegOne() throws IOException {
135     m_fixture.write(new byte[0], 0, -1);
136   }
137
138   @Test(expected = IndexOutOfBoundsException.class)
139   public void writeBeyondEnd() throws IOException {
140     m_fixture.write(new byte[16], 8, 9);
141   }
142
143   @Test
144   public void writeLengthZero() throws IOException {
145     m_fixture.write(new byte[1], 0, 0);
146   }
147
148   @Test
149   public void writeArray() throws IOException {
150     byte buffer[];
151
152     buffer = new byte[]{2, 3, 5, 7, 11, 13};
153
154     m_fixture.write(buffer);
155   }
156 }