Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 47EA8E214 for ; Tue, 29 Jan 2013 15:57:27 +0000 (UTC) Received: (qmail 35870 invoked by uid 500); 29 Jan 2013 15:57:27 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 35742 invoked by uid 500); 29 Jan 2013 15:57:26 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 35732 invoked by uid 99); 29 Jan 2013 15:57:26 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 29 Jan 2013 15:57:26 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 29 Jan 2013 15:57:25 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 6B48B2388BFF; Tue, 29 Jan 2013 15:57:06 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1439958 - /commons/proper/net/trunk/src/test/java/org/apache/commons/net/io/ToNetASCIIInputStreamTest.java Date: Tue, 29 Jan 2013 15:57:06 -0000 To: commits@commons.apache.org From: sebb@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130129155706.6B48B2388BFF@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: sebb Date: Tue Jan 29 15:57:06 2013 New Revision: 1439958 URL: http://svn.apache.org/viewvc?rev=1439958&view=rev Log: Add test for ToNetAsciiInput Added: commons/proper/net/trunk/src/test/java/org/apache/commons/net/io/ToNetASCIIInputStreamTest.java (with props) Added: commons/proper/net/trunk/src/test/java/org/apache/commons/net/io/ToNetASCIIInputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/test/java/org/apache/commons/net/io/ToNetASCIIInputStreamTest.java?rev=1439958&view=auto ============================================================================== --- commons/proper/net/trunk/src/test/java/org/apache/commons/net/io/ToNetASCIIInputStreamTest.java (added) +++ commons/proper/net/trunk/src/test/java/org/apache/commons/net/io/ToNetASCIIInputStreamTest.java Tue Jan 29 15:57:06 2013 @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.commons.net.io; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; +import org.junit.Assert; +import org.junit.Test; + +public class ToNetASCIIInputStreamTest { + + private static final Charset ASCII = Charset.forName("ASCII"); + + @Test + public void testToNetASCIIInputStream1() throws Exception + { + byteTest(false, "", ""); + byteTest(false, "\r", "\r"); + byteTest(false, "a", "a"); + byteTest(false, "a\nb", "a\r\nb"); + byteTest(false, "a\r\nb", "a\r\nb"); + byteTest(false, "\n", "\r\n"); + byteTest(false, "Hello\nWorld\n", "Hello\r\nWorld\r\n"); + byteTest(false, "Hello\nWorld\r\n", "Hello\r\nWorld\r\n"); + byteTest(false, "Hello\nWorld\n\r", "Hello\r\nWorld\r\n\r"); + } + + @Test + public void testToNetASCIIInputStream_single_bytes() throws Exception + { + byteTest(true, "", ""); + byteTest(true, "\r", "\r"); + byteTest(true, "\n", "\r\n"); + byteTest(true, "a", "a"); + byteTest(true, "a\nb", "a\r\nb"); + byteTest(true, "a\r\nb", "a\r\nb"); + byteTest(true, "Hello\nWorld\n", "Hello\r\nWorld\r\n"); + byteTest(true, "Hello\nWorld\r\n", "Hello\r\nWorld\r\n"); + byteTest(true, "Hello\nWorld\n\r", "Hello\r\nWorld\r\n\r"); + } + + private void byteTest(boolean byByte, String input, String expect) throws IOException { + byte[] data = input.getBytes(ASCII); + byte[] expected = expect.getBytes(ASCII); + InputStream source = new ByteArrayInputStream(data); + ToNetASCIIInputStream toNetASCII = new ToNetASCIIInputStream(source); + byte[] output = new byte[data.length*2]; // cannot be longer than twice the input + + int length = byByte ? + getSingleBytes(toNetASCII, output) : + getBuffer(toNetASCII, output); + + byte[] result = new byte[length]; + System.arraycopy(output, 0, result, 0, length); + Assert.assertArrayEquals("Failed converting "+input,expected, result); + toNetASCII.close(); + } + + private int getSingleBytes(ToNetASCIIInputStream toNetASCII, byte[] output) + throws IOException { + int b; + int length=0; + while((b=toNetASCII.read()) != -1) { + output[length++]=(byte)b; + } + return length; + } + + private int getBuffer(ToNetASCIIInputStream toNetASCII, byte[] output) + throws IOException { + int length=0; + int remain=output.length; + int chunk; + int offset=0; + while(remain > 0 && (chunk=toNetASCII.read(output,offset,remain)) != -1){ + length+=chunk; + offset+=chunk; + remain-=chunk; + } + return length; + } + +} Propchange: commons/proper/net/trunk/src/test/java/org/apache/commons/net/io/ToNetASCIIInputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/net/trunk/src/test/java/org/apache/commons/net/io/ToNetASCIIInputStreamTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision