Project

General

Profile

« Previous | Next » 

Revision 2440

Added by sgarg over 19 years ago

Adding test for checking Metacat behaviour when characters outside A-Z,a-z,0-9 are entered.

Related bugs: 1711, 1538 (1711 has been fixed as tested by this file. Have to add code to test 1711)

Currently checking for
> when < > & is entered - test fails as expected as document is invalid
> when > < & is entered - test expected to succeeds but failing (when the document is read again it comes back as &gt; &amp; &lt;)
> when µ is entered for testing micro sign - test succeeds
> when ', " and ` are entered - succeeds in case of ' and `. fails in case of " as metacat returns &quot;

So some of the tests are failing. (Probably because normalization is done while reading of the document from database)

View differences:

test/edu/ucsb/nceas/metacattest/NonAsciiCharacterTest.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author$'
8
 *     '$Date$'
9
 * '$Revision$'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

  
26
package edu.ucsb.nceas.metacattest;
27

  
28
import java.io.Reader;
29
import java.io.StringReader;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.GregorianCalendar;
33
import java.util.SimpleTimeZone;
34
import java.util.TimeZone;
35

  
36
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
37
import edu.ucsb.nceas.metacat.client.Metacat;
38
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
39
import edu.ucsb.nceas.metacat.client.MetacatException;
40
import edu.ucsb.nceas.metacat.client.MetacatFactory;
41
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
42
import edu.ucsb.nceas.utilities.IOUtil;
43
import junit.framework.Test;
44
import junit.framework.TestCase;
45
import junit.framework.TestSuite;
46
import java.io.File;
47

  
48
/**
49
 * A JUnit test for testing Metacat when Non Ascii Characters are inserted
50
 */
51
public class NonAsciiCharacterTest
52
    extends TestCase {
53

  
54
    private String metacatUrl = "@systemidserver@@servlet-path@";
55
    private String username = "@mcuser@";
56
    private String password = "@mcpassword@";
57
    private String anotheruser = "@mcanotheruser@";
58
    private String anotherpassword = "@mcanotherpassword@";
59
    private String prefix = "test";
60
    private String newdocid = null;
61
    private String testdocument = "";
62

  
63
    private Metacat m;
64

  
65
    private boolean SUCCESS = true;
66
    private boolean FAILURE = false;
67

  
68
    /**
69
     * These variables are for eml-2.0.1 only. For other eml versions,
70
     * this function might have to modified
71
     */
72

  
73
    private String testEml_Header =
74
        "<?xml version=\"1.0\"?><eml:eml" +
75
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
76
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
77
        " packageId=\"eml.1.1\" system=\"knb\"" +
78
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
79
        " scope=\"system\">";
80

  
81
    private String testEmlCreatorBlock =
82
        "<creator scope=\"document\">                                       " +
83
        " <individualName>                                                  " +
84
        "    <surName>Smith</surName>                                       " +
85
        " </individualName>                                                 " +
86
        "</creator>                                                         ";
87

  
88
    private String testEmlContactBlock =
89
        "<contact scope=\"document\">                                       " +
90
        " <individualName>                                                  " +
91
        "    <surName>Jackson</surName>                                     " +
92
        " </individualName>                                                 " +
93
        "</contact>                                                         ";
94

  
95
    /**
96
     * This function returns an access block based on the params passed
97
     */
98
    private String getAccessBlock(String principal, boolean grantAccess,
99
                                  boolean read, boolean write,
100
                                  boolean changePermission, boolean all) {
101
        String accessBlock = "<access " +
102
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
103
            " order=\"allowFirst\" scope=\"document\">";
104

  
105
        if (grantAccess) {
106
            accessBlock += "<allow>";
107
        }
108
        else {
109
            accessBlock += "<deny>";
110
        }
111

  
112
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
113

  
114
        if (all) {
115
            accessBlock += "<permission>all</permission>";
116
        }
117
        else {
118
            if (read) {
119
                accessBlock += "<permission>read</permission>";
120
            }
121
            if (write) {
122
                accessBlock += "<permission>write</permission>";
123
            }
124
            if (changePermission) {
125
                accessBlock += "<permission>changePermission</permission>";
126
            }
127
        }
128

  
129
        if (grantAccess) {
130
            accessBlock += "</allow>";
131
        }
132
        else {
133
            accessBlock += "</deny>";
134
        }
135
        accessBlock += "</access>";
136

  
137
        return accessBlock;
138

  
139
    }
140

  
141
    /**
142
     * This function returns a valid eml document with no access rules
143
     * This function is for eml-2.0.1 only. For other eml versions,
144
     * this function might have to modified
145
     */
146
    private String getTestEmlDoc(String title) {
147

  
148
        String testDocument = "";
149
        testDocument = testDocument + testEml_Header +
150
            "<dataset scope=\"document\"><title>" + title + "</title>" +
151
            testEmlCreatorBlock;
152

  
153
        testDocument += testEmlContactBlock;
154
        testDocument += getAccessBlock("public", true, true, false, false, false);
155
        testDocument += "</dataset>";
156
        testDocument += "</eml:eml>";
157

  
158
        return testDocument;
159
    }
160

  
161
    /**
162
     * Constructor to build the test
163
     *
164
     * @param name the name of the test method
165
     */
166
    public NonAsciiCharacterTest(String name) {
167
        super(name);
168
        newdocid = generateDocid();
169
    }
170

  
171
    /**
172
     * Establish a testing framework by initializing appropriate objects
173
     */
174
    public void setUp() {
175
        try {
176
            System.err.println("Test Metacat: " + metacatUrl);
177
            m = MetacatFactory.createMetacatConnection(metacatUrl);
178
        }
179
        catch (MetacatInaccessibleException mie) {
180
            System.err.println("Metacat is: " + metacatUrl);
181
            fail("Metacat connection failed." + mie.getMessage());
182
        }
183
    }
184

  
185
    /**
186
     * Release any objects after tests are complete
187
     */
188
    public void tearDown() {
189
    }
190

  
191
    /**
192
     * Create a suite of tests to be run together
193
     */
194
    public static Test suite() {
195
        TestSuite suite = new TestSuite();
196
        suite.addTest(new NonAsciiCharacterTest("initialize"));
197
        // Test basic functions
198
        suite.addTest(new NonAsciiCharacterTest("nonAsciiTest_1"));
199
        suite.addTest(new NonAsciiCharacterTest("nonAsciiTest_2"));
200
        suite.addTest(new NonAsciiCharacterTest("nonAsciiTest_3"));
201
        suite.addTest(new NonAsciiCharacterTest("nonAsciiTest_4"));
202

  
203
        return suite;
204
    }
205

  
206
    /**
207
     * Run an initial test that always passes to check that the test
208
     * harness is working.
209
     */
210
    public void initialize() {
211
        assertTrue(1 == 1);
212
    }
213

  
214

  
215
    /** *********
216
     * Test inserting document with > & <
217
     * should fail because this means an invalid xml document is being inserted
218
     */
219
    public void nonAsciiTest_1() {
220
        try {
221
            newdocid = generateDocid();
222
            m.login(username, password);
223
            testdocument = getTestEmlDoc("Checking > & <");
224
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
225
            m.logout();
226
        }
227
        catch (MetacatAuthException mae) {
228
            fail("Authorization failed:\n" + mae.getMessage());
229
        }
230
        catch (MetacatInaccessibleException mie) {
231
            fail("Metacat Inaccessible:\n" + mie.getMessage());
232
        }
233
        catch (Exception e) {
234
            fail("General exception:\n" + e.getMessage());
235
        }
236
    }
237

  
238

  
239
    /** *********
240
     * Test inserting document with &gt; $amp; &lt;
241
     * should fail because this means an invalid xml document is being inserted
242
     */
243
    public void nonAsciiTest_2() {
244
        try {
245
            newdocid = generateDocid();
246
            m.login(username, password);
247
            testdocument = getTestEmlDoc("Checking &gt; $amp; &lt;");
248
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
249
            m.logout();
250
        }
251
        catch (MetacatAuthException mae) {
252
            fail("Authorization failed:\n" + mae.getMessage());
253
        }
254
        catch (MetacatInaccessibleException mie) {
255
            fail("Metacat Inaccessible:\n" + mie.getMessage());
256
        }
257
        catch (Exception e) {
258
            fail("General exception:\n" + e.getMessage());
259
        }
260
    }
261

  
262

  
263
    /** *********
264
     * Test inserting document with single quote and double quote
265
     * should fail because this means an invalid xml document is being inserted
266
     */
267
    public void nonAsciiTest_3() {
268
        try {
269
            newdocid = generateDocid();
270
            m.login(username, password);
271
            testdocument = getTestEmlDoc("Checking ' ` \"");
272
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
273
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
274
            m.logout();
275
        }
276
        catch (MetacatAuthException mae) {
277
            fail("Authorization failed:\n" + mae.getMessage());
278
        }
279
        catch (MetacatInaccessibleException mie) {
280
            fail("Metacat Inaccessible:\n" + mie.getMessage());
281
        }
282
        catch (Exception e) {
283
            fail("General exception:\n" + e.getMessage());
284
        }
285
    }
286

  
287

  
288

  
289
    /** *********
290
     * Test inserting document with micro sign
291
     * should fail because this means an invalid xml document is being inserted
292
     */
293
    public void nonAsciiTest_4() {
294
        try {
295
            newdocid = generateDocid();
296
            m.login(username, password);
297
            testdocument = getTestEmlDoc("Checking &#181;");
298
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
299
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
300
            m.logout();
301
        }
302
        catch (MetacatAuthException mae) {
303
            fail("Authorization failed:\n" + mae.getMessage());
304
        }
305
        catch (MetacatInaccessibleException mie) {
306
            fail("Metacat Inaccessible:\n" + mie.getMessage());
307
        }
308
        catch (Exception e) {
309
            fail("General exception:\n" + e.getMessage());
310
        }
311
    }
312

  
313
    /**
314
     * Insert a document into metacat. The expected result is passed as result
315
     */
316

  
317
    private String insertDocid(String docid, String docText, boolean result,
318
                               boolean expectMetacatException) {
319
        String response = null;
320
        try {
321
            response = m.insert(docid,
322
                                new StringReader(docText), null);
323
            System.err.println(response);
324
            if (result) {
325
                assertTrue( (response.indexOf("<success>") != -1));
326
                assertTrue(response.indexOf(docid) != -1);
327
            }
328
            else {
329
                assertTrue( (response.indexOf("<success>") == -1));
330
            }
331
        }
332
        catch (MetacatInaccessibleException mie) {
333
            fail("Metacat Inaccessible:\n" + mie.getMessage());
334
        }
335
        catch (InsufficientKarmaException ike) {
336
                fail("Insufficient karma:\n" + ike.getMessage());
337
        }
338
        catch (MetacatException me) {
339
            if (!expectMetacatException) {
340
                fail("Metacat Error:\n" + me.getMessage());
341
            }
342
        }
343
        catch (Exception e) {
344
            fail("General exception:\n" + e.getMessage());
345
        }
346
        return response;
347
    }
348

  
349
    /**
350
     * Insert a document into metacat. The expected result is passed as result
351
     */
352

  
353
    private String uploadDocid(String docid, String filePath, boolean result,
354
                               boolean expectedKarmaException) {
355
        String response = null;
356
        try {
357
            response = m.upload(docid, new File(filePath));
358
            if (result) {
359
                assertTrue( (response.indexOf("<success>") != -1));
360
                assertTrue(response.indexOf(docid) != -1);
361
            }
362
            else {
363
                assertTrue( (response.indexOf("<success>") == -1));
364
            }
365
            System.err.println("respose from metacat: " + response);
366
        }
367
        catch (MetacatInaccessibleException mie) {
368
            fail("Metacat Inaccessible:\n" + mie.getMessage());
369
        }
370
        catch (InsufficientKarmaException ike) {
371
            if (!expectedKarmaException) {
372
                fail("Insufficient karma:\n" + ike.getMessage());
373
            }
374
        }
375
        catch (MetacatException me) {
376
            if (result) {
377
                fail("Metacat Error:\n" + me.getMessage());
378
            } else {
379
                System.err.println("Metacat Error:\n" + me.getMessage());
380
            }
381
        }
382
        catch (Exception e) {
383
            fail("General exception:\n" + e.getMessage());
384
        }
385
        return response;
386
    }
387

  
388
    /**
389
     * Update a document in metacat. The expected result is passed as result
390
     */
391
    private String updateDocid(String docid, String docText, boolean result,
392
                               boolean expectedKarmaFailure) {
393
        String response = null;
394
        try {
395
            response = m.update(docid,
396
                                new StringReader(testdocument), null);
397

  
398
            if (result) {
399
                assertTrue( (response.indexOf("<success>") != -1));
400
                assertTrue(response.indexOf(docid) != -1);
401
            }
402
            else {
403
                assertTrue( (response.indexOf("<success>") == -1));
404
            }
405
            System.err.println(response);
406
        }
407
        catch (MetacatInaccessibleException mie) {
408
            fail("Metacat Inaccessible:\n" + mie.getMessage());
409
        }
410
        catch (InsufficientKarmaException ike) {
411
            if (!expectedKarmaFailure) {
412
                fail("Insufficient karma:\n" + ike.getMessage());
413
            }
414
        }
415
        catch (MetacatException me) {
416
            if (result) {
417
                fail("Metacat Error:\n" + me.getMessage());
418
            } else {
419
                System.err.println("Metacat Error:\n" + me.getMessage());
420
            }
421
        }
422
        catch (Exception e) {
423
            fail("General exception:\n" + e.getMessage());
424
        }
425

  
426
        return response;
427
    }
428

  
429
    /**
430
     * Delete a document into metacat. The expected result is passed as result
431
     */
432
    private void deleteDocid(String docid, boolean result,
433
                             boolean expextedKarmaFailure) {
434
        try {
435
            String response = m.delete(docid);
436
            if (result) {
437
                assertTrue(response.indexOf("<success>") != -1);
438
            }
439
            else {
440
                assertTrue(response.indexOf("<success>") == -1);
441
            }
442
            System.err.println(response);
443
        }
444
        catch (MetacatInaccessibleException mie) {
445
            fail("Metacat Inaccessible:\n" + mie.getMessage());
446
        }
447
        catch (InsufficientKarmaException ike) {
448
            if(!expextedKarmaFailure){
449
                fail("Insufficient karma:\n" + ike.getMessage());
450
            }
451
        }
452
        catch (MetacatException me) {
453
            if (result) {
454
                fail("Metacat Error:\n" + me.getMessage());
455
            } else {
456
                System.err.println("Metacat Error:\n" + me.getMessage());
457
            }
458
        }
459
        catch (Exception e) {
460
            fail("General exception:\n" + e.getMessage());
461
        }
462
    }
463

  
464
    /**
465
     * Read a document from metacat. The expected result is passed as result
466
     */
467
    private void readDocid(String docid, boolean result,
468
                           boolean expextedKarmaFailure) {
469
        try {
470
            Reader r = m.read(docid);
471
            String response = IOUtil.getAsString(r, true);
472

  
473
            if (!result) {
474
                assertTrue(response.indexOf("<success>") == -1);
475
            }
476
            // System.err.println(response);
477
        }
478
        catch (MetacatInaccessibleException mie) {
479
            fail("Metacat Inaccessible:\n" + mie.getMessage());
480
        }
481
        catch (InsufficientKarmaException ike) {
482
            if (!expextedKarmaFailure) {
483
                fail("Insufficient karma:\n" + ike.getMessage());
484
            }
485
        }
486
        catch (MetacatException me) {
487
            fail("Metacat Error:\n" + me.getMessage());
488
        }
489
        catch (Exception e) {
490
            fail("General exception:\n" + e.getMessage());
491
        }
492
    }
493

  
494
    /**
495
     * Read a document from metacat and check if it is equal to a given string.
496
     * The expected result is passed as result
497
     */
498

  
499
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
500
                                         boolean result,
501
                                         boolean expextedKarmaFailure) {
502
        try {
503
            Reader r = m.read(docid);
504
            String doc = IOUtil.getAsString(r, true);
505
            if (result) {
506

  
507
                if (!testDoc.equals(doc)) {
508
                    System.out.println("doc    :" + doc);
509
                    System.out.println("testDoc:" + testDoc);
510
                }
511

  
512
                assertTrue(testDoc.equals(doc));
513
            }
514
            else {
515
                assertTrue(doc.indexOf("<error>") != -1);
516
            }
517
        }
518
        catch (MetacatInaccessibleException mie) {
519
            fail("Metacat Inaccessible:\n" + mie.getMessage());
520
        }
521
        catch (InsufficientKarmaException ike) {
522
            if (!expextedKarmaFailure) {
523
                fail("Insufficient karma:\n" + ike.getMessage());
524
            }
525
        }
526
        catch (MetacatException me) {
527
            fail("Metacat Error:\n" + me.getMessage());
528
        }
529
        catch (Exception e) {
530
            fail("General exception:\n" + e.getMessage());
531
        }
532

  
533
    }
534

  
535
    /**
536
     * Create a hopefully unique docid for testing insert and update. Does
537
     * not include the 'revision' part of the id.
538
     *
539
     * @return a String docid based on the current date and time
540
     */
541
    private String generateDocid() {
542
        StringBuffer docid = new StringBuffer(prefix);
543
        docid.append(".");
544

  
545
        // Create a calendar to get the date formatted properly
546
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
547
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
548
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
549
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
550
                       2 * 60 * 60 * 1000);
551
        Calendar calendar = new GregorianCalendar(pdt);
552
        Date trialTime = new Date();
553
        calendar.setTime(trialTime);
554
        docid.append(calendar.get(Calendar.YEAR));
555
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
556
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
557
        docid.append(calendar.get(Calendar.MINUTE));
558
        docid.append(calendar.get(Calendar.SECOND));
559

  
560
        return docid.toString();
561
    }
562
}
0 563

  

Also available in: Unified diff