You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by fg...@apache.org on 2010/01/13 17:30:20 UTC

svn commit: r898825 - in /incubator/chemistry/trunk/chemistry: chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/ chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/ chemistry-tests/src/main/java/org/apache/chemis...

Author: fguillaume
Date: Wed Jan 13 16:30:20 2010
New Revision: 898825

URL: http://svn.apache.org/viewvc?rev=898825&view=rev
Log:
CMIS-95: fix NPE in SimpleConnection#getChildren, throw proper exceptions instead

Modified:
    incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java
    incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleConnection.java
    incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java?rev=898825&r1=898824&r2=898825&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java Wed Jan 13 16:30:20 2010
@@ -214,7 +214,7 @@
 
     public List<ObjectEntry> getDescendants(ObjectId folder, int depth,
             String orderBy, Inclusion inclusion) {
-        String href = getObjectEntry(folder).getLink(AtomPub.LINK_DOWN,
+        String href = getFolderEntry(folder).getLink(AtomPub.LINK_DOWN,
                 AtomPubCMIS.MEDIA_TYPE_CMIS_TREE);
         Request req = new Request(href);
         req.setParameter(AtomPubCMIS.PARAM_DEPTH, Integer.toString(depth));
@@ -253,7 +253,7 @@
     public ListPage<ObjectEntry> getChildren(ObjectId folder,
             Inclusion inclusion, String orderBy, Paging paging) {
         // TODO filter, includeRelationship, includeAllowableActions, orderBy
-        String href = getObjectEntry(folder).getLink(AtomPub.LINK_DOWN,
+        String href = getFolderEntry(folder).getLink(AtomPub.LINK_DOWN,
                 AtomPub.MEDIA_TYPE_ATOM_FEED);
         Request req = new Request(href);
         if (paging != null) {
@@ -273,11 +273,8 @@
 
     public ObjectEntry getFolderParent(ObjectId folder, String filter) {
         // TODO filter
-        APPObjectEntry current = getObjectEntry(folder);
-        if (!current.getBaseType().equals(BaseType.FOLDER)) {
-            throw new IllegalArgumentException("Not a folder: " + folder);
-        }
-        String rootId = current.connection.getRootFolder().getId();
+        APPObjectEntry current = getFolderEntry(folder);
+        String rootId = repository.getInfo().getRootFolderId().getId();
         if (current.getId().equals(rootId)) {
             return null;
         }
@@ -346,6 +343,14 @@
         return (APPObjectEntry) resp.getObject(new ReadContext(this));
     }
 
+    protected APPObjectEntry getFolderEntry(ObjectId objectId) {
+        APPObjectEntry entry = getObjectEntry(objectId);
+        if (!entry.getBaseType().equals(BaseType.FOLDER)) {
+            throw new IllegalArgumentException("Not a folder: " + objectId);
+        }
+        return entry;
+    }
+
     protected String replace(String template, String param, String value) {
         return template.replace('{' + param + '}', value);
     }

Modified: incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleConnection.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleConnection.java?rev=898825&r1=898824&r2=898825&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleConnection.java (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleConnection.java Wed Jan 13 16:30:20 2010
@@ -180,6 +180,7 @@
 
     public List<ObjectEntry> getFolderTree(ObjectId folder, int depth,
             Inclusion inclusion) {
+        checkFolder(folder);
         List<ObjectEntry> list = new ArrayList<ObjectEntry>();
         accumulateFolders(folder, depth, inclusion, list);
         return list;
@@ -206,6 +207,7 @@
 
     public List<ObjectEntry> getDescendants(ObjectId folder, int depth,
             String orderBy, Inclusion inclusion) {
+        checkFolder(folder);
         List<ObjectEntry> list = new ArrayList<ObjectEntry>();
         accumulateDescendants(folder, depth, inclusion, orderBy, list);
         return list;
@@ -214,6 +216,7 @@
     public ListPage<ObjectEntry> getChildren(ObjectId folder,
             Inclusion inclusion, String orderBy, Paging paging) {
         // TODO orderBy
+        checkFolder(folder);
         Set<String> ids = repository.children.get(folder.getId());
         List<ObjectEntry> all = new ArrayList<ObjectEntry>(ids.size());
         for (String id : ids) {
@@ -223,6 +226,20 @@
         return getListPage(all, paging);
     }
 
+    protected void checkFolder(ObjectId object) throws ObjectNotFoundException,
+            ConstraintViolationException {
+        String id = object.getId();
+        SimpleData data = repository.datas.get(id);
+        if (data == null) {
+            throw new ObjectNotFoundException(id);
+        }
+        String baseTypeId = (String) data.get(Property.BASE_TYPE_ID);
+        if (baseTypeId != BaseType.FOLDER.getId()) {
+            throw new IllegalArgumentException("Not a folder: " + id);
+        }
+
+    }
+
     /**
      * Extracts part of a list according to given paging parameters.
      *

Modified: incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java?rev=898825&r1=898824&r2=898825&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java Wed Jan 13 16:30:20 2010
@@ -318,18 +318,60 @@
         assertEquals(2, page.size());
         assertTrue(page.getHasMoreItems());
         assertEquals(3, page.getNumItems());
+
+        ObjectEntry doc1 = spi.getObjectByPath("/folder 1/doc 1", null);
+        try {
+            spi.getChildren(doc1, null, null, null);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            spi.getChildren(spi.newObjectId("no-such-id"), null, null, null);
+            fail();
+        } catch (ObjectNotFoundException e) {
+            // ok
+        }
     }
 
     public void testGetFolderTree() {
         Folder root = conn.getRootFolder();
         List<ObjectEntry> desc = spi.getFolderTree(root, 4, null);
         assertEquals(2, desc.size());
+
+        ObjectEntry doc1 = spi.getObjectByPath("/folder 1/doc 1", null);
+        try {
+            spi.getFolderTree(doc1, -1, null);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            spi.getFolderTree(spi.newObjectId("no-such-id"), -1, null);
+            fail();
+        } catch (ObjectNotFoundException e) {
+            // ok
+        }
     }
 
     public void testGetDescendants() {
         Folder root = conn.getRootFolder();
         List<ObjectEntry> desc = spi.getDescendants(root, 4, null, null);
         assertEquals(6, desc.size());
+
+        ObjectEntry doc1 = spi.getObjectByPath("/folder 1/doc 1", null);
+        try {
+            spi.getDescendants(doc1, -1, null, null);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            spi.getDescendants(spi.newObjectId("no-such-id"), -1, null, null);
+            fail();
+        } catch (ObjectNotFoundException e) {
+            // ok
+        }
     }
 
     public void testGetFolderParent() {