olingo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tbo...@apache.org
Subject [06/50] [abbrv] git commit: [OLINGO-86] Improved 'readRelated' and added additional tests.
Date Thu, 19 Dec 2013 15:31:17 GMT
[OLINGO-86] Improved 'readRelated' and added additional tests.


Project: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/commit/33c4701f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/tree/33c4701f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/diff/33c4701f

Branch: refs/heads/ODataServlet
Commit: 33c4701f79c47c66dc7e111678b3a065ec18fcb2
Parents: ed3adbc
Author: Michael Bolz <mibo@apache.org>
Authored: Tue Dec 17 15:41:29 2013 +0100
Committer: Michael Bolz <mibo@apache.org>
Committed: Tue Dec 17 15:41:29 2013 +0100

----------------------------------------------------------------------
 .../annotation/data/AnnotationInMemoryDs.java   |  8 ++--
 .../odata2/core/annotation/data/DataStore.java  | 42 ++++++++++++++++++
 .../annotation/processor/ListsProcessor.java    | 35 +++++++--------
 .../data/AnnotationsInMemoryDsTest.java         | 46 ++++++++++++++++++--
 .../odata2/core/annotation/model/Room.java      |  4 ++
 5 files changed, 112 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/33c4701f/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/AnnotationInMemoryDs.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/AnnotationInMemoryDs.java
b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/AnnotationInMemoryDs.java
index 564c2bf..640662f 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/AnnotationInMemoryDs.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/AnnotationInMemoryDs.java
@@ -138,11 +138,11 @@ public class AnnotationInMemoryDs implements DataSource {
     for (Object targetInstance : targetStore.read()) {
       if (navigationInstance instanceof Collection) {
         for (Object object : (Collection<?>) navigationInstance) {
-          if (ANNOTATION_HELPER.keyMatch(targetInstance, object)) {
+          if (targetStore.isKeyEqualChecked(targetInstance, object)) {
             resultData.add(targetInstance);
           }
         }
-      } else if (ANNOTATION_HELPER.keyMatch(targetInstance, navigationInstance)) {
+      } else if (targetStore.isKeyEqualChecked(targetInstance, navigationInstance)) {
         resultData.add(targetInstance);
       }
     }
@@ -151,8 +151,10 @@ public class AnnotationInMemoryDs implements DataSource {
       if (targetKeys.isEmpty()) {
         return resultData;
       } else {
+        Object keyInstance = targetStore.createInstance();
+        ANNOTATION_HELPER.setKeyFields(keyInstance, targetKeys);
         for (Object result : resultData) {
-          if (ANNOTATION_HELPER.keyMatch(result, targetKeys)) {
+          if (targetStore.isKeyEqualChecked(result, keyInstance)) {
             return result;
           }
         }

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/33c4701f/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/DataStore.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/DataStore.java
b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/DataStore.java
index 7dca42b..ab52c0b 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/DataStore.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/data/DataStore.java
@@ -138,6 +138,47 @@ public class DataStore<T> {
       return dataStore.remove(keyElement);
     }
   }
+  
+  /**
+   * Are the key values equal for both instances.
+   * If all compared key values are <code>null</code> this also means equal.
+   * 
+   * @param first first instance to check for key equal
+   * @param second second instance to check for key equal
+   * @return <code>true</code> if object instance have equal keys set.
+   */
+  public boolean isKeyEqual(final T first, final T second) {
+    KeyElement firstKeys = getKeys(first);
+    KeyElement secondKeys = getKeys(second);
+    
+    return firstKeys.equals(secondKeys);
+  }
+  
+  /**
+   * Are the key values equal for both instances.
+   * If all compared key values are <code>null</code> this also means equal.
+   * Before object (keys) are compared it is validated that both object instance are NOT
null
+   * and that both are from the same class as this {@link DataStore} (see {@link #dataTypeClass}).
+   * For the equal check on {@link #dataTypeClass} instances without validation see
+   * {@link #isKeyEqual(Object, Object)}.
+   * 
+   * @param first first instance to check for key equal
+   * @param second second instance to check for key equal
+   * @return <code>true</code> if object instance have equal keys set.
+   */
+  @SuppressWarnings("unchecked")
+  public boolean isKeyEqualChecked(Object first, Object second) throws DataStoreException
{
+    if(first == null || second == null) {
+      throw new DataStoreException("Tried to compare null values which is not allowed.");
+    } else if(first.getClass() != dataTypeClass) {
+      throw new DataStoreException("First value is no instance from required class '" + dataTypeClass
+ "'.");
+    } else if(second.getClass() != dataTypeClass) {
+      throw new DataStoreException("Second value is no instance from required class '" +
dataTypeClass + "'.");
+    }
+    
+    return isKeyEqual((T) first, (T) second);
+  }
+
 
   private class KeyElement {
     private int cachedHashCode = 42;
@@ -169,6 +210,7 @@ public class DataStore<T> {
       if (getClass() != obj.getClass()) {
         return false;
       }
+      @SuppressWarnings("unchecked")
       final KeyElement other = (KeyElement) obj;
       if (this.keyValues != other.keyValues && (this.keyValues == null || !this.keyValues.equals(other.keyValues)))
{
         return false;

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/33c4701f/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/ListsProcessor.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/ListsProcessor.java
b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/ListsProcessor.java
index d335d9c..053d2da 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/ListsProcessor.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/core/annotation/processor/ListsProcessor.java
@@ -896,24 +896,25 @@ public class ListsProcessor extends DataSourceProcessor {
     ODataContext context = getContext();
     final int timingHandle = context.startRuntimeMeasurement(getClass().getSimpleName(),
"retrieveData");
 
-    data = functionImport == null ?
-        keys.isEmpty() ?
-            dataSource.readData(startEntitySet) : dataSource.readData(startEntitySet, keys)
:
-        dataSource.readData(functionImport, functionImportParameters, keys);
-
-    EdmEntitySet currentEntitySet =
-        functionImport == null ? startEntitySet : functionImport.getEntitySet();
-    for (NavigationSegment navigationSegment : navigationSegments) {
-      data = dataSource.readRelatedData(
-          currentEntitySet,
-          data,
-          navigationSegment.getEntitySet(),
-          mapKey(navigationSegment.getKeyPredicates()));
-      currentEntitySet = navigationSegment.getEntitySet();
+    try {
+      data = functionImport == null ?
+          keys.isEmpty() ?
+              dataSource.readData(startEntitySet) : dataSource.readData(startEntitySet, keys)
:
+          dataSource.readData(functionImport, functionImportParameters, keys);
+  
+      EdmEntitySet currentEntitySet =
+          functionImport == null ? startEntitySet : functionImport.getEntitySet();
+      for (NavigationSegment navigationSegment : navigationSegments) {
+        data = dataSource.readRelatedData(
+            currentEntitySet,
+            data,
+            navigationSegment.getEntitySet(),
+            mapKey(navigationSegment.getKeyPredicates()));
+        currentEntitySet = navigationSegment.getEntitySet();
+      }
+    } finally {
+      context.stopRuntimeMeasurement(timingHandle);
     }
-
-    context.stopRuntimeMeasurement(timingHandle);
-
     return data;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/33c4701f/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/core/annotation/data/AnnotationsInMemoryDsTest.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/core/annotation/data/AnnotationsInMemoryDsTest.java
b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/core/annotation/data/AnnotationsInMemoryDsTest.java
index 07da4d5..82becde 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/core/annotation/data/AnnotationsInMemoryDsTest.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/core/annotation/data/AnnotationsInMemoryDsTest.java
@@ -149,8 +149,7 @@ public class AnnotationsInMemoryDsTest {
         } catch (Exception ex) {
           ex.printStackTrace();
           throw new RuntimeException(ex);
-        }
-        finally {
+        } finally {
           latch.countDown();
         }
       }
@@ -161,7 +160,7 @@ public class AnnotationsInMemoryDsTest {
 
   @Test
   @SuppressWarnings("unchecked")
-  public void readRelatedEntity() throws Exception {
+  public void readRelatedEntities() throws Exception {
     EdmEntitySet buildingsEntitySet = createMockedEdmEntitySet("Buildings");
     EdmEntitySet roomsEntitySet = createMockedEdmEntitySet("Rooms");
 
@@ -203,6 +202,47 @@ public class AnnotationsInMemoryDsTest {
   }
 
   @Test
+  public void readRelatedTargetEntity() throws Exception {
+    EdmEntitySet buildingsEntitySet = createMockedEdmEntitySet("Buildings");
+    EdmEntitySet roomsEntitySet = createMockedEdmEntitySet("Rooms");
+
+    Building building = new Building();
+    building.setName("Common Building");
+
+    final int roomsCount = 10;
+    List<Room> rooms = new ArrayList<Room>();
+    for (int i = 0; i < roomsCount; i++) {
+      Room room = new Room(i, "Room " + i);
+      room.setBuilding(building);
+      datasource.createData(roomsEntitySet, room);
+      rooms.add(room);
+    }
+
+    building.getRooms().addAll(rooms);
+    datasource.createData(buildingsEntitySet, building);
+
+    Map<String, Object> keys = new HashMap<String, Object>();
+    keys.put("Id", "1");
+
+    Building read = (Building) datasource.readData(buildingsEntitySet, keys);
+    Assert.assertEquals("Common Building", read.getName());
+    Assert.assertEquals("1", read.getId());
+
+    // execute
+    Map<String, Object> targetKeys = new HashMap<String, Object>();
+    targetKeys.put("Id", 3);
+    Object relatedData = datasource.readRelatedData(
+        buildingsEntitySet, building, roomsEntitySet, targetKeys);
+
+    // validate
+    Assert.assertTrue("Result is no Room.", relatedData instanceof Room);
+    Room relatedRoom = (Room) relatedData;
+    Assert.assertEquals("3", relatedRoom.getId());
+    Assert.assertEquals("Room 3", relatedRoom.getName());
+    Assert.assertEquals("Common Building", relatedRoom.getBuilding().getName());
+  }
+
+  @Test
   public void createSimpleEntity() throws Exception {
     EdmEntitySet edmEntitySet = createMockedEdmEntitySet("Buildings");
 

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/33c4701f/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/core/annotation/model/Room.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/core/annotation/model/Room.java
b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/core/annotation/model/Room.java
index 98ddf3d..6ab8d74 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/core/annotation/model/Room.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/core/annotation/model/Room.java
@@ -39,6 +39,10 @@ public class Room extends RefBase {
   @EdmNavigationProperty(name = "nr_Employees")
   private List<Employee> employees = new ArrayList<Employee>();
 
+  public Room() {
+    this(0, null);
+  }
+  
   public Room(final int id, final String name) {
     super(id, name);
   }


Mime
View raw message