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.modules.collection;
019:
020: import org.apache.avalon.framework.service.ServiceException;
021: import org.apache.avalon.framework.service.ServiceSelector;
022: import org.apache.lenya.ac.AccessControlException;
023: import org.apache.lenya.ac.impl.AbstractAccessControlTest;
024: import org.apache.lenya.cms.publication.Document;
025: import org.apache.lenya.cms.publication.DocumentBuildException;
026: import org.apache.lenya.cms.publication.DocumentFactory;
027: import org.apache.lenya.cms.publication.DocumentManager;
028: import org.apache.lenya.cms.publication.DocumentUtil;
029: import org.apache.lenya.cms.publication.Publication;
030: import org.apache.lenya.cms.publication.PublicationException;
031: import org.apache.lenya.cms.publication.ResourceType;
032: import org.apache.lenya.cms.repository.Session;
033: import org.apache.lenya.cms.site.SiteManager;
034: import org.apache.lenya.cms.site.SiteStructure;
035: import org.apache.lenya.transaction.TransactionException;
036:
037: /**
038: * Collection wrapper test.
039: */
040: public class CollectionWrapperTest extends AbstractAccessControlTest {
041:
042: /**
043: * @throws PublicationException
044: * @throws AccessControlException
045: * @throws TransactionException
046: * @throws ServiceException
047: */
048: public void testXLinkCollection() throws PublicationException,
049: AccessControlException, TransactionException,
050: ServiceException {
051:
052: Session session = login("lenya");
053: DocumentFactory map = DocumentUtil.createDocumentFactory(
054: getManager(), session);
055:
056: Publication pub = getPublication("test");
057:
058: Document collectionDoc = createCollectionDocument(pub);
059:
060: CollectionWrapper collection = new CollectionWrapper(
061: collectionDoc, getLogger());
062:
063: SiteStructure structure = pub.getArea("authoring").getSite();
064: structure.getRepositoryNode().lock();
065:
066: SiteManager siteManager = null;
067: ServiceSelector selector = null;
068: try {
069: selector = (ServiceSelector) getManager().lookup(
070: SiteManager.ROLE + "Selector");
071: siteManager = (SiteManager) selector.select(pub
072: .getSiteManagerHint());
073:
074: siteManager.add("/collection", collection.getDelegate());
075: } finally {
076: selector.release(siteManager);
077: getManager().release(selector);
078: }
079:
080: Document doc = map.get(pub, Publication.AUTHORING_AREA,
081: "/index", "en");
082: collection.add(doc);
083: collection.save();
084:
085: collection.getDelegate().getRepositoryNode().unlock();
086: structure.getRepositoryNode().unlock();
087:
088: CollectionWrapper coll2 = new CollectionWrapper(collectionDoc,
089: getLogger());
090:
091: assertSame(collection.getDelegate().getRepositoryNode(), coll2
092: .getDelegate().getRepositoryNode());
093:
094: assertEquals(coll2.size(), 1);
095: assertTrue(coll2.contains(doc));
096:
097: }
098:
099: protected Document createCollectionDocument(Publication pub)
100: throws ServiceException, DocumentBuildException,
101: PublicationException {
102: ServiceSelector typeSelector = null;
103: ResourceType type = null;
104: DocumentManager docMgr = null;
105: Document doc;
106: try {
107: typeSelector = (ServiceSelector) getManager().lookup(
108: ResourceType.ROLE + "Selector");
109: type = (ResourceType) typeSelector.select("collection");
110: docMgr = (DocumentManager) getManager().lookup(
111: DocumentManager.ROLE);
112: ResourceType.Sample sample = type.getSample(type
113: .getSampleNames()[0]);
114: doc = docMgr.add(getFactory(), type, sample.getUri(), pub,
115: "authoring", "en", "xml");
116: doc.setMimeType(sample.getMimeType());
117:
118: } finally {
119: if (docMgr != null) {
120: getManager().release(docMgr);
121: }
122: if (typeSelector != null) {
123: getManager().release(typeSelector);
124: }
125: }
126: return doc;
127: }
128:
129: }
|