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.io.IOException;
021: import java.net.MalformedURLException;
022:
023: import org.apache.avalon.framework.logger.Logger;
024: import org.apache.avalon.framework.service.ServiceException;
025: import org.apache.avalon.framework.service.ServiceManager;
026: import org.apache.avalon.framework.service.ServiceSelector;
027: import org.apache.excalibur.source.SourceResolver;
028: import org.apache.excalibur.source.TraversableSource;
029: import org.apache.lenya.ac.AccessControlException;
030: import org.apache.lenya.ac.impl.AbstractAccessControlTest;
031: import org.apache.lenya.cms.metadata.MetaDataException;
032: import org.apache.lenya.cms.repository.RepositoryException;
033: import org.apache.lenya.cms.repository.Session;
034:
035: /**
036: * Resource wrapper test.
037: */
038: public class ResourceWrapperTest extends AbstractAccessControlTest {
039:
040: protected static final String IMAGE_URL = "context://lenya/resources/images/project-logo.png";
041:
042: /**
043: * @throws RepositoryException
044: * @throws PublicationException
045: * @throws ServiceException
046: * @throws MetaDataException
047: * @throws IOException
048: * @throws MalformedURLException
049: * @throws AccessControlException
050: */
051: public void testResourceWrapper() throws RepositoryException,
052: PublicationException, ServiceException,
053: MalformedURLException, IOException, MetaDataException,
054: AccessControlException {
055:
056: String path = "/testResource";
057:
058: Session session = login("lenya");
059: DocumentFactory factory = DocumentUtil.createDocumentFactory(
060: getManager(), session);
061:
062: Publication pub = getPublication("test");
063:
064: Document doc = createResource(factory, pub, path, getManager(),
065: getLogger());
066:
067: SourceResolver resolver = null;
068: TraversableSource source = null;
069:
070: try {
071: resolver = (SourceResolver) getManager().lookup(
072: SourceResolver.ROLE);
073: source = (TraversableSource) resolver.resolveURI(IMAGE_URL);
074:
075: assertEquals(doc.getMimeType(), source.getMimeType());
076: assertEquals(doc.getContentLength(), source
077: .getContentLength());
078:
079: } finally {
080: if (resolver != null) {
081: if (source != null) {
082: resolver.release(source);
083: }
084: getManager().release(resolver);
085: }
086:
087: }
088:
089: }
090:
091: /**
092: * @param factory
093: * @param pub
094: * @param path
095: * @param manager
096: * @param logger
097: * @return A document.
098: * @throws ServiceException
099: * @throws DocumentBuildException
100: * @throws PublicationException
101: * @throws MalformedURLException
102: * @throws IOException
103: * @throws RepositoryException
104: * @throws DocumentException
105: * @throws MetaDataException
106: */
107: public static Document createResource(DocumentFactory factory,
108: Publication pub, String path, ServiceManager manager,
109: Logger logger) throws ServiceException,
110: DocumentBuildException, PublicationException,
111: MalformedURLException, IOException, RepositoryException,
112: DocumentException, MetaDataException {
113:
114: String extension = "png";
115:
116: Document doc = null;
117:
118: ResourceType resourceType = null;
119: ServiceSelector selector = null;
120: DocumentManager docManager = null;
121:
122: try {
123: docManager = (DocumentManager) manager
124: .lookup(DocumentManager.ROLE);
125:
126: pub.getArea(Publication.AUTHORING_AREA).getSite()
127: .getRepositoryNode().lock();
128:
129: selector = (ServiceSelector) manager
130: .lookup(ResourceType.ROLE + "Selector");
131: resourceType = (ResourceType) selector.select("resource");
132:
133: ResourceType.Sample sample = resourceType
134: .getSample(resourceType.getSampleNames()[0]);
135: doc = docManager.add(factory, resourceType,
136: sample.getUri(), pub, Publication.AUTHORING_AREA,
137: path, pub.getDefaultLanguage(), extension,
138: "Test Resource", true);
139: doc.setMimeType(sample.getMimeType());
140:
141: ResourceWrapper resource = new ResourceWrapper(doc,
142: manager, logger);
143: resource.write(IMAGE_URL);
144: } finally {
145: if (docManager != null) {
146: manager.release(docManager);
147: }
148: if (selector != null) {
149: manager.release(selector);
150: }
151: }
152: return doc;
153: }
154:
155: }
|