From 78235e39dfb5c36324739976fa54ec9cfaaba08a Mon Sep 17 00:00:00 2001 From: Jarek Jarcec Cecho Date: Mon, 19 Aug 2013 08:26:52 -0700 Subject: [PATCH] SQOOP-827: Sqoop2: MMapInput is null while retrieving from DB if pass empty map on write (Mengwei Ding via Jarek Jarcec Cecho) --- .../org/apache/sqoop/model/MMapInput.java | 35 ++++++++++--------- .../org/apache/sqoop/model/TestMMapInput.java | 9 ++++- .../sqoop/repository/derby/DerbyTestCase.java | 9 ++--- .../derby/TestConnectionHandling.java | 25 ++++++++++--- .../repository/derby/TestJobHandling.java | 25 ++++++++++--- 5 files changed, 74 insertions(+), 29 deletions(-) diff --git a/common/src/main/java/org/apache/sqoop/model/MMapInput.java b/common/src/main/java/org/apache/sqoop/model/MMapInput.java index de17e2d1..37dd265a 100644 --- a/common/src/main/java/org/apache/sqoop/model/MMapInput.java +++ b/common/src/main/java/org/apache/sqoop/model/MMapInput.java @@ -32,8 +32,8 @@ public MMapInput(String name, boolean sensitive) { @Override public String getUrlSafeValueString() { Map valueMap = getValue(); - if (valueMap == null || valueMap.size() == 0) { - return ""; + if (valueMap == null) { + return null; } boolean first = true; StringBuilder vsb = new StringBuilder(); @@ -51,24 +51,27 @@ public String getUrlSafeValueString() { @Override public void restoreFromUrlSafeValueString(String valueString) { - Map valueMap = null; - if (valueString != null && valueString.trim().length() > 0) { - valueMap = new HashMap(); - String[] valuePairs = valueString.split("&"); - for (String pair : valuePairs) { - String[] nameAndVal = pair.split("="); - if (nameAndVal.length > 0) { - String name = nameAndVal[0]; - String value = null; - if (nameAndVal.length > 1) { - value = nameAndVal[1]; - } + if (valueString == null) { + setValue(null); + } else { + Map valueMap = new HashMap(); + if (valueString.trim().length() > 0) { + String[] valuePairs = valueString.split("&"); + for (String pair : valuePairs) { + String[] nameAndVal = pair.split("="); + if (nameAndVal.length > 0) { + String name = nameAndVal[0]; + String value = null; + if (nameAndVal.length > 1) { + value = nameAndVal[1]; + } - valueMap.put(name, value); + valueMap.put(name, value); + } } } + setValue(valueMap); } - setValue(valueMap); } @Override diff --git a/common/src/test/java/org/apache/sqoop/model/TestMMapInput.java b/common/src/test/java/org/apache/sqoop/model/TestMMapInput.java index eb877cfa..120fb07a 100644 --- a/common/src/test/java/org/apache/sqoop/model/TestMMapInput.java +++ b/common/src/test/java/org/apache/sqoop/model/TestMMapInput.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -82,7 +83,13 @@ public void testUrlSafe() { String tmp = input1.getUrlSafeValueString(); // Restore to actual value input1.restoreFromUrlSafeValueString(tmp); - assertEquals(null, input1.getValue()); + assertNotNull(input1.getValue()); + assertEquals(0, input1.getValue().size()); + + input1.setValue(null); + tmp = input1.getUrlSafeValueString(); + input1.restoreFromUrlSafeValueString(tmp); + assertNull(input1.getValue()); } /** diff --git a/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/DerbyTestCase.java b/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/DerbyTestCase.java index 8a5aee88..a2a0bc73 100644 --- a/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/DerbyTestCase.java +++ b/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/DerbyTestCase.java @@ -27,6 +27,7 @@ import org.apache.sqoop.model.MInput; import org.apache.sqoop.model.MJob; import org.apache.sqoop.model.MJobForms; +import org.apache.sqoop.model.MMapInput; import org.apache.sqoop.model.MStringInput; import java.sql.Connection; @@ -197,7 +198,7 @@ protected void loadConnectorAndFramework() throws Exception { + " VALUES('I1', " + (x * 6 + i * 2 + 1) + ", 0, 'STRING', false, 30)"); runQuery("INSERT INTO SQOOP.SQ_INPUT" +"(SQI_NAME, SQI_FORM, SQI_INDEX, SQI_TYPE, SQI_STRMASK, SQI_STRLENGTH)" - + " VALUES('I2', " + (x * 6 + i * 2 + 1) + ", 1, 'STRING', false, 30)"); + + " VALUES('I2', " + (x * 6 + i * 2 + 1) + ", 1, 'MAP', false, 30)"); // Second form runQuery("INSERT INTO SQOOP.SQ_INPUT" @@ -205,7 +206,7 @@ protected void loadConnectorAndFramework() throws Exception { + " VALUES('I3', " + (x * 6 + i * 2 + 2) + ", 0, 'STRING', false, 30)"); runQuery("INSERT INTO SQOOP.SQ_INPUT" +"(SQI_NAME, SQI_FORM, SQI_INDEX, SQI_TYPE, SQI_STRMASK, SQI_STRLENGTH)" - + " VALUES('I4', " + (x * 6 + i * 2 + 2) + ", 1, 'STRING', false, 30)"); + + " VALUES('I4', " + (x * 6 + i * 2 + 2) + ", 1, 'MAP', false, 30)"); } } } @@ -360,14 +361,14 @@ protected List getForms() { inputs = new LinkedList>(); input = new MStringInput("I1", false, (short)30); inputs.add(input); - input = new MStringInput("I2", false, (short)30); + input = new MMapInput("I2", false); inputs.add(input); forms.add(new MForm("F1", inputs)); inputs = new LinkedList>(); input = new MStringInput("I3", false, (short)30); inputs.add(input); - input = new MStringInput("I4", false, (short)30); + input = new MMapInput("I4", false); inputs.add(input); forms.add(new MForm("F2", inputs)); diff --git a/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/TestConnectionHandling.java b/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/TestConnectionHandling.java index 6959f2d9..f9e92178 100644 --- a/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/TestConnectionHandling.java +++ b/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/TestConnectionHandling.java @@ -21,9 +21,12 @@ import org.apache.sqoop.model.MConnection; import org.apache.sqoop.model.MForm; import org.apache.sqoop.model.MJob; +import org.apache.sqoop.model.MMapInput; import org.apache.sqoop.model.MStringInput; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Test connection methods on Derby repository. @@ -166,10 +169,16 @@ public void testUpdateConnection() throws Exception { List forms; forms = connection.getConnectorPart().getForms(); - ((MStringInput)forms.get(0).getInputs().get(1)).setValue("Injected"); + ((MStringInput)forms.get(0).getInputs().get(0)).setValue("Updated"); + ((MMapInput)forms.get(0).getInputs().get(1)).setValue(null); + ((MStringInput)forms.get(1).getInputs().get(0)).setValue("Updated"); + ((MMapInput)forms.get(1).getInputs().get(1)).setValue(null); forms = connection.getFrameworkPart().getForms(); - ((MStringInput)forms.get(1).getInputs().get(1)).setValue("Injected"); + ((MStringInput)forms.get(0).getInputs().get(0)).setValue("Updated"); + ((MMapInput)forms.get(0).getInputs().get(1)).setValue(new HashMap()); // inject new map value + ((MStringInput)forms.get(1).getInputs().get(0)).setValue("Updated"); + ((MMapInput)forms.get(1).getInputs().get(1)).setValue(new HashMap()); // inject new map value connection.setName("name"); @@ -183,10 +192,18 @@ public void testUpdateConnection() throws Exception { assertEquals("name", connection.getName()); forms = retrieved.getConnectorPart().getForms(); - assertEquals("Injected", forms.get(0).getInputs().get(1).getValue()); + assertEquals("Updated", forms.get(0).getInputs().get(0).getValue()); + assertNull(forms.get(0).getInputs().get(1).getValue()); + assertEquals("Updated", forms.get(1).getInputs().get(0).getValue()); + assertNull(forms.get(1).getInputs().get(1).getValue()); forms = retrieved.getFrameworkPart().getForms(); - assertEquals("Injected", forms.get(1).getInputs().get(1).getValue()); + assertEquals("Updated", forms.get(0).getInputs().get(0).getValue()); + assertNotNull(forms.get(0).getInputs().get(1).getValue()); + assertEquals(((Map)forms.get(0).getInputs().get(1).getValue()).size(), 0); + assertEquals("Updated", forms.get(1).getInputs().get(0).getValue()); + assertNotNull(forms.get(1).getInputs().get(1).getValue()); + assertEquals(((Map)forms.get(1).getInputs().get(1).getValue()).size(), 0); } public void testEnableAndDisableConnection() throws Exception { diff --git a/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/TestJobHandling.java b/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/TestJobHandling.java index a3d804e9..4325c5c8 100644 --- a/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/TestJobHandling.java +++ b/repository/repository-derby/src/test/java/org/apache/sqoop/repository/derby/TestJobHandling.java @@ -20,9 +20,12 @@ import org.apache.sqoop.common.SqoopException; import org.apache.sqoop.model.MForm; import org.apache.sqoop.model.MJob; +import org.apache.sqoop.model.MMapInput; import org.apache.sqoop.model.MStringInput; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Test job methods on Derby repository. @@ -182,10 +185,16 @@ public void testUpdateJob() throws Exception { List forms; forms = job.getConnectorPart().getForms(); - ((MStringInput)forms.get(0).getInputs().get(1)).setValue("Injected"); + ((MStringInput)forms.get(0).getInputs().get(0)).setValue("Updated"); + ((MMapInput)forms.get(0).getInputs().get(1)).setValue(null); + ((MStringInput)forms.get(1).getInputs().get(0)).setValue("Updated"); + ((MMapInput)forms.get(1).getInputs().get(1)).setValue(null); forms = job.getFrameworkPart().getForms(); - ((MStringInput)forms.get(1).getInputs().get(1)).setValue("Injected"); + ((MStringInput)forms.get(0).getInputs().get(0)).setValue("Updated"); + ((MMapInput)forms.get(0).getInputs().get(1)).setValue(new HashMap()); // inject new map value + ((MStringInput)forms.get(1).getInputs().get(0)).setValue("Updated"); + ((MMapInput)forms.get(1).getInputs().get(1)).setValue(new HashMap()); // inject new map value job.setName("name"); @@ -199,10 +208,18 @@ public void testUpdateJob() throws Exception { assertEquals("name", retrieved.getName()); forms = retrieved.getConnectorPart().getForms(); - assertEquals("Injected", forms.get(0).getInputs().get(1).getValue()); + assertEquals("Updated", forms.get(0).getInputs().get(0).getValue()); + assertNull(forms.get(0).getInputs().get(1).getValue()); + assertEquals("Updated", forms.get(1).getInputs().get(0).getValue()); + assertNull(forms.get(1).getInputs().get(1).getValue()); forms = retrieved.getFrameworkPart().getForms(); - assertEquals("Injected", forms.get(1).getInputs().get(1).getValue()); + assertEquals("Updated", forms.get(0).getInputs().get(0).getValue()); + assertNotNull(forms.get(0).getInputs().get(1).getValue()); + assertEquals(((Map)forms.get(0).getInputs().get(1).getValue()).size(), 0); + assertEquals("Updated", forms.get(1).getInputs().get(0).getValue()); + assertNotNull(forms.get(1).getInputs().get(1).getValue()); + assertEquals(((Map)forms.get(1).getInputs().get(1).getValue()).size(), 0); } public void testEnableAndDisableJob() throws Exception {