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.metadata;
019:
020: import java.util.Map;
021:
022: import org.apache.lenya.ac.impl.AbstractAccessControlTest;
023: import org.apache.lenya.cms.metadata.dublincore.DublinCore;
024: import org.apache.lenya.cms.publication.Document;
025: import org.apache.lenya.cms.publication.DocumentFactory;
026: import org.apache.lenya.cms.publication.Publication;
027:
028: /**
029: * Meta data test.
030: */
031: public class MetaDataTest extends AbstractAccessControlTest {
032:
033: /**
034: * Tests the meta data.
035: * @throws Exception
036: */
037: public void testMetaData() throws Exception {
038:
039: Publication publication = getPublication("test");
040: Document doc = publication.getArea("authoring").getSite()
041: .getNode("/index").getLink("en").getDocument();
042:
043: String namespaceUri = "foobar";
044: Exception e = null;
045: try {
046: doc.getMetaData(namespaceUri);
047: } catch (Exception e1) {
048: e = e1;
049: }
050: assertNotNull(e);
051:
052: namespaceUri = DublinCore.DC_NAMESPACE;
053: MetaData dc = doc.getMetaData(namespaceUri);
054:
055: doc.getRepositoryNode().lock();
056:
057: checkSetTitle(dc);
058: checkRemoveAllValues(dc);
059:
060: }
061:
062: protected void checkSetTitle(MetaData dc) throws MetaDataException {
063: Exception e = null;
064: try {
065: dc.setValue("foo", "bar");
066: } catch (Exception e1) {
067: e = e1;
068: }
069: assertNotNull(e);
070: dc.setValue("title", "This is the title");
071:
072: e = null;
073: // addValue() should throw an exception because a value is already set
074: try {
075: dc.addValue("title", "bar");
076: } catch (Exception e1) {
077: e = e1;
078: }
079: assertNotNull(e);
080:
081: }
082:
083: String NAMESPACE = "http://apache.org/lenya/test/metadata";
084:
085: protected void checkOnCopy(Publication pub) throws Exception {
086: MetaDataRegistry registry = null;
087: try {
088: registry = (MetaDataRegistry) getManager().lookup(
089: MetaDataRegistry.ROLE);
090: ElementSet set = new TestElementSet();
091: registry.register(NAMESPACE, set);
092: } finally {
093: getManager().release(registry);
094: }
095:
096: DocumentFactory factory = getFactory();
097: Document source = factory.get(pub, Publication.AUTHORING_AREA,
098: "/index", "en");
099: Document target = factory.get(pub, Publication.AUTHORING_AREA,
100: "/index", "en");
101:
102: MetaData sourceMeta = source.getMetaData(NAMESPACE);
103: sourceMeta.setValue("copy", "sourceCopy");
104: sourceMeta.setValue("ignore", "sourceIgnore");
105: sourceMeta.setValue("delete", "sourceDelete");
106:
107: MetaData targetMeta = target.getMetaData(NAMESPACE);
108: targetMeta.setValue("ignore", "targetIgnore");
109: targetMeta.setValue("delete", "targetDelete");
110:
111: targetMeta.replaceBy(sourceMeta);
112:
113: assertTrue(targetMeta.getValues("copy").length == 1);
114: assertEquals(sourceMeta.getValues("copy"), targetMeta
115: .getValues("copy"));
116:
117: assertTrue(targetMeta.getValues("ignore").length == 1);
118: assertEquals(targetMeta.getFirstValue("ignore"), "targetIgnore");
119:
120: assertTrue(targetMeta.getValues("delete").length == 0);
121: }
122:
123: protected void checkRemoveAllValues(MetaData dc)
124: throws MetaDataException {
125: dc.removeAllValues("title");
126: assertTrue(dc.getValues("title").length == 0);
127: }
128:
129: protected class TestElement implements Element {
130:
131: private String name;
132: private int actionOnCopy;
133:
134: protected TestElement(String name, int actionOnCopy) {
135: this .name = name;
136: this .actionOnCopy = actionOnCopy;
137: }
138:
139: public int getActionOnCopy() {
140: return actionOnCopy;
141: }
142:
143: public String getDescription() {
144: return "";
145: }
146:
147: public String getName() {
148: return name;
149: }
150:
151: public boolean isEditable() {
152: return false;
153: }
154:
155: public boolean isMultiple() {
156: return false;
157: }
158:
159: }
160:
161: protected class TestElementSet implements ElementSet {
162:
163: private Element[] elements = {
164: new TestElement("copy", Element.ONCOPY_COPY),
165: new TestElement("ignore", Element.ONCOPY_IGNORE),
166: new TestElement("delete", Element.ONCOPY_DELETE) };
167:
168: private Map name2element;
169:
170: protected TestElementSet() {
171: for (int i = 0; i < elements.length; i++) {
172: this .name2element.put(elements[i].getName(),
173: elements[i]);
174: }
175: }
176:
177: public boolean containsElement(String name) {
178: return true;
179: }
180:
181: public Element getElement(String name) throws MetaDataException {
182: return (Element) this .name2element.get(name);
183: }
184:
185: public Element[] getElements() {
186: return elements;
187: }
188:
189: public String getNamespaceUri() {
190: return NAMESPACE;
191: }
192:
193: }
194:
195: }
|