|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This program is licensed to you under the Apache License Version 2.0, |
| 5 | + * and you may not use this file except in compliance with the Apache License Version 2.0. |
| 6 | + * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, |
| 9 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 10 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 12 | + */ |
| 13 | + |
| 14 | +package com.ning.http.client.generators; |
| 15 | + |
| 16 | +import com.ning.http.client.Body; |
| 17 | + |
| 18 | +import org.testng.annotations.Test; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.ByteBuffer; |
| 22 | +import java.util.Random; |
| 23 | + |
| 24 | +import static org.testng.Assert.assertEquals; |
| 25 | +import static org.testng.Assert.assertTrue; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Bryan Davis [email protected] |
| 29 | + */ |
| 30 | +public class ByteArrayBodyGeneratorTest { |
| 31 | + |
| 32 | + private final Random random = new Random(); |
| 33 | + private final int chunkSize = 1024 * 8; |
| 34 | + |
| 35 | + @Test(groups = "standalone") |
| 36 | + public void testSingleRead() throws IOException { |
| 37 | + final int srcArraySize = chunkSize - 1; |
| 38 | + final byte[] srcArray = new byte[srcArraySize]; |
| 39 | + random.nextBytes(srcArray); |
| 40 | + |
| 41 | + final ByteArrayBodyGenerator babGen = |
| 42 | + new ByteArrayBodyGenerator(srcArray); |
| 43 | + final Body body = babGen.createBody(); |
| 44 | + |
| 45 | + final ByteBuffer chunkBuffer = ByteBuffer.allocate(chunkSize); |
| 46 | + |
| 47 | + // should take 1 read to get through the srcArray |
| 48 | + assertEquals(body.read(chunkBuffer), srcArraySize); |
| 49 | + assertEquals(chunkBuffer.position(), srcArraySize, "bytes read"); |
| 50 | + chunkBuffer.clear(); |
| 51 | + |
| 52 | + assertEquals(body.read(chunkBuffer), -1, "body at EOF"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test(groups = "standalone") |
| 56 | + public void testMultipleReads() throws IOException { |
| 57 | + final int srcArraySize = (3 * chunkSize) + 42; |
| 58 | + final byte[] srcArray = new byte[srcArraySize]; |
| 59 | + random.nextBytes(srcArray); |
| 60 | + |
| 61 | + final ByteArrayBodyGenerator babGen = |
| 62 | + new ByteArrayBodyGenerator(srcArray); |
| 63 | + final Body body = babGen.createBody(); |
| 64 | + |
| 65 | + final ByteBuffer chunkBuffer = ByteBuffer.allocate(chunkSize); |
| 66 | + |
| 67 | + int reads = 0; |
| 68 | + int bytesRead = 0; |
| 69 | + while (body.read(chunkBuffer) != -1) { |
| 70 | + reads += 1; |
| 71 | + bytesRead += chunkBuffer.position(); |
| 72 | + chunkBuffer.clear(); |
| 73 | + } |
| 74 | + assertEquals(reads, 4, "reads to drain generator"); |
| 75 | + assertEquals(bytesRead, srcArraySize, "bytes read"); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments