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: package org.apache.jetspeed.page.document.psml;
018:
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.ArrayList;
022: import java.util.Map;
023: import java.util.HashMap;
024:
025: import junit.framework.Test;
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028:
029: import org.apache.jetspeed.om.page.Document;
030: import org.apache.jetspeed.om.folder.psml.FolderMetaDataImpl;
031: import org.apache.jetspeed.page.psml.CastorXmlPageManager;
032: import org.apache.jetspeed.page.document.DocumentHandlerFactory;
033: import org.apache.jetspeed.page.document.psml.DocumentHandlerFactoryImpl;
034: import org.apache.jetspeed.cache.file.FileCache;
035:
036: /**
037: * <p>
038: * TestCastorFileSystemDocumentHandler
039: * </p>
040: * <p>
041: *
042: * </p>
043: *
044: * @author <a href="mailto:woonsan@apache.org">Woonsan Ko</a>
045: * @version $Id$
046: *
047: */
048: public class TestCastorFileSystemDocumentHandler extends TestCase {
049:
050: protected CastorFileSystemDocumentHandler folderMetaDataDocumentHandler;
051:
052: /*
053: * (non-Javadoc)
054: *
055: * @see junit.framework.TestCase#setUp()
056: */
057: protected void setUp() throws Exception {
058: super .setUp();
059:
060: folderMetaDataDocumentHandler = new CastorFileSystemDocumentHandler(
061: "/JETSPEED-INF/castor/page-mapping.xml",
062: "folder.metadata", FolderMetaDataImpl.class,
063: "testdata/pages", new FileCache());
064:
065: Map handlerMap = new HashMap();
066: handlerMap
067: .put("folder.metadata", folderMetaDataDocumentHandler);
068: DocumentHandlerFactory handlerFactory = new DocumentHandlerFactoryImpl(
069: handlerMap);
070: folderMetaDataDocumentHandler.setHandlerFactory(handlerFactory);
071: }
072:
073: /**
074: * <p>
075: * tearDown
076: * </p>
077: *
078: * @see junit.framework.TestCase#tearDown()
079: * @throws java.lang.Exception
080: */
081: protected void tearDown() throws Exception {
082: super .tearDown();
083: }
084:
085: /**
086: * Defines the testcase name for JUnit.
087: *
088: * @param name
089: * the testcase's name.
090: */
091: public TestCastorFileSystemDocumentHandler(String name) {
092: super (name);
093: }
094:
095: /**
096: * Start the tests.
097: *
098: * @param args
099: * the arguments. Not used
100: */
101: public static void main(String args[]) {
102: junit.awtui.TestRunner
103: .main(new String[] { TestCastorFileSystemDocumentHandler.class
104: .getName() });
105: }
106:
107: /**
108: * Creates the test suite.
109: *
110: * @return a test suite (<code>TestSuite</code>) that includes all
111: * methods starting with "test"
112: */
113: public static Test suite() {
114: // All methods starting with "test" will be executed in the test suite.
115: return new TestSuite(TestCastorFileSystemDocumentHandler.class);
116: }
117:
118: public void testFolderMetaData() throws Exception {
119: Document doc = folderMetaDataDocumentHandler.getDocument(
120: "/folder1/folder.metadata", false);
121: assertNotNull(doc);
122: String title = doc.getTitle();
123: assertEquals("Default Title for Folder 1", title);
124: }
125:
126: public void testFolderMetaDataInParallel() throws Exception {
127: Thread[] threads = new Thread[10];
128: int i;
129: final List exceptions = new ArrayList(10);
130:
131: for (i = 0; i < threads.length; i++) {
132: threads[i] = new Thread(new Runnable() {
133: public void run() {
134: try {
135: Document doc = folderMetaDataDocumentHandler
136: .getDocument(
137: "/folder1/folder.metadata",
138: false);
139: } catch (Exception e) {
140: e.printStackTrace(System.out);
141: exceptions.add(e);
142: }
143: }
144: });
145: }
146:
147: for (i = 0; i < threads.length; i++) {
148: threads[i].start();
149: }
150:
151: for (i = 0; i < threads.length; i++) {
152: threads[i].join();
153: }
154:
155: assertTrue(
156: "folderMetaDataDocumentHandler.getDocument() is not thread-safe!",
157: exceptions.size() == 0);
158: }
159:
160: }
|