This project has retired. For details please refer to its Attic page.
FolderTest xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.inmemory;
20  
21  import java.util.List;
22  
23  import junit.framework.TestCase;
24  
25  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Filing;
26  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Folder;
27  import org.apache.chemistry.opencmis.inmemory.storedobj.api.ObjectStore;
28  import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoredObject;
29  import org.apache.chemistry.opencmis.inmemory.storedobj.impl.FolderImpl;
30  import org.apache.chemistry.opencmis.inmemory.storedobj.impl.ObjectStoreImpl;
31  import org.junit.Before;
32  import org.junit.Test;
33  
34  /**
35   * @author <a href="mailto:fmueller@opentext.com">Florian M&uuml;ller</a>
36   * @author Jens
37   */
38  
39  /**
40   * Some test directly against the in-memory folder object.
41   */
42  public class FolderTest extends TestCase {
43  
44      private ObjectStore fStore;
45      private FolderImpl fRoot;
46      private FolderImpl f1;
47      private FolderImpl f2;
48      private FolderImpl f3;
49      private FolderImpl f4;
50      private FolderImpl f11;
51      private static final String TEST_REPOSITORY_ID = "TestRepositoryId";
52      private static final String USER = "user";
53  
54      @Before
55      protected void setUp() throws Exception {
56          fStore = new ObjectStoreImpl(TEST_REPOSITORY_ID);
57          createFolders();
58      }
59  
60      @Test
61      public void testCreatAndGetFolders() {
62          try {
63              Folder childFolder = createFolder("Folder 1");
64              fRoot.addChildFolder(childFolder);
65              fail("Should throw exception if folder already exists.");
66          } catch (Exception e) {
67          }
68          assertEquals(f1.getName(), "Folder 1");
69          assertEquals(f11.getName(), "Folder 1.1");
70          assertNull(fRoot.getParent());
71          assertEquals(fRoot, f1.getParent());
72          assertEquals(f1, f11.getParent());
73          assertEquals(Filing.PATH_SEPARATOR, fRoot.getPath());
74          assertEquals("/Folder 1", f1.getPath());
75          assertEquals("/Folder 1/Folder 1.1", f11.getPath());
76          StoredObject fTest = fStore.getObjectByPath("/", USER);
77          assertEquals(fRoot, fTest);
78          fTest = fStore.getObjectByPath("/Folder 1", USER);
79          assertEquals(f1, fTest);
80          fTest = fStore.getObjectByPath("/Folder 1/Folder 1.1", USER);
81          assertEquals(f11, fTest);
82          List<StoredObject> subFolders = fRoot.getChildren(-1, -1, "user");
83          assertEquals(4, subFolders.size());
84          subFolders = f2.getChildren(-1, -1, "user");
85          assertEquals(0, subFolders.size());
86          subFolders = f1.getChildren(-1, -1, "user");
87          assertEquals(1, subFolders.size());
88      }
89  
90      @Test
91      public void testRenameFolder() {
92          // rename top level folder
93          String newName = "Folder B";
94          String oldPath = f2.getPath();
95          f2.rename(newName);
96          assertEquals(f2.getName(), newName);
97          assertEquals(f2.getPath(), Filing.PATH_SEPARATOR + newName);
98          assertNull(fStore.getObjectByPath(oldPath, USER));
99          assertEquals(f2, fStore.getObjectByPath(Filing.PATH_SEPARATOR + newName, USER));
100         try {
101             f2.rename("Folder 3");
102             fail("Should not allow to rename a folder to an existing name");
103         } catch (Exception e) {
104         }
105 
106         // rename sub folder
107         oldPath = f11.getPath();
108         f11.rename(newName);
109         assertEquals(f11.getName(), newName);
110         assertEquals(f11.getPath(), "/Folder 1/Folder B");
111         assertNull(fStore.getObjectByPath(oldPath, USER));
112         assertEquals(f11, fStore.getObjectByPath("/Folder 1/Folder B", USER));
113         try {
114             f2.rename(newName);
115             fail("Should not allow to rename a folder to an existing name");
116         } catch (Exception e) {
117         }
118         try {
119             f2.rename("illegal/name");
120             fail("Should not allow to rename a folder to a name with illegal name");
121         } catch (Exception e) {
122         }
123 
124         // rename root folder
125         try {
126             fRoot.rename("abc");
127             fail("Should not be possible to rename root folder");
128         } catch (Exception e) {
129         }
130     }
131 
132     @Test
133     public void testMoveFolder() {
134         String oldPath = f1.getPath();
135         Folder f1Parent = f1.getParent();
136         f1.move(f1Parent, f3);
137         assertNull(fStore.getObjectByPath(oldPath, USER));
138         assertEquals(f1.getPath(), "/Folder 3/Folder 1");
139         assertEquals(f1, fStore.getObjectByPath("/Folder 3/Folder 1", USER));
140 
141         f2.rename("Folder 1");
142         try {
143             Folder f2Parent = f2.getParent();
144             f2.move(f2Parent, f3);
145             fail("Should not be possible to move folder to a folder that has a child with same name");
146         } catch (Exception e) {
147         }
148     }
149 
150     @Test
151     public void testDeleteFolder() {
152         String oldPath = f2.getPath();
153         fStore.deleteObject(f2.getId(), true, "TestUser");
154         assertNull(fStore.getObjectByPath(oldPath, USER));
155 
156         try {
157             fStore.deleteObject(f1.getId(), true, "TestUser");
158             fail("Should not be possible to move folder that has children");
159         } catch (Exception e) {
160         }
161     }
162 
163     private void createFolders() {
164         fRoot = (FolderImpl) fStore.getRootFolder();
165         f1 = (FolderImpl) createFolder("Folder 1");
166         fRoot.addChildFolder(f1);
167         f1.persist();
168 
169         f2 = (FolderImpl) createFolder("Folder 2");
170         fRoot.addChildFolder(f2);
171         f2.persist();
172 
173         f3 = (FolderImpl) createFolder("Folder 3");
174         fRoot.addChildFolder(f3);
175         f3.persist();
176 
177         f4 = (FolderImpl) createFolder("Folder 4");
178         fRoot.addChildFolder(f4);
179         f4.persist();
180 
181         f11 = (FolderImpl) createFolder("Folder 1.1");
182         f1.addChildFolder(f11);
183         f11.persist();
184     }
185     
186     private Folder createFolder(String name) {
187     	return fStore.createFolder(name, null, "user", null, null, null);    	
188     }
189 }