File: ScriptTestCase.java

package info (click to toggle)
derby 10.14.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 78,896 kB
  • sloc: java: 691,930; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 60
file content (227 lines) | stat: -rw-r--r-- 6,969 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 *
 * Derby - Class org.apache.derbyTesting.functionTests.util.ScriptTestCase
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an 
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. See the License for the specific 
 * language governing permissions and limitations under the License.
 */
package org.apache.derbyTesting.functionTests.util;

import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.util.Locale;
import junit.framework.Test;
import org.apache.derby.iapi.tools.i18n.LocalizedResource;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.Derby;
import org.apache.derbyTesting.junit.LocaleTestSetup;

/**
 * Run a .sql script as a test comparing it to
 * a master output file.
 *
 */
public abstract class ScriptTestCase extends CanonTestCase {

	private final String inputEncoding;
	private final String user;
    private boolean useSystemProperties = false;
    private Locale oldLocale;

    /**
	 * Create a ScriptTestCase to run a single test
     * using a connection obtained from getConnection()
	 * @param script Base name of the .sql script
     * @param useSystemProperties Whether to use system properties for this test
	 * excluding the .sql suffix.
	 */
	public ScriptTestCase(String script, boolean useSystemProperties)
	{
        this(script, null, null, null);
        this.useSystemProperties = useSystemProperties;
	}

	/**
	 * Create a ScriptTestCase to run a single test
     * using a connection obtained from getConnection()
	 * @param script Base name of the .sql script
	 * excluding the .sql suffix.
	 */
	public ScriptTestCase(String script)
	{
        this(script, null, null, null);
	}
	
    /**
     * Create a ScriptTestCase to run a single test
     * using a connection obtained from getConnection() with a
     * different encoding
     * @param script Base name of the .sql script
     * excluding the .sql suffix.
     */
    public ScriptTestCase(String script, String encoding)
    {
        this(script, encoding, encoding, null);
    }

    /**
     * Create a ScriptTestCase to run a single test
     * using a connection obtained from getConnection() with a
     * different encoding.
     * @param script     Base name of the .sql script
     *                   excluding the .sql suffix.
     * @param inputEnc   The encoding for the script, if not null,
     *                   else use "US-ASCII"
     * @param outputEnc  The encoding for the ouput from the script,
     *                   if not null, else use "US-ASCII"
     * @param user       Run script as user
     */
    public ScriptTestCase(String script,
            String inputEnc, String outputEnc, String user)
    {
        super(script, outputEnc);
        setSystemProperty("ij.showNoConnectionsAtStart", "true");
        setSystemProperty("ij.showNoCountForSelect", "true");
        inputEncoding = (inputEnc == null) ? DEFAULT_ENCODING : inputEnc;

		this.user = user;
    }

    /**
	 * Return the folder (last element of the package) where
	 * the .sql script lives, e.g. lang.
	 */
	protected String getArea() {
		
		String name =  getClass().getName();
		
		int lastDot = name.lastIndexOf('.');
		
		name = name.substring(0, lastDot);
		
		lastDot = name.lastIndexOf('.');
		
		return name.substring(lastDot+1);
	}
		
	/**
	 * Get a decorator to setup the ij in order
	 * to run the test. A sub-class must decorate
	 * its suite using this call.
	 */
	public static Test getIJConfig(Test test)
	{
        // Need the tools to run the scripts as this
        // test uses ij as the script runner.
        if (!Derby.hasTools())
            return new BaseTestSuite("empty: no tools support");
            
		// No decorator needed currently.
		return test;
	}
	
	/**
	 * Run the test, using the resource as the input.
	 * Compare to the master file using a very simple
	 * line by line comparision. Fails at the first
	 * difference. If a failure occurs the output
	 * is written into the current directory as
	 * testScript.out, otherwise the output is only
	 * kept in memory.
	 * @throws Throwable 
	 */
	public void runTest() throws Throwable
	{
		String resource =
			"org/apache/derbyTesting/functionTests/tests/"
			+ getArea() + "/"
			+ getName() + ".sql";
		
		String canon =
			"org/apache/derbyTesting/functionTests/master/"
			+ getName() + ".out";

		URL sql = getTestResource(resource);
		assertNotNull("SQL script missing: " + resource, sql);
		
		InputStream sqlIn = openTestResource(sql);

		Connection conn;

		if (user != null) {
			conn = openUserConnection(user);
		} else {
			conn = getConnection();
		}

        final String outputEnc;
        final String derby_ui_codeset = getSystemProperty("derby.ui.codeset");

        if (derby_ui_codeset != null) {
            // IJ should format output according to the derby.ui.codeset
            // variable. If we pass in an encoding explicitly to runScript(),
            // we won't test that derby.ui.codeset is obeyed. Therefore,
            // leave it as null.
            outputEnc = null;
            assertEquals(
                    "Requested output encoding and derby.ui.codeset differ",
                    outputEncoding, derby_ui_codeset);
        } else {
            // derby.ui.codeset isn't set. Tell runScript() which output
            // encoding to use.
            outputEnc = outputEncoding;
        }
        
		org.apache.derby.tools.ij.runScript(
				conn,
				sqlIn,
				inputEncoding,
                getOutputStream(),
				outputEnc,
                useSystemProperties);
		
		if (!conn.isClosed() && !conn.getAutoCommit())
		    conn.commit();
		
		sqlIn.close();
        
        this.compareCanon(canon);
	}
    
    /**
     * Set up the new locale for the test
     */
    protected void setUp() {
        oldLocale = Locale.getDefault();
        LocaleTestSetup.setDefaultLocale(Locale.US);

        // Reset IJ's locale to allow it to pick up the new locale from
        // the environment.
        LocalizedResource.resetLocalizedResourceCache();
    }

    /**
     * Revert the locale back to the old one
     */
    protected void tearDown() throws Exception {
        super.tearDown();

        LocaleTestSetup.setDefaultLocale(oldLocale);

        // Forget the locale used by this test.
        LocalizedResource.resetLocalizedResourceCache();
    }
}