13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.webkit;
27
28 import java.io.IOException;
29 import java.util.LinkedList;
30 import java.util.Random;
31 import org.junit.After;
32 import org.junit.BeforeClass;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import static org.junit.Assert.assertArrayEquals;
36 import static org.junit.Assert.assertEquals;
37 import static org.junit.Assert.assertFalse;
38 import static org.junit.Assert.assertTrue;
39 import static org.junit.Assert.fail;
40
41 public class SimpleSharedBufferInputStreamTest {
42
43 private static final int SEGMENT_SIZE = 0x1000;
44 private static final Random random = new Random();
45
46 private final SharedBuffer sb = new SharedBuffer();
47 private final SimpleSharedBufferInputStream is =
48 new SimpleSharedBufferInputStream(sb);
49
50
51 @BeforeClass
52 public static void beforeClass() throws ClassNotFoundException {
53 Class.forName(WebPage.class.getName());
146 @Test
147 public void testRead3FirstSegmentFirstTenBytes() {
148 append(SEGMENT_SIZE * 2.5);
149 assertArrayEquals(g(0, 10), read(10));
150 }
151
152 @Test
153 public void testRead3FirstSegmentInteriorTenBytes() {
154 append(SEGMENT_SIZE * 2.5);
155 readOut(7);
156 assertArrayEquals(g(7, 10), read(10));
157 }
158
159 @Test
160 public void testRead3FirstSegmentLastTenBytes() {
161 append(SEGMENT_SIZE * 2.5);
162 readOut(SEGMENT_SIZE - 10);
163 assertArrayEquals(g(SEGMENT_SIZE - 10, 10), read(10));
164 }
165
166 @Test @Ignore
167 public void testRead3InteriorSegmentFirstTenBytes() {
168 append(SEGMENT_SIZE * 2.5);
169 readOut(SEGMENT_SIZE);
170 assertArrayEquals(g(SEGMENT_SIZE, 10), read(10));
171 }
172
173 @Test
174 public void testRead3InteriorSegmentInteriorTenBytes() {
175 append(SEGMENT_SIZE * 2.5);
176 readOut(SEGMENT_SIZE + 7);
177 assertArrayEquals(g(SEGMENT_SIZE + 7, 10), read(10));
178 }
179
180 @Test
181 public void testRead3InteriorSegmentLastTenBytes() {
182 append(SEGMENT_SIZE * 2.5);
183 readOut(SEGMENT_SIZE * 2 - 10);
184 assertArrayEquals(g(SEGMENT_SIZE * 2 - 10, 10), read(10));
185 }
186
187 @Test @Ignore
188 public void testRead3LastSegmentFirstTenBytes() {
189 append(SEGMENT_SIZE * 2.5);
190 readOut(SEGMENT_SIZE * 2);
191 assertArrayEquals(g(SEGMENT_SIZE * 2, 10), read(10));
192 }
193
194 @Test
195 public void testRead3LastSegmentInteriorTenBytes() {
196 append(SEGMENT_SIZE * 2.5);
197 readOut(SEGMENT_SIZE * 2 + 7);
198 assertArrayEquals(g(SEGMENT_SIZE * 2 + 7, 10), read(10));
199 }
200
201 @Test
202 public void testRead3LastSegmentLastTenBytes() {
203 append(SEGMENT_SIZE * 2.5);
204 readOut(SEGMENT_SIZE * 2.5 - 10);
205 assertArrayEquals(g(SEGMENT_SIZE * 2.5 - 10, 10), read(10));
206 }
207
213 }
214
215 @Test
216 public void testRead3TenBytesAfterLastByte() {
217 append(SEGMENT_SIZE * 2.5);
218 readOut(SEGMENT_SIZE * 2.5);
219 assertArrayEquals(null, read(10));
220 }
221
222 @Test
223 public void testRead3TenBytesFromEmptyBuffer() {
224 assertArrayEquals(null, read(10));
225 }
226
227 @Test
228 public void testRead3FirstSegment() {
229 append(SEGMENT_SIZE * 2.5);
230 assertArrayEquals(g(0, SEGMENT_SIZE), read(SEGMENT_SIZE));
231 }
232
233 @Test @Ignore
234 public void testRead3InteriorSegment() {
235 append(SEGMENT_SIZE * 2.5);
236 readOut(SEGMENT_SIZE);
237 assertArrayEquals(g(SEGMENT_SIZE, SEGMENT_SIZE), read(SEGMENT_SIZE));
238 }
239
240 @Test @Ignore
241 public void testRead3LastSegment() {
242 append(SEGMENT_SIZE * 2.5);
243 readOut(SEGMENT_SIZE * 2);
244 assertArrayEquals(
245 g(SEGMENT_SIZE * 2, SEGMENT_SIZE * 0.5),
246 read(SEGMENT_SIZE));
247 }
248
249 @Test
250 public void testRead3FirstSegmentFirstZeroBytes() {
251 append(SEGMENT_SIZE * 2.5);
252 assertArrayEquals(new byte[0], read(0));
253 }
254
255 @Test
256 public void testRead3FirstSegmentInteriorZeroBytes() {
257 append(SEGMENT_SIZE * 2.5);
258 readOut(SEGMENT_SIZE * 0.5);
259 assertArrayEquals(new byte[0], read(0));
260 }
344 is.read(new byte[10], 1, 10);
345 fail("IndexOutOfBoundsException expected but not thrown");
346 } catch (IndexOutOfBoundsException expected) {}
347 }
348
349 private void testSkipSmallNumberOfBytes(long skip) {
350 int streamSize = (int) (SEGMENT_SIZE * 2.5);
351 int skipCount = streamSize / SEGMENT_SIZE + 1;
352 append(streamSize);
353 int position = 0;
354 for (int i = 0; i < skipCount; i++) {
355 long skipped = is.skip(skip);
356 assertEquals(Math.max(skip, 0), skipped);
357 position += skipped;
358 long len = Math.min(SEGMENT_SIZE - skipped, streamSize - position);
359 assertArrayEquals(g(position, len), read(SEGMENT_SIZE));
360 position += len;
361 }
362 }
363
364 @Test @Ignore
365 public void testSkipZeroBytes() {
366 testSkipSmallNumberOfBytes(0);
367 }
368
369 @Test
370 public void testSkipMinusOneByte() {
371 testSkipSmallNumberOfBytes(-1);
372 }
373
374 @Test
375 public void testSkipMinusTenBytes() {
376 testSkipSmallNumberOfBytes(-10);
377 }
378
379 @Test
380 public void testSkipIntegerMinValueBytes() {
381 testSkipSmallNumberOfBytes(Integer.MIN_VALUE);
382 }
383
384 @Test
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.webkit;
27
28 import java.io.IOException;
29 import java.util.LinkedList;
30 import java.util.Random;
31 import org.junit.After;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import static org.junit.Assert.assertArrayEquals;
35 import static org.junit.Assert.assertEquals;
36 import static org.junit.Assert.assertFalse;
37 import static org.junit.Assert.assertTrue;
38 import static org.junit.Assert.fail;
39
40 public class SimpleSharedBufferInputStreamTest {
41
42 private static final int SEGMENT_SIZE = 0x1000;
43 private static final Random random = new Random();
44
45 private final SharedBuffer sb = new SharedBuffer();
46 private final SimpleSharedBufferInputStream is =
47 new SimpleSharedBufferInputStream(sb);
48
49
50 @BeforeClass
51 public static void beforeClass() throws ClassNotFoundException {
52 Class.forName(WebPage.class.getName());
145 @Test
146 public void testRead3FirstSegmentFirstTenBytes() {
147 append(SEGMENT_SIZE * 2.5);
148 assertArrayEquals(g(0, 10), read(10));
149 }
150
151 @Test
152 public void testRead3FirstSegmentInteriorTenBytes() {
153 append(SEGMENT_SIZE * 2.5);
154 readOut(7);
155 assertArrayEquals(g(7, 10), read(10));
156 }
157
158 @Test
159 public void testRead3FirstSegmentLastTenBytes() {
160 append(SEGMENT_SIZE * 2.5);
161 readOut(SEGMENT_SIZE - 10);
162 assertArrayEquals(g(SEGMENT_SIZE - 10, 10), read(10));
163 }
164
165 @Test
166 public void testRead3InteriorSegmentFirstTenBytes() {
167 append(SEGMENT_SIZE * 2.5);
168 readOut(SEGMENT_SIZE);
169 assertArrayEquals(g(SEGMENT_SIZE, 10), read(10));
170 }
171
172 @Test
173 public void testRead3InteriorSegmentInteriorTenBytes() {
174 append(SEGMENT_SIZE * 2.5);
175 readOut(SEGMENT_SIZE + 7);
176 assertArrayEquals(g(SEGMENT_SIZE + 7, 10), read(10));
177 }
178
179 @Test
180 public void testRead3InteriorSegmentLastTenBytes() {
181 append(SEGMENT_SIZE * 2.5);
182 readOut(SEGMENT_SIZE * 2 - 10);
183 assertArrayEquals(g(SEGMENT_SIZE * 2 - 10, 10), read(10));
184 }
185
186 @Test
187 public void testRead3LastSegmentFirstTenBytes() {
188 append(SEGMENT_SIZE * 2.5);
189 readOut(SEGMENT_SIZE * 2);
190 assertArrayEquals(g(SEGMENT_SIZE * 2, 10), read(10));
191 }
192
193 @Test
194 public void testRead3LastSegmentInteriorTenBytes() {
195 append(SEGMENT_SIZE * 2.5);
196 readOut(SEGMENT_SIZE * 2 + 7);
197 assertArrayEquals(g(SEGMENT_SIZE * 2 + 7, 10), read(10));
198 }
199
200 @Test
201 public void testRead3LastSegmentLastTenBytes() {
202 append(SEGMENT_SIZE * 2.5);
203 readOut(SEGMENT_SIZE * 2.5 - 10);
204 assertArrayEquals(g(SEGMENT_SIZE * 2.5 - 10, 10), read(10));
205 }
206
212 }
213
214 @Test
215 public void testRead3TenBytesAfterLastByte() {
216 append(SEGMENT_SIZE * 2.5);
217 readOut(SEGMENT_SIZE * 2.5);
218 assertArrayEquals(null, read(10));
219 }
220
221 @Test
222 public void testRead3TenBytesFromEmptyBuffer() {
223 assertArrayEquals(null, read(10));
224 }
225
226 @Test
227 public void testRead3FirstSegment() {
228 append(SEGMENT_SIZE * 2.5);
229 assertArrayEquals(g(0, SEGMENT_SIZE), read(SEGMENT_SIZE));
230 }
231
232 @Test
233 public void testRead3InteriorSegment() {
234 append(SEGMENT_SIZE * 2.5);
235 readOut(SEGMENT_SIZE);
236 assertArrayEquals(g(SEGMENT_SIZE, SEGMENT_SIZE), read(SEGMENT_SIZE));
237 }
238
239 @Test
240 public void testRead3LastSegment() {
241 append(SEGMENT_SIZE * 2.5);
242 readOut(SEGMENT_SIZE * 2);
243 assertArrayEquals(
244 g(SEGMENT_SIZE * 2, SEGMENT_SIZE * 0.5),
245 read(SEGMENT_SIZE));
246 }
247
248 @Test
249 public void testRead3FirstSegmentFirstZeroBytes() {
250 append(SEGMENT_SIZE * 2.5);
251 assertArrayEquals(new byte[0], read(0));
252 }
253
254 @Test
255 public void testRead3FirstSegmentInteriorZeroBytes() {
256 append(SEGMENT_SIZE * 2.5);
257 readOut(SEGMENT_SIZE * 0.5);
258 assertArrayEquals(new byte[0], read(0));
259 }
343 is.read(new byte[10], 1, 10);
344 fail("IndexOutOfBoundsException expected but not thrown");
345 } catch (IndexOutOfBoundsException expected) {}
346 }
347
348 private void testSkipSmallNumberOfBytes(long skip) {
349 int streamSize = (int) (SEGMENT_SIZE * 2.5);
350 int skipCount = streamSize / SEGMENT_SIZE + 1;
351 append(streamSize);
352 int position = 0;
353 for (int i = 0; i < skipCount; i++) {
354 long skipped = is.skip(skip);
355 assertEquals(Math.max(skip, 0), skipped);
356 position += skipped;
357 long len = Math.min(SEGMENT_SIZE - skipped, streamSize - position);
358 assertArrayEquals(g(position, len), read(SEGMENT_SIZE));
359 position += len;
360 }
361 }
362
363 @Test
364 public void testSkipZeroBytes() {
365 testSkipSmallNumberOfBytes(0);
366 }
367
368 @Test
369 public void testSkipMinusOneByte() {
370 testSkipSmallNumberOfBytes(-1);
371 }
372
373 @Test
374 public void testSkipMinusTenBytes() {
375 testSkipSmallNumberOfBytes(-10);
376 }
377
378 @Test
379 public void testSkipIntegerMinValueBytes() {
380 testSkipSmallNumberOfBytes(Integer.MIN_VALUE);
381 }
382
383 @Test
|