File: DerbyDistribution.java

package info (click to toggle)
derby 10.14.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,740 kB
  • sloc: java: 691,931; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 46
file content (339 lines) | stat: -rw-r--r-- 12,293 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*

   Derby - Class org.apache.derbyTesting.junit.DerbyDistribution

   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.junit;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.derbyTesting.functionTests.util.PrivilegedFileOpsForTests;

/**
 * Holds information required to run a Derby distribution and make choices
 * based on the version of the Derby distribution.
 * <p>
 * <em>Implementation note</em>: For simplicity distributions off the classes
 * directory have been forbidden. The main reason for this is that it is
 * sometimes a hard requirement that you must include only a single JAR from a
 * distribution on the classpath. One such example is the compatibility test,
 * where you need the testing code from one distribution and the client driver
 * from another. While it is possible to support such a configuration running
 * off the {@code classes}-directory in many scenarios, it complicates
 * the creation and handling of classpath string. Generating the JARs when
 * testing on trunk seems like an acceptable price to pay.
 */
public class DerbyDistribution implements Comparable<DerbyDistribution> {

    private static File[] EMPTY_FILE_ARRAY = new File[] {};
    public static final String JAR_RUN = "derbyrun.jar";
    public static final String JAR_CLIENT = "derbyclient.jar";
    public static final String JAR_ENGINE = "derby.jar";
    public static final String JAR_NET = "derbynet.jar";
    public static final String JAR_TESTING = "derbyTesting.jar";
    private static final String[] REQUIRED_JARS = {
        JAR_ENGINE, JAR_NET, JAR_CLIENT
    };

    /** The version of the Derby distribution, i.e. 10.8.1.2. */
    private final DerbyVersion version;
    /** Path to derbyrun.jar (may be {@code null}). */
    private final String derbyRunJarPath;
    /** Path to derbyclient.jar. */
    private final String derbyClientJarPath;
    /** Path to derby.jar. */
    private final String derbyEngineJarPath;
    /** Path to derbynet.jar. */
    private final String derbyNetJarPath;
    /**
     * Production classpath, i.e. all JAR files found except for
     * derbyTesting.jar.
     */
    private final String productionClasspath;
    /** Testing classpath, i.e. path to derbyTesting.jar. */
    private final String testingClasspath;

    /**
     * Derives the information for a Derby distribution.
     *
     * @throws NullPointerException if version is {@code null}
     * @see #newInstance(DerbyVersion, File)
     */
    private DerbyDistribution(DerbyVersion version,
                              File[] productionJars, File[] testingJars) {
        if (version == null) {
            throw new NullPointerException("version is null");
        }
        this.version = version;
        this.productionClasspath = constructJarClasspath(productionJars);
        this.testingClasspath = constructJarClasspath(testingJars);
        File root = productionJars[0].getParentFile();
        this.derbyRunJarPath = getPath(root, JAR_RUN);
        this.derbyClientJarPath = getPath(root, JAR_CLIENT);
        this.derbyEngineJarPath = getPath(root, JAR_ENGINE);
        this.derbyNetJarPath = getPath(root, JAR_NET);
    }

    /** Returns the absolute path to the JAR if it exists, otherwise null. */
    private String getPath(File root, String jar) {
        File f = new File(root, jar);
        if (PrivilegedFileOpsForTests.exists(f)) {
            return f.getAbsolutePath();
        } else {
            return null;
        }
    }

    /** Tells if this distribution has a {@code derbyrun.jar}. */
    public boolean hasDerbyRunJar() {
        return derbyRunJarPath != null;
    }

    /**
     * Returns the path to {@code derbyrun.jar}.
     *
     * @return A path, or {@code null} if this distribution doesn't come with
     *      {@code derbyrun.jar}.
     * @see #hasDerbyRunJar()
     */
    public String getDerbyRunJarPath() {
        return derbyRunJarPath;
    }

    /** Returns the path to {@code derbyclient.jar}. */
    public String getDerbyClientJarPath() {
        return derbyClientJarPath;
    }

    /** Returns the path to {@code derby.jar}. */
    public String getDerbyEngineJarPath() {
        return derbyEngineJarPath;
    }

    /** Returns the path to {@code derbynet.jar}. */
    public String getDerbyNetJarPath() {
        return derbyEngineJarPath;
    }

    /** Returns a classpath with the network server production JARs. */
    public String getServerClasspath() {
        return
            this.derbyNetJarPath + File.pathSeparator + this.derbyEngineJarPath;
    }

    /** Returns a classpath with all production JARs. */
    public String getProductionClasspath() {
        return productionClasspath;
    }

    /** Returns a classpath with all testing JARs. */
    public String getTestingClasspath() {
        return testingClasspath;
    }

    /** Returns a classpath with all production and testing JARs. */
    public String getFullClassPath() {
        return productionClasspath + File.pathSeparatorChar + testingClasspath;
    }

    /** Returns the version of this distribution. */
    public DerbyVersion getVersion() {
        return version;
    }

    /**
     * Orders this distribution and the other distribution based on the version.
     *
     * @param o the other distribution
     * @return {@code 1} if this version is newer, {@code 0} if both
     *      distributions have the same version, and {@code -1} if the other
     *      version is newer.
     */
    public int compareTo(DerbyDistribution o) {
        return version.compareTo(o.version);
    }

    private static boolean hasRequiredJars(List jars) {
        for (int i=0; i < REQUIRED_JARS.length; i++) {
            boolean hasJar = false;
            for (Iterator jarIter = jars.iterator(); jarIter.hasNext(); ) {
                File jar = (File)jarIter.next();
                if (jar.getName().equalsIgnoreCase(REQUIRED_JARS[i])) {
                    hasJar = true;
                    break;
                }
            }
            if (!hasJar) {
                BaseTestCase.println("missing jar: " + REQUIRED_JARS[i]);
                return false;
            }
        }
        return true;
    }

    /**
     * Helper method extracting Derby production JARs from a directory.
     *
     * @param libDir directory
     * @return A list of JARs (possibly empty).
     */
    private static File[] getProductionJars(File libDir) {
        File[] pJars = libDir.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.toUpperCase().endsWith(".JAR") &&
                        !isTestingJar(name);
            }
        });
        if (pJars == null) {
            return EMPTY_FILE_ARRAY;
        } else {
            return pJars;
        }
    }

    /**
     * Helper method extracting Derby testing JARs from a directory.
     *
     * @param libDir directory
     * @return A list of JARs (possibly empty).
     */
    private static File[] getTestingJars(File libDir) {
        File[] tJars = libDir.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return isTestingJar(name);
            }
        });
        if (tJars == null) {
            return EMPTY_FILE_ARRAY;
        } else {
            return tJars;
        }
    }

    public static File[] getJars(File libDir) {
        File[] jars = libDir.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.toUpperCase().endsWith(".JAR");
            }
        });
        return jars;
    }

    /**
     * Tells if the given file is a Derby testing JAR.
     *
     * @param name name of the file
     * @return {@code true} if a testing JAR, {@code false} otherwise
     */
    private static boolean isTestingJar(String name) {
        return name.toUpperCase().endsWith(JAR_TESTING.toUpperCase());
    }

    /**
     * Merges a list of JAR files into a classpath string.
     *
     * @param jars JAR files to merge
     * @return A classpath string.
     */
    private static String constructJarClasspath(File[] jars) {
        StringBuffer sb = new StringBuffer(512);
        for (int i=0; i < jars.length; i++) {
            try {
                sb.append(jars[i].getCanonicalPath());
            } catch (IOException ioe) {
                // Do the next best thing; use absolute path.
                String absPath = jars[i].getAbsolutePath();
                sb.append(absPath);
                BaseTestCase.println("obtaining canonical path for " +
                        absPath + " failed: " + ioe.getMessage());
            }
            sb.append(File.pathSeparatorChar);
        }
        if (jars.length > 0) {
            sb.deleteCharAt(sb.length() -1);
        }
        return sb.toString();
    }

    /**
     * <p>
     * Returns a distribution with the specified version, based on the given
     * library directory.
     * </p>
     *
     * <p>
     * It is the responsibility of the caller to ensure that the specified
     * version matches the JARs in the given directory.
     * </p>
     *
     * @param version the version of the distribution
     * @param baseDir the base dir for the distribution, holding the Derby JARs
     * @return A representation of the distribution, or {@code null} if
     *      the specified directory doesn't contain a valid distribution.
     * @throws IllegalArgumentException if {@code version} is {@code null}
     */
    public static DerbyDistribution newInstance(DerbyVersion version,
                                                File baseDir) {
        return newInstance(version, baseDir, baseDir);
    }

    /**
     * <p>
     * Returns a distribution with the specified version, based on the given
     * library and testing directories.
     * </p>
     *
     * <p>
     * It is the responsibility of the caller to ensure that the specified
     * version matches the JARs in the given directories.
     * </p>
     *
     * @param version the version of the distribution
     * @param baseDir the directory holding the production JARs
     * @param testDir the directory holding the testing JAR
     * @return A representation of the distribution, or {@code null} if
     *      the specified directories don't make up a valid distribution.
     * @throws IllegalArgumentException if {@code version} is {@code null}
     */
    public static DerbyDistribution newInstance(DerbyVersion version,
                                                File baseDir, File testDir) {
        File[] productionJars = getProductionJars(baseDir);
        File[] testingJars = getTestingJars(testDir);
        List<File> tmpJars = new ArrayList<File>();
        tmpJars.addAll(Arrays.asList(productionJars));
        tmpJars.addAll(Arrays.asList(testingJars));
        if (hasRequiredJars(tmpJars)) {
            return new DerbyDistribution(version, productionJars, testingJars);
        }
        // Invalid distribution, ignore it.
        BaseTestCase.println("Distribution deemed invalid (note that running " +
                "off classes isn't supported): " + baseDir.getAbsolutePath() +
                (baseDir.equals(testDir) ? ""
                                         : ", " + testDir.getAbsolutePath()));
        return null;
    }
}