Updated Branches:
  refs/heads/master cb31cb1bf -> e949c810b

SAMZA-55; make string serde handle null objects.


Project: http://git-wip-us.apache.org/repos/asf/incubator-samza/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-samza/commit/e949c810
Tree: http://git-wip-us.apache.org/repos/asf/incubator-samza/tree/e949c810
Diff: http://git-wip-us.apache.org/repos/asf/incubator-samza/diff/e949c810

Branch: refs/heads/master
Commit: e949c810b517a1fb1c01fdc8fd74221de8e076ed
Parents: cb31cb1
Author: Chris Riccomini <[email protected]>
Authored: Fri Oct 11 15:22:53 2013 -0700
Committer: Chris Riccomini <[email protected]>
Committed: Fri Oct 11 15:22:53 2013 -0700

----------------------------------------------------------------------
 .../apache/samza/serializers/StringSerde.scala  | 10 ++++--
 .../samza/serializers/TestStringSerde.scala     | 38 ++++++++++++++++++++
 2 files changed, 46 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-samza/blob/e949c810/samza-core/src/main/scala/org/apache/samza/serializers/StringSerde.scala
----------------------------------------------------------------------
diff --git 
a/samza-core/src/main/scala/org/apache/samza/serializers/StringSerde.scala 
b/samza-core/src/main/scala/org/apache/samza/serializers/StringSerde.scala
index 45cde95..7b4b23d 100644
--- a/samza-core/src/main/scala/org/apache/samza/serializers/StringSerde.scala
+++ b/samza-core/src/main/scala/org/apache/samza/serializers/StringSerde.scala
@@ -31,9 +31,15 @@ class StringSerdeFactory extends SerdeFactory[String] {
 }
 
 class StringSerde(val encoding: String) extends Serde[String] {
-  def toBytes(obj: String): Array[Byte] =
+  def toBytes(obj: String): Array[Byte] = if (obj != null) {
     obj.toString.getBytes(encoding)
+  } else {
+    null
+  }
 
-  def fromBytes(bytes: Array[Byte]): String =
+  def fromBytes(bytes: Array[Byte]): String = if (bytes != null) {
     new String(bytes, 0, bytes.size, encoding)
+  } else {
+    null
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-samza/blob/e949c810/samza-core/src/test/scala/org/apache/samza/serializers/TestStringSerde.scala
----------------------------------------------------------------------
diff --git 
a/samza-core/src/test/scala/org/apache/samza/serializers/TestStringSerde.scala 
b/samza-core/src/test/scala/org/apache/samza/serializers/TestStringSerde.scala
new file mode 100644
index 0000000..7fbf0c2
--- /dev/null
+++ 
b/samza-core/src/test/scala/org/apache/samza/serializers/TestStringSerde.scala
@@ -0,0 +1,38 @@
+/*
+ * 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.samza.serializers
+
+import org.junit.Assert._
+import org.junit.Test
+import java.util.Arrays
+
+class TestStringSerde {
+  @Test
+  def testStringSerde {
+    val serde = new StringSerde("UTF-8")
+    assertEquals(null, serde.toBytes(null))
+    assertEquals(null, serde.fromBytes(null))
+
+    val fooBar = "foo bar"
+    val fooBarBytes = serde.toBytes(fooBar)
+    assertTrue(Arrays.equals(fooBar.getBytes("UTF-8"), fooBarBytes))
+    assertEquals(fooBar, serde.fromBytes(fooBarBytes))
+  }
+}
\ No newline at end of file

Reply via email to