001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.publication;
019:
020: import java.util.Arrays;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.lenya.ac.impl.AbstractAccessControlTest;
026: import org.apache.lenya.cms.site.NodeSet;
027: import org.apache.lenya.cms.site.SiteException;
028: import org.apache.lenya.cms.site.SiteNode;
029: import org.apache.lenya.cms.site.SiteStructure;
030: import org.apache.lenya.cms.site.SiteUtil;
031: import org.apache.lenya.util.StringUtil;
032:
033: /**
034: * Document manager test.
035: */
036: public class DocumentManagerTest extends AbstractAccessControlTest {
037:
038: /**
039: * Do the test.
040: * @throws Exception
041: */
042: public void testDocumentManager() throws Exception {
043: DocumentManager docManager = null;
044: try {
045: docManager = (DocumentManager) getManager().lookup(
046: DocumentManager.ROLE);
047: doTestMoveAll(docManager, "/doctypes", "/tutorial/doctypes");
048: doTestMoveAll(docManager, "/tutorial/doctypes", "/doctypes");
049: doTestCopyAll(docManager, "/doctypes", "/tutorial/doctypes");
050:
051: String areaName1 = "authoring";
052: String areaName2 = "live";
053: String path1 = "/tutorial";
054: String path2 = "/doctypes";
055:
056: doTestCopyToArea(docManager, areaName1, areaName2, path1,
057: path2);
058:
059: } finally {
060: if (docManager != null) {
061: getManager().release(docManager);
062: }
063: }
064: }
065:
066: protected void doTestCopyToArea(DocumentManager docManager,
067: String sourceAreaName, String destAreaName, String path1,
068: String path2) throws PublicationException, SiteException {
069:
070: DocumentFactory factory = getFactory();
071: Publication pub = factory.getPublication("test");
072: SiteStructure sourceArea = pub.getArea(sourceAreaName)
073: .getSite();
074: SiteStructure destArea = pub.getArea(destAreaName).getSite();
075:
076: if (destArea.contains(path1)) {
077: destArea.getNode(path1).delete();
078: }
079: if (destArea.contains(path2)) {
080: destArea.getNode(path2).delete();
081: }
082:
083: assertFalse(destArea.contains(path1));
084: assertFalse(destArea.contains(path2));
085:
086: // copy second node first to test correct ordering
087: doTestCopyToArea(docManager, path2, sourceAreaName,
088: destAreaName);
089: doTestCopyToArea(docManager, path1, sourceAreaName,
090: destAreaName);
091:
092: List sourceNodes = Arrays.asList(sourceArea.getNodes());
093:
094: SiteNode authoringNode1 = sourceArea.getNode(path1);
095: assertTrue(sourceNodes.contains(authoringNode1));
096: int sourcePos1 = sourceNodes.indexOf(authoringNode1);
097:
098: SiteNode sourceNode2 = sourceArea.getNode(path2);
099: int sourcePos2 = sourceNodes.indexOf(sourceNode2);
100:
101: assertTrue(sourcePos1 < sourcePos2);
102:
103: assertTrue(destArea.contains(path1));
104: List liveNodes = Arrays.asList(destArea.getNodes());
105: SiteNode liveNode1 = destArea.getNode(path1);
106: assertTrue(liveNodes.contains(liveNode1));
107: int livePos1 = liveNodes.indexOf(liveNode1);
108:
109: SiteNode liveNode2 = destArea.getNode(path2);
110: int livePos2 = liveNodes.indexOf(liveNode2);
111:
112: assertTrue(livePos1 < livePos2);
113: }
114:
115: protected void doTestCopyToArea(DocumentManager docManager,
116: String path, String areaName1, String areaName2)
117: throws PublicationException {
118: DocumentFactory factory = getFactory();
119: Publication pub = factory.getPublication("test");
120: Area area1 = pub.getArea(areaName1);
121: Document doc = area1.getSite().getNode(path).getLink("en")
122: .getDocument();
123: docManager.copyToArea(doc, areaName2);
124: }
125:
126: protected void doTestCopyAll(DocumentManager docManager,
127: String sourcePath, String targetPath) throws SiteException,
128: DocumentException, PublicationException {
129: DocumentFactory factory = getFactory();
130: Publication pub = factory.getPublication("test");
131: Area authoring = pub.getArea("authoring");
132:
133: SiteNode sourceNode = authoring.getSite().getNode(sourcePath);
134:
135: NodeSet nodes = SiteUtil.getSubSite(getManager(), sourceNode);
136: Document[] docs = nodes.getDocuments();
137: Map doc2path = new HashMap();
138:
139: String sourceBase = sourcePath.substring(0, sourcePath
140: .lastIndexOf("/"));
141: String targetBase = targetPath.substring(0, targetPath
142: .lastIndexOf("/"));
143:
144: for (int i = 0; i < docs.length; i++) {
145: doc2path.put(docs[i], docs[i].getPath().substring(
146: sourceBase.length()));
147: }
148:
149: docManager
150: .copyAll(authoring, sourcePath, authoring, targetPath);
151: for (int i = 0; i < docs.length; i++) {
152: assertTrue(docs[i].hasLink());
153: String oldPath = (String) doc2path.get(docs[i]);
154: String newPath = targetBase + oldPath;
155: assertTrue(authoring.getSite().contains(newPath));
156: SiteNode newNode = authoring.getSite().getNode(newPath);
157: Document newDoc = newNode.getLink(docs[i].getLanguage())
158: .getDocument();
159: assertEquals(newDoc.getContentLength(), docs[i]
160: .getContentLength());
161: assertFalse(newDoc.getUUID().equals(docs[i].getUUID()));
162: }
163:
164: String[] sourceNames = getChildNames(sourceNode);
165: SiteNode targetNode = authoring.getSite().getNode(targetPath);
166: String[] targetNames = getChildNames(targetNode);
167: assertEquals(StringUtil.join(sourceNames, ","), StringUtil
168: .join(targetNames, ","));
169: }
170:
171: protected String[] getChildNames(SiteNode node) {
172: SiteNode[] sourceChildren = node.getChildren();
173: String[] names = new String[sourceChildren.length];
174: for (int i = 0; i < names.length; i++) {
175: names[i] = sourceChildren[i].getName();
176: }
177: return names;
178: }
179:
180: protected void doTestMoveAll(DocumentManager docManager,
181: String sourcePath, String targetPath) throws SiteException,
182: DocumentException, PublicationException {
183: DocumentFactory factory = getFactory();
184: Publication pub = factory.getPublication("test");
185: Area authoring = pub.getArea("authoring");
186:
187: SiteNode sourceNode = authoring.getSite().getNode(sourcePath);
188: String[] sourceNames = getChildNames(sourceNode);
189:
190: NodeSet nodes = SiteUtil.getSubSite(getManager(), sourceNode);
191: Document[] docs = nodes.getDocuments();
192: Map doc2path = new HashMap();
193:
194: String sourceBase = sourcePath.substring(0, sourcePath
195: .lastIndexOf("/"));
196: String targetBase = targetPath.substring(0, targetPath
197: .lastIndexOf("/"));
198:
199: for (int i = 0; i < docs.length; i++) {
200: doc2path.put(docs[i], docs[i].getPath().substring(
201: sourceBase.length()));
202: }
203:
204: docManager
205: .moveAll(authoring, sourcePath, authoring, targetPath);
206: for (int i = 0; i < docs.length; i++) {
207: assertTrue(docs[i].hasLink());
208: String oldPath = (String) doc2path.get(docs[i]);
209: String newPath = docs[i].getPath();
210: assertEquals(targetBase + oldPath, newPath);
211: }
212:
213: SiteNode targetNode = authoring.getSite().getNode(targetPath);
214: String[] targetNames = getChildNames(targetNode);
215: assertEquals(StringUtil.join(sourceNames, ","), StringUtil
216: .join(targetNames, ","));
217:
218: }
219: }
|