1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.page.document.psml;
18  
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.ArrayList;
22  import java.util.Map;
23  import java.util.HashMap;
24  
25  import junit.framework.Test;
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  
29  import org.apache.jetspeed.om.page.Document;
30  import org.apache.jetspeed.om.folder.psml.FolderMetaDataImpl;
31  import org.apache.jetspeed.page.psml.CastorXmlPageManager;
32  import org.apache.jetspeed.page.document.DocumentHandlerFactory;
33  import org.apache.jetspeed.page.document.psml.DocumentHandlerFactoryImpl;
34  import org.apache.jetspeed.cache.file.FileCache;
35  
36  /***
37   * <p>
38   * TestCastorFileSystemDocumentHandler
39   * </p>
40   * <p>
41   * 
42   * </p>
43   * 
44   * @author <a href="mailto:woonsan@apache.org">Woonsan Ko</a>
45   * @version $Id$
46   *  
47   */
48  public class TestCastorFileSystemDocumentHandler extends TestCase
49  {
50  
51      protected CastorFileSystemDocumentHandler folderMetaDataDocumentHandler;
52  
53      /*
54       * (non-Javadoc)
55       * 
56       * @see junit.framework.TestCase#setUp()
57       */
58      protected void setUp() throws Exception
59      {
60          super.setUp();
61          
62          folderMetaDataDocumentHandler = new CastorFileSystemDocumentHandler(
63              "/JETSPEED-INF/castor/page-mapping.xml",
64              "folder.metadata",
65              FolderMetaDataImpl.class,
66              "testdata/pages",
67              new FileCache());
68              
69          Map handlerMap = new HashMap();
70          handlerMap.put("folder.metadata", folderMetaDataDocumentHandler);
71          DocumentHandlerFactory handlerFactory = new DocumentHandlerFactoryImpl(handlerMap);
72          folderMetaDataDocumentHandler.setHandlerFactory(handlerFactory);
73      }
74  
75      /***
76       * <p>
77       * tearDown
78       * </p>
79       * 
80       * @see junit.framework.TestCase#tearDown()
81       * @throws java.lang.Exception
82       */
83      protected void tearDown() throws Exception
84      {
85          super.tearDown();
86      }
87  
88      /***
89       * Defines the testcase name for JUnit.
90       * 
91       * @param name
92       *            the testcase's name.
93       */
94      public TestCastorFileSystemDocumentHandler( String name )
95      {
96          super(name);
97      }
98  
99      /***
100      * Start the tests.
101      * 
102      * @param args
103      *            the arguments. Not used
104      */
105     public static void main( String args[] )
106     {
107         junit.awtui.TestRunner.main(new String[]{TestCastorFileSystemDocumentHandler.class.getName()});
108     }
109 
110     /***
111      * Creates the test suite.
112      * 
113      * @return a test suite (<code>TestSuite</code>) that includes all
114      *         methods starting with "test"
115      */
116     public static Test suite()
117     {
118         // All methods starting with "test" will be executed in the test suite.
119         return new TestSuite(TestCastorFileSystemDocumentHandler.class);
120     }
121     
122     public void testFolderMetaData() throws Exception
123     {
124         Document doc = folderMetaDataDocumentHandler.getDocument("/folder1/folder.metadata", false);
125         assertNotNull(doc);
126         String title = doc.getTitle();
127         assertEquals("Default Title for Folder 1", title);
128     }
129 
130     public void testFolderMetaDataInParallel() throws Exception
131     {
132         Thread [] threads = new Thread[10];
133         int i;
134         final List exceptions = new ArrayList(10);
135         
136         for (i = 0; i < threads.length; i++)
137         {
138             threads[i] = new Thread(new Runnable()
139                 {
140                     public void run()
141                     {
142                         try
143                         {
144                             Document doc = folderMetaDataDocumentHandler.getDocument("/folder1/folder.metadata", false);
145                         }
146                         catch (Exception e)
147                         {
148                             e.printStackTrace(System.out);
149                             exceptions.add(e);
150                         }
151                     }
152                 });
153         }
154         
155         for (i = 0; i < threads.length; i++)
156         {
157             threads[i].start();
158         }
159         
160         for (i = 0; i < threads.length; i++)
161         {
162             threads[i].join();
163         }
164         
165         assertTrue("folderMetaDataDocumentHandler.getDocument() is not thread-safe!", exceptions.size() == 0);
166     }
167 
168 }