PollingInputStreamTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.android.acm_serial;
00018 
00019 import static org.junit.Assert.assertArrayEquals;
00020 import static org.junit.Assert.assertEquals;
00021 
00022 import org.junit.Test;
00023 
00024 import java.io.IOException;
00025 import java.io.PipedInputStream;
00026 import java.io.PipedOutputStream;
00027 import java.util.concurrent.Executors;
00028 
00032 public class PollingInputStreamTest {
00033 
00034   @Test
00035   public void testSplitUpWrites() throws IOException {
00036     PipedInputStream pipedInputStream = new PipedInputStream();
00037     PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream);
00038     PollingInputStream pollingInputStream =
00039         new PollingInputStream(pipedInputStream, Executors.newCachedThreadPool());
00040     byte[] expectedBuffer = new byte[64];
00041     for (int i = 0; i < expectedBuffer.length; i++) {
00042       expectedBuffer[i] = (byte) i;
00043     }
00044     pipedOutputStream.write(expectedBuffer, 0, 16);
00045     pipedOutputStream.write(expectedBuffer, 16, 16);
00046     pipedOutputStream.write(expectedBuffer, 32, 16);
00047     pipedOutputStream.write(expectedBuffer, 48, 16);
00048     byte[] actualBuffer = new byte[64];
00049     assertEquals(64, pollingInputStream.read(actualBuffer));
00050     assertArrayEquals(expectedBuffer, actualBuffer);
00051   }
00052 
00053   @Test
00054   public void testSplitUpReads() throws IOException {
00055     PipedInputStream pipedInputStream = new PipedInputStream();
00056     PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream);
00057     PollingInputStream pollingInputStream =
00058         new PollingInputStream(pipedInputStream, Executors.newCachedThreadPool());
00059     byte[] expectedBuffer = new byte[64];
00060     for (int i = 0; i < expectedBuffer.length; i++) {
00061       expectedBuffer[i] = (byte) i;
00062     }
00063     pipedOutputStream.write(expectedBuffer, 0, 64);
00064     byte[] actualBuffer = new byte[64];
00065     assertEquals(32, pollingInputStream.read(actualBuffer, 0, 32));
00066     assertEquals(32, pollingInputStream.read(actualBuffer, 32, 32));
00067     assertArrayEquals(expectedBuffer, actualBuffer);
00068   }
00069 
00070   @Test
00071   public void testInterlevedReadAndWrite() throws IOException {
00072     PipedInputStream pipedInputStream = new PipedInputStream();
00073     PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream);
00074     PollingInputStream pollingInputStream =
00075         new PollingInputStream(pipedInputStream, Executors.newCachedThreadPool());
00076     byte[] expectedBuffer = new byte[64];
00077     for (int i = 0; i < expectedBuffer.length; i++) {
00078       expectedBuffer[i] = (byte) i;
00079     }
00080     byte[] actualBuffer = new byte[64];
00081     pipedOutputStream.write(expectedBuffer, 0, 16);
00082     assertEquals(8, pollingInputStream.read(actualBuffer, 0, 8));
00083     pipedOutputStream.write(expectedBuffer, 16, 48);
00084     int bytesRead = 0;
00085     while (bytesRead < 56) {
00086       bytesRead += pollingInputStream.read(actualBuffer, 8 + bytesRead, 64 - bytesRead);
00087     }
00088     assertArrayEquals(expectedBuffer, actualBuffer);
00089   }
00090 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Jun 6 2019 21:20:07