You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2014/10/14 20:35:11 UTC

[24/39] git commit: [OLINGO-455] Null check for list access

[OLINGO-455] Null check for list access


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

Branch: refs/heads/Olingo-129_PocJpaDataStore
Commit: 14c6dd5be84f1ef8a0fe790caee299a5b1326b47
Parents: b07ba0f
Author: Michael Bolz <mi...@sap.com>
Authored: Thu Oct 9 11:00:32 2014 +0200
Committer: Michael Bolz <mi...@sap.com>
Committed: Thu Oct 9 11:00:32 2014 +0200

----------------------------------------------------------------------
 .../provider/EdmEntityContainerImplProv.java    |  2 +-
 .../provider/EdmServiceMetadataImplProv.java    | 57 +++++++++++---------
 .../EdmServiceMetadataImplProvTest.java         | 25 +++++++--
 3 files changed, 56 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/14c6dd5b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/edm/provider/EdmEntityContainerImplProv.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/edm/provider/EdmEntityContainerImplProv.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/edm/provider/EdmEntityContainerImplProv.java
index e4026e6..1472388 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/edm/provider/EdmEntityContainerImplProv.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/edm/provider/EdmEntityContainerImplProv.java
@@ -260,7 +260,7 @@ public class EdmEntityContainerImplProv implements EdmEntityContainer, EdmAnnota
       return entityContainerHierachy;
     }
 
-    List<EntityContainer> entityContainerHierachy = new ArrayList<EntityContainer>();
+    entityContainerHierachy = new ArrayList<EntityContainer>();
     Map<String, EntityContainer> name2Container = getEntityContainerMap();
     String currentName = getName();
     while (currentName != null) {

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/14c6dd5b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProv.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProv.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProv.java
index 16262fc..e7853eb 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProv.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProv.java
@@ -23,6 +23,7 @@ import java.io.InputStream;
 import java.io.OutputStreamWriter;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import javax.xml.stream.XMLOutputFactory;
@@ -104,29 +105,23 @@ public class EdmServiceMetadataImplProv implements EdmServiceMetadata {
     if (dataServiceVersion == null) {
       dataServiceVersion = ODataServiceVersion.V10;
 
-      if (schemas != null) {
-        for (Schema schema : schemas) {
-          List<EntityType> entityTypes = schema.getEntityTypes();
-          if (entityTypes != null) {
-            for (EntityType entityType : entityTypes) {
-              List<Property> properties = entityType.getProperties();
-              if (properties != null) {
-                for (Property property : properties) {
-                  if (property.getCustomizableFeedMappings() != null) {
-                    if (property.getCustomizableFeedMappings().getFcKeepInContent() != null) {
-                      if (!property.getCustomizableFeedMappings().getFcKeepInContent()) {
-                        dataServiceVersion = ODataServiceVersion.V20;
-                        return dataServiceVersion;
-                      }
-                    }
-                  }
+      for (Schema schema : listOrEmptyList(schemas)) {
+        List<EntityType> entityTypes = listOrEmptyList(schema.getEntityTypes());
+        for (EntityType entityType : entityTypes) {
+          List<Property> properties = listOrEmptyList(entityType.getProperties());
+          for (Property property : properties) {
+            if (property.getCustomizableFeedMappings() != null) {
+              if (property.getCustomizableFeedMappings().getFcKeepInContent() != null) {
+                if (!property.getCustomizableFeedMappings().getFcKeepInContent()) {
+                  dataServiceVersion = ODataServiceVersion.V20;
+                  return dataServiceVersion;
                 }
-                if (entityType.getCustomizableFeedMappings() != null) {
-                  if (entityType.getCustomizableFeedMappings().getFcKeepInContent() != null) {
-                    if (entityType.getCustomizableFeedMappings().getFcKeepInContent()) {
-                      dataServiceVersion = ODataServiceVersion.V20;
-                      return dataServiceVersion;
-                    }
+              }
+              if (entityType.getCustomizableFeedMappings() != null) {
+                if (entityType.getCustomizableFeedMappings().getFcKeepInContent() != null) {
+                  if (entityType.getCustomizableFeedMappings().getFcKeepInContent()) {
+                    dataServiceVersion = ODataServiceVersion.V20;
+                    return dataServiceVersion;
                   }
                 }
               }
@@ -148,8 +143,8 @@ public class EdmServiceMetadataImplProv implements EdmServiceMetadata {
       }
 
       for (Schema schema : schemas) {
-        for (EntityContainer entityContainer : schema.getEntityContainers()) {
-          for (EntitySet entitySet : entityContainer.getEntitySets()) {
+        for (EntityContainer entityContainer : listOrEmptyList(schema.getEntityContainers())) {
+          for (EntitySet entitySet : listOrEmptyList(entityContainer.getEntitySets())) {
             EdmEntitySetInfo entitySetInfo = new EdmEntitySetInfoImplProv(entitySet, entityContainer);
             entitySetInfos.add(entitySetInfo);
           }
@@ -160,4 +155,18 @@ public class EdmServiceMetadataImplProv implements EdmServiceMetadata {
 
     return entitySetInfos;
   }
+
+  /**
+   * Return original list if parameter is not NULL or an empty list.
+   *
+   * @param list list which is checked and probably returned
+   * @param <T> type of list
+   * @return original list if parameter is not NULL or an empty list
+   */
+  private <T> List<T> listOrEmptyList(List<T> list) {
+    if(list == null) {
+      return Collections.emptyList();
+    }
+    return list;
+  }
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/14c6dd5b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProvTest.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProvTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProvTest.java
index 2a6786e..05912c4 100644
--- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProvTest.java
+++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProvTest.java
@@ -28,7 +28,6 @@ import static org.mockito.Mockito.when;
 
 import java.net.URI;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -76,7 +75,7 @@ public class EdmServiceMetadataImplProvTest extends BaseTest {
 
     List<EdmEntitySetInfo> infos = serviceMetadata.getEntitySetInfos();
     assertNotNull(infos);
-    assertEquals(Collections.emptyList(), infos);
+    assertTrue(infos.isEmpty());
   }
 
   @Test
@@ -90,7 +89,27 @@ public class EdmServiceMetadataImplProvTest extends BaseTest {
 
     List<EdmEntitySetInfo> infos = serviceMetadata.getEntitySetInfos();
     assertNotNull(infos);
-    assertEquals(Collections.emptyList(), infos);
+    assertTrue(infos.isEmpty());
+  }
+
+  /**
+   * Check that no NPE can occur with a new created Schema
+   *
+   * @throws Exception
+   */
+  @Test
+  public void getEntitySetInfosForNewEdmProviderSchemas() throws Exception {
+    List<Schema> schemas = new ArrayList<Schema>();
+    schemas.add(new Schema());
+
+    EdmProvider edmProvider = mock(EdmProvider.class);
+    when(edmProvider.getSchemas()).thenReturn(schemas);
+
+    EdmServiceMetadata serviceMetadata = new EdmServiceMetadataImplProv(edmProvider);
+
+    List<EdmEntitySetInfo> infos = serviceMetadata.getEntitySetInfos();
+    assertNotNull(infos);
+    assertTrue(infos.isEmpty());
   }
 
   @Test