summaryrefslogtreecommitdiffstats
path: root/java/rasj/test/TestException.java
blob: 9cfe894cef3eeb37c15b368d729bf1547bc8aeeb (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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* 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>.
*/
/** ***********************************************************
 * <pre>
 *
 * PURPOSE:
 * exception testing (was: Systemtest_rasj).
 *
 *
 * COMMENTS:
 *
 * </pre>
 *********************************************************** */

import java.io.*;

import rasj.*;
import rasj.global.*;
import org.odmg.*;


// auxiliary classes for exception testing
class TestRasResultIsNoCellException extends RasResultIsNoCellException
  {
    TestRasResultIsNoCellException()
      {
	super();
      }
  }

class TestRasResultIsNoIntervalException extends RasResultIsNoIntervalException
  {
    TestRasResultIsNoIntervalException()
      {
	super();
      }
  }

class TestRasClientInternalException extends RasClientInternalException
  {
    TestRasClientInternalException(String msg)
      {
	super("TestException","TestExceptions",msg);
      }
  }

/**
 * class for testing rasj interface
 * @version $$
 */
public class TestException
  {
    /**
    * prefixes for test output
    **/
    static final String PREFIX_PROGRAM  = "+++ +++ +++ ";
    static final String PREFIX_TESTSET  = "+++ +++ ";
    static final String PREFIX_TESTCASE = "+++ ";

    /**
    * std error tag printed if a test fails
    **/
    static final String ERROR_TAG = "ERROR: ";

    /**
    * default values (override with cmd line option)
    **/
    static final String DEFAULT_HOST   = "localhost";
    static final int    DEFAULT_PORT   = 7001;
    static final String DEFAULT_BASE   = "RASBASE";
    static final String DEFAULT_USER   = "rasguest";
    static final String DEFAULT_PASSWD = "rasguest";

    // main program
    public static void main(String[] args) throws Exception
      {
	String server = DEFAULT_HOST;
	int port      = DEFAULT_PORT;
        String base   = DEFAULT_BASE;
        String user   = DEFAULT_USER;
        String passwd = DEFAULT_PASSWD;
	boolean wrongUsage = false;

	// get args
	if(args.length == 0)
	    wrongUsage = true;
	for(int i = 0; i < args.length; i+=2)
          {
            if (args[i].equals("--server"))
                server = args[i+1];
            else if (args[i].equals("--port"))
              {
                try
                  {
                    port = Integer.parseInt(args[i+1]);
                  }
                catch(Exception e)
                  {
                    wrongUsage = true;
                  }
              }
            else if (args[i].equals("--database"))
                base = args[i+1];
            else if (args[i].equals("--user"))
                user = args[i+1];
            else if (args[i].equals("--passwd"))
                passwd = args[i+1];
          }

	if(wrongUsage)
	  {
            System.err.println("Usage: TestException [--server s] [--port p] [--database d]");
            System.out.println( "defaults: server=" + DEFAULT_HOST + ", port=" + DEFAULT_PORT + ", database=" + DEFAULT_BASE
+ ", user=" + DEFAULT_USER + ", passwd=" + DEFAULT_PASSWD );
            System.exit(-1);
	  }



        System.out.println( "rasdaman system test v5.1revC: testing exceptions." );
        System.out.println( PREFIX_PROGRAM + "system test started, using server " + server + ", port " + port + ", database " + base + ", user=" + user + ", passwd=" + passwd );

	// Implementation myApp = new RasImplementation("http://" + server + ":" + port);
        RasImplementation myApp = new RasImplementation( "http://" + server + ":" + port );
        myApp.setUserIdentification(user, passwd);

        // -- START test cases -------------------------------------------------
	testExceptions( myApp, base );
        // -- END test cases ---------------------------------------------------

        System.out.println( PREFIX_PROGRAM + "system test done." );
        return;
      } // main()

    /**
    * test exceptions
    * any eventual exception that is not caught here is an error, and will cause an abort
    **/
    static void testExceptions( RasImplementation imp, String database ) throws Exception
      {
        boolean resultIsOk = false;
        Database myDb = null;
        Transaction myTa = null;
        OQLQuery myQu = null;

        System.out.println( PREFIX_TESTSET + "testing exceptions started." );

        System.out.print( PREFIX_TESTCASE + "provoking RasConnectionFailedException..." );
        RasImplementation errorApp;
        String logMsg;
        try
          {
            errorApp = new RasImplementation("wrongserver:8080");
            myDb = errorApp.newDatabase();
            myDb.open( database, Database.OPEN_READ_WRITE );
            System.out.println( ERROR_TAG + "does not catch this." );
            myDb.close();
          }
        catch(RasConnectionFailedException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

        System.out.print( PREFIX_TESTCASE + "provoking RasDimensionMismatchException..." );
        try
          {
            RasMInterval test1;
            RasMInterval test2 = new RasMInterval("[1:20,1:20]");
            RasMInterval test3 = new RasMInterval("[1:4,1:9,1:6]");
            test1 = test2.intersectionWith(test3);
            System.out.println( ERROR_TAG + "does not catch this." );
          }
        catch(RasDimensionMismatchException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

        System.out.print( PREFIX_TESTCASE + "provoking RasIndexOutOfBoundsException..." );
        try
          {
            RasGMArray myMDD = new RasGMArray(new RasMInterval("[1:5,1:5]"),1);
            byte[] retVal = myMDD.getCell(new RasPoint(4,7));
            System.out.println( ERROR_TAG + "does not catch this." );
          }
        catch(RasIndexOutOfBoundsException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

        System.out.print( PREFIX_TESTCASE + "provoking RasClientInternalException (via subclassed e.)..." );
        try
          {
            throw new TestRasClientInternalException("This is a test error");
          }
        catch(RasClientInternalException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

        System.out.print( PREFIX_TESTCASE + "provoking RasResultIsNoCellException (via subclassed e.)..." );
        try
          {
            throw new TestRasResultIsNoCellException();
          }
        catch(RasResultIsNoCellException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

        System.out.print( PREFIX_TESTCASE + "provoking RasResultIsNoIntervalException (via subclassed e.)..." );
        try
          {
            throw new TestRasResultIsNoIntervalException();
          }
        catch(RasResultIsNoIntervalException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

        System.out.print( PREFIX_TESTCASE + "provoking RasStreamInputOverflowException..." );
        try
          {
            RasMInterval int1 = new RasMInterval(2);
            int1.stream(new RasSInterval("1:10"));
            int1.stream(new RasSInterval("1:5"));
            int1.stream(new RasSInterval("1:20"));
            System.out.println( ERROR_TAG + "does not catch this." );
          }
        catch(RasStreamInputOverflowException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

        System.out.print( PREFIX_TESTCASE + "provoking RasTypeInvalidException..." );
        try
          {
            RasPrimitiveType myType = new RasPrimitiveType("myPrivateType",RasGlobalDefs.RAS_BOOLEAN);
            myType.getFloat(new Object());
            System.out.println( ERROR_TAG + "does not catch this." );
          }
        catch(RasTypeInvalidException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

        System.out.print( PREFIX_TESTCASE + "provoking RasTypeNotSupportedException..." );
        try
          {
            throw new RasTypeNotSupportedException("test");
          }
        catch(RasTypeNotSupportedException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

        System.out.print( PREFIX_TESTCASE + "provoking RasIllegalULongValueException (faked)..." );
        try
          {
            throw new RasIllegalULongValueException( 42 );
          }
        catch(RasIllegalULongValueException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

        System.out.print( PREFIX_TESTCASE + "provoking RasIllegalUShortValueException (faked)..." );
        try
          {
            throw new RasIllegalUShortValueException( 42 );
          }
        catch(RasIllegalUShortValueException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

        System.out.print( PREFIX_TESTCASE + "provoking RasInvalidNameException (illegal chars in obj type name)..." );
        try
          {
            RasGMArray myMDD = new RasGMArray(new RasMInterval("[1:400,1:400]"),1);
            myMDD.setObjectTypeName("_!_$_%_&_ _");
            System.out.println( ERROR_TAG + "does not catch this." );
          }
        catch(RasInvalidNameException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

        // FIXME: PB9
        System.out.print( PREFIX_TESTCASE + "provoking RasInvalidNameException (name too long)..." );
        try
          {
            RasGMArray myMDD = new RasGMArray(new RasMInterval("[1:400,1:400]"),1);
            myMDD.setObjectTypeName("loooongname__1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_" );
            System.out.println( ERROR_TAG + "does not catch this." );
          }
        catch(RasInvalidNameException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

        System.out.print( PREFIX_TESTCASE + "provoking RasQueryExecutionFailedException..." );
        try
          {
            // Database myDb2 = imp.newDatabase();
            // myDb2.open( database, Database.OPEN_READ_ONLY );
            // Transaction myTa2 = imp.newTransaction();
            // myTa2.begin();

            // OQLQuery myQu2 = imp.newOQLQuery();
            // myQu2.create( "select r r from RAS_COLLECTIONNAMES as r" );
            // DBag result = (DBag) myQu2.execute();

            // myTa2.abort();
            // myDb2.close();
            System.out.print( "this is meant for a severe internal problem which I cannot provoke; will fake..." );
            throw new RasQueryExecutionFailedException( 1, 2, 3, "(fake)" );
          }
        catch(RasQueryExecutionFailedException e)
          {
            System.out.println( "OK, recognized: " + e.getMessage() );
          }

	// protected exception in rasj, not accessible
        // System.out.print( PREFIX_TESTCASE + "provoking RasTypeUnknownException..." );
        // try
          // {
            // // RasGMArray myMDD = new RasGMArray(new RasMInterval("[1:400,1:400]"),1);
            // // myMDD.setObjectTypeName( "charrrrr" );
            // System.out.print( "superseded by RasTypeNotSupportedException - cannot catch it; will fake..." );
            // throw new RasTypeUnknownException("test");
          // }
        // catch(RasTypeUnknownException e)
          // {
            // System.out.println( "OK, recognized: " + e.getMessage() );
          // }

        System.out.println( PREFIX_TESTSET + "testing exceptions done." );
        return;
      } // testExceptions()

  } // TestException