Repository: sqoop
Updated Branches:
  refs/heads/sqoop2 6bd8fe3e9 -> deacc90c4


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

(Veena Basavaraj via Abraham Elmahrek)


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

Branch: refs/heads/sqoop2
Commit: deacc90c4909e851f6a3351bc0a1ca1d58fd13de
Parents: 6bd8fe3
Author: Abraham Elmahrek <[email protected]>
Authored: Tue Nov 18 18:41:29 2014 -0800
Committer: Abraham Elmahrek <[email protected]>
Committed: Tue Nov 18 18:41:29 2014 -0800

----------------------------------------------------------------------
 .../sqoop/json/util/SchemaSerialization.java    |  2 +-
 .../schema/type/AbstractComplexListType.java    | 34 +++++-----
 .../org/apache/sqoop/schema/type/Array.java     |  2 +
 .../java/org/apache/sqoop/schema/type/Map.java  | 40 ++++++------
 .../java/org/apache/sqoop/schema/type/Set.java  |  4 --
 .../org/apache/sqoop/schema/type/TestArray.java | 57 +++++++++++++++++
 .../org/apache/sqoop/schema/type/TestEnum.java  | 67 ++++++++++++++++++++
 .../org/apache/sqoop/schema/type/TestMap.java   | 57 +++++++++++++++++
 .../org/apache/sqoop/schema/type/TestSet.java   | 49 ++++++++++++++
 9 files changed, 273 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/deacc90c/common/src/main/java/org/apache/sqoop/json/util/SchemaSerialization.java
----------------------------------------------------------------------
diff --git 
a/common/src/main/java/org/apache/sqoop/json/util/SchemaSerialization.java 
b/common/src/main/java/org/apache/sqoop/json/util/SchemaSerialization.java
index 112a404..634c84a 100644
--- a/common/src/main/java/org/apache/sqoop/json/util/SchemaSerialization.java
+++ b/common/src/main/java/org/apache/sqoop/json/util/SchemaSerialization.java
@@ -260,7 +260,7 @@ public class SchemaSerialization {
       output = new Map(name, key, value);
       break;
     case SET:
-      output = new Set(name).setListType(listType);
+      output = new Set(name, listType);
       break;
     case TEXT:
       charSize = (Long) obj.get(CHAR_SIZE);

http://git-wip-us.apache.org/repos/asf/sqoop/blob/deacc90c/common/src/main/java/org/apache/sqoop/schema/type/AbstractComplexListType.java
----------------------------------------------------------------------
diff --git 
a/common/src/main/java/org/apache/sqoop/schema/type/AbstractComplexListType.java
 
b/common/src/main/java/org/apache/sqoop/schema/type/AbstractComplexListType.java
index 5d8f28b..f594f4b 100644
--- 
a/common/src/main/java/org/apache/sqoop/schema/type/AbstractComplexListType.java
+++ 
b/common/src/main/java/org/apache/sqoop/schema/type/AbstractComplexListType.java
@@ -23,6 +23,7 @@ package org.apache.sqoop.schema.type;
 public abstract class AbstractComplexListType extends AbstractComplexType {
 
   // represents the type of the list elements
+  // NOTE: required for Array/Set, optional for Enum
   Column listType;
 
   public AbstractComplexListType(String name) {
@@ -55,27 +56,28 @@ public abstract class AbstractComplexListType extends 
AbstractComplexType {
   }
 
   @Override
-  public boolean equals(Object o) {
-    if (this == o)
+  public int hashCode() {
+    final int prime = 31;
+    int result = super.hashCode();
+    result = prime * result + ((listType == null) ? 0 : listType.hashCode());
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
       return true;
-    if (!(o instanceof AbstractComplexListType))
+    if (!super.equals(obj))
       return false;
-    if (!super.equals(o))
+    if (getClass() != obj.getClass())
       return false;
-
-    AbstractComplexListType that = (AbstractComplexListType) o;
-
-    if (listType != null ? !listType.equals(that.listType) : that.listType != 
null)
+    AbstractComplexListType other = (AbstractComplexListType) obj;
+    if (listType == null) {
+      if (other.listType != null)
+        return false;
+    } else if (!listType.equals(other.listType))
       return false;
-
     return true;
   }
 
-  @Override
-  public int hashCode() {
-    int result = super.hashCode();
-    result = 31 * result + (listType != null ? listType.hashCode() : 0);
-    return result;
-  }
-
 }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/deacc90c/common/src/main/java/org/apache/sqoop/schema/type/Array.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/schema/type/Array.java 
b/common/src/main/java/org/apache/sqoop/schema/type/Array.java
index af13eb7..d34fba8 100644
--- a/common/src/main/java/org/apache/sqoop/schema/type/Array.java
+++ b/common/src/main/java/org/apache/sqoop/schema/type/Array.java
@@ -58,6 +58,8 @@ public class Array extends AbstractComplexListType {
   public String toString() {
     return new StringBuilder("Array{").
          append(super.toString()).
+         append(", size=").
+         append(size).
          append("}").toString();
   }
 

http://git-wip-us.apache.org/repos/asf/sqoop/blob/deacc90c/common/src/main/java/org/apache/sqoop/schema/type/Map.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/schema/type/Map.java 
b/common/src/main/java/org/apache/sqoop/schema/type/Map.java
index 5a0b18e..244506c 100644
--- a/common/src/main/java/org/apache/sqoop/schema/type/Map.java
+++ b/common/src/main/java/org/apache/sqoop/schema/type/Map.java
@@ -66,30 +66,34 @@ public class Map extends AbstractComplexType {
   }
 
   @Override
-  public boolean equals(Object o) {
-    if (this == o)
+  public int hashCode() {
+    final int prime = 31;
+    int result = super.hashCode();
+    result = prime * result + ((key == null) ? 0 : key.hashCode());
+    result = prime * result + ((value == null) ? 0 : value.hashCode());
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
       return true;
-    if (!(o instanceof Map))
+    if (!super.equals(obj))
       return false;
-    if (!super.equals(o))
+    if (getClass() != obj.getClass())
       return false;
-
-    Map map = (Map) o;
-
-    if (key != null ? !key.equals(map.key) : map.key != null)
+    Map other = (Map) obj;
+    if (key == null) {
+      if (other.key != null)
+        return false;
+    } else if (!key.equals(other.key))
       return false;
-
-    if (value != null ? !value.equals(map.value) : map.value != null)
+    if (value == null) {
+      if (other.value != null)
+        return false;
+    } else if (!value.equals(other.value))
       return false;
-
     return true;
   }
 
-  @Override
-  public int hashCode() {
-    int result = super.hashCode();
-    result = 31 * result + (key != null ? key.hashCode() : 0);
-    result = 31 * result + (value != null ? value.hashCode() : 0);
-    return result;
-  }
 }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/deacc90c/common/src/main/java/org/apache/sqoop/schema/type/Set.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/schema/type/Set.java 
b/common/src/main/java/org/apache/sqoop/schema/type/Set.java
index 5585847..4e28cad 100644
--- a/common/src/main/java/org/apache/sqoop/schema/type/Set.java
+++ b/common/src/main/java/org/apache/sqoop/schema/type/Set.java
@@ -24,10 +24,6 @@ package org.apache.sqoop.schema.type;
  */
 public class Set extends AbstractComplexListType {
 
-  public Set(String name) {
-    super(name);
-  }
-
   public Set(String name, Column listType) {
     super(name, listType);
   }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/deacc90c/common/src/test/java/org/apache/sqoop/schema/type/TestArray.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/sqoop/schema/type/TestArray.java 
b/common/src/test/java/org/apache/sqoop/schema/type/TestArray.java
new file mode 100644
index 0000000..82a7d8e
--- /dev/null
+++ b/common/src/test/java/org/apache/sqoop/schema/type/TestArray.java
@@ -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());
+  }
+}

http://git-wip-us.apache.org/repos/asf/sqoop/blob/deacc90c/common/src/test/java/org/apache/sqoop/schema/type/TestEnum.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/sqoop/schema/type/TestEnum.java 
b/common/src/test/java/org/apache/sqoop/schema/type/TestEnum.java
new file mode 100644
index 0000000..4f207c8
--- /dev/null
+++ b/common/src/test/java/org/apache/sqoop/schema/type/TestEnum.java
@@ -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());
+  }
+}

http://git-wip-us.apache.org/repos/asf/sqoop/blob/deacc90c/common/src/test/java/org/apache/sqoop/schema/type/TestMap.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/sqoop/schema/type/TestMap.java 
b/common/src/test/java/org/apache/sqoop/schema/type/TestMap.java
new file mode 100644
index 0000000..1f3abfb
--- /dev/null
+++ b/common/src/test/java/org/apache/sqoop/schema/type/TestMap.java
@@ -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());
+  }
+}

http://git-wip-us.apache.org/repos/asf/sqoop/blob/deacc90c/common/src/test/java/org/apache/sqoop/schema/type/TestSet.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/sqoop/schema/type/TestSet.java 
b/common/src/test/java/org/apache/sqoop/schema/type/TestSet.java
new file mode 100644
index 0000000..daca037
--- /dev/null
+++ b/common/src/test/java/org/apache/sqoop/schema/type/TestSet.java
@@ -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());
+  }
+}

Reply via email to