1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.routeheader;
17
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 import java.sql.Timestamp;
25
26 import org.junit.Test;
27 import org.kuali.rice.core.api.config.property.Config;
28 import org.kuali.rice.core.api.config.property.ConfigContext;
29 import org.kuali.rice.kew.api.WorkflowDocument;
30 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
31 import org.kuali.rice.kew.api.exception.LockingException;
32 import org.kuali.rice.kew.doctype.bo.DocumentType;
33 import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
34 import org.kuali.rice.kew.service.KEWServiceLocator;
35 import org.kuali.rice.kew.test.KEWTestCase;
36 import org.kuali.rice.kew.api.KewApiConstants;
37 import org.kuali.rice.test.BaselineTestCase;
38 import org.springframework.transaction.TransactionStatus;
39 import org.springframework.transaction.support.TransactionCallback;
40
41
42 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
43 public class RouteHeaderServiceTest extends KEWTestCase {
44
45 private Object lock = new Object();
46 private RouteHeaderService routeHeaderService;
47
48 protected void setUpAfterDataLoad() throws Exception {
49 super.setUpAfterDataLoad();
50 routeHeaderService = KEWServiceLocator.getRouteHeaderService();
51 }
52
53
54
55
56
57
58
59 @Test
60 public void testLargeDocumentContent() throws Exception {
61 StringBuffer buffer = new StringBuffer();
62 buffer.append("<content>");
63 for (int index = 0; index < 10000; index++) {
64 buffer.append("abcdefghijklmnopqrstuvwxyz");
65 }
66 buffer.append("</content>");
67 DocumentRouteHeaderValue document = new DocumentRouteHeaderValue();
68 document.setDocContent(buffer.toString());
69 document.setDocRouteStatus(KewApiConstants.ROUTE_HEADER_INITIATED_CD);
70 document.setDocRouteLevel(0);
71 document.setDateModified(new Timestamp(System.currentTimeMillis()));
72 document.setCreateDate(new Timestamp(System.currentTimeMillis()));
73 document.setInitiatorWorkflowId("1");
74 DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName("TestDocumentType");
75 assertNotNull(documentType);
76 document.setDocumentTypeId(documentType.getDocumentTypeId());
77 routeHeaderService.saveRouteHeader(document);
78 assertNotNull("Document was saved, it should have an ID now.", document.getDocumentId());
79
80
81 document = routeHeaderService.getRouteHeader(document.getDocumentId());
82 String docContent = document.getDocContent();
83 assertEquals("Doc content should be the same size as original string buffer.", buffer.length(), docContent.length());
84 assertTrue("Should be greater than about 5000 bytes.", docContent.getBytes().length > 5000);
85 }
86
87 @Test public void testGetApplicationIdByDocumentId() throws Exception {
88 WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType2");
89 String documentId = document.getDocumentId();
90 String applicationId = routeHeaderService.getApplicationIdByDocumentId(documentId);
91 assertEquals("applicationId should be KEWNEW", "KEWNEW", applicationId);
92
93
94 document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
95 documentId = document.getDocumentId();
96 applicationId = routeHeaderService.getApplicationIdByDocumentId(documentId);
97 assertEquals("applicationId should be KUALI", "KUALI", applicationId);
98 }
99
100 @Test public void testLockRouteHeader() throws Exception {
101 WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "TestDocumentType");
102 document.saveDocumentData();
103 final String documentId = document.getDocumentId();
104 Locker locker = null;
105 synchronized (lock) {
106 locker = new Locker(documentId);
107 locker.start();
108 lock.wait();
109 }
110
111 try {
112 routeHeaderService.lockRouteHeader(documentId, false);
113 fail("The route header should be locked.");
114 } catch (LockingException e) {
115
116 }
117 synchronized (lock) {
118 lock.notify();
119 }
120 locker.join();
121
122 routeHeaderService.lockRouteHeader(documentId, false);
123 assertTrue("Locker thread should have completed.", locker.isCompleted());
124
125
126 ConfigContext.getCurrentContextConfig().putProperty(Config.DOCUMENT_LOCK_TIMEOUT, "2");
127 synchronized (lock) {
128 locker = new Locker(documentId);
129 locker.start();
130 lock.wait();
131 }
132
133 long millisStart = System.currentTimeMillis();
134 try {
135 routeHeaderService.lockRouteHeader(documentId, true);
136 fail("The route header should be locked.");
137 } catch (LockingException e) {
138
139 }
140
141 long millisEnd = System.currentTimeMillis();
142 long timeLocked = (millisEnd - millisStart);
143
144 synchronized(lock) {
145 lock.notify();
146 }
147 locker.join();
148
149
150
151
152
153 routeHeaderService.lockRouteHeader(document.getDocumentId(), false);
154 assertTrue("Locker thread should have completed.", locker.isCompleted());
155 }
156
157 private class Locker extends Thread {
158 private String documentId;
159 private boolean isCompleted = false;
160 public Locker(String documentId) {
161 this.documentId = documentId;
162 }
163 public void run() {
164 getTransactionTemplate().execute(new TransactionCallback() {
165 public Object doInTransaction(TransactionStatus status) {
166 synchronized (lock) {
167 routeHeaderService.lockRouteHeader(documentId, true);
168 try {
169 lock.notify();
170 lock.wait();
171 } catch (InterruptedException e) {
172 fail("Shouldn't have been interrupted");
173 }
174 }
175 return null;
176 }
177 });
178 isCompleted = true;
179 }
180 public boolean isCompleted() {
181 return isCompleted;
182 }
183 }
184
185 }