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.observation;
019:
020: import org.apache.lenya.ac.impl.AbstractAccessControlTest;
021: import org.apache.lenya.cms.publication.Document;
022: import org.apache.lenya.cms.publication.DocumentManager;
023: import org.apache.lenya.cms.publication.Publication;
024: import org.apache.lenya.cms.site.SiteStructure;
025: import org.apache.lenya.xml.DocumentHelper;
026: import org.apache.lenya.xml.NamespaceHelper;
027:
028: public class ObservationTest extends AbstractAccessControlTest {
029:
030: public void testObservation() throws Exception {
031:
032: Publication publication = getPublication("test");
033: SiteStructure site = publication.getArea("authoring").getSite();
034: Document doc = site.getNode("/index").getLink("en")
035: .getDocument();
036:
037: TestListener docListener = new TestListener();
038: TestListener allListener = new TestListener();
039:
040: ObservationRegistry registry = null;
041: try {
042: registry = (ObservationRegistry) getManager().lookup(
043: ObservationRegistry.ROLE);
044:
045: // check if it works if only the allListener is registered
046: registry.registerListener(allListener);
047: testChanged(doc, allListener);
048:
049: registry.registerListener(docListener, doc);
050: Exception e = null;
051: try {
052: registry.registerListener(docListener, doc);
053: } catch (ObservationException e1) {
054: e = e1;
055: }
056: assertNotNull(e);
057:
058: testChanged(doc, docListener);
059: testChanged(doc, allListener);
060:
061: testMetaDataChanged(doc, docListener);
062: testMetaDataChanged(doc, allListener);
063:
064: } finally {
065: if (registry != null) {
066: getManager().release(registry);
067: }
068: }
069:
070: }
071:
072: protected void testChanged(Document doc, TestListener listener)
073: throws Exception {
074: listener.reset();
075: org.w3c.dom.Document oldXml = DocumentHelper.readDocument(doc
076: .getInputStream());
077: NamespaceHelper xml = new NamespaceHelper(
078: "http://apache.org/lenya/test", "", "test");
079: xml.save(doc.getOutputStream());
080: DocumentHelper.writeDocument(oldXml, doc.getOutputStream());
081:
082: assertFalse(listener.wasChanged());
083: doc.getRepositoryNode().getSession().commit();
084: Thread.currentThread().sleep(100);
085: assertTrue(listener.wasChanged());
086: }
087:
088: protected void testMetaDataChanged(Document doc,
089: TestListener listener) throws Exception {
090: listener.reset();
091:
092: String mimeType = doc.getMimeType();
093: doc.setMimeType("");
094: doc.setMimeType(mimeType);
095:
096: assertFalse(listener.wasChanged());
097: doc.getRepositoryNode().getSession().commit();
098: Thread.currentThread().sleep(100);
099: assertTrue(listener.wasChanged());
100: }
101:
102: protected void testRemoved(Document doc, TestListener listener)
103: throws Exception {
104: listener.reset();
105:
106: DocumentManager docManager = null;
107: try {
108: docManager = (DocumentManager) getManager().lookup(
109: DocumentManager.ROLE);
110: Document target = doc.getFactory().get(
111: doc.getPublication(), doc.getArea(), "/testTarget",
112: doc.getLanguage());
113: docManager.move(doc, target.getLocator());
114:
115: assertFalse(listener.wasRemoved());
116: doc.getRepositoryNode().getSession().commit();
117: Thread.currentThread().sleep(100);
118: assertTrue(listener.wasRemoved());
119:
120: docManager.move(target, doc.getLocator());
121: assertFalse(listener.wasChanged());
122: doc.getRepositoryNode().getSession().commit();
123: Thread.currentThread().sleep(100);
124: assertTrue(listener.wasChanged());
125: } finally {
126: if (docManager != null) {
127: getManager().release(docManager);
128: }
129: }
130:
131: }
132:
133: }
|