You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sqoop.apache.org by ja...@apache.org on 2013/07/27 19:26:25 UTC

git commit: SQOOP-1145: Sqoop2: Use JSON object for serializing MAP input type

Updated Branches:
  refs/heads/sqoop2 8c19c8c33 -> 9f8ac6b6b


SQOOP-1145: Sqoop2: Use JSON object for serializing MAP input type

(Abraham Elmahrek via Jarek Jarcec Cecho)


Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/9f8ac6b6
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/9f8ac6b6
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/9f8ac6b6

Branch: refs/heads/sqoop2
Commit: 9f8ac6b6b27d1fc1f29e7521a6fb6f18884fc4f8
Parents: 8c19c8c
Author: Jarek Jarcec Cecho <ja...@apache.org>
Authored: Sat Jul 27 10:25:50 2013 -0700
Committer: Jarek Jarcec Cecho <ja...@apache.org>
Committed: Sat Jul 27 10:25:50 2013 -0700

----------------------------------------------------------------------
 .../sqoop/json/util/FormSerialization.java      | 23 +++++++--
 .../sqoop/json/util/SerializationError.java     | 41 +++++++++++++++
 .../sqoop/json/util/TestFormSerialization.java  | 52 ++++++++++++++++++++
 3 files changed, 113 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/9f8ac6b6/common/src/main/java/org/apache/sqoop/json/util/FormSerialization.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/json/util/FormSerialization.java b/common/src/main/java/org/apache/sqoop/json/util/FormSerialization.java
index 98768d6..f01bc57 100644
--- a/common/src/main/java/org/apache/sqoop/json/util/FormSerialization.java
+++ b/common/src/main/java/org/apache/sqoop/json/util/FormSerialization.java
@@ -18,6 +18,7 @@
 package org.apache.sqoop.json.util;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.sqoop.common.SqoopException;
 import org.apache.sqoop.model.MBooleanInput;
 import org.apache.sqoop.model.MEnumInput;
 import org.apache.sqoop.model.MForm;
@@ -32,6 +33,7 @@ import org.json.simple.JSONObject;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 /**
  * Convenient static methods for serializing forms.
@@ -113,7 +115,11 @@ public final class FormSerialization {
       // Serialize value if is there
       // Skip if sensitive
       if (!mInput.isEmpty() && !(skipSensitive && mInput.isSensitive())) {
-        input.put(FORM_INPUT_VALUE, mInput.getUrlSafeValueString());
+        if (mInput.getType() == MInputType.MAP) {
+          input.put(FORM_INPUT_VALUE, mInput.getValue());
+        } else {
+          input.put(FORM_INPUT_VALUE, mInput.getUrlSafeValueString());
+        }
       }
 
       mInputs.add(input);
@@ -185,8 +191,19 @@ public final class FormSerialization {
 
       // Propagate form optional value
       if(input.containsKey(FORM_INPUT_VALUE)) {
-        mInput.restoreFromUrlSafeValueString(
-          (String) input.get(FORM_INPUT_VALUE));
+        switch (type) {
+        case MAP:
+          try {
+            mInput.setValue((Map<String, String>)input.get(FORM_INPUT_VALUE));
+          } catch (ClassCastException e) {
+            throw new SqoopException(SerializationError.SERIALIZATION_001, name + " requires a 'map' value.");
+          }
+          break;
+        default:
+          mInput.restoreFromUrlSafeValueString(
+              (String) input.get(FORM_INPUT_VALUE));
+          break;
+        }
       }
       mInputs.add(mInput);
     }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/9f8ac6b6/common/src/main/java/org/apache/sqoop/json/util/SerializationError.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/json/util/SerializationError.java b/common/src/main/java/org/apache/sqoop/json/util/SerializationError.java
new file mode 100644
index 0000000..13989a3
--- /dev/null
+++ b/common/src/main/java/org/apache/sqoop/json/util/SerializationError.java
@@ -0,0 +1,41 @@
+/**
+ * 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.sqoop.json.util;
+
+import org.apache.sqoop.common.ErrorCode;
+
+public enum SerializationError implements ErrorCode {
+
+  SERIALIZATION_001("Attempt to pass a non-map object to MAP type."),
+
+  ;
+
+  private final String message;
+
+  private SerializationError(String message) {
+    this.message = message;
+  }
+
+  public String getCode() {
+    return name();
+  }
+
+  public String getMessage() {
+    return message;
+  }
+}

http://git-wip-us.apache.org/repos/asf/sqoop/blob/9f8ac6b6/common/src/test/java/org/apache/sqoop/json/util/TestFormSerialization.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/sqoop/json/util/TestFormSerialization.java b/common/src/test/java/org/apache/sqoop/json/util/TestFormSerialization.java
index 98a70f1..c4223ec 100644
--- a/common/src/test/java/org/apache/sqoop/json/util/TestFormSerialization.java
+++ b/common/src/test/java/org/apache/sqoop/json/util/TestFormSerialization.java
@@ -17,6 +17,7 @@
  */
 package org.apache.sqoop.json.util;
 
+import org.apache.sqoop.common.SqoopException;
 import org.apache.sqoop.model.MBooleanInput;
 import org.apache.sqoop.model.MEnumInput;
 import org.apache.sqoop.model.MForm;
@@ -74,6 +75,57 @@ public class TestFormSerialization {
     assertEquals("YES", retrieved.getEnumInput("Enum").getValue());
   }
 
+  @Test
+  public void testMapDataType() {
+    MForm form = getMapForm();
+
+    // Inserted values
+    Map<String, String> map = new HashMap<String, String>();
+    map.put("A", "B");
+    form.getMapInput("Map").setValue(map);
+
+    // Serialize
+    JSONObject jsonObject = FormSerialization.extractForm(form, false);
+    String serializedJson = jsonObject.toJSONString();
+
+    // Deserialize
+    JSONObject retrievedJson = (JSONObject) JSONValue.parse(serializedJson);
+    MForm retrieved = FormSerialization.restoreForm(retrievedJson);
+    assertEquals(map, retrieved.getMapInput("Map").getValue());
+  }
+
+  @Test(expected=SqoopException.class)
+  public void testMapDataTypeException() {
+    MForm form = getMapForm();
+
+    // Inserted values
+    Map<String, String> map = new HashMap<String, String>();
+    map.put("A", "B");
+    form.getMapInput("Map").setValue(map);
+
+    // Serialize
+    JSONObject jsonObject = FormSerialization.extractForm(form, false);
+    String serializedJson = jsonObject.toJSONString();
+
+    // Replace map value with a fake string to force exception
+    String badSerializedJson = serializedJson.replace("{\"A\":\"B\"}", "\"nonsensical string\"");
+    System.out.println(badSerializedJson);
+    JSONObject retrievedJson = (JSONObject) JSONValue.parse(badSerializedJson);
+    FormSerialization.restoreForm(retrievedJson);
+  }
+
+  protected MForm getMapForm() {
+    List<MInput<?>> inputs;
+    MInput input;
+
+    inputs = new LinkedList<MInput<?>>();
+
+    input = new MMapInput("Map", false);
+    inputs.add(input);
+
+    return new MForm("f", inputs);
+  }
+
   /**
    * Return form with all data types.
    *