mirror of
https://github.com/apache/sqoop.git
synced 2025-05-20 19:00:48 +08:00
SQOOP-774: exception is thrown when creating a connection object if
connectionString starts with an empty space (Jarek Jarcec Cecho via Cheolsoo Park)
This commit is contained in:
parent
61d9f39e89
commit
21a3943678
@ -94,9 +94,9 @@ private JSONObject extractValidation(Validation validation) {
|
||||
object.put(STATUS, validation.getStatus().name());
|
||||
|
||||
JSONObject jsonMessages = new JSONObject();
|
||||
Map<String, Validation.Message> messages = validation.getMessages();
|
||||
Map<Validation.FormInput, Validation.Message> messages = validation.getMessages();
|
||||
|
||||
for(Map.Entry<String, Validation.Message> entry : messages.entrySet()) {
|
||||
for(Map.Entry<Validation.FormInput, Validation.Message> entry : messages.entrySet()) {
|
||||
JSONObject jsonEntry = new JSONObject();
|
||||
jsonEntry.put(STATUS, entry.getValue().getStatus().name());
|
||||
jsonEntry.put(MESSAGE, entry.getValue().getMessage());
|
||||
@ -121,8 +121,8 @@ public void restore(JSONObject jsonObject) {
|
||||
|
||||
public Validation restoreValidation(JSONObject jsonObject) {
|
||||
JSONObject jsonMessages = (JSONObject) jsonObject.get(MESSAGES);
|
||||
Map<String, Validation.Message> messages
|
||||
= new HashMap<String, Validation.Message>();
|
||||
Map<Validation.FormInput, Validation.Message> messages
|
||||
= new HashMap<Validation.FormInput, Validation.Message>();
|
||||
|
||||
for(Object key : jsonMessages.keySet()) {
|
||||
JSONObject jsonMessage = (JSONObject) jsonMessages.get(key);
|
||||
@ -133,11 +133,11 @@ public Validation restoreValidation(JSONObject jsonObject) {
|
||||
Validation.Message message
|
||||
= new Validation.Message(status, stringMessage);
|
||||
|
||||
messages.put((String)key, message);
|
||||
messages.put(new Validation.FormInput((String)key), message);
|
||||
}
|
||||
|
||||
Status status = Status.valueOf((String) jsonObject.get(STATUS));
|
||||
|
||||
return new Validation(status,messages);
|
||||
return new Validation(status, messages);
|
||||
}
|
||||
}
|
||||
|
@ -255,13 +255,13 @@ public static void fromForms(List<MForm> forms, Object configuration) {
|
||||
* @param validation Validation that we should apply
|
||||
*/
|
||||
public static void applyValidation(List<MForm> forms, Validation validation) {
|
||||
Map<String, Validation.Message> messages = validation.getMessages();
|
||||
Map<Validation.FormInput, Validation.Message> messages = validation.getMessages();
|
||||
|
||||
for(MForm form : forms) {
|
||||
for(MInput input : form.getInputs()) {
|
||||
String inputName = input.getName();
|
||||
if(messages.containsKey(inputName)){
|
||||
Validation.Message message = messages.get(inputName);
|
||||
Validation.FormInput fi = new Validation.FormInput(input.getName());
|
||||
if(messages.containsKey(fi)) {
|
||||
Validation.Message message = messages.get(fi);
|
||||
|
||||
input.setValidationMessage(message.getStatus(), message.getMessage());
|
||||
} else {
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
import org.apache.sqoop.common.SqoopException;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ -34,7 +35,7 @@ public class Validation {
|
||||
Status status;
|
||||
|
||||
// Status messages for various fields
|
||||
Map<String, Message> messages;
|
||||
Map<FormInput, Message> messages;
|
||||
|
||||
private Validation() {
|
||||
klass = null;
|
||||
@ -44,9 +45,9 @@ public Validation(Class klass) {
|
||||
|
||||
this.klass = klass;
|
||||
status = Status.getDefault();
|
||||
messages = new HashMap<String, Message>();
|
||||
messages = new HashMap<FormInput, Message>();
|
||||
}
|
||||
public Validation(Status status, Map<String, Message> messages) {
|
||||
public Validation(Status status, Map<FormInput, Message> messages) {
|
||||
this();
|
||||
|
||||
this.status = status;
|
||||
@ -57,25 +58,35 @@ public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public Map<String, Message> getMessages() {
|
||||
public Map<FormInput, Message> getMessages() {
|
||||
return messages;
|
||||
}
|
||||
|
||||
public void addMessage(Status status, String field, String message ) {
|
||||
public void addMessage(Status status, String form, String field, String message ) {
|
||||
if( klass == null) {
|
||||
throw new SqoopException(ValidationError.VALIDATION_0001);
|
||||
}
|
||||
|
||||
// Verify that this is valid field in configuration object
|
||||
Field formField;
|
||||
|
||||
// Verify that such form exists
|
||||
try {
|
||||
klass.getDeclaredField(field);
|
||||
formField = klass.getDeclaredField(form);
|
||||
} catch (NoSuchFieldException e) {
|
||||
throw new SqoopException(ValidationError.VALIDATION_0002,
|
||||
"Field " + field + " is not present in " + klass.getName());
|
||||
"Can't get form " + form + " from " + klass.getName(), e);
|
||||
}
|
||||
|
||||
// Verify that such input exists on given form
|
||||
try {
|
||||
formField.getType().getDeclaredField(field);
|
||||
} catch (NoSuchFieldException e) {
|
||||
throw new SqoopException(ValidationError.VALIDATION_0002,
|
||||
"Can't get input " + field + " from form" + formField.getType().getName(), e);
|
||||
}
|
||||
|
||||
this.status = Status.getWorstStatus(this.status, status);
|
||||
messages.put(field, new Message(status, message));
|
||||
messages.put(new FormInput(form, field), new Message(status, message));
|
||||
}
|
||||
|
||||
public static class Message {
|
||||
@ -121,4 +132,60 @@ public String toString() {
|
||||
return "{" + status.name() + ": " + message + "}";
|
||||
}
|
||||
}
|
||||
|
||||
public static class FormInput {
|
||||
private String form;
|
||||
private String input;
|
||||
|
||||
public FormInput(String form, String input) {
|
||||
this.form = form;
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
public FormInput(String formInput) {
|
||||
String []parts = formInput.split("\\.");
|
||||
if(parts.length != 2) {
|
||||
throw new SqoopException(ValidationError.VALIDATION_0003,
|
||||
"Specification " + formInput + " is not in valid format form.input");
|
||||
}
|
||||
|
||||
this.form = parts[0];
|
||||
this.input = parts[1];
|
||||
}
|
||||
|
||||
public String getForm() {
|
||||
return form;
|
||||
}
|
||||
|
||||
public String getInput() {
|
||||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
FormInput formInput = (FormInput) o;
|
||||
|
||||
if (form != null ? !form.equals(formInput.form) : formInput.form != null)
|
||||
return false;
|
||||
if (input != null ? !input.equals(formInput.input) : formInput.input != null)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = form != null ? form.hashCode() : 0;
|
||||
result = 31 * result + (input != null ? input.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return form + "." + input;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,6 +30,7 @@ public enum ValidationError implements ErrorCode {
|
||||
|
||||
VALIDATION_0002("Usage of missing field"),
|
||||
|
||||
VALIDATION_0003("Invalid representation of form and input field"),
|
||||
;
|
||||
|
||||
private final String message;
|
||||
|
@ -52,19 +52,22 @@ public void testSerialization() {
|
||||
|
||||
assertNull(retrievedBean.getId());
|
||||
|
||||
Validation.FormInput fa = new Validation.FormInput("f", "i");
|
||||
Validation.FormInput fb = new Validation.FormInput("f2", "i2");
|
||||
|
||||
Validation connector = retrievedBean.getConnectorValidation();
|
||||
assertEquals(Status.FINE, connector.getStatus());
|
||||
assertEquals(2, connector.getMessages().size());
|
||||
assertTrue(connector.getMessages().containsKey("a"));
|
||||
assertTrue(connector.getMessages().containsKey(fa));
|
||||
assertEquals(new Validation.Message(Status.FINE, "d"),
|
||||
connector.getMessages().get("a"));
|
||||
connector.getMessages().get(fa));
|
||||
|
||||
Validation framework = retrievedBean.getFrameworkValidation();
|
||||
assertEquals(Status.UNACCEPTABLE, framework.getStatus());
|
||||
assertEquals(2, framework.getMessages().size());
|
||||
assertTrue(framework.getMessages().containsKey("b"));
|
||||
assertTrue(framework.getMessages().containsKey(fb));
|
||||
assertEquals(new Validation.Message(Status.UNACCEPTABLE, "c"),
|
||||
framework.getMessages().get("b"));
|
||||
framework.getMessages().get(fb));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -89,11 +92,15 @@ public void testId() {
|
||||
}
|
||||
|
||||
public Validation getValidation(Status status) {
|
||||
Map<String, Validation.Message> messages =
|
||||
new HashMap<String, Validation.Message>();
|
||||
Map<Validation.FormInput, Validation.Message> messages =
|
||||
new HashMap<Validation.FormInput, Validation.Message>();
|
||||
|
||||
messages.put("a", new Validation.Message(status, "d"));
|
||||
messages.put("b", new Validation.Message(status, "c"));
|
||||
messages.put(
|
||||
new Validation.FormInput("f", "i"),
|
||||
new Validation.Message(status, "d"));
|
||||
messages.put(
|
||||
new Validation.FormInput("f2", "i2"),
|
||||
new Validation.Message(status, "c"));
|
||||
|
||||
return new Validation(status, messages);
|
||||
}
|
||||
|
@ -115,11 +115,15 @@ public void testApplyValidation() {
|
||||
}
|
||||
|
||||
protected Validation getValidation() {
|
||||
Map<String, Validation.Message> messages
|
||||
= new HashMap<String, Validation.Message>();
|
||||
Map<Validation.FormInput, Validation.Message> messages
|
||||
= new HashMap<Validation.FormInput, Validation.Message>();
|
||||
|
||||
messages.put("aForm.a1", new Validation.Message(Status.ACCEPTABLE, "e1"));
|
||||
messages.put("aForm.a2", new Validation.Message(Status.UNACCEPTABLE, "e2"));
|
||||
messages.put(
|
||||
new Validation.FormInput("aForm", "a1"),
|
||||
new Validation.Message(Status.ACCEPTABLE, "e1"));
|
||||
messages.put(
|
||||
new Validation.FormInput("aForm", "a2"),
|
||||
new Validation.Message(Status.UNACCEPTABLE, "e2"));
|
||||
|
||||
return new Validation(Status.UNACCEPTABLE, messages);
|
||||
}
|
||||
|
@ -34,7 +34,8 @@ public Validation validateConnection(Object configuration) {
|
||||
|
||||
if(config.connection.connectionString == null
|
||||
|| !config.connection.connectionString.startsWith("jdbc:")) {
|
||||
validation.addMessage(Status.UNACCEPTABLE, "connectionString",
|
||||
validation.addMessage(Status.UNACCEPTABLE,
|
||||
"connection", "connectionString",
|
||||
"This do not seem as a valid JDBC URL");
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user