You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by je...@apache.org on 2013/04/09 21:52:08 UTC
git commit: Fix text line encoder bug + add unit tests
Updated Branches:
refs/heads/trunk 41cdd79bd -> d941e8572
Fix text line encoder bug + add unit tests
Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/d941e857
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/d941e857
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/d941e857
Branch: refs/heads/trunk
Commit: d941e85723d0b742e0c8d70eb02c7104f2c39493
Parents: 41cdd79
Author: Jeff MAURY <je...@apache.org>
Authored: Tue Apr 9 21:51:41 2013 +0200
Committer: Jeff MAURY <je...@apache.org>
Committed: Tue Apr 9 21:51:41 2013 +0200
----------------------------------------------------------------------
.../mina/codec/textline/TextLineEncoder.java | 2 +-
.../codec/textline/UnixTextLineEncoderTest.java | 53 +++++++++++++++
.../codec/textline/WindowsTextLineEncoderTest.java | 53 +++++++++++++++
3 files changed, 107 insertions(+), 1 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina/blob/d941e857/codec/src/main/java/org/apache/mina/codec/textline/TextLineEncoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/textline/TextLineEncoder.java b/codec/src/main/java/org/apache/mina/codec/textline/TextLineEncoder.java
index 6ce02c3..893c87a 100644
--- a/codec/src/main/java/org/apache/mina/codec/textline/TextLineEncoder.java
+++ b/codec/src/main/java/org/apache/mina/codec/textline/TextLineEncoder.java
@@ -138,7 +138,7 @@ public class TextLineEncoder implements StatelessProtocolEncoder<String, ByteBuf
if (value.length() > maxLineLength) {
throw new IllegalArgumentException("Line length: " + message.length());
}
- return charsetEncoder.encode(CharBuffer.wrap(value).append(CharBuffer.wrap(delimiter.getValue())));
+ return charsetEncoder.encode(CharBuffer.wrap(value + delimiter.getValue()));
} catch (CharacterCodingException e) {
throw new RuntimeException(e);
}
http://git-wip-us.apache.org/repos/asf/mina/blob/d941e857/codec/src/test/java/org/apache/mina/codec/textline/UnixTextLineEncoderTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/textline/UnixTextLineEncoderTest.java b/codec/src/test/java/org/apache/mina/codec/textline/UnixTextLineEncoderTest.java
new file mode 100644
index 0000000..a072ec0
--- /dev/null
+++ b/codec/src/test/java/org/apache/mina/codec/textline/UnixTextLineEncoderTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.mina.codec.textline;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.nio.ByteBuffer;
+
+import org.junit.Test;
+
+/**
+ * A {@link TextLineDecoder} test.
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class UnixTextLineEncoderTest {
+
+ @Test
+ public void testThatEmptyBufferReturnsOnlyDelimiter() {
+ TextLineEncoder encoder = new TextLineEncoder(LineDelimiter.UNIX);
+ Void context = encoder.createEncoderState();
+ ByteBuffer result = encoder.encode("", context);
+ assertNotNull(result);
+ assertEquals(1, result.remaining());
+ }
+
+ @Test
+ public void testThatNonEmptyBufferReturnsDataAndDelimiter() {
+ TextLineEncoder encoder = new TextLineEncoder(LineDelimiter.UNIX);
+ Void context = encoder.createEncoderState();
+ ByteBuffer result = encoder.encode("a string", context);
+ assertNotNull(result);
+ assertEquals(9, result.remaining());
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina/blob/d941e857/codec/src/test/java/org/apache/mina/codec/textline/WindowsTextLineEncoderTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/textline/WindowsTextLineEncoderTest.java b/codec/src/test/java/org/apache/mina/codec/textline/WindowsTextLineEncoderTest.java
new file mode 100644
index 0000000..248fa88
--- /dev/null
+++ b/codec/src/test/java/org/apache/mina/codec/textline/WindowsTextLineEncoderTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.mina.codec.textline;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.nio.ByteBuffer;
+
+import org.junit.Test;
+
+/**
+ * A {@link TextLineDecoder} test.
+ *
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class WindowsTextLineEncoderTest {
+
+ @Test
+ public void testThatEmptyBufferReturnsOnlyDelimiter() {
+ TextLineEncoder encoder = new TextLineEncoder(LineDelimiter.WINDOWS);
+ Void context = encoder.createEncoderState();
+ ByteBuffer result = encoder.encode("", context);
+ assertNotNull(result);
+ assertEquals(2, result.remaining());
+ }
+
+ @Test
+ public void testThatNonEmptyBufferReturnsDataAndDelimiter() {
+ TextLineEncoder encoder = new TextLineEncoder(LineDelimiter.WINDOWS);
+ Void context = encoder.createEncoderState();
+ ByteBuffer result = encoder.encode("a string", context);
+ assertNotNull(result);
+ assertEquals(10, result.remaining());
+ }
+}