0001 /*
0002 * TestPersist.java
0003 *
0004 * Copyright (c) 1995-2010, The University of Sheffield. See the file
0005 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
0006 *
0007 * This file is part of GATE (see http://gate.ac.uk/), and is free
0008 * software, licenced under the GNU Library General Public License,
0009 * Version 2, June 1991 (in the distribution as file licence.html,
0010 * and also available at http://gate.ac.uk/gate/licence.html).
0011 *
0012 * Hamish Cunningham, 19/Jan/01
0013 *
0014 * $Id: TestPersist.java 12006 2009-12-01 17:24:28Z thomas_heitz $
0015 */
0016
0017 package gate.persist;
0018
0019 import java.io.File;
0020 import java.io.Serializable;
0021 import java.net.URL;
0022 import java.util.*;
0023
0024 import junit.framework.*;
0025
0026 import gate.*;
0027 import gate.annotation.AnnotationSetImpl;
0028 import gate.corpora.*;
0029 import gate.event.DatastoreListener;
0030 import gate.security.*;
0031 import gate.util.*;
0032
0033 /** Persistence test class
0034 */
0035 public class TestPersist extends TestCase
0036 {
0037 private static String JDBC_URL_1;
0038 private static String JDBC_URL_2;
0039 private static String JDBC_URL;
0040
0041 /** Debug flag */
0042 private static final boolean DEBUG = false;
0043 private static Long sampleDoc_lrID = null;
0044 private static Long sampleCorpus_lrID = null;
0045 private static Corpus sampleCorpus = null;
0046 private static int dbType;
0047
0048 /* cached properties of the original transient document that will be
0049 compared with the DB copies
0050 */
0051 private static AnnotationSet sample_defaultASet = null;
0052 private static String sample_name = null;
0053 private static FeatureMap sample_docFeatures = null;
0054 private static URL sample_sourceURL = null;
0055 private static Long sample_startOffset = null;
0056 private static Long sample_endOffset = null;
0057 private static Boolean sample_markupAware = null;
0058 private static DocumentContent sample_content = null;
0059 private static String sample_encoding = null;
0060 private static Map sample_namedASets = null;
0061
0062 // private static final String UNICODE_STRING = "\u65e5\u672c\u8a9e\u6587\u5b57\u5217";
0063 private static final String UNICODE_STRING = "\u0915\u0932\u094d\u0907\u0928\u0643\u0637\u0628\u041a\u0430\u043b\u0438\u043d\u0430 Kalina";
0064 private static final String ASCII_STRING = "Never mistake motion for action (Ernest Hemingway)";
0065
0066 private final String VERY_LONG_STRING =
0067 "The memory of Father came back to her. Ever since she had seen him retreat from those "+
0068 "twelve-year-old boys she often imagined him in this situation: he is on a sinking ship; "+
0069 "there are only a few lifeboats and there isn't enough room for everyone; there is a "+
0070 "furious stampede on the deck. At first Father rushes along with the others, but when he "+
0071 "sees how they push and shove, ready to trample each other under foot, and a wild-eyed "+
0072 "woman strikes him with her fist because he is in her way, he suddenly stops and steps "+
0073 "aside. And in the end he merely watches the overloaded lifeboats as they are slowly "+
0074 "lowered amid shouts and curses, towards the raging waves. "+
0075 "[p.111-113]";
0076
0077 /** Construction */
0078 public TestPersist(String name) throws GateException { super(name); }
0079
0080 /** Fixture set up */
0081 public void setUp() throws Exception {
0082 if (! DataStoreRegister.getConfigData().containsKey("url-test"))
0083 throw new GateRuntimeException("DB URL not configured in gate.xml");
0084 else
0085 JDBC_URL_1 =
0086 (String) DataStoreRegister.getConfigData().get("url-test");
0087 JDBC_URL_2 =
0088 (String) DataStoreRegister.getConfigData().get("url-test1");
0089 } // setUp
0090
0091 /** Put things back as they should be after running tests
0092 * (reinitialise the CREOLE register).
0093 */
0094 public void tearDown() throws Exception {
0095 } // tearDown
0096
0097 /** Test resource save and restore */
0098 public void testSaveRestore() throws Exception {
0099 File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
0100 storageDir.delete(); // get rid of the temp file
0101 storageDir.mkdir(); // create an empty dir of same name
0102
0103 SerialDataStore sds = new SerialDataStore(storageDir.toURI().toURL().toString());
0104 sds.create();
0105 sds.open();
0106
0107 // create a document
0108 String server = TestDocument.getTestServerName();
0109 assertNotNull(server);
0110 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
0111 assertNotNull(doc);
0112 doc.getFeatures().put("hi there", new Integer(23232));
0113 doc.getAnnotations().add(
0114 new Long(0), new Long(20), "thingymajig", Factory.newFeatureMap()
0115 );
0116
0117 // check that we can't save a resource without adopting it
0118 boolean cannotSync = false;
0119 try { sds.sync(doc); } catch(PersistenceException e) { cannotSync=true; }
0120 if(! cannotSync) assertTrue("doc synced ok before adoption", false);
0121
0122 // check that we can't adopt a resource that's stored somewhere else
0123 doc.setDataStore(new SerialDataStore(new File("z:\\").toURI().toURL().toString()));
0124 try { sds.adopt(doc,null); } catch(PersistenceException e) { cannotSync=true; }
0125 if(! cannotSync)
0126 assertTrue("doc adopted but in other datastore already", false);
0127 doc.setDataStore(null);
0128 doc.setName("Alicia Tonbridge, a Document");
0129
0130 // save the document
0131 Document persDoc = (Document) sds.adopt(doc,null);
0132 sds.sync(persDoc);
0133 Object lrPersistenceId = persDoc.getLRPersistenceId();
0134
0135 // test the getLrTypes method
0136 List lrTypes = sds.getLrTypes();
0137 assertTrue("wrong number of types in SDS", lrTypes.size() == 1);
0138 assertTrue(
0139 "wrong type LR in SDS",
0140 lrTypes.get(0).equals("gate.corpora.DocumentImpl")
0141 );
0142
0143 // test the getLrNames method
0144 Iterator iter = sds.getLrNames("gate.corpora.DocumentImpl").iterator();
0145 String name = (String) iter.next();
0146 assertEquals(name, "Alicia Tonbridge, a Document");
0147
0148 // read the document back
0149 FeatureMap features = Factory.newFeatureMap();
0150 features.put(DataStore.LR_ID_FEATURE_NAME, lrPersistenceId);
0151 features.put(DataStore.DATASTORE_FEATURE_NAME, sds);
0152 Document doc2 =
0153 (Document) Factory.createResource("gate.corpora.DocumentImpl", features);
0154 Document doc3 =
0155 (Document) sds.getLr("gate.corpora.DocumentImpl", lrPersistenceId);
0156
0157 try{
0158 boolean value = TestEqual.documentsEqual(doc3, doc2);
0159 assertTrue(TestEqual.message, value);
0160 value = TestEqual.documentsEqual(persDoc, doc2);
0161 assertTrue(TestEqual.message, value);
0162 }finally{
0163 // delete the datastore
0164 sds.delete();
0165 }
0166 } // testSaveRestore()
0167
0168 /** Simple test */
0169 public void testSimple() throws Exception {
0170 // create a temporary directory; because File.createTempFile actually
0171 // writes the bloody thing, we need to delete it from disk before calling
0172 // DataStore.create
0173 File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
0174 storageDir.delete();
0175
0176 // create and open a serial data store
0177 DataStore sds = Factory.createDataStore(
0178 "gate.persist.SerialDataStore", storageDir.toURI().toURL().toString()
0179 );
0180
0181 // check we can get empty lists from empty data stores
0182 List lrTypes = sds.getLrTypes();
0183
0184 // create a document with some annotations / features on it
0185 String server = TestDocument.getTestServerName();
0186 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
0187 doc.getFeatures().put("hi there", new Integer(23232));
0188 doc.getAnnotations().add(
0189 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap()
0190 );
0191
0192 // save the document
0193 Document persDoc = (Document) sds.adopt(doc,null);
0194 sds.sync(persDoc);
0195
0196 // remember the persistence ID for reading back
0197 // (in the normal case these ids are obtained by DataStore.getLrIds(type))
0198 Object lrPersistenceId = persDoc.getLRPersistenceId();
0199
0200 // read the document back
0201 FeatureMap features = Factory.newFeatureMap();
0202 features.put(DataStore.LR_ID_FEATURE_NAME, lrPersistenceId);
0203 features.put(DataStore.DATASTORE_FEATURE_NAME, sds);
0204 Document doc2 =
0205 (Document) Factory.createResource("gate.corpora.DocumentImpl", features);
0206
0207 //parameters should be different
0208 // check that the version we read back matches the original
0209 assertTrue(TestEqual.documentsEqual(persDoc, doc2));
0210
0211 // delete the datastore
0212 sds.delete();
0213 } // testSimple()
0214
0215 /** Test multiple LRs */
0216 public void testMultipleLrs() throws Exception {
0217 // create a temporary directory; because File.createTempFile actually
0218 // writes the bloody thing, we need to delete it from disk before calling
0219 // DataStore.create
0220 File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
0221 storageDir.delete();
0222
0223 // create and open a serial data store
0224 SerialDataStore sds = new SerialDataStore(storageDir.toURI().toURL().toString());
0225 sds.create();
0226 sds.open();
0227
0228 // create a document with some annotations / features on it
0229 String server = TestDocument.getTestServerName();
0230 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
0231 doc.getFeatures().put("hi there", new Integer(23232));
0232 doc.getAnnotations().add(
0233 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap()
0234 );
0235
0236 // create another document with some annotations / features on it
0237 Document doc2 =
0238 Factory.newDocument(new URL(server + "tests/html/test1.htm"));
0239 doc.getFeatures().put("hi there again", new Integer(23232));
0240 doc.getAnnotations().add(
0241 new Long(5), new Long(25), "dog poo irritates", Factory.newFeatureMap()
0242 );
0243
0244 // create a corpus with the documents
0245 Corpus corp = Factory.newCorpus("Hamish test corpus");
0246 corp.add(doc);
0247 corp.add(doc2);
0248 LanguageResource persCorpus = sds.adopt(corp,null);
0249 sds.sync(persCorpus);
0250
0251
0252 // read the documents back
0253 ArrayList lrsFromDisk = new ArrayList();
0254 List lrIds = sds.getLrIds("gate.corpora.SerialCorpusImpl");
0255
0256 Iterator idsIter = lrIds.iterator();
0257 while(idsIter.hasNext()) {
0258 String lrId = (String) idsIter.next();
0259 FeatureMap features = Factory.newFeatureMap();
0260 features.put(DataStore.DATASTORE_FEATURE_NAME, sds);
0261 features.put(DataStore.LR_ID_FEATURE_NAME, lrId);
0262 Resource lr = Factory.createResource( "gate.corpora.SerialCorpusImpl",
0263 features);
0264 lrsFromDisk.add(lr);
0265 } // for each LR ID
0266
0267 if (DEBUG) System.out.println("LRs on disk" + lrsFromDisk);
0268
0269 // check that the versions we read back match the originals
0270 Corpus diskCorp = (Corpus) lrsFromDisk.get(0);
0271
0272 Document diskDoc = (Document) diskCorp.get(0);
0273
0274 if (DEBUG) Out.prln("Documents in corpus: " + corp.getDocumentNames());
0275 assertTrue("corp name != mem name", corp.getName().equals(diskCorp.getName()));
0276 if (DEBUG) Out.prln("Memory features " + corp.getFeatures());
0277 if (DEBUG) Out.prln("Disk features " + diskCorp.getFeatures());
0278 assertTrue("corp feat != mem feat",
0279 corp.getFeatures().equals(diskCorp.getFeatures()));
0280 if (DEBUG)
0281 Out.prln("Annotations in doc: " + diskDoc.getAnnotations());
0282 assertTrue("doc annotations from disk not equal to memory version",
0283 TestEqual.annotationSetsEqual(doc.getAnnotations(),
0284 diskDoc.getAnnotations()));
0285
0286 assertTrue("doc from disk not equal to memory version",
0287 TestEqual.documentsEqual(doc, diskDoc));
0288
0289 Iterator corpusIter = diskCorp.iterator();
0290 while(corpusIter.hasNext()){
0291 if (DEBUG)
0292 Out.prln(((Document) corpusIter.next()).getName());
0293 else
0294 corpusIter.next();
0295 }
0296
0297
0298 // assertTrue("doc2 from disk not equal to memory version", doc2.equals(diskDoc2));
0299
0300 // delete the datastore
0301 sds.delete();
0302 } // testMultipleLrs()
0303
0304 /** Test LR deletion */
0305 public void testDelete() throws Exception {
0306 // create a temporary directory; because File.createTempFile actually
0307 // writes the bloody thing, we need to delete it from disk before calling
0308 // DataStore.create
0309 File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
0310 if (DEBUG) Out.prln("Corpus stored to: " + storageDir.getAbsolutePath());
0311 storageDir.delete();
0312
0313 // create and open a serial data store
0314 SerialDataStore sds = new SerialDataStore();
0315 sds.setStorageUrl(storageDir.toURI().toURL().toString());
0316 sds.create();
0317 sds.open();
0318
0319 // create a document with some annotations / features on it
0320 String server = TestDocument.getTestServerName();
0321 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
0322 doc.getFeatures().put("hi there", new Integer(23232));
0323 doc.getAnnotations().add(
0324 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap()
0325 );
0326
0327 // save the document
0328 Document persDoc = (Document) sds.adopt(doc,null);
0329 sds.sync(persDoc);
0330
0331 // remember the persistence ID for reading back
0332 // (in the normal case these ids are obtained by DataStore.getLrIds(type))
0333 Object lrPersistenceId = persDoc.getLRPersistenceId();
0334
0335 // delete document back
0336 sds.delete("gate.corpora.DocumentImpl", lrPersistenceId);
0337
0338 // check that there are no LRs left in the DS
0339 assertTrue(sds.getLrIds("gate.corpora.DocumentImpl").size() == 0);
0340
0341 // delete the datastore
0342 sds.delete();
0343 } // testDelete()
0344
0345
0346
0347
0348 /** Test the DS register. */
0349 public void testDSR() throws Exception {
0350 DataStoreRegister dsr = Gate.getDataStoreRegister();
0351 assertTrue("DSR has wrong number elements (not 0): " + dsr.size(),
0352 dsr.size() == 0);
0353
0354 // create a temporary directory; because File.createTempFile actually
0355 // writes the bloody thing, we need to delete it from disk before calling
0356 // DataStore.create
0357 File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
0358 storageDir.delete();
0359
0360 // create and open a serial data store
0361 DataStore sds = Factory.createDataStore(
0362 "gate.persist.SerialDataStore", storageDir.toURI().toURL().toString()
0363 );
0364
0365 // create a document with some annotations / features on it
0366 String server = TestDocument.getTestServerName();
0367 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
0368 doc.getFeatures().put("hi there", new Integer(23232));
0369 doc.getAnnotations().add(
0370 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap()
0371 );
0372
0373 // save the document
0374 Document persDoc = (Document) sds.adopt(doc,null);
0375 sds.sync(persDoc);
0376
0377 // DSR should have one member
0378 assertTrue("DSR has wrong number elements (expected 1): " + dsr.size(),
0379 dsr.size() == 1);
0380
0381 // create and open another serial data store
0382 storageDir = File.createTempFile("TestPersist__", "__StorageDir");
0383 storageDir.delete();
0384 DataStore sds2 = Factory.createDataStore(
0385 "gate.persist.SerialDataStore", storageDir.toURI().toURL().toString()
0386 );
0387
0388 // DSR should have two members
0389 assertTrue("DSR has wrong number elements: " + dsr.size(), dsr.size() == 2);
0390
0391 // peek at the DSR members
0392 Iterator dsrIter = dsr.iterator();
0393 while(dsrIter.hasNext()) {
0394 DataStore ds = (DataStore) dsrIter.next();
0395 assertNotNull("null ds in ds reg", ds);
0396 if(DEBUG)
0397 Out.prln(ds);
0398 }
0399
0400 // delete the datastores
0401 sds.close();
0402 assertTrue("DSR has wrong number elements (expected 1): " + dsr.size(),
0403 dsr.size() == 1);
0404 sds.delete();
0405 assertTrue("DSR has wrong number elements (expected 1): " + dsr.size(),
0406 dsr.size() == 1);
0407 sds2.delete();
0408 assertTrue("DSR has wrong number elements (expected 0): " + dsr.size(),
0409 dsr.size() == 0);
0410
0411 } // testDSR()
0412
0413
0414
0415 /** Test suite routine for the test runner */
0416 public static Test suite() {
0417 return new TestSuite(TestPersist.class);
0418 } // suite
0419
0420
0421 private Document createTestDocument()
0422 throws Exception {
0423
0424 String server = TestDocument.getTestServerName();
0425 assertNotNull(server);
0426 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
0427 assertNotNull(doc);
0428
0429 doc.getFeatures().put("hi there", new Integer(23232));
0430 doc.getFeatures().put("LONG STRING feature", this.VERY_LONG_STRING);
0431 doc.getFeatures().put("NULL feature",null);
0432 doc.getFeatures().put("BINARY feature",new Dummy(101,"101",true,101.101f));
0433 doc.getFeatures().put("LONG feature",new Long(101));
0434 // doc.getFeatures().put("FLOAT feature",new Double(101.102d));
0435 doc.getFeatures().put("ASCII feature",ASCII_STRING);
0436 doc.getFeatures().put("UNICODE feature",UNICODE_STRING);
0437
0438 //create a complex feature - array of strings
0439 Vector complexFeature = new Vector();
0440 complexFeature.add("string 1");
0441 complexFeature.add("string 2");
0442 complexFeature.add("string 3");
0443 complexFeature.add("string 4");
0444 complexFeature.add("string 5");
0445 doc.getFeatures().put("complex feature",complexFeature);
0446 FeatureMap fm = Factory.newFeatureMap();
0447 // fm.put("FLOAT feature ZZZ",new Double(101.102d));
0448 // fm.put("ASCII feature",ASCII_STRING);
0449 // fm.put("INT feature",new Integer(1212));
0450 // fm.put("UNICODE feature",UNICODE_STRING);
0451 doc.getAnnotations().add(
0452 new Long(0), new Long(20), "thingymajig", fm);
0453 doc.setName("DB test Document---");
0454
0455 return doc;
0456 }
0457
0458
0459 private Corpus createTestCorpus()
0460 throws Exception {
0461
0462 String server = TestDocument.getTestServerName();
0463 assertNotNull(server);
0464 Document doc1 = Factory.newDocument(new URL(server + "tests/doc0.html"));
0465 assertNotNull(doc1);
0466
0467 doc1.getFeatures().put("hi there", new Integer(23232));
0468 doc1.getAnnotations().add(
0469 new Long(0), new Long(20), "thingymajig", Factory.newFeatureMap()
0470 );
0471 doc1.setName("DB test Document1");
0472
0473 // create another document with some annotations / features on it
0474 Document doc2 =
0475 Factory.newDocument(new URL(server + "tests/html/test1.htm"));
0476 doc2.getFeatures().put("hi there again", new Integer(23232));
0477 doc2.getAnnotations().add(
0478 new Long(5), new Long(25), "dog poo irritates", Factory.newFeatureMap()
0479 );
0480 doc2.setName("DB test Document2");
0481
0482 //create corpus
0483 Corpus corp = Factory.newCorpus("My test corpus");
0484 //add docs
0485 corp.add(doc1);
0486 corp.add(doc2);
0487 //add features
0488 corp.getFeatures().put("my STRING feature ", new String("string string"));
0489 corp.getFeatures().put("my BOOL feature ", new Boolean("false"));
0490 corp.getFeatures().put("my INT feature ", new Integer("1234"));
0491 corp.getFeatures().put("my LONG feature ", new Long("123456789"));
0492 corp.getFeatures().put("my LONG STRING feature", this.VERY_LONG_STRING);
0493 corp.getFeatures().put("my NULL feature", null);
0494 corp.getFeatures().put("my BINARY feature",new Dummy(101,"101",true,101.101f));
0495 return corp;
0496 }
0497
0498 private DatabaseDataStore _createDS() {
0499
0500 DatabaseDataStore ds = null;
0501 if (TestPersist.dbType == DBHelper.ORACLE_DB) {
0502 ds = new OracleDataStore();
0503 }
0504 else if (TestPersist.dbType == DBHelper.POSTGRES_DB) {
0505 ds = new PostgresDataStore();
0506 }
0507 else {
0508 throw new IllegalArgumentException();
0509 }
0510
0511 Assert.assertNotNull(ds);
0512 return ds;
0513 }
0514
0515 private void prepareDB(String db) {
0516
0517 if (TestPersist.JDBC_URL_1.indexOf(db) > 0 ) {
0518 TestPersist.JDBC_URL = TestPersist.JDBC_URL_1;
0519 }
0520 else {
0521 TestPersist.JDBC_URL = TestPersist.JDBC_URL_2;
0522 }
0523
0524 Assert.assertNotNull("jdbc url not set for Oracle or Postgres",TestPersist.JDBC_URL);
0525
0526 TestPersist.dbType = DBHelper.getDatabaseType(JDBC_URL);
0527 }
0528
0529
0530 /** Test the DS register. */
0531 private void _testDB_UseCase01() throws Exception {
0532 ///Err.prln("Use case 01 started...");
0533 //descr: create a document in the DB
0534
0535
0536 //1. open data storage
0537 DatabaseDataStore ds = this._createDS();
0538 Assert.assertNotNull(ds);
0539 ds.setStorageUrl(TestPersist.JDBC_URL);
0540 ds.open();
0541
0542 //2. get test document
0543 Document transDoc = createTestDocument();
0544 Assert.assertNotNull(transDoc);
0545
0546 //3. get security factory & login
0547 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
0548 ac.open();
0549 Assert.assertNotNull(ac);
0550
0551 User usr = ac.findUser("kalina");
0552 Assert.assertNotNull(usr);
0553
0554 Group grp = (Group)usr.getGroups().get(0);
0555 Assert.assertNotNull(grp);
0556
0557 Session usrSession = ac.login("kalina","sesame",grp.getID());
0558 Assert.assertNotNull(usrSession);
0559 Assert.assertTrue(ac.isValidSession(usrSession));
0560
0561 //4. create security settings for doc
0562 SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp);
0563
0564 //5 set DS session
0565 ds.setSession(usrSession);
0566
0567 //6. cache the transient document properties for comparison
0568 /// ...since it will be cleanup upon adoption from the datastore.
0569 //... We'll need the cached values for the comparison only (asserts)
0570 sample_defaultASet = new AnnotationSetImpl(transDoc.getAnnotations());
0571 sample_name = transDoc.getName();
0572 sample_docFeatures = transDoc.getFeatures();
0573 sample_sourceURL = transDoc.getSourceUrl();
0574 sample_startOffset = transDoc.getSourceUrlStartOffset();
0575 sample_endOffset = transDoc.getSourceUrlEndOffset();
0576 sample_markupAware = transDoc.getMarkupAware();
0577 sample_content = transDoc.getContent();
0578 sample_encoding = (String)transDoc.getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME);
0579
0580 sample_namedASets = new HashMap();
0581 Map transDocNamedSets = transDoc.getNamedAnnotationSets();
0582 Iterator it = transDocNamedSets.keySet().iterator();
0583 while (it.hasNext()) {
0584 String asetName = (String)it.next();
0585 AnnotationSet transAset = (AnnotationSet)transDocNamedSets.get(asetName);
0586 AnnotationSet asetNew = new AnnotationSetImpl(transAset);
0587 TestPersist.sample_namedASets.put(transAset.getName(),asetNew);
0588 }
0589
0590
0591 //7. try adding doc to data store
0592 LanguageResource lr = ds.adopt(transDoc,si);
0593
0594 Assert.assertTrue(lr instanceof DatabaseDocumentImpl);
0595 Assert.assertNotNull(lr.getDataStore());
0596 Assert.assertTrue(lr.getDataStore() instanceof DatabaseDataStore);
0597 Assert.assertEquals(sample_defaultASet, ((DatabaseDocumentImpl)lr).getAnnotations());
0598
0599 sampleDoc_lrID = (Long)lr.getLRPersistenceId();
0600 if (DEBUG) Out.prln("lr id: " + TestPersist.sampleDoc_lrID);
0601
0602 //8.close
0603 ac.close();
0604 ds.close();
0605
0606 if(DEBUG) {
0607 Err.prln("Use case 01 passed...");
0608 }
0609 }
0610
0611
0612 private void _testDB_UseCase02() throws Exception {
0613 ///Err.prln("Use case 02 started...");
0614 //read a document
0615 //use the one created in UC01
0616 LanguageResource lr = null;
0617
0618 //1. open data storage
0619 DatabaseDataStore ds = this._createDS();
0620 Assert.assertNotNull(ds);
0621 ds.setStorageUrl(TestPersist.JDBC_URL);
0622 ds.open();
0623
0624 //3. get security factory & login
0625 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
0626 Assert.assertNotNull(ac);
0627 ac.open();
0628
0629 User usr = ac.findUser("kalina");
0630 Assert.assertNotNull(usr);
0631
0632 Group grp = (Group)usr.getGroups().get(0);
0633 Assert.assertNotNull(grp);
0634
0635 Session usrSession = ac.login("kalina","sesame",grp.getID());
0636 Assert.assertNotNull(usrSession);
0637 Assert.assertTrue(ac.isValidSession(usrSession));
0638
0639 //4. create security settings for doc
0640 SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp);
0641
0642 //4.5 set DS session
0643 ds.setSession(usrSession);
0644
0645 //2. read LR
0646 ///Err.println(">>>");
0647 FeatureMap params = Factory.newFeatureMap();
0648 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
0649 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
0650 lr = (LanguageResource) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
0651 ///Err.println("<<<");
0652 //3. check name
0653 String name = lr.getName();
0654 Assert.assertNotNull(name);
0655 Assert.assertEquals(name,sample_name);
0656
0657 //4. check features
0658 FeatureMap fm = lr.getFeatures();
0659
0660 Assert.assertNotNull(fm);
0661 Assert.assertNotNull(sample_docFeatures);
0662 Assert.assertTrue(fm.size() == sample_docFeatures.size());
0663
0664 Iterator keys = fm.keySet().iterator();
0665
0666 while (keys.hasNext()) {
0667 String currKey = (String)keys.next();
0668 Assert.assertTrue(sample_docFeatures.containsKey(currKey));
0669 Assert.assertEquals(fm.get(currKey),sample_docFeatures.get(currKey));
0670 }
0671
0672 //6. URL
0673 DatabaseDocumentImpl dbDoc = (DatabaseDocumentImpl)lr;
0674 Assert.assertEquals(dbDoc.getSourceUrl(),sample_sourceURL);
0675
0676 //5.start/end
0677 Assert.assertEquals(dbDoc.getSourceUrlStartOffset(),sample_startOffset);
0678 Assert.assertEquals(dbDoc.getSourceUrlEndOffset(),sample_endOffset);
0679
0680 //6.markupAware
0681 Assert.assertEquals(dbDoc.getMarkupAware(),sample_markupAware);
0682
0683 //7. content
0684 DocumentContent cont = dbDoc.getContent();
0685 Assert.assertEquals(cont,sample_content);
0686
0687 //8. access the content again and assure it's not read from the DB twice
0688 Assert.assertEquals(cont,sample_content);
0689
0690 //9. encoding
0691 String encNew = (String)dbDoc.
0692 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME);
0693 String encOld = sample_encoding;
0694 Assert.assertEquals(encNew,encOld);
0695
0696 //10. default annotations
0697 ///System.out.println("GETTING default ANNOTATIONS...");
0698 AnnotationSet defaultNew = dbDoc.getAnnotations();
0699 AnnotationSet defaultOld = sample_defaultASet;
0700
0701 Assert.assertNotNull(defaultNew);
0702 Assert.assertTrue(defaultNew.size() == defaultOld.size());
0703 Assert.assertEquals(defaultNew,defaultOld);
0704
0705
0706 //10. iterate named annotations
0707 Iterator itOld = TestPersist.sample_namedASets.keySet().iterator();
0708 while (itOld.hasNext()) {
0709 String asetName = (String)itOld.next();
0710 AnnotationSet asetOld = (AnnotationSet)sample_namedASets.get(asetName);
0711 AnnotationSet asetNew = (AnnotationSet)dbDoc.getAnnotations(asetName);
0712 Assert.assertNotNull(asetNew);
0713 Assert.assertTrue(asetNew.size() == asetOld.size());
0714 Assert.assertEquals(asetNew.get(),asetOld.get());
0715 }
0716
0717 /*
0718 //10. iterate named annotations
0719 Map namedOld = this.sampleDoc.getNamedAnnotationSets();
0720 Iterator itOld = namedOld.keySet().iterator();
0721 while (itOld.hasNext()) {
0722 String asetName = (String)itOld.next();
0723 AnnotationSet asetOld = (AnnotationSet)namedOld.get(asetName);
0724 AnnotationSet asetNew = (AnnotationSet)dbDoc.getAnnotations(asetName);
0725 Assert.assertNotNull(asetNew);
0726 Assert.assertTrue(asetNew.size() == asetOld.size());
0727 Assert.assertEquals(asetNew,asetOld);
0728 }
0729 */
0730
0731 //11. ALL named annotation (ensure nothing is read from DB twice)
0732 Map namedNew = dbDoc.getNamedAnnotationSets();
0733
0734 Assert.assertNotNull(namedNew);
0735 Assert.assertTrue(namedNew.size() == TestPersist.sample_namedASets.size());
0736
0737 Iterator itNames = namedNew.keySet().iterator();
0738 while (itNames.hasNext()) {
0739 String asetName = (String)itNames.next();
0740 AnnotationSet asetNew = (AnnotationSet)namedNew.get(asetName);
0741 AnnotationSet asetOld = (AnnotationSet)sample_namedASets.get(asetName);
0742 Assert.assertNotNull(asetNew);
0743 Assert.assertNotNull(asetOld);
0744 Assert.assertEquals(asetNew.get(),asetOld.get());
0745 }
0746
0747 //close
0748 ds.removeDatastoreListener((DatastoreListener)lr);
0749 lr = null;
0750
0751 ds.close();
0752 ac.close();
0753
0754 if(DEBUG) {
0755 Err.prln("Use case 02 passed...");
0756 }
0757
0758 }
0759
0760
0761 private void _testDB_UseCase03() throws Exception {
0762 ///Err.prln("Use case 03 started...");
0763 //sync a document
0764 LanguageResource lr = null;
0765
0766 //0. get security factory & login
0767 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
0768 Assert.assertNotNull(ac);
0769 ac.open();
0770
0771 User usr = ac.findUser("kalina");
0772 Assert.assertNotNull(usr);
0773
0774 Group grp = (Group)usr.getGroups().get(0);
0775 Assert.assertNotNull(grp);
0776
0777 Session usrSession = ac.login("kalina","sesame",grp.getID());
0778 Assert.assertNotNull(usrSession);
0779 Assert.assertTrue(ac.isValidSession(usrSession));
0780
0781 //1. open data storage
0782 DatabaseDataStore ds = this._createDS();
0783 Assert.assertNotNull(ds);
0784 ds.setStorageUrl(TestPersist.JDBC_URL);
0785 ds.open();
0786
0787 //1.5 set DS session
0788 ds.setSession(usrSession);
0789
0790 if (DEBUG) Out.prln("ID " + sampleDoc_lrID);
0791 //2. read LR
0792 FeatureMap params = Factory.newFeatureMap();
0793 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
0794 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
0795 lr = (LanguageResource) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
0796 Document dbDoc = (Document)lr;
0797 Document doc2 = null;
0798
0799 //2.5 get exclusive lock
0800 if (false == ds.lockLr(lr)) {
0801 throw new PersistenceException("document is locked by another user");
0802 }
0803
0804 //3. change name
0805 String oldName = dbDoc.getName();
0806 String newName = oldName + "__UPD";
0807 dbDoc.setName(newName);
0808 dbDoc.sync();
0809 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
0810 params.clear();
0811 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
0812 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
0813 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
0814
0815 Assert.assertEquals(newName,dbDoc.getName());
0816 Assert.assertEquals(newName,doc2.getName());
0817
0818 Factory.deleteResource(doc2);
0819 doc2 = null;
0820
0821 //4. change features
0822 FeatureMap fm = dbDoc.getFeatures();
0823 Iterator keys = fm.keySet().iterator();
0824
0825 //4.1 change the value of the first feature
0826 while(keys.hasNext()) {
0827 String currKey = (String)keys.next();
0828 Object val = fm.get(currKey);
0829 Object newVal = null;
0830 if (val instanceof Long) {
0831 newVal = new Long(101010101);
0832 }
0833 else if (val instanceof Integer) {
0834 newVal = new Integer(2121212);
0835 }
0836 else if (val instanceof String) {
0837 newVal = new String("UPD__").concat( (String)val).concat("__UPD");
0838 }
0839 if (newVal != null)
0840 fm.put(currKey,newVal);
0841 }
0842 dbDoc.sync();
0843 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
0844 params.clear();
0845 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
0846 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
0847 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
0848
0849 Assert.assertEquals(fm,dbDoc.getFeatures());
0850 Assert.assertEquals(fm,doc2.getFeatures());
0851 Factory.deleteResource(doc2);
0852 doc2 = null;
0853
0854 //6. URL
0855 URL docURL = dbDoc.getSourceUrl();
0856 URL newURL = null;
0857 newURL = new URL(docURL.toString()+".UPDATED");
0858 dbDoc.setSourceUrl(newURL);
0859 dbDoc.sync();
0860 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
0861 params.clear();
0862 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
0863 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
0864 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
0865
0866 Assert.assertEquals(newURL,dbDoc.getSourceUrl());
0867 Assert.assertEquals(newURL,doc2.getSourceUrl());
0868 Factory.deleteResource(doc2);
0869 doc2 = null;
0870
0871 //5.start/end
0872 Long newStart = new Long(123);
0873 Long newEnd = new Long(789);
0874 dbDoc.setSourceUrlStartOffset(newStart);
0875 dbDoc.setSourceUrlEndOffset(newEnd);
0876 dbDoc.sync();
0877
0878 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
0879 params.clear();
0880 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
0881 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
0882 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
0883
0884 Assert.assertEquals(newStart,dbDoc.getSourceUrlStartOffset());
0885 Assert.assertEquals(newStart,doc2.getSourceUrlStartOffset());
0886 Assert.assertEquals(newEnd,dbDoc.getSourceUrlEndOffset());
0887 Assert.assertEquals(newEnd,doc2.getSourceUrlEndOffset());
0888
0889 Factory.deleteResource(doc2);
0890 doc2 = null;
0891
0892
0893 //6.markupAware
0894 Boolean oldMA = dbDoc.getMarkupAware();
0895 Boolean newMA = oldMA.booleanValue() ? Boolean.FALSE : Boolean.TRUE;
0896 dbDoc.setMarkupAware(newMA);
0897 dbDoc.sync();
0898
0899 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
0900 params.clear();
0901 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
0902 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
0903 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
0904
0905 Assert.assertEquals(newMA,doc2.getMarkupAware());
0906 Assert.assertEquals(newMA,dbDoc.getMarkupAware());
0907
0908 Factory.deleteResource(doc2);
0909 doc2 = null;
0910
0911
0912 //7. content
0913 DocumentContent contOld = dbDoc.getContent();
0914 DocumentContent contNew = new DocumentContentImpl(new String("UPDATED__").concat(contOld.toString().concat("__UPDATED")));
0915 dbDoc.setContent(contNew);
0916 dbDoc.sync();
0917
0918 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
0919 params.clear();
0920 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
0921 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
0922 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
0923
0924 Assert.assertEquals(contNew,dbDoc.getContent());
0925 Assert.assertEquals(contNew,doc2.getContent());
0926
0927 Factory.deleteResource(doc2);
0928 doc2 = null;
0929
0930 //8. encoding
0931 String encOld = (String)dbDoc.
0932 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME);
0933 dbDoc.setParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME,"XXX");
0934 dbDoc.sync();
0935 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
0936 params.clear();
0937 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
0938 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
0939 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
0940
0941 String encNew = (String)doc2.
0942 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME);
0943 Assert.assertEquals(encNew,encOld);
0944
0945 Factory.deleteResource(doc2);
0946 doc2 = null;
0947
0948
0949 //9. add annotations
0950 AnnotationSet dbDocSet = dbDoc.getAnnotations("TEST SET");
0951 Assert.assertNotNull(dbDocSet);
0952
0953 FeatureMap fm1 = new SimpleFeatureMapImpl();
0954 fm1.put("string key","string value");
0955
0956 Integer annInd = dbDocSet.add(new Long(0), new Long(10),
0957 "TEST TYPE",
0958 fm1);
0959
0960 dbDoc.sync();
0961 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
0962 params.clear();
0963 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
0964 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
0965 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
0966
0967 AnnotationSet doc2Set = doc2.getAnnotations("TEST SET");
0968 Assert.assertTrue(dbDocSet.size() == doc2Set.size());
0969 //-- Assert.assertEquals(doc2Set,dbDocSet);
0970
0971 Factory.deleteResource(doc2);
0972 doc2 = null;
0973
0974
0975 //9.1. change+add annotations
0976 Annotation dbDocAnn = dbDocSet.get(annInd);
0977
0978 FeatureMap fm2 = new SimpleFeatureMapImpl();
0979 fm2.put("string2","uuuuuu");
0980 fm2.put("int2",new Integer(98989898));
0981 Integer newInd = dbDocSet.add(dbDocAnn.getStartNode().getOffset(),
0982 dbDocAnn.getEndNode().getOffset(),
0983 dbDocAnn.getType() + "__XX",
0984 fm2);
0985 Annotation dbDocAnnNew = dbDocSet.get(newInd);
0986 dbDoc.sync();
0987
0988 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
0989 params.clear();
0990 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
0991 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
0992 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
0993
0994 doc2Set = doc2.getAnnotations("TEST SET");
0995 Assert.assertTrue(dbDocSet.size() == doc2Set.size());
0996 Assert.assertTrue(TestEqual.annotationSetsEqual(doc2Set, dbDocSet));
0997 Assert.assertTrue(doc2Set.contains(dbDocAnnNew));
0998
0999 Factory.deleteResource(doc2);
1000 doc2 = null;
1001 /*
1002 //10. iterate named annotations
1003 Map namedOld = ((DocumentImpl)this.uc01_LR).getNamedAnnotationSets();
1004 Iterator itOld = namedOld.keySet().iterator();
1005 while (itOld.hasNext()) {
1006 String asetName = (String)itOld.next();
1007 AnnotationSet asetOld = (AnnotationSet)namedOld.get(asetName);
1008 AnnotationSet asetNew = (AnnotationSet)dbDoc.getAnnotations(asetName);
1009 Assert.assertNotNull(asetNew);
1010 Assert.assertEquals(asetNew,asetOld);
1011 // Features fmNew = asetNew.getFea
1012 }
1013 */
1014
1015 //11. add a new ann-set
1016 String dummySetName = "--NO--SUCH--SET--";
1017 AnnotationSet aset = dbDoc.getAnnotations(dummySetName);
1018 aset.addAll(dbDoc.getAnnotations());
1019 dbDoc.sync();
1020
1021 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
1022 params.clear();
1023 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
1024 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
1025 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
1026
1027 Assert.assertTrue(dbDoc.getNamedAnnotationSets().containsKey(dummySetName));
1028 Assert.assertTrue(doc2.getNamedAnnotationSets().containsKey(dummySetName));
1029 AnnotationSet copy1 = (AnnotationSet)
1030 dbDoc.getNamedAnnotationSets().get(dummySetName);
1031 AnnotationSet copy2 = (AnnotationSet)
1032 doc2.getNamedAnnotationSets().get(dummySetName);
1033 Assert.assertTrue(dbDoc.getNamedAnnotationSets().containsValue(aset));
1034 Assert.assertTrue(TestEqual.annotationSetsEqual(copy1, copy2));
1035 Assert.assertTrue(dbDoc.getNamedAnnotationSets().size() == doc2.getNamedAnnotationSets().size());
1036 //maps aren't equal since removing the equals impementations
1037 // Assert.assertEquals(doc2.getNamedAnnotationSets(),dbDoc.getNamedAnnotationSets());
1038
1039 Factory.deleteResource(doc2);
1040 doc2 = null;
1041
1042 //12. remove aset
1043 dbDoc.removeAnnotationSet(dummySetName);
1044 dbDoc.sync();
1045 Assert.assertTrue(false == ((EventAwareDocument)dbDoc).getLoadedAnnotationSets().contains(dummySetName));
1046 Assert.assertTrue(false == dbDoc.getNamedAnnotationSets().containsKey(dummySetName));
1047
1048 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
1049 params.clear();
1050 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
1051 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
1052 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
1053
1054 Assert.assertTrue(false == doc2.getNamedAnnotationSets().containsKey(dummySetName));
1055
1056 Factory.deleteResource(doc2);
1057 doc2 = null;
1058
1059 //13. unlock
1060 ds.unlockLr(lr);
1061 ds.sync(lr);
1062
1063 //close
1064 Factory.deleteResource(dbDoc);
1065 dbDoc = null;
1066
1067 ac.close();
1068 ds.close();
1069
1070 if(DEBUG) {
1071 Err.prln("Use case 03 passed...");
1072 }
1073 }
1074
1075
1076 private void _testDB_UseCase04() throws Exception {
1077 ///Err.prln("Use case 04 started...");
1078 //delete a document
1079 LanguageResource lr = null;
1080
1081 //0. get security factory & login
1082 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
1083 Assert.assertNotNull(ac);
1084 ac.open();
1085
1086 User usr = ac.findUser("kalina");
1087 Assert.assertNotNull(usr);
1088
1089 Group grp = (Group)usr.getGroups().get(0);
1090 Assert.assertNotNull(grp);
1091
1092 Session usrSession = ac.login("kalina","sesame",grp.getID());
1093 Assert.assertNotNull(usrSession);
1094 Assert.assertTrue(ac.isValidSession(usrSession));
1095
1096 //1. open data storage
1097 DatabaseDataStore ds = this._createDS();
1098 Assert.assertNotNull(ds);
1099 ds.setStorageUrl(TestPersist.JDBC_URL);
1100 ds.open();
1101 ds.setSession(usrSession);
1102
1103 //2. read LR
1104 FeatureMap params = Factory.newFeatureMap();
1105 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
1106 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
1107 lr = (LanguageResource) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
1108
1109 //2.5 get exclusive lock
1110 if (false == ds.lockLr(lr)) {
1111 throw new PersistenceException("document is locked by another user");
1112 }
1113
1114 //3. try to delete it
1115 ds.delete(DBHelper.DOCUMENT_CLASS,lr.getLRPersistenceId());
1116
1117 //no need to unlock
1118
1119 //close
1120 ds.close();
1121 ac.close();
1122
1123 if(DEBUG) {
1124 Err.prln("Use case 04 passed...");
1125 }
1126
1127 }
1128
1129
1130 /** Test the DS register. */
1131 private void _testDB_UseCase101() throws Exception {
1132 ///Err.prln("Use case 101 started...");
1133 //descr : create a corpus
1134
1135 //0. get security factory & login
1136 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
1137 Assert.assertNotNull(ac);
1138 ac.open();
1139
1140 User usr = ac.findUser("kalina");
1141 Assert.assertNotNull(usr);
1142
1143 Group grp = (Group)usr.getGroups().get(0);
1144 Assert.assertNotNull(grp);
1145
1146 Session usrSession = ac.login("kalina","sesame",grp.getID());
1147 Assert.assertNotNull(usrSession);
1148 Assert.assertTrue(ac.isValidSession(usrSession));
1149
1150 //1. open data storage
1151 DatabaseDataStore ds = this._createDS();
1152 Assert.assertNotNull(ds);
1153 ds.setStorageUrl(TestPersist.JDBC_URL);
1154 ds.open();
1155 ds.setSession(usrSession);
1156
1157 //2. get test document
1158 Corpus corp = createTestCorpus();
1159 Assert.assertNotNull(corp);
1160
1161 //4. create security settings for doc
1162 SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp);
1163
1164 //5. try adding corpus to data store
1165 Corpus result = (Corpus)ds.adopt(corp,si);
1166 Assert.assertNotNull(result);
1167 Assert.assertTrue(result instanceof DatabaseCorpusImpl);
1168 Assert.assertNotNull(result.getLRPersistenceId());
1169
1170 TestPersist.sampleCorpus = result;
1171 TestPersist.sampleCorpus_lrID = (Long)result.getLRPersistenceId();
1172
1173 //6.close
1174 ac.close();
1175 ds.close();
1176
1177 if(DEBUG) {
1178 Err.prln("Use case 101 passed...");
1179 }
1180
1181 }
1182
1183
1184
1185 /** Test the DS register. */
1186 private void _testDB_UseCase102() throws Exception {
1187 //read a corpus
1188 ///Err.prln("Use case 102 started...");
1189 LanguageResource lr = null;
1190
1191 //0. get security factory & login
1192 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
1193 Assert.assertNotNull(ac);
1194 ac.open();
1195
1196 User usr = ac.findUser("kalina");
1197 Assert.assertNotNull(usr);
1198
1199 Group grp = (Group)usr.getGroups().get(0);
1200 Assert.assertNotNull(grp);
1201
1202 Session usrSession = ac.login("kalina","sesame",grp.getID());
1203 Assert.assertNotNull(usrSession);
1204 Assert.assertTrue(ac.isValidSession(usrSession));
1205
1206 //1. open data storage
1207 DatabaseDataStore ds = this._createDS();
1208 Assert.assertNotNull(ds);
1209 ds.setStorageUrl(TestPersist.JDBC_URL);
1210 ds.open();
1211 ds.setSession(usrSession);
1212
1213 //2. read LR
1214 FeatureMap params = Factory.newFeatureMap();
1215 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
1216 params.put(DataStore.LR_ID_FEATURE_NAME, sampleCorpus_lrID);
1217 lr = (LanguageResource) Factory.createResource(DBHelper.CORPUS_CLASS, params);
1218
1219 //3. check name
1220 String name = lr.getName();
1221 Assert.assertNotNull(name);
1222 Assert.assertEquals(name,sampleCorpus.getName());
1223
1224 //4. check features
1225 FeatureMap fm = lr.getFeatures();
1226 FeatureMap fmOrig = sampleCorpus.getFeatures();
1227
1228 Assert.assertNotNull(fm);
1229 Assert.assertNotNull(fmOrig);
1230 Assert.assertTrue(fm.size() == fmOrig.size());
1231
1232 Iterator keys = fm.keySet().iterator();
1233
1234 while (keys.hasNext()) {
1235 String currKey = (String)keys.next();
1236 Assert.assertTrue(fmOrig.containsKey(currKey));
1237 Assert.assertEquals(fm.get(currKey),fmOrig.get(currKey));
1238 }
1239
1240 //close
1241 ds.close();
1242
1243 if(DEBUG) {
1244 Err.prln("Use case 102 passed...");
1245 }
1246
1247 }
1248
1249
1250 private void _testDB_UseCase103() throws Exception {
1251 ///Err.prln("Use case 103 started...");
1252 //sync a corpus
1253 LanguageResource lr = null;
1254
1255 //0. get security factory & login
1256 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
1257 Assert.assertNotNull(ac);
1258 ac.open();
1259
1260 User usr = ac.findUser("kalina");
1261 Assert.assertNotNull(usr);
1262
1263 Group grp = (Group)usr.getGroups().get(0);
1264 Assert.assertNotNull(grp);
1265
1266 Session usrSession = ac.login("kalina","sesame",grp.getID());
1267 Assert.assertNotNull(usrSession);
1268 Assert.assertTrue(ac.isValidSession(usrSession));
1269
1270 //1. open data storage
1271 DatabaseDataStore ds = this._createDS();
1272 Assert.assertNotNull(ds);
1273 ds.setStorageUrl(TestPersist.JDBC_URL);
1274 ds.open();
1275 ds.setSession(usrSession);
1276
1277 if (DEBUG) Out.prln("ID " + sampleCorpus_lrID);
1278
1279 //2. read LR
1280 FeatureMap params = Factory.newFeatureMap();
1281 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
1282 params.put(DataStore.LR_ID_FEATURE_NAME, sampleCorpus_lrID);
1283 lr = (LanguageResource) Factory.createResource(DBHelper.CORPUS_CLASS, params);
1284
1285 Corpus dbCorp = (Corpus)lr;
1286 Corpus corp2 = null;
1287
1288 //3. change name
1289 String oldName = dbCorp.getName();
1290 String newName = oldName + "__UPD";
1291 dbCorp.setName(newName);
1292 dbCorp.sync();
1293 corp2 = (Corpus)ds.getLr(DBHelper.CORPUS_CLASS,sampleCorpus_lrID);
1294 Assert.assertEquals(newName,dbCorp.getName());
1295 Assert.assertEquals(newName,corp2.getName());
1296
1297 //4. change features
1298 FeatureMap fm = dbCorp.getFeatures();
1299 Iterator keys = fm.keySet().iterator();
1300
1301 //4.1 change the value of the first feature
1302 while(keys.hasNext()) {
1303 String currKey = (String)keys.next();
1304 Object val = fm.get(currKey);
1305 Object newVal = null;
1306 if (val instanceof Long) {
1307 newVal = new Long(101010101);
1308 }
1309 else if (val instanceof Integer) {
1310 newVal = new Integer(2121212);
1311 }
1312 else if (val instanceof String) {
1313 newVal = new String("UPD__").concat( (String)val).concat("__UPD");
1314 }
1315 if (newVal != null)
1316 fm.put(currKey,newVal);
1317 }
1318 dbCorp.sync();
1319 corp2 = (Corpus)ds.getLr(DBHelper.CORPUS_CLASS,sampleCorpus_lrID);
1320 Assert.assertEquals(fm,dbCorp.getFeatures());
1321 Assert.assertEquals(fm,corp2.getFeatures());
1322
1323 //close
1324 ds.close();
1325
1326 if(DEBUG) {
1327 Err.prln("Use case 103 passed...");
1328 }
1329
1330 }
1331
1332 // public void testOracle_01() throws Exception {
1333 //
1334 // if (DEBUG)
1335 // System.out.println(">> 01");
1336 //
1337 // prepareDB("oracle");
1338 // _testDB_UseCase01();
1339 //
1340 // if (DEBUG)
1341 // System.out.println("<< 01");
1342 // }
1343 //
1344 // public void testOracle_02() throws Exception {
1345 //
1346 // if (DEBUG)
1347 // System.out.println(">> 02");
1348 //
1349 // prepareDB("oracle");
1350 // _testDB_UseCase02();
1351 //
1352 // if (DEBUG)
1353 // System.out.println("<< 02");
1354 // }
1355 //
1356 // public void testOracle_03() throws Exception {
1357 // if (DEBUG)
1358 // System.out.println(">> 03");
1359 //
1360 // prepareDB("oracle");
1361 // _testDB_UseCase03();
1362 //
1363 // if (DEBUG)
1364 // System.out.println("<< 03");
1365 // }
1366 //
1367 // public void testOracle_04() throws Exception {
1368 // if (DEBUG)
1369 // System.out.println(">> 04");
1370 //
1371 // prepareDB("oracle");
1372 // _testDB_UseCase04();
1373 //
1374 // if (DEBUG)
1375 // System.out.println("<< 04");
1376 // }
1377 //
1378 // public void testOracle_101() throws Exception {
1379 // if (DEBUG)
1380 // System.out.println(">> 101");
1381 //
1382 // prepareDB("oracle");
1383 // _testDB_UseCase101();
1384 //
1385 // if (DEBUG)
1386 // System.out.println("<< 101");
1387 // }
1388 //
1389 // public void testOracle_102() throws Exception {
1390 // if (DEBUG)
1391 // System.out.println(">> 102");
1392 //
1393 // prepareDB("oracle");
1394 // _testDB_UseCase102();
1395 //
1396 // if (DEBUG)
1397 // System.out.println("<< 102");
1398 // }
1399 //
1400 // public void testOracle_103() throws Exception {
1401 // if (DEBUG)
1402 // System.out.println(">> 103");
1403 //
1404 // prepareDB("oracle");
1405 // _testDB_UseCase103();
1406 //
1407 // if (DEBUG)
1408 // System.out.println("<< 103");
1409 // }
1410
1411 // public void testPostgres_01() throws Exception {
1412 //
1413 // prepareDB("postgres");
1414 // _testDB_UseCase01();
1415 // }
1416 //
1417 // public void testPostgres_02() throws Exception {
1418 //
1419 // prepareDB("postgres");
1420 // _testDB_UseCase02();
1421 // }
1422 //
1423 // public void testPostgres_03() throws Exception {
1424 //
1425 // prepareDB("postgres");
1426 // _testDB_UseCase03();
1427 // }
1428 //
1429 // public void testPostgres_04() throws Exception {
1430 //
1431 // prepareDB("postgres");
1432 // _testDB_UseCase04();
1433 // }
1434 //
1435 // public void testPostgres_101() throws Exception {
1436 //
1437 // prepareDB("postgres");
1438 // _testDB_UseCase101();
1439 // }
1440 //
1441 // public void testPostgres_102() throws Exception {
1442 //
1443 // prepareDB("postgres");
1444 // _testDB_UseCase102();
1445 // }
1446 //
1447 // public void testPostgres_103() throws Exception {
1448 //
1449 // prepareDB("postgres");
1450 // _testDB_UseCase103();
1451 // }
1452 //
1453
1454
1455 public static void main(String[] args){
1456 try{
1457
1458 //-System.setProperty(Gate.GATE_CONFIG_PROPERTY,"y:/gate.xml") ;
1459 Gate.setLocalWebServer(false);
1460 Gate.setNetConnected(false);
1461 Gate.init();
1462
1463
1464 TestPersist test = new TestPersist("");
1465
1466 /*
1467 long timeStart = 0;
1468 timeStart = System.currentTimeMillis();
1469 int size = 512*1024;
1470 // test.testOracleLOB(size,3);
1471 test.testPostgresLOB(size,3);
1472 System.out.println("time: ["+ (System.currentTimeMillis()-timeStart) +"]");
1473
1474 if (true) {
1475 throw new RuntimeException();
1476 }
1477 */
1478
1479 /* oracle */
1480
1481 // test.setUp();
1482 // test.testOracle_01();
1483 // test.tearDown();
1484 //
1485 // test.setUp();
1486 // test.testOracle_02();
1487 // test.tearDown();
1488 //
1489 // test.setUp();
1490 // test.testOracle_03();
1491 // test.tearDown();
1492 //
1493 // test.setUp();
1494 // test.testOracle_04();
1495 // test.tearDown();
1496 //
1497 // test.setUp();
1498 // test.testOracle_101();
1499 // test.tearDown();
1500 //
1501 // test.setUp();
1502 // test.testOracle_102();
1503 // test.tearDown();
1504 //
1505 // test.setUp();
1506 // test.testOracle_103();
1507 // test.tearDown();
1508
1509
1510 /* postgres */
1511
1512 // test.setUp();
1513 // test.testPostgres_01();
1514 // test.tearDown();
1515 //
1516 // test.setUp();
1517 // test.testPostgres_02();
1518 // test.tearDown();
1519 //
1520 // test.setUp();
1521 // test.testPostgres_03();
1522 // test.tearDown();
1523 //
1524 // test.setUp();
1525 // test.testPostgres_04();
1526 // test.tearDown();
1527 //
1528 // test.setUp();
1529 // test.testPostgres_101();
1530 // test.tearDown();
1531 //
1532 // test.setUp();
1533 // test.testPostgres_102();
1534 // test.tearDown();
1535 //
1536 // test.setUp();
1537 // test.testPostgres_103();
1538 // test.tearDown();
1539
1540 /* SerialDS */
1541
1542 test.setUp();
1543 test.testDelete();
1544 test.tearDown();
1545
1546 test.setUp();
1547 test.testDSR();
1548 test.tearDown();
1549
1550 test.setUp();
1551 test.testMultipleLrs();
1552 test.tearDown();
1553
1554 test.setUp();
1555 // test.testSaveRestore();
1556 test.tearDown();
1557
1558 test.setUp();
1559 test.testSimple();
1560 test.tearDown();
1561
1562 //I put this last because its failure is dependent on the gc() and
1563 //there's nothing I can do about it. Maybe I'll remove this from the
1564 //test
1565 test.setUp();
1566 test.testMultipleLrs();
1567 test.tearDown();
1568
1569 if (DEBUG) {
1570 Err.println("done.");
1571 }
1572 }catch(Exception e){
1573 e.printStackTrace();
1574 }
1575 }
1576
1577 /*
1578 public void testPostgresLOB(int size, int count) throws Exception {
1579
1580 byte[] buffer = new byte[size];
1581 String url = "jdbc:postgresql://192.168.128.208:5432/gate09?user=gateuser&password=gate";
1582 // ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
1583
1584 try {
1585 Connection conn = DBHelper.connect(url);
1586 conn.setAutoCommit(false);
1587 PreparedStatement pstmt = conn.prepareStatement("insert into lob_test values(?)");
1588
1589 for (int i =0; i< count; i++) {
1590 // bais.reset();
1591 // pstmt.setBinaryStream(1,bais,buffer.length);
1592 pstmt.setBytes(1,buffer);
1593 pstmt.executeUpdate();
1594 conn.commit();
1595 }
1596 }
1597 catch(Exception e) {
1598 e.printStackTrace();
1599 }
1600
1601
1602 }
1603
1604 public void testOracleLOB(int size,int count) throws Exception {
1605 byte[] buffer = new byte[size];
1606 String url = "jdbc:oracle:thin:GATEUSER/gate@192.168.128.208:1521:gate07";
1607 ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
1608
1609 CallableStatement cstmt = null;
1610 PreparedStatement pstmt = null;
1611 ResultSet rs = null;
1612 Blob blobValue = null;
1613
1614 try {
1615 Connection conn = DBHelper.connect(url);
1616 conn.setAutoCommit(false);
1617 cstmt = conn.prepareCall("{ call gateadmin.create_lob(?) }");
1618
1619
1620 for (int i =0; i< count; i++) {
1621
1622 cstmt.registerOutParameter(1,java.sql.Types.BIGINT);
1623 cstmt.execute();
1624 long blobID = cstmt.getLong(1);
1625
1626 pstmt = conn.prepareStatement("select blob_value from gateadmin.lob_test where id=?");
1627 pstmt.setLong(1,blobID);
1628 pstmt.execute();
1629 rs = pstmt.getResultSet();
1630 rs.next();
1631
1632 blobValue = rs.getBlob(1);
1633 BLOB oraBlob = (BLOB)blobValue;
1634 OutputStream output = oraBlob.getBinaryOutputStream();
1635 output.write(buffer,0,buffer.length);
1636 output.close();
1637
1638 conn.commit();
1639 }
1640
1641 }
1642 catch(Exception e) {
1643 e.printStackTrace();
1644 }
1645
1646 }
1647
1648 */
1649
1650
1651 /*
1652 public void testPostgres01() throws Exception {
1653
1654 String url = "jdbc:postgresql://192.168.128.208:5432/gate09";
1655 try {
1656
1657 Connection c = DBHelper.connect(url,"gateuser","gate");
1658 c.setAutoCommit(false);
1659
1660 Object src = new Long(1234);
1661
1662 PreparedStatement pstmt = c.prepareStatement("insert into test3 values (nextval('seq3'), ?)");
1663 Object o = new Object();
1664
1665 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1666 ObjectOutputStream oos = new ObjectOutputStream(baos);
1667 oos.writeObject(src);
1668 oos.flush();
1669 oos.close();
1670 baos.close();
1671
1672 byte[] buff = baos.toByteArray();
1673 System.out.println(buff.length);
1674 ByteArrayInputStream bais = new ByteArrayInputStream(buff);
1675
1676 pstmt.setBinaryStream(1,bais,buff.length);
1677 pstmt.execute();
1678 bais.close();
1679 c.commit();
1680 bais.close();
1681
1682 PreparedStatement pstmt2 = c.prepareStatement("select blob from test3 where id = (select max(id) from test3)");
1683 pstmt2.execute();
1684 ResultSet rs = pstmt2.getResultSet();
1685 if (false == rs.next()) {
1686 throw new Exception("empty result set");
1687 }
1688
1689 InputStream is = rs.getBinaryStream("blob");
1690 ObjectInputStream ois = new ObjectInputStream(is);
1691 Object result = ois.readObject();
1692 System.out.println(result);
1693 ois.close();
1694 is.close();
1695
1696 rs.close();
1697 pstmt2.close();
1698
1699 c.commit();
1700
1701 }
1702 catch(SQLException e) {
1703 System.out.println(e.getErrorCode());
1704 e.printStackTrace();
1705 }
1706
1707 }
1708 */
1709
1710 } // class TestPersist
1711
1712
1713 class Dummy implements Serializable {
1714
1715 static final long serialVersionUID = 3632609241787241900L;
1716
1717 public int intValue;
1718 public String stringValue;
1719 public boolean boolValue;
1720 public float floatValue;
1721
1722
1723 public Dummy(int _int, String _string, boolean _bool, float _float) {
1724
1725 this.intValue = _int;
1726 this.stringValue= _string;
1727 this.boolValue = _bool;
1728 this.floatValue = _float;
1729 }
1730
1731 public boolean equals(Object obj) {
1732 Dummy d2 = (Dummy)obj;
1733
1734 return this.intValue == d2.intValue &&
1735 this.stringValue.equals(d2.stringValue) &&
1736 this.boolValue == d2.boolValue &&
1737 this.floatValue == d2.floatValue;
1738 }
1739
1740 public String toString() {
1741 return "Dummy: intV=["+this.intValue+"], stringV=["+this.stringValue+"], boolV=["+this.boolValue+"], floatV = ["+this.floatValue+"]";
1742 }
1743
1744 }
|