SQOOP-827: Sqoop2: MMapInput is null while retrieving from DB if pass empty map on write

(Mengwei Ding via Jarek Jarcec Cecho)
This commit is contained in:
Jarek Jarcec Cecho 2013-08-19 08:26:52 -07:00
Родитель d140c5ddd0
Коммит 78235e39df
5 изменённых файлов: 74 добавлений и 29 удалений

Просмотреть файл

@ -32,8 +32,8 @@ public final class MMapInput extends MInput<Map<String, String>> {
@Override
public String getUrlSafeValueString() {
Map<String, String> 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 final class MMapInput extends MInput<Map<String, String>> {
@Override
public void restoreFromUrlSafeValueString(String valueString) {
Map<String, String> valueMap = null;
if (valueString != null && valueString.trim().length() > 0) {
valueMap = new HashMap<String, String>();
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<String, String> valueMap = new HashMap<String, String>();
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

Просмотреть файл

@ -19,6 +19,7 @@ package org.apache.sqoop.model;
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 class TestMMapInput {
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());
}
/**

Просмотреть файл

@ -27,6 +27,7 @@ import org.apache.sqoop.model.MFramework;
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 @@ abstract public class DerbyTestCase extends TestCase {
+ " 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 @@ abstract public class DerbyTestCase extends TestCase {
+ " 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 @@ abstract public class DerbyTestCase extends TestCase {
inputs = new LinkedList<MInput<?>>();
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<MInput<?>>();
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));

Просмотреть файл

@ -21,9 +21,12 @@ import org.apache.sqoop.common.SqoopException;
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 class TestConnectionHandling extends DerbyTestCase {
List<MForm> 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<String, String>()); // inject new map value
((MStringInput)forms.get(1).getInputs().get(0)).setValue("Updated");
((MMapInput)forms.get(1).getInputs().get(1)).setValue(new HashMap<String, String>()); // inject new map value
connection.setName("name");
@ -183,10 +192,18 @@ public class TestConnectionHandling extends DerbyTestCase {
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 {

Просмотреть файл

@ -20,9 +20,12 @@ package org.apache.sqoop.repository.derby;
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 class TestJobHandling extends DerbyTestCase {
List<MForm> 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<String, String>()); // inject new map value
((MStringInput)forms.get(1).getInputs().get(0)).setValue("Updated");
((MMapInput)forms.get(1).getInputs().get(1)).setValue(new HashMap<String, String>()); // inject new map value
job.setName("name");
@ -199,10 +208,18 @@ public class TestJobHandling extends DerbyTestCase {
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 {