KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > ScriptTestCase


1 /*
2  *
3  * Derby - Class org.apache.derbyTesting.functionTests.util.ScriptTestCase
4  *
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements. See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */

20 package org.apache.derbyTesting.functionTests.util;
21
22 import java.io.InputStream JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.sql.Connection JavaDoc;
25
26 import junit.framework.Test;
27
28 /**
29  * Run a .sql script as a test comparing it to
30  * a master output file.
31  *
32  */

33 public abstract class ScriptTestCase extends CanonTestCase {
34     
35     private final String JavaDoc inputEncoding;
36
37     /**
38      * Create a ScriptTestCase to run a single test
39      * using a connection obtained from getConnection()
40      * @param script Base name of the .sql script
41      * excluding the .sql suffix.
42      */

43     public ScriptTestCase(String JavaDoc script)
44     {
45         super(script);
46         inputEncoding = "US-ASCII";
47     }
48     
49     /**
50      * Return the folder (last element of the package) where
51      * the .sql script lives, e.g. lang.
52      * @return
53      */

54     protected String JavaDoc getArea() {
55         
56         String JavaDoc name = getClass().getName();
57         
58         int lastDot = name.lastIndexOf('.');
59         
60         name = name.substring(0, lastDot);
61         
62         lastDot = name.lastIndexOf('.');
63         
64         return name.substring(lastDot+1);
65     }
66         
67     /**
68      * Get a decorator to setup the ij in order
69      * to run the test. A sub-class must decorate
70      * its suite using this call.
71      */

72     public static Test getIJConfig(Test test)
73     {
74         // No decorator needed currently.
75
return test;
76     }
77     
78     /**
79      * Run the test, using the resource as the input.
80      * Compare to the master file using a very simple
81      * line by line comparision. Fails at the first
82      * difference. If a failure occurs the output
83      * is written into the current directory as
84      * testScript.out, otherwise the output is only
85      * kept in memory.
86      * @throws Throwable
87      */

88     public void runTest() throws Throwable JavaDoc
89     {
90         String JavaDoc resource =
91             "org/apache/derbyTesting/functionTests/tests/"
92             + getArea() + "/"
93             + getName() + ".sql";
94         
95         String JavaDoc canon =
96             "org/apache/derbyTesting/functionTests/master/"
97             + getName() + ".out";
98
99         URL JavaDoc sql = getTestResource(resource);
100         assertNotNull("SQL script missing: " + resource, sql);
101         
102         InputStream JavaDoc sqlIn = openTestResource(sql);
103                     
104         Connection JavaDoc conn = getConnection();
105         org.apache.derby.tools.ij.runScript(
106                 conn,
107                 sqlIn,
108                 inputEncoding,
109                 getOutputStream(),
110                 outputEncoding);
111         
112         if (!conn.isClosed() && !conn.getAutoCommit())
113             conn.commit();
114         
115         sqlIn.close();
116         
117         this.compareCanon(canon);
118     }
119 }
120
Popular Tags