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.site;
019:
020: import org.apache.avalon.framework.service.ServiceException;
021: import org.apache.avalon.framework.service.ServiceSelector;
022: import org.apache.lenya.ac.impl.AbstractAccessControlTest;
023: import org.apache.lenya.cms.publication.Document;
024: import org.apache.lenya.cms.publication.DocumentBuildException;
025: import org.apache.lenya.cms.publication.DocumentFactory;
026: import org.apache.lenya.cms.publication.DocumentManager;
027: import org.apache.lenya.cms.publication.DocumentUtil;
028: import org.apache.lenya.cms.publication.Publication;
029: import org.apache.lenya.cms.publication.PublicationException;
030: import org.apache.lenya.cms.publication.ResourceType;
031: import org.apache.lenya.cms.repository.RepositoryException;
032: import org.apache.lenya.cms.repository.Session;
033: import org.apache.lenya.cms.site.Link;
034: import org.apache.lenya.cms.site.SiteException;
035: import org.apache.lenya.cms.site.SiteManager;
036: import org.apache.lenya.cms.site.SiteNode;
037: import org.apache.lenya.cms.site.SiteStructure;
038: import org.apache.lenya.cms.site.simple.DocumentStore;
039:
040: public class SimpleSiteManagerTest extends AbstractAccessControlTest {
041:
042: protected static final String PATH = "/foo/bar";
043:
044: protected static final String PARENT_PATH = "/foo";
045:
046: public void testSimpleSiteManager() throws Exception {
047:
048: Session session = login("lenya");
049:
050: DocumentFactory factory = DocumentUtil.createDocumentFactory(
051: getManager(), session);
052: Publication[] pubs = factory.getPublications();
053:
054: for (int i = 0; i < pubs.length; i++) {
055: checkPublication(session, factory, pubs[i]);
056: }
057: }
058:
059: protected void checkPublication(Session session,
060: DocumentFactory factory, Publication pub)
061: throws ServiceException, SiteException,
062: DocumentBuildException, PublicationException,
063: RepositoryException {
064: DocumentManager docManager = null;
065: ServiceSelector selector = null;
066: SiteManager siteManager = null;
067: ServiceSelector resourceTypeSelector = null;
068:
069: try {
070: selector = (ServiceSelector) getManager().lookup(
071: SiteManager.ROLE + "Selector");
072: siteManager = (SiteManager) selector.select(pub
073: .getSiteManagerHint());
074: SiteStructure structure = siteManager.getSiteStructure(
075: factory, pub, Publication.AUTHORING_AREA);
076:
077: docManager = (DocumentManager) getManager().lookup(
078: DocumentManager.ROLE);
079:
080: resourceTypeSelector = (ServiceSelector) getManager()
081: .lookup(ResourceType.ROLE + "Selector");
082: ResourceType type = (ResourceType) resourceTypeSelector
083: .select("entry");
084: String contentSourceUri = "context://sitemap.xmap";
085:
086: Document doc = docManager.add(factory, type,
087: contentSourceUri, pub, Publication.AUTHORING_AREA,
088: "en", "xml");
089:
090: structure.add(PATH, doc);
091: assertTrue(structure.contains(PATH));
092: Document linkDoc = structure.getNode(PATH).getLink("en")
093: .getDocument();
094: assertSame(linkDoc, doc);
095:
096: if (!(structure instanceof DocumentStore)) {
097: Link link = doc.getLink();
098: checkSetLabel(link);
099: }
100:
101: SiteNode[] nodes = structure.getNodes();
102: assertTrue(nodes.length > 0);
103:
104: for (int i = 0; i < nodes.length; i++) {
105:
106: assertTrue(structure.contains(nodes[i].getPath()));
107:
108: SiteNode node = structure.getNode(nodes[i].getPath());
109: assertNotNull(node);
110: assertEquals(nodes[i], node);
111:
112: checkLinks(siteManager, node);
113: }
114:
115: doc.getLink().delete();
116: assertFalse(structure.containsByUuid(doc.getUUID(), doc
117: .getLanguage()));
118: assertFalse(structure.contains(PATH));
119: assertFalse(structure.contains(PARENT_PATH));
120:
121: } finally {
122: if (selector != null) {
123: if (siteManager != null) {
124: selector.release(siteManager);
125: }
126: getManager().release(selector);
127: }
128: if (docManager != null) {
129: getManager().release(docManager);
130: }
131: if (resourceTypeSelector != null) {
132: getManager().release(resourceTypeSelector);
133: }
134: }
135: // session.commit();
136: }
137:
138: protected void checkSetLabel(Link link) {
139: String newLabel = "New Label";
140: String oldLabel = link.getLabel();
141: assertFalse(oldLabel.equals(newLabel));
142: link.setLabel(newLabel);
143: assertTrue(link.getLabel().equals(newLabel));
144: link.setLabel(oldLabel);
145: }
146:
147: protected void checkLinks(SiteManager siteManager, SiteNode node)
148: throws SiteException {
149: String[] languages = node.getLanguages();
150: for (int i = 0; i < languages.length; i++) {
151: Link link = node.getLink(languages[i]);
152: assertEquals(link.getLanguage(), languages[i]);
153: assertNotNull(link.getLabel());
154:
155: if (node.getUuid() != null) {
156: Document doc = link.getDocument();
157: assertNotNull(doc);
158:
159: String docUuid = doc.getUUID();
160: String nodeUuid = node.getUuid();
161:
162: assertNotNull(doc.getUUID());
163: assertEquals(docUuid, nodeUuid);
164: assertEquals(doc.getLanguage(), link.getLanguage());
165:
166: // it may not be allowed to insert the doc twice
167: try {
168: siteManager.add("/sidebar", doc);
169: assertTrue("No exception thrown", false);
170: } catch (Exception expected) {
171: }
172: }
173: }
174: }
175:
176: }
|