KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > execution > CancellationTest


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 scriptella.execution;
17
18 import scriptella.DBTestCase;
19 import scriptella.jdbc.QueryHelper;
20 import scriptella.spi.ParametersCallback;
21 import scriptella.spi.QueryCallback;
22
23 import java.sql.Connection JavaDoc;
24 import java.sql.SQLException JavaDoc;
25
26 /**
27  * Tests if ETL cancellation(interruption) is working.
28  *
29  * @author Fyodor Kupolov
30  * @version 1.0
31  */

32 public class CancellationTest extends DBTestCase {
33     private boolean interrupted;
34
35     public void test() throws EtlExecutorException {
36         EtlExecutor etlExecutor = newEtlExecutor();
37         Connection JavaDoc c = getConnection("cancelTest");
38         final Thread JavaDoc etlThread = Thread.currentThread();
39         interrupted = false;
40         new Thread JavaDoc() {
41             public void run() {
42                 try {
43                     Thread.sleep(200); //wait for ETL to start
44
etlThread.interrupt();
45                     interrupted = true;
46                 } catch (InterruptedException JavaDoc e) {
47                     Thread.currentThread().interrupt();
48                 }
49             }
50         }.start();
51         long ti = System.currentTimeMillis();
52         try {
53             etlExecutor.execute();
54         } catch (EtlExecutorException e) {
55             assertTrue(e.isCancelled());
56         }
57         ti = System.currentTimeMillis() - ti;
58         assertTrue(interrupted);
59         assertTrue(ti < 1000); //Long running ETL must be terminated ASAP
60
//Now check if the tables were removed
61
new QueryHelper("select count(*) from t1, t2") {
62             @Override JavaDoc protected void onSQLException(SQLException JavaDoc e) {
63                 if (e.getMessage().indexOf("not found")<0) {
64                     super.onSQLException(e);
65                 }
66
67             }
68         }.execute(c, new QueryCallback() {
69             public void processRow(final ParametersCallback parameters) {
70                 assertEquals(0, parameters.getParameter("1"));
71             }
72         });
73     }
74 }
75
Popular Tags