[OLINGO-786] Implement Tests for qualifiermapkey

Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/546cf34c
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/546cf34c
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/546cf34c

Branch: refs/heads/master
Commit: 546cf34c574e317839c6797b54f7ef71be110912
Parents: 3647a63
Author: Christian Amend <[email protected]>
Authored: Thu Oct 8 13:43:54 2015 +0200
Committer: Christian Amend <[email protected]>
Committed: Thu Oct 8 13:43:54 2015 +0200

----------------------------------------------------------------------
 .../commons/core/edm/TargetQualifierMapKey.java |  21 ++--
 .../commons/core/edm/EdmImplCachingTest.java    |  56 +++++++++-
 .../commons/core/edm/EdmImplCallCreateTest.java |  38 ++++++-
 .../core/edm/TargetQualifierMapKeyTest.java     | 108 +++++++++++++++++++
 4 files changed, 206 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/546cf34c/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/TargetQualifierMapKey.java
----------------------------------------------------------------------
diff --git 
a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/TargetQualifierMapKey.java
 
b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/TargetQualifierMapKey.java
index 3236cd1..8c46321 100644
--- 
a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/TargetQualifierMapKey.java
+++ 
b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/TargetQualifierMapKey.java
@@ -6,9 +6,9 @@
  * 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
@@ -18,17 +18,21 @@
  */
 package org.apache.olingo.commons.core.edm;
 
+import org.apache.olingo.commons.api.edm.EdmException;
 import org.apache.olingo.commons.api.edm.FullQualifiedName;
 
 public class TargetQualifierMapKey {
 
- private final FullQualifiedName targetName;
- private final String qualifier;
- 
+  private final FullQualifiedName targetName;
+  private final String qualifier;
+
   public TargetQualifierMapKey(FullQualifiedName targetName, String qualifier) 
{
-  this.targetName = targetName;
-  this.qualifier = qualifier;
-}
+    if (targetName == null) {
+      throw new EdmException("targetName for TargetQualifierMapKey must not be 
null.");
+    }
+    this.targetName = targetName;
+    this.qualifier = qualifier;
+  }
 
   @Override
   public int hashCode() {
@@ -68,5 +72,4 @@ public class TargetQualifierMapKey {
     return true;
   }
 
- 
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/546cf34c/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/EdmImplCachingTest.java
----------------------------------------------------------------------
diff --git 
a/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/EdmImplCachingTest.java
 
b/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/EdmImplCachingTest.java
index 986abf6..ed99114 100644
--- 
a/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/EdmImplCachingTest.java
+++ 
b/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/EdmImplCachingTest.java
@@ -6,9 +6,9 @@
  * 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
@@ -247,6 +247,40 @@ public class EdmImplCachingTest {
     assertNotSame(function, function2);
   }
 
+  @Test
+  public void cacheTerm() {
+    EdmTerm term1 = edm.getTerm(NAME1);
+    assertNotNull(term1);
+
+    EdmTerm cachedTerm = edm.getTerm(NAME1);
+    assertNotNull(cachedTerm);
+
+    assertEquals(term1, cachedTerm);
+    assertTrue(term1 == cachedTerm);
+
+    EdmTerm term2 = edm.getTerm(NAME2);
+    assertNotNull(term2);
+
+    assertNotSame(term1, term2);
+  }
+
+  @Test
+  public void cacheAnnotationsGroup() {
+    EdmAnnotations annotationGroup1 = edm.getAnnotationGroup(NAME1, null);
+    assertNotNull(annotationGroup1);
+
+    EdmAnnotations cachedAnnotationGroup = edm.getAnnotationGroup(NAME1, null);
+    assertNotNull(cachedAnnotationGroup);
+
+    assertEquals(annotationGroup1, cachedAnnotationGroup);
+    assertTrue(annotationGroup1 == cachedAnnotationGroup);
+    
+    EdmAnnotations annotationGroup2 = edm.getAnnotationGroup(NAME1, "");
+    assertNotNull(annotationGroup2);
+    
+    assertNotSame(annotationGroup1, annotationGroup2);
+  }
+
   @Before
   public void setup() {
     edm = new LocalEdm();
@@ -417,12 +451,26 @@ public class EdmImplCachingTest {
 
     @Override
     protected EdmTerm createTerm(final FullQualifiedName termName) {
-      throw new UnsupportedOperationException("Not supported yet.");
+      if(NAME1.equals(termName) || NAME2.equals(termName)){
+        EdmTerm term = mock(EdmTerm.class);
+        when(term.getFullQualifiedName()).thenReturn(termName);
+        return term;
+      }
+      return null;
     }
 
     @Override
     protected EdmAnnotations createAnnotationGroup(final FullQualifiedName 
target, String qualifier) {
-      throw new UnsupportedOperationException("Not supported yet.");
+      if(NAME1.equals(target) && qualifier == null){
+        EdmAnnotations annotationGroup = mock(EdmAnnotations.class);
+        when(annotationGroup.getQualifier()).thenReturn(null);
+        return annotationGroup;
+      }else if(NAME1.equals(target) && "".equals(qualifier)){
+        EdmAnnotations annotationGroup = mock(EdmAnnotations.class);
+        when(annotationGroup.getQualifier()).thenReturn("");
+        return annotationGroup;
+      }
+      return null;
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/546cf34c/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/EdmImplCallCreateTest.java
----------------------------------------------------------------------
diff --git 
a/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/EdmImplCallCreateTest.java
 
b/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/EdmImplCallCreateTest.java
index b5a1734..f4a4c27 100644
--- 
a/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/EdmImplCallCreateTest.java
+++ 
b/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/EdmImplCallCreateTest.java
@@ -6,9 +6,9 @@
  * 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
@@ -149,6 +149,26 @@ public class EdmImplCallCreateTest {
     assertNull(edm.getUnboundFunction(WRONG_FQN, null));
   }
 
+  @Test
+  public void callCreateTerm() {
+    EdmTerm term = edm.getTerm(FQN);
+    assertNotNull(term);
+
+    assertEquals(FQN, term.getFullQualifiedName());
+
+    assertNull(edm.getTerm(WRONG_FQN));
+  }
+
+  @Test
+  public void callCreateAnnotationGroup() {
+    EdmAnnotations annotationGroup = edm.getAnnotationGroup(FQN, null);
+    assertNotNull(annotationGroup);
+
+    assertEquals(null, annotationGroup.getQualifier());
+
+    assertNull(edm.getAnnotationGroup(WRONG_FQN, null));
+  }
+
   @Before
   public void setup() {
     edm = new LocalEdm();
@@ -281,12 +301,22 @@ public class EdmImplCallCreateTest {
 
     @Override
     protected EdmTerm createTerm(final FullQualifiedName termName) {
-      throw new UnsupportedOperationException("Not supported yet.");
+      if (FQN.equals(termName)) {
+        EdmTerm term = mock(EdmTerm.class);
+        when(term.getFullQualifiedName()).thenReturn(FQN);
+        return term;
+      }
+      return null;
     }
 
     @Override
     protected EdmAnnotations createAnnotationGroup(final FullQualifiedName 
targetName, String qualifier) {
-      throw new UnsupportedOperationException("Not supported yet.");
+      if (FQN.equals(targetName) && qualifier == null) {
+        EdmAnnotations annotationGroup = mock(EdmAnnotations.class);
+        when(annotationGroup.getQualifier()).thenReturn(qualifier);
+        return annotationGroup;
+      }
+      return null;
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/546cf34c/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/TargetQualifierMapKeyTest.java
----------------------------------------------------------------------
diff --git 
a/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/TargetQualifierMapKeyTest.java
 
b/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/TargetQualifierMapKeyTest.java
new file mode 100644
index 0000000..57f5003
--- /dev/null
+++ 
b/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/TargetQualifierMapKeyTest.java
@@ -0,0 +1,108 @@
+/*
+ * 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.olingo.commons.core.edm;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.fail;
+
+import org.apache.olingo.commons.api.edm.EdmException;
+import org.apache.olingo.commons.api.edm.FullQualifiedName;
+import org.junit.Test;
+
+public class TargetQualifierMapKeyTest {
+
+  private static final FullQualifiedName TARGET_NAME_1 = new 
FullQualifiedName("namespace", "name");
+
+  @Test
+  public void invalidParametersTest() {
+    createAndCheckForEdmException(null, null);
+    createAndCheckForEdmException(null, "qualifier");
+  }
+
+  @Test
+  public void validParametersTest() {
+    new TargetQualifierMapKey(TARGET_NAME_1, null);
+    new TargetQualifierMapKey(TARGET_NAME_1, "qualifier");
+  }
+
+  @Test
+  public void testEqualsMethod() {
+    TargetQualifierMapKey key1 = new TargetQualifierMapKey(TARGET_NAME_1, 
"qualifier");
+    TargetQualifierMapKey key2 = new TargetQualifierMapKey(new 
FullQualifiedName("namespace", "name"), "qualifier");
+    assertEquals(key1, key1);
+
+    key1 = new TargetQualifierMapKey(TARGET_NAME_1, "qualifier");
+    key2 = new TargetQualifierMapKey(new FullQualifiedName("namespace", 
"name"), "qualifier");
+    assertEquals(key1, key2);
+
+    key1 = new TargetQualifierMapKey(TARGET_NAME_1, null);
+    key2 = new TargetQualifierMapKey(TARGET_NAME_1, null);
+    assertEquals(key1, key2);
+
+    key1 = new TargetQualifierMapKey(TARGET_NAME_1, null);
+    key2 = new TargetQualifierMapKey(new FullQualifiedName("namespace", 
"name"), null);
+    assertEquals(key1, key2);
+
+    key1 = new TargetQualifierMapKey(TARGET_NAME_1, "qualifier");
+    key2 = new TargetQualifierMapKey(TARGET_NAME_1, null);
+    assertNotSame(key1, key2);
+
+    key1 = new TargetQualifierMapKey(new FullQualifiedName("namespace", 
"name"), null);
+    key2 = new TargetQualifierMapKey(new FullQualifiedName("namespace", 
"wrong"), null);
+    assertNotSame(key1, key2);
+  }
+
+  @Test
+  public void testHashMethod() {
+    TargetQualifierMapKey key1 = new TargetQualifierMapKey(TARGET_NAME_1, 
"qualifier");
+    TargetQualifierMapKey key2 = new TargetQualifierMapKey(new 
FullQualifiedName("namespace", "name"), "qualifier");
+    assertEquals(key1.hashCode(), key1.hashCode());
+
+    key1 = new TargetQualifierMapKey(TARGET_NAME_1, "qualifier");
+    key2 = new TargetQualifierMapKey(new FullQualifiedName("namespace", 
"name"), "qualifier");
+    assertEquals(key1.hashCode(), key2.hashCode());
+
+    key1 = new TargetQualifierMapKey(TARGET_NAME_1, null);
+    key2 = new TargetQualifierMapKey(TARGET_NAME_1, null);
+    assertEquals(key1.hashCode(), key2.hashCode());
+
+    key1 = new TargetQualifierMapKey(TARGET_NAME_1, null);
+    key2 = new TargetQualifierMapKey(new FullQualifiedName("namespace", 
"name"), null);
+    assertEquals(key1.hashCode(), key2.hashCode());
+
+    key1 = new TargetQualifierMapKey(TARGET_NAME_1, "qualifier");
+    key2 = new TargetQualifierMapKey(TARGET_NAME_1, null);
+    assertNotSame(key1.hashCode(), key2.hashCode());
+
+    key1 = new TargetQualifierMapKey(new FullQualifiedName("namespace", 
"name"), null);
+    key2 = new TargetQualifierMapKey(new FullQualifiedName("namespace", 
"wrong"), null);
+    assertNotSame(key1.hashCode(), key2.hashCode());
+  }
+
+  private void createAndCheckForEdmException(final FullQualifiedName fqn, 
final String qualifier) {
+    try {
+      new TargetQualifierMapKey(fqn, qualifier);
+    } catch (EdmException e) {
+      return;
+    }
+    fail("EdmException expected for parameters: " + fqn + " " + qualifier);
+  }
+
+}

Reply via email to