5
0
mirror of https://github.com/apache/sqoop.git synced 2025-05-20 19:00:48 +08:00

SQOOP-1761: Sqoop2: Unit tests for different Column sub classes Array/Set and Map types

(Veena Basavaraj via Abraham Elmahrek)
This commit is contained in:
Abraham Elmahrek 2014-11-18 18:41:29 -08:00
parent 6bd8fe3e93
commit deacc90c49
9 changed files with 279 additions and 45 deletions

View File

@ -260,7 +260,7 @@ private static Column restoreColumn(JSONObject obj) {
output = new Map(name, key, value); output = new Map(name, key, value);
break; break;
case SET: case SET:
output = new Set(name).setListType(listType); output = new Set(name, listType);
break; break;
case TEXT: case TEXT:
charSize = (Long) obj.get(CHAR_SIZE); charSize = (Long) obj.get(CHAR_SIZE);

View File

@ -23,6 +23,7 @@
public abstract class AbstractComplexListType extends AbstractComplexType { public abstract class AbstractComplexListType extends AbstractComplexType {
// represents the type of the list elements // represents the type of the list elements
// NOTE: required for Array/Set, optional for Enum
Column listType; Column listType;
public AbstractComplexListType(String name) { public AbstractComplexListType(String name) {
@ -54,28 +55,29 @@ public String toString() {
return new StringBuilder(super.toString()).append(",listType=").append(listType).toString(); return new StringBuilder(super.toString()).append(",listType=").append(listType).toString();
} }
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof AbstractComplexListType))
return false;
if (!super.equals(o))
return false;
AbstractComplexListType that = (AbstractComplexListType) o;
if (listType != null ? !listType.equals(that.listType) : that.listType != null)
return false;
return true;
}
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31;
int result = super.hashCode(); int result = super.hashCode();
result = 31 * result + (listType != null ? listType.hashCode() : 0); result = prime * result + ((listType == null) ? 0 : listType.hashCode());
return result; return result;
} }
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
AbstractComplexListType other = (AbstractComplexListType) obj;
if (listType == null) {
if (other.listType != null)
return false;
} else if (!listType.equals(other.listType))
return false;
return true;
}
} }

View File

@ -58,6 +58,8 @@ public ColumnType getType() {
public String toString() { public String toString() {
return new StringBuilder("Array{"). return new StringBuilder("Array{").
append(super.toString()). append(super.toString()).
append(", size=").
append(size).
append("}").toString(); append("}").toString();
} }

View File

@ -66,30 +66,34 @@ public String toString() {
} }
@Override @Override
public boolean equals(Object o) { public int hashCode() {
if (this == o) final int prime = 31;
return true; int result = super.hashCode();
if (!(o instanceof Map)) result = prime * result + ((key == null) ? 0 : key.hashCode());
return false; result = prime * result + ((value == null) ? 0 : value.hashCode());
if (!super.equals(o)) return result;
return false;
Map map = (Map) o;
if (key != null ? !key.equals(map.key) : map.key != null)
return false;
if (value != null ? !value.equals(map.value) : map.value != null)
return false;
return true;
} }
@Override @Override
public int hashCode() { public boolean equals(Object obj) {
int result = super.hashCode(); if (this == obj)
result = 31 * result + (key != null ? key.hashCode() : 0); return true;
result = 31 * result + (value != null ? value.hashCode() : 0); if (!super.equals(obj))
return result; return false;
if (getClass() != obj.getClass())
return false;
Map other = (Map) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
} }
} }

View File

@ -24,10 +24,6 @@
*/ */
public class Set extends AbstractComplexListType { public class Set extends AbstractComplexListType {
public Set(String name) {
super(name);
}
public Set(String name, Column listType) { public Set(String name, Column listType) {
super(name, listType); super(name, listType);
} }

View File

@ -0,0 +1,57 @@
/**
* 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.schema.type;
import static org.junit.Assert.*;
import org.junit.Test;
public class TestArray {
@Test
public void testArrayWithSameListType() {
Array a1 = new Array("A", new Text("T"));
Array a2 = new Array("A", new Text("T"));
assertTrue(a1.equals(a2));
assertEquals(a1.toString(), a2.toString());
}
@Test
public void testArrayWithDifferentName() {
Array a1 = new Array("A", new Text("T"));
Array a2 = new Array("B", new Text("T"));
assertFalse(a1.equals(a2));
assertNotEquals(a1.toString(), a2.toString());
}
@Test
public void testArrayWithDifferentSize() {
Array a1 = new Array("A", new Text("T")).setSize(22L);
Array a2 = new Array("A", new Text("T")).setSize(2333L);
assertFalse(a1.equals(a2));
assertNotEquals(a1.toString(), a2.toString());
}
@Test
public void testArrayWithDifferentListType() {
Array a1 = new Array("A", new Text("T"));
Array a2 = new Array("A", new Binary("B"));
assertFalse(a1.equals(a2));
assertNotEquals(a1.toString(), a2.toString());
}
}

View File

@ -0,0 +1,67 @@
/**
* 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.schema.type;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import org.junit.Test;
public class TestEnum {
@Test
public void testEnumWithNoOptions() {
Enum e1 = new Enum("A");
Enum e2 = new Enum("A");
assertTrue(e1.equals(e2));
assertEquals(e1.toString(), e2.toString());
}
@Test
public void testEnumWithDifferentOptions() {
Enum e1 = new Enum("A").setOptions(Collections.unmodifiableSet(new HashSet<String>(Arrays
.asList(new String[] { "A", "B" }))));
Enum e2 = new Enum("A").setOptions(Collections.unmodifiableSet(new HashSet<String>(Arrays
.asList(new String[] { "A1", "B1" }))));
assertFalse(e1.equals(e2));
assertNotEquals(e1.toString(), e2.toString());
}
@Test
public void testEnumWithSameOptions() {
Enum e1 = new Enum("A").setOptions(Collections.unmodifiableSet(new HashSet<String>(Arrays
.asList(new String[] { "A", "B" }))));
Enum e2 = new Enum("A").setOptions(Collections.unmodifiableSet(new HashSet<String>(Arrays
.asList(new String[] { "A", "B" }))));
assertTrue(e1.equals(e2));
assertEquals(e1.toString(), e2.toString());
}
@Test
public void testEnumWithDifferentListType() {
Enum e1 = new Enum("A");
e1.setListType(new Text("T"));
Enum e2 = new Enum("A");
e2.setListType(new Binary("B"));
assertFalse(e1.equals(e2));
assertNotEquals(e1.toString(), e2.toString());
}
}

View File

@ -0,0 +1,57 @@
/**
* 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.schema.type;
import static org.junit.Assert.*;
import org.junit.Test;
public class TestMap {
@Test
public void testMapWithSameListKeyValue() {
Map m1 = new Map("m1", new Text("T"), new Text("T"));
Map m2 = new Map("m1", new Text("T"), new Text("T"));
assertTrue(m1.equals(m2));
assertEquals(m1.toString(), m2.toString());
}
@Test
public void testMapWithDifferentName() {
Map m1 = new Map("m1", new Text("T"), new Text("T"));
Map m2 = new Map("m2", new Text("T"), new Text("T"));
assertFalse(m1.equals(m2));
assertNotEquals(m1.toString(), m2.toString());
}
@Test
public void testMapWithDifferentKey() {
Map m1 = new Map("m1", new Text("T"), new Text("T"));
Map m2 = new Map("m1", new Text("T2"), new Text("T"));
assertFalse(m1.equals(m2));
assertNotEquals(m1.toString(), m2.toString());
}
@Test
public void testMapWithDifferentValue() {
Map m1 = new Map("m1", new Text("T"), new Text("T2"));
Map m2 = new Map("m1", new Text("T2"), new Text("T4"));
assertFalse(m1.equals(m2));
assertNotEquals(m1.toString(), m2.toString());
}
}

View File

@ -0,0 +1,49 @@
/**
* 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.schema.type;
import static org.junit.Assert.*;
import org.junit.Test;
public class TestSet {
@Test
public void testSetWithSameListType() {
Set a1 = new Set("A", new Text("T"));
Set a2 = new Set("A", new Text("T"));
assertTrue(a1.equals(a2));
assertEquals(a1.toString(), a2.toString());
}
@Test
public void testSetWithDifferentName() {
Set a1 = new Set("A", new Text("T"));
Set a2 = new Set("B", new Text("T"));
assertFalse(a1.equals(a2));
assertNotEquals(a1.toString(), a2.toString());
}
@Test
public void testSetWithDifferentListType() {
Set a1 = new Set("A", new Text("T"));
Set a2 = new Set("A", new Binary("B"));
assertFalse(a1.equals(a2));
assertNotEquals(a1.toString(), a2.toString());
}
}