KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > test > AbstractDocumentTaskTest


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.test;
17
18 import org.outerj.daisy.repository.testsupport.AbstractDaisyTestCase;
19 import org.outerj.daisy.repository.*;
20 import org.outerj.daisy.repository.schema.RepositorySchema;
21 import org.outerj.daisy.repository.schema.DocumentType;
22 import org.outerj.daisy.repository.user.Role;
23 import org.outerj.daisy.repository.user.User;
24 import org.outerj.daisy.doctaskrunner.*;
25 import org.outerj.daisy.doctaskrunner.commonimpl.EnumerationDocumentSelection;
26
27 public abstract class AbstractDocumentTaskTest extends AbstractDaisyTestCase {
28     protected boolean resetDataStores() {
29         return true;
30     }
31
32     protected abstract RepositoryManager getRepositoryManager() throws Exception JavaDoc;
33
34     public void testTaskManager() throws Exception JavaDoc {
35         RepositoryManager repositoryManager = getRepositoryManager();
36         Repository repository = repositoryManager.getRepository(new Credentials("testuser", "testuser"));
37         repository.switchRole(Role.ADMINISTRATOR);
38
39         RepositorySchema schema = repository.getRepositorySchema();
40         DocumentType documentType = schema.createDocumentType("doctype1");
41         documentType.save();
42
43         final Document document1 = repository.createDocument("document1", documentType.getId());
44         document1.save();
45
46         final Document document2 = repository.createDocument("document2", documentType.getId());
47         document2.save();
48
49         DocumentTaskManager taskManager = (DocumentTaskManager)repository.getExtension("DocumentTaskManager");
50         DocumentSelection selection = new EnumerationDocumentSelection(new VariantKey[] {document1.getVariantKey(), document2.getVariantKey()});
51
52         {
53             TaskSpecification specification = taskManager.createTaskSpecification("my test task", "throw 'oops';", "javascript", true);
54             long taskId = taskManager.runTask(selection, specification);
55
56             waitTillTaskCompletion(taskId, taskManager);
57
58             Task task = taskManager.getTask(taskId);
59             assertTrue(TaskState.INTERRUPTED_BY_ERROR == task.getState());
60             TaskDocDetail[] taskDocDetails = taskManager.getTaskDocDetails(taskId).getArray();
61             assertEquals(2, taskDocDetails.length);
62             assertEquals(document1.getVariantKey(), taskDocDetails[0].getVariantKey());
63             assertEquals(document2.getVariantKey(), taskDocDetails[1].getVariantKey());
64             assertTrue(taskDocDetails[0].getState() == DocumentExecutionState.ERROR);
65             assertTrue(taskDocDetails[1].getState() == DocumentExecutionState.WAITING);
66         }
67
68         // The same but don't stop at the first error
69
{
70             TaskSpecification specification = taskManager.createTaskSpecification("my test task", "throw 'oops';", "javascript", false);
71             long taskId = taskManager.runTask(selection, specification);
72             waitTillTaskCompletion(taskId, taskManager);
73
74             // same tests
75
Task task = taskManager.getTask(taskId);
76             assertTrue(TaskState.FINISHED_WITH_ERRORS == task.getState());
77             TaskDocDetail[] taskDocDetails = taskManager.getTaskDocDetails(taskId).getArray();
78             assertEquals(2, taskDocDetails.length);
79             assertEquals(document1.getVariantKey(), taskDocDetails[0].getVariantKey());
80             assertEquals(document2.getVariantKey(), taskDocDetails[1].getVariantKey());
81             assertTrue(taskDocDetails[0].getState() == DocumentExecutionState.ERROR);
82             assertTrue(taskDocDetails[1].getState() == DocumentExecutionState.ERROR);
83             assertNotNull(taskDocDetails[0].getDetails());
84             assertNotNull(taskDocDetails[1].getDetails());
85         }
86
87         // now run a meaningful task which does something with the documents
88
{
89             String JavaDoc script = "var doc = repository.getDocument(variantKey, true);\n" +
90                     "doc.setCustomField('a', 'b' + doc.getId());\n" +
91                     "doc.save()";
92             TaskSpecification specification = taskManager.createTaskSpecification("my test task", script, "javascript", false);
93             long taskId = taskManager.runTask(selection, specification);
94
95             waitTillTaskCompletion(taskId, taskManager);
96             Task task = taskManager.getTask(taskId);
97             assertTrue(TaskState.FINISHED == task.getState());
98             TaskDocDetail[] taskDocDetails = taskManager.getTaskDocDetails(taskId).getArray();
99             assertEquals(2, taskDocDetails.length);
100             assertTrue(taskDocDetails[0].getState() == DocumentExecutionState.DONE);
101             assertTrue(taskDocDetails[1].getState() == DocumentExecutionState.DONE);
102             assertNull(taskDocDetails[0].getDetails());
103             assertNull(taskDocDetails[1].getDetails());
104
105             Document document1Reloaded = repository.getDocument(document1.getVariantKey(), false);
106             assertEquals("b" + document1Reloaded.getId(), document1Reloaded.getCustomField("a"));
107             Document document2Reloaded = repository.getDocument(document2.getVariantKey(), false);
108             assertEquals("b" + document2Reloaded.getId(), document2Reloaded.getCustomField("a"));
109         }
110
111         // check that another users cannot see these tasks
112
{
113             User newUser = repository.getUserManager().createUser("jan");
114             Role userRole = repository.getUserManager().getRole("User", false);
115             newUser.addToRole(userRole);
116             newUser.setDefaultRole(userRole);
117             newUser.setPassword("pwd");
118             newUser.save();
119
120             Repository janRepository = repositoryManager.getRepository(new Credentials("jan", "pwd"));
121             DocumentTaskManager janTaskManager = (DocumentTaskManager)janRepository.getExtension("DocumentTaskManager");
122             assertEquals(0, janTaskManager.getTasks().getArray().length);
123
124             // check that testuser can also see the tasks in non-admin role
125
repository.switchRole(userRole.getId());
126             Task[] tasks = taskManager.getTasks().getArray();
127             assertEquals(3, tasks.length);
128             repository.switchRole(Role.ADMINISTRATOR);
129
130             // check unability for jan to delete task
131
try {
132                 janTaskManager.deleteTask(tasks[0].getId());
133                 fail("User jan should not be able to delete task that does not belong to him.");
134             } catch (TaskException e) {}
135
136             // check unability for jan to retrieve task details
137
try {
138                 janTaskManager.getTaskDocDetails(tasks[0].getId());
139                 fail("User jan should not be able to get task details of a task that does not belong to him.");
140             } catch (TaskException e) {}
141         }
142
143         // try getting all tasks and deleting tasks
144
{
145             Task[] tasks = taskManager.getTasks().getArray();
146             assertEquals(3, tasks.length);
147             for (int i = 0; i < tasks.length;i ++) {
148                 taskManager.deleteTask(tasks[i].getId());
149             }
150
151             try {
152                 taskManager.getTask(tasks[0].getId());
153                 fail("Getting a deleted task should fail.");
154             } catch (TaskException e) {}
155         }
156
157         // try user interruption of task
158
{
159             String JavaDoc script = "java.lang.Thread.sleep(3000);";
160             TaskSpecification specification = taskManager.createTaskSpecification("my test task", script, "javascript", false);
161             long taskId = taskManager.runTask(selection, specification);
162             taskManager.interruptTask(taskId);
163             waitTillTaskCompletion(taskId, taskManager);
164             Task task = taskManager.getTask(taskId);
165             assertTrue(task.getState() == TaskState.INTERRUPTED_BY_USER);
166         }
167
168         // test simple actions
169
{
170             DocumentCollection collection = repository.getCollectionManager().createCollection("abc");
171             collection.save();
172
173             SimpleActionsTaskSpecification specification = taskManager.createSimpleActionsTaskSpecification("simple actions test", false);
174             specification.addAddToCollection("abc");
175             specification.addRemoveFromCollection("abc");
176             long taskId = taskManager.runTask(selection, specification);
177             waitTillTaskCompletion(taskId, taskManager);
178             Task task = taskManager.getTask(taskId);
179             assertTrue(task.getState() == TaskState.FINISHED);
180         }
181     }
182
183     private void waitTillTaskCompletion(long taskId, DocumentTaskManager taskManager) throws Exception JavaDoc {
184         Task task = taskManager.getTask(taskId);
185         while (!task.getState().isStoppedState()) {
186             System.out.println("Waiting for task to finish...");
187             Thread.sleep(500);
188             task = taskManager.getTask(taskId);
189         }
190     }
191 }
192
Popular Tags