summaryrefslogtreecommitdiffstats
path: root/petascope/src/petascope/wcps/server/test/GrammarTestOnline.java
blob: 472488f39245754286ce5a35aaf5ee62628410c4 (plain)
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
/*
 * This file is part of rasdaman community.
 *
 * Rasdaman community is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rasdaman community is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
 rasdaman GmbH.
 *
 * For more information please see <http://www.rasdaman.org>
 * or contact Peter Baumann via <baumann@rasdaman.com>.
 */
package petascope.wcps.server.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import org.apache.commons.io.FileUtils;

/** Runs the available Grammar tests against a deployed version of Petascope,
 * available at some URL.
 *
 * Assumes that a tests succeeds if the server does not throw or display
 * an error.
 *
 * @author Andrei Aiordachioaie
 */
public class GrammarTestOnline {
    // Put new test cases in this folder
//    public final String PetascopeURL = "http://localhost:8080/PetaScope/WCPService";
//    public final String PetascopeURL = "http://localhost:8080/petascope/wcps/";

    public final String PetascopeURL = "http://kahlua.eecs.jacobs-university.de:8080/petascope-new/wcps/";
    String folder = "test/testcases-wcps/";
    // How many tests we have to run
    int numTests = 0;
    // tests
    File[] tests;
    // queries
    String[] queries;
    // success code
    boolean[] ok;
    // message for tests
    String[] errors;
    // How many successes?
    int passCount = 0;

    public void printResults() {
        System.out.println("\n \nRESULTS \n");

        for (int i = 0; i < numTests; i++) {
            String tname = tests[i].getName();
            tname = tname.substring(0, tname.length() - 5);
            if (ok[i] == true) {
                System.out.println("*** Test '" + tname + "' ok");
//                System.out.println("\t" + queries[i]);
            } else {
                System.out.println("*** Test '" + tname + "' FAILED");
                System.out.println("\t" + queries[i]);
                System.out.println("\t ERROR: " + errors[i]);
            }
        }
        System.out.println("\n\nRESULTS\n");
        System.out.println("Tested PetaScope implementation from: " + PetascopeURL);
        System.out.println("Tests succeeded: " + String.valueOf(passCount));
        System.out.println("Tests failed: " + String.valueOf(numTests - passCount));
    }

    public GrammarTestOnline() {
        // Find out how many tests we have to run
        File dir = new File(folder);
        System.out.println("Looking for tests in " + dir.getAbsolutePath() + "\n");
        TestFileFilter filter = new TestFileFilter();
        tests = dir.listFiles(filter);
        numTests = tests.length;
//        numTests = 3;
        ok = new boolean[numTests];
        errors = new String[numTests];
        queries = new String[numTests];
    }

    /* Accept all files with extension TEST. */
    private class TestFileFilter implements FilenameFilter {

        @Override
        public boolean accept(File dir, String name) {
            if (name.endsWith("test")) {
                return true;
            }
            return false;
        }
    }

    public void runAllTests() {
        String query = "";
        String tname = "";
        for (int i = 0; i < numTests; i++) {
            ok[i] = false;
            tname = tests[i].getName();
            tname = tname.substring(0, tname.length() - 5);
            System.out.println("Running test '" + tname + "'...");
            try {
                query = FileUtils.readFileToString(tests[i]);
                queries[i] = query;
            } catch (IOException e) {
                errors[i] = "Could not read file " + tests[i].getName();
                continue;
            }
            try {
                String err = runOneTest("query", query);
                if (err == null) {
                    ok[i] = true;
                } else {
                    errors[i] = err;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (ok[i] == true) {
                passCount++;
            }
        }
    }

    /** Send an XML request to the WCPS server. Hopefully it will succeed.
     * Returns a message on error or null otherwise.
     **/
    public String runOneTest(String param, String query)
            throws MalformedURLException, IOException {

//        System.out.println("--------------------");
//        System.out.println(query);
//        System.out.println("\t--------------------");

        // connect to the servlet
        URL servlet = new URL(PetascopeURL);
        HttpURLConnection conn = (HttpURLConnection) servlet.openConnection();

        // inform the connection that we will send output and accept input
        conn.setDoInput(true);
        conn.setDoOutput(true);

        // Don't use a cached version of URL connection.
        conn.setUseCaches(false);
        conn.setDefaultUseCaches(false);

        // Send POST request
        conn.setRequestMethod("POST");

        // Specify the content type that we will send binary data
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        String data = param + "=" + URLEncoder.encode(query);
        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        out.writeBytes(data);
        out.flush();
        out.close();

        BufferedReader cgiOutput = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
        String line1 = cgiOutput.readLine();
        String line2 = cgiOutput.readLine();
        String line3 = cgiOutput.readLine();
        System.out.println("\t" + line1);
        System.out.println("\t" + line2);
        System.out.println("\t" + line3);

        if (line1 != null && line2 != null && line3 != null
                && line2.equals("<h1>An error has occured</h1>")) {
            while (cgiOutput.ready()) {
                System.out.println("\t" + cgiOutput.readLine());
            }
//            System.out.println("Error executing query: ");
            String error = line3.substring(10, line3.length() - 4);
//            System.out.println("\t" + error);
            return error;
        } else {
            return null;
        }

    }

    public static void main(String args[]) {
        GrammarTestOnline tester = new GrammarTestOnline();
        tester.runAllTests();
        tester.printResults();
    }
}