KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > importer > FileImporterTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.importer;
18
19 import java.io.File JavaDoc;
20 import java.io.FileFilter JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.transaction.HeuristicMixedException JavaDoc;
25 import javax.transaction.HeuristicRollbackException JavaDoc;
26 import javax.transaction.NotSupportedException JavaDoc;
27 import javax.transaction.RollbackException JavaDoc;
28 import javax.transaction.SystemException JavaDoc;
29 import javax.transaction.UserTransaction JavaDoc;
30
31 import junit.framework.TestCase;
32
33 import org.alfresco.error.AlfrescoRuntimeException;
34 import org.alfresco.model.ContentModel;
35 import org.alfresco.repo.content.transform.AbstractContentTransformerTest;
36 import org.alfresco.repo.security.authentication.AuthenticationComponent;
37 import org.alfresco.service.ServiceRegistry;
38 import org.alfresco.service.cmr.dictionary.DictionaryService;
39 import org.alfresco.service.cmr.repository.ContentService;
40 import org.alfresco.service.cmr.repository.MimetypeService;
41 import org.alfresco.service.cmr.repository.NodeRef;
42 import org.alfresco.service.cmr.repository.NodeService;
43 import org.alfresco.service.cmr.repository.StoreRef;
44 import org.alfresco.service.cmr.search.ResultSet;
45 import org.alfresco.service.cmr.search.SearchParameters;
46 import org.alfresco.service.cmr.search.SearchService;
47 import org.alfresco.service.cmr.security.AuthenticationService;
48 import org.alfresco.service.namespace.NamespaceService;
49 import org.alfresco.service.transaction.TransactionService;
50 import org.alfresco.util.ApplicationContextHelper;
51 import org.alfresco.util.TempFileProvider;
52 import org.springframework.context.ApplicationContext;
53
54 public class FileImporterTest extends TestCase
55 {
56     static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
57     private NodeService nodeService;
58     private SearchService searchService;
59     private DictionaryService dictionaryService;
60     private ContentService contentService;
61     private AuthenticationService authenticationService;
62     private AuthenticationComponent authenticationComponent;
63     private MimetypeService mimetypeService;
64     private NamespaceService namespaceService;
65
66     private ServiceRegistry serviceRegistry;
67     private NodeRef rootNodeRef;
68
69     private SearchService searcher;
70
71     public FileImporterTest()
72     {
73         super();
74     }
75
76     public FileImporterTest(String JavaDoc arg0)
77     {
78         super(arg0);
79     }
80
81     public void setUp()
82     {
83         serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
84
85         searcher = serviceRegistry.getSearchService();
86         nodeService = serviceRegistry.getNodeService();
87         searchService = serviceRegistry.getSearchService();
88         dictionaryService = serviceRegistry.getDictionaryService();
89         contentService = serviceRegistry.getContentService();
90         authenticationService = (AuthenticationService) ctx.getBean("authenticationService");
91         authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponent");
92         mimetypeService = serviceRegistry.getMimetypeService();
93         namespaceService = serviceRegistry.getNamespaceService();
94
95         authenticationComponent.setSystemUserAsCurrentUser();
96         StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
97         rootNodeRef = nodeService.getRootNode(storeRef);
98     }
99
100     private FileImporter createFileImporter()
101     {
102         FileImporterImpl fileImporter = new FileImporterImpl();
103         fileImporter.setAuthenticationService(authenticationService);
104         fileImporter.setContentService(contentService);
105         fileImporter.setMimetypeService(mimetypeService);
106         fileImporter.setNodeService(nodeService);
107         fileImporter.setDictionaryService(dictionaryService);
108         return fileImporter;
109     }
110
111     public void testCreateFile() throws Exception JavaDoc
112     {
113         FileImporter fileImporter = createFileImporter();
114         File JavaDoc file = AbstractContentTransformerTest.loadQuickTestFile("xml");
115         fileImporter.loadFile(rootNodeRef, file);
116     }
117
118     public void testLoadRootNonRecursive1()
119     {
120         FileImporter fileImporter = createFileImporter();
121         URL JavaDoc url = this.getClass().getClassLoader().getResource("");
122         File JavaDoc root = new File JavaDoc(url.getFile());
123         int count = fileImporter.loadFile(rootNodeRef, new File JavaDoc(url.getFile()));
124         assertEquals("Expected to load a single file", 1, count);
125     }
126
127     public void testLoadRootNonRecursive2()
128     {
129         FileImporter fileImporter = createFileImporter();
130         URL JavaDoc url = this.getClass().getClassLoader().getResource("");
131         File JavaDoc root = new File JavaDoc(url.getFile());
132         int count = fileImporter.loadFile(rootNodeRef, root, null, false);
133         assertEquals("Expected to load a single file", 1, count);
134     }
135
136     public void testLoadXMLFiles()
137     {
138         FileImporter fileImporter = createFileImporter();
139         URL JavaDoc url = this.getClass().getClassLoader().getResource("");
140         FileFilter JavaDoc filter = new XMLFileFilter();
141         fileImporter.loadFile(rootNodeRef, new File JavaDoc(url.getFile()), filter, true);
142     }
143
144     public void testLoadSourceTestResources()
145     {
146         FileImporter fileImporter = createFileImporter();
147         URL JavaDoc url = this.getClass().getClassLoader().getResource("quick");
148         FileFilter JavaDoc filter = new QuickFileFilter();
149         fileImporter.loadFile(rootNodeRef, new File JavaDoc(url.getFile()), filter, true);
150     }
151
152     private static class XMLFileFilter implements FileFilter JavaDoc
153     {
154         public boolean accept(File JavaDoc file)
155         {
156             return file.getName().endsWith(".xml");
157         }
158     }
159
160     private static class QuickFileFilter implements FileFilter JavaDoc
161     {
162         public boolean accept(File JavaDoc file)
163         {
164             return file.getName().startsWith("quick");
165         }
166     }
167
168     /**
169      * @param args
170      * <ol>
171      * <li>StoreRef
172      * <li>Store Path
173      * <li>Directory
174      * <li>Optional maximum time in seconds for node loading
175      * </ol>
176      * @throws SystemException
177      * @throws NotSupportedException
178      * @throws HeuristicRollbackException
179      * @throws HeuristicMixedException
180      * @throws RollbackException
181      * @throws IllegalStateException
182      * @throws SecurityException
183      */

184     public static final void main(String JavaDoc[] args) throws Exception JavaDoc
185     {
186
187         int exitCode = 0;
188
189         int grandTotal = 0;
190         int count = 0;
191         int target = Integer.parseInt(args[4]);
192         while (count < target)
193         {
194             File JavaDoc directory = TempFileProvider.getTempDir();
195             File JavaDoc[] files = directory.listFiles();
196             System.out.println("Start temp file count = " + files.length);
197             
198             count++;
199             FileImporterTest test = new FileImporterTest();
200             test.setUp();
201             
202             test.authenticationComponent.setSystemUserAsCurrentUser();
203             TransactionService transactionService = test.serviceRegistry.getTransactionService();
204             UserTransaction JavaDoc tx = transactionService.getUserTransaction();
205             tx.begin();
206
207             try
208             {
209                 StoreRef spacesStore = new StoreRef(args[0]);
210                 if (!test.nodeService.exists(spacesStore))
211                 {
212                     test.nodeService.createStore(spacesStore.getProtocol(), spacesStore.getIdentifier());
213                 }
214
215                 NodeRef storeRoot = test.nodeService.getRootNode(spacesStore);
216                 List JavaDoc<NodeRef> location = test.searchService.selectNodes(
217                         storeRoot,
218                         args[1],
219                         null,
220                         test.namespaceService,
221                         false);
222                 if (location.size() == 0)
223                 {
224                     throw new AlfrescoRuntimeException(
225                             "Root node not found, " +
226                             args[1] +
227                             " not found in store, " +
228                             storeRoot);
229                 }
230
231                 long start = System.nanoTime();
232                 int importCount = test.createFileImporter().loadNamedFile(location.get(0), new File JavaDoc(args[2]), true, args[3]+count);
233                 grandTotal += importCount;
234                 long end = System.nanoTime();
235                 long first = end-start;
236                 System.out.println("Created in: " + ((end - start) / 1000000.0) + "ms");
237                 start = System.nanoTime();
238
239                 tx.commit();
240                 end = System.nanoTime();
241                 long second = end-start;
242                 System.out.println("Committed in: " + ((end - start) / 1000000.0) + "ms");
243                 double total = ((first+second)/1000000.0);
244                 System.out.println("Grand Total: "+ grandTotal);
245                 System.out.println("Count: "+ count + "ms");
246                 System.out.println("Imported: " + importCount + " files or directories");
247                 System.out.println("Average: " + (importCount / (total / 1000.0)) + " files per second");
248                 
249                 directory = TempFileProvider.getTempDir();
250                 files = directory.listFiles();
251                 System.out.println("End temp file count = " + files.length);
252                 
253                 
254                 tx = transactionService.getUserTransaction();
255                 tx.begin();
256                 SearchParameters sp = new SearchParameters();
257                 sp.setLanguage("lucene");
258                 sp.setQuery("ISNODE:T");
259                 sp.addStore(spacesStore);
260                 start = System.nanoTime();
261                 ResultSet rs = test.searchService.query(sp);
262                 end = System.nanoTime();
263                 System.out.println("Find all in: " + ((end - start) / 1000000.0) + "ms");
264                 System.out.println(" = "+rs.length() +"\n\n");
265                 rs.close();
266                 
267                 sp = new SearchParameters();
268                 sp.setLanguage("lucene");
269                 sp.setQuery("TEXT:\"andy\"");
270                 sp.addStore(spacesStore);
271                 start = System.nanoTime();
272                 rs = test.searchService.query(sp);
273                 end = System.nanoTime();
274                 System.out.println("Find andy in: " + ((end - start) / 1000000.0) + "ms");
275                 System.out.println(" = "+rs.length() +"\n\n");
276                 rs.close();
277                 
278                 sp = new SearchParameters();
279                 sp.setLanguage("lucene");
280                 sp.setQuery("TYPE:\"" + ContentModel.TYPE_CONTENT.toString() + "\"");
281                 sp.addStore(spacesStore);
282                 start = System.nanoTime();
283                 rs = test.searchService.query(sp);
284                 end = System.nanoTime();
285                 System.out.println("Find content in: " + ((end - start) / 1000000.0) + "ms");
286                 System.out.println(" = "+rs.length() +"\n\n");
287                 rs.close();
288                 
289                 sp = new SearchParameters();
290                 sp.setLanguage("lucene");
291                 sp.setQuery("PATH:\"/*/*/*\"");
292                 sp.addStore(spacesStore);
293                 start = System.nanoTime();
294                 rs = test.searchService.query(sp);
295                 end = System.nanoTime();
296                 System.out.println("Find /*/*/* in: " + ((end - start) / 1000000.0) + "ms");
297                 System.out.println(" = "+rs.length() +"\n\n");
298                 rs.close();
299                 
300                 tx.commit();
301                 
302             }
303             catch (Throwable JavaDoc e)
304             {
305                 tx.rollback();
306                 e.printStackTrace();
307                 exitCode = 1;
308             }
309             //System.exit(exitCode);
310
}
311         System.exit(0);
312     }
313 }
314
Popular Tags