001: ///////////////////////////////
002: // Makumba, Makumba tag library
003: // Copyright (C) 2000-2003 http://www.makumba.org
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //
019: // -------------
020:
021: package test;
022:
023: import java.io.BufferedInputStream;
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.util.Calendar;
029: import java.util.Date;
030: import java.util.Dictionary;
031: import java.util.Hashtable;
032: import java.util.Properties;
033: import java.util.Vector;
034:
035: import junit.framework.Test;
036: import junit.framework.TestCase;
037: import junit.framework.TestResult;
038: import junit.framework.TestSuite;
039:
040: import org.makumba.Transaction;
041: import org.makumba.MakumbaSystem;
042: import org.makumba.Pointer;
043: import org.makumba.Text;
044: import org.makumba.commons.NamedResources;
045: import org.makumba.db.hibernate.HibernateTransactionProvider;
046: import org.makumba.providers.TransactionProvider;
047:
048: /**
049: * Testing table operations
050: *
051: * @author Cristian Bogdan
052: */
053: public class tableHibernate extends TestCase {
054:
055: static Transaction db;
056:
057: static long epsilon = 2000;
058:
059: public tableHibernate(String name) {
060: super (name);
061: }
062:
063: public static void main(String[] args) {
064: junit.textui.TestRunner.run(suite());
065: }
066:
067: public static Test suite() {
068: return new TestSuite(tableHibernate.class);
069: }
070:
071: public void setUp() {
072: tp = new TransactionProvider(new HibernateTransactionProvider());
073: db = tp
074: .getConnectionTo(tp
075: .getDataSourceName("test/testHibernateDatabase.properties"));
076: }
077:
078: public void tearDown() {
079: db.close();
080: }
081:
082: private TransactionProvider tp;
083:
084: static Pointer ptr, ptr1;
085:
086: static Pointer fptr1, fptr2, fptr;
087:
088: static Date create;
089:
090: static String[] personFields = { "TS_modify", "TS_create",
091: "extraData", "birthdate" };
092:
093: static String[] ptrOneFields = { "something" };
094:
095: static String[] subsetFields = { "description" };
096:
097: static Dictionary pc, pc1;
098:
099: static Date now;
100:
101: static Pointer ptrOne;
102:
103: static Pointer set1, set2;
104:
105: String readPerson = "SELECT p.indiv.name AS name, p.indiv.surname AS surname, p.birthdate AS birthdate, p.TS_modify as TS_modify, p.TS_create as TS_create, p.extraData.something as something, p.extraData.id as extraData FROM test.Person p WHERE p.id= ?";
106:
107: String readPerson1 = "SELECT p.indiv.name AS name, p.indiv.surname AS surname, p.birthdate AS birthdate, p.weight as weight, p.TS_modify as TS_modify, p.TS_create as TS_create, p.extraData.something as something, p.extraData.id as extraData, p.comment as comment, p.picture AS picture FROM test.Person p WHERE p.id = ?";
108:
109: String readPerson2 = "SELECT p.indiv.name AS name, p.indiv.surname AS surname, p.birthdate AS birthdate, p.weight as weight, p.brother.id as brother, p.TS_modify as TS_modify, p.TS_create as TS_create, p.extraData.something as something, p.extraData.id as extraData, p.comment as comment, p.picture AS picture FROM test.Person p WHERE p.id= ?";
110:
111: String readIntSet = "SELECT i.enum_ as member FROM test.Person p JOIN p.intSet i WHERE p.id=? ORDER BY i.enum_";
112:
113: String readCharSet = "SELECT c.enum_ as member FROM test.Person p JOIN p.charSet c WHERE p.id=? ORDER BY c.enum_";
114:
115: static InputStream getExampleData() {
116: try {
117: return new BufferedInputStream(new FileInputStream(
118: "lib/antlr.jar".replace('/', File.separatorChar)));
119: } catch (IOException e) {
120: e.printStackTrace();
121: return null;
122: }
123: }
124:
125: /*public void testQueryValidMdds() {
126: Vector v = org.makumba.MakumbaSystem.mddsInDirectory("test/validMdds");
127: Vector errors = new Vector();
128: for (int i = 0; i < v.size(); i++) {
129: try {
130: Vector v1 = db.executeQuery("SELECT t.id FROM test.validMdds."
131: + (String) v.elementAt(i) + " t", null);
132: Vector fields = ddp.getDataDefinition(
133: "test.validMdds." + (String) v.elementAt(i))
134: .getFieldNames();
135: String what = "";
136: for (Enumeration e = fields.elements(); e.hasMoreElements();) {
137: String fname = (String) e.nextElement();
138: String ftype = ddp.getDataDefinition(
139: "test.validMdds." + (String) v.elementAt(i))
140: .getFieldDefinition(fname).getDataType();
141: // System.out.println(fname+": "+ftype);
142: if (ftype != null && !ftype.equals("null")
143: && !ftype.startsWith("set")) // skip setComplex
144: // fields
145: what = what + (what.length() > 0 ? ", " : "") + "t."
146: + fname;
147: }
148: // System.out.println(what);
149: if (what.length() > 0)
150: v1 = db.executeQuery("SELECT " + what
151: + " FROM test.validMdds." + (String) v.elementAt(i)
152: + " t", null);
153: } catch (Exception e) {
154: errors.add("\n ." + (errors.size() + 1)
155: + ") Error querying valid MDD <"
156: + (String) v.elementAt(i) + ">:\n\t " + e);
157: }
158: }
159: if (errors.size() > 0)
160: fail("\n Tested " + v.size() + " valid MDDs, of which "
161: + errors.size() + " cant be used for DB queries:"
162: + errors.toString());
163: }*/
164:
165: public void testInsert() {
166: Properties p = new Properties();
167: Calendar c = Calendar.getInstance();
168: c.clear();
169: c.set(1977, 2, 5);
170: Date birth = c.getTime();
171:
172: Text comment = new Text("SomeComment");
173:
174: p.put("birthdate", birth);
175: p.put("comment", comment);
176: p.put("picture", new Text(getExampleData()));
177:
178: p.put("weight", new Double(85.7d));
179:
180: p.put("indiv.name", "john");
181: p.put("indiv.surname", "doe");
182: p.put("extraData.something", "else");
183:
184: Vector setintElem = new Vector();
185: setintElem.addElement(new Integer(1));
186: setintElem.addElement(new Integer(0));
187:
188: Vector setcharElem = new Vector();
189: setcharElem.addElement("f");
190: setcharElem.addElement("e");
191:
192: p.put("intSet", setintElem);
193: p.put("charSet", setcharElem);
194:
195: ptr = db.insert("test.Person", p);
196: assertNotNull(ptr);
197: assertEquals(ptr.getType(), "test.Person");
198:
199: now = new Date();
200:
201: Vector v = db.executeQuery(readPerson1, ptr);
202:
203: assertEquals(1, v.size());
204:
205: pc = (Dictionary) v.elementAt(0);
206:
207: create = (Date) pc.get("TS_create");
208: ptrOne = (Pointer) pc.get("extraData");
209: assertEquals("Name", "john", pc.get("name"));
210: assertEquals("Surname", "doe", pc.get("surname"));
211: assertEquals("Weight(real)", new Double(85.7d), pc
212: .get("weight"));
213: assertEquals("Birthdate", birth, pc.get("birthdate"));
214: assertEquals("Something else", "else", pc.get("something"));
215: assertEquals("Comment text", pc.get("comment").toString(),
216: comment.toString());
217: assertEquals("Picture", pc.get("picture"), new Text(
218: getExampleData()));
219: assertNotNull(ptrOne);
220:
221: v = db.executeQuery(readIntSet, ptr);
222: assertEquals(2, v.size());
223: assertEquals(new Integer(0), ((Dictionary) v.elementAt(0))
224: .get("member"));
225: assertEquals(new Integer(1), ((Dictionary) v.elementAt(1))
226: .get("member"));
227:
228: v = db.executeQuery(readCharSet, ptr);
229: assertEquals(v.size(), 2);
230: assertEquals("e", ((Dictionary) v.elementAt(0)).get("member"));
231: assertEquals("f", ((Dictionary) v.elementAt(1)).get("member"));
232:
233: assertEquals(create, pc.get("TS_modify"));
234: assertTrue(now.getTime() - create.getTime() < 3 * epsilon);
235:
236: }
237:
238: public void testForeignKeys() {
239: //FIXME assertTrue(org.makumba.db.sql.Database.supportsForeignKeys());
240:
241: // try to delete brother = that ID
242: // try to delete the other brother
243:
244: // insert the first person
245: Properties p = new Properties();
246:
247: Text comment = new Text("Hello world!!!!");
248:
249: p.put("comment", comment);
250:
251: p.put("indiv.name", "john");
252: p.put("indiv.surname", "doe_1");
253: p.put("extraData.something", "else");
254:
255: fptr = db.insert("test.Person", p);
256:
257: // check if he got inserted
258: assertNotNull(fptr);
259: assertEquals(fptr.getType(), "test.Person");
260:
261: Vector v = db.executeQuery(readPerson2, fptr);
262: //System.out.println(v.size());
263: assertEquals(1, v.size());
264:
265: // insert the second person (brother)
266: p = new Properties();
267:
268: comment = new Text("SomeComment");
269:
270: p.put("comment", comment);
271: p.put("brother", fptr);
272: p.put("indiv.name", "john");
273: p.put("indiv.surname", "doe_2");
274: p.put("extraData.something", "else");
275:
276: fptr1 = db.insert("test.Person", p);
277: assertNotNull(fptr1);
278: assertEquals(fptr.getType(), "test.Person");
279:
280: // check if it links to the first one correctly
281: v = db.executeQuery(readPerson2, fptr1);
282:
283: assertEquals(1, v.size());
284:
285: pc = (Dictionary) v.elementAt(0);
286:
287: fptr2 = (Pointer) pc.get("brother");
288: assertNotNull(fptr2);
289: assertEquals("Brother", fptr2, fptr);
290:
291: // try to delete the first guy (who was set as a brother. should fail)
292: try {
293: db.delete(fptr);
294: // we could delete him... the foreign keys don't work
295: assertTrue(false);
296: } catch (org.makumba.DBError e) {
297: }
298:
299: // try to delete the second guy
300: db.delete(fptr1);
301:
302: // delete the first guy again, this time he shouldn't be linked to from anywhere
303: db.delete(fptr);
304:
305: }
306:
307: static String subsetQuery = "SELECT a.description, a.id, a.description, a.sth.aaa FROM test.Person p JOIN p.address a WHERE p.id=? ORDER BY a.description";
308:
309: public void testSetInsert() {
310: Dictionary p = new Hashtable();
311: p.put("description", "home");
312: p.put("sth.aaa", "bbb");
313:
314: set1 = db.insert(ptr, "address", p);
315:
316: assertNotNull(set1);
317: Vector v = db.executeQuery(subsetQuery, ptr);
318: assertEquals(1, v.size());
319: assertEquals("home", ((Dictionary) v.elementAt(0)).get("col1"));
320: assertEquals(set1, ((Dictionary) v.elementAt(0)).get("col2"));
321: assertEquals("home", ((Dictionary) v.elementAt(0)).get("col3"));
322: assertEquals("bbb", ((Dictionary) v.elementAt(0)).get("col4"));
323:
324: p.put("description", "away");
325:
326: set2 = db.insert(ptr, "address", p);
327: assertNotNull(set2);
328: assertEquals("away", db.read(set2, subsetFields).get(
329: "description"));
330: v = db.executeQuery(subsetQuery, ptr);
331: assertEquals(2, v.size());
332: assertEquals("away", ((Dictionary) v.elementAt(0)).get("col1"));
333: assertEquals(set2, ((Dictionary) v.elementAt(0)).get("col2"));
334: assertEquals("home", ((Dictionary) v.elementAt(1)).get("col1"));
335: assertEquals(set1, ((Dictionary) v.elementAt(1)).get("col2"));
336: }
337:
338: public void testSetMemberUpdate() {
339: Dictionary p = new Hashtable();
340: p.put("description", "somewhere");
341:
342: db.update(set2, p);
343:
344: Vector v = db.executeQuery(subsetQuery, ptr);
345:
346: assertEquals("somewhere", db.read(set2, subsetFields).get(
347: "description"));
348: v = db.executeQuery(subsetQuery, ptr);
349: assertEquals(v.size(), 2);
350: assertEquals("home", ((Dictionary) v.elementAt(0)).get("col1"));
351: assertEquals(set1, ((Dictionary) v.elementAt(0)).get("col2"));
352: assertEquals("somewhere", ((Dictionary) v.elementAt(1))
353: .get("col1"));
354: assertEquals(set2, ((Dictionary) v.elementAt(1)).get("col2"));
355: }
356:
357: public void testSetMemberDelete() {
358: db.delete(set1);
359: assertNull(db.read(set1, subsetFields));
360: Vector v = db.executeQuery(subsetQuery, ptr);
361: assertEquals(1, v.size());
362: assertEquals("somewhere", ((Dictionary) v.elementAt(0))
363: .get("col1"));
364: assertEquals(set2, ((Dictionary) v.elementAt(0)).get("col2"));
365:
366: // we put it back
367: Dictionary p = new Hashtable();
368: p.put("description", "home");
369:
370: set1 = db.insert(ptr, "address", p);
371: }
372:
373: public void testSubrecordUpdate() {
374: Dictionary p = new Hashtable();
375: p.put("something", "else2");
376:
377: db.update(ptrOne, p);
378:
379: Dictionary d = db.read(ptr, personFields);
380: assertNotNull(d);
381: assertEquals(ptrOne, d.get("extraData"));
382:
383: d = db.read(ptrOne, ptrOneFields);
384: assertNotNull(d);
385: assertEquals("else2", d.get("something"));
386: }
387:
388: static Object[][] languageData = { { "English", "en" },
389: { "French", "fr" }, { "German", "de" },
390: { "Italian", "it" }, { "Spanish", "sp" } };
391:
392: static String[] toInsert = { "German", "Italian" };
393:
394: static String langQuery = "SELECT l.id FROM test.Language l WHERE l.name=?";
395:
396: static String speaksQuery = "SELECT l.id as k, l.name as name FROM test.Person p JOIN p.speaks l WHERE p.id=?";
397:
398: static String checkSpeaksQuery = "SELECT l.id FROM test.Person s JOIN s.speaks l WHERE s.id=?";
399:
400: void workWithSet(String[] t) {
401: Vector v = new Vector();
402: for (int i = 0; i < t.length; i++) {
403: Vector getLanguagesFromDb = db
404: .executeQuery(langQuery, t[i]);
405: Dictionary resultDic = (Dictionary) (getLanguagesFromDb
406: .elementAt(0));
407: v.addElement(resultDic.get("col1"));
408: }
409:
410: Hashtable dt = new Hashtable();
411: dt.put("speaks", v);
412: db.update(ptr, dt);
413:
414: Vector result = db.executeQuery(speaksQuery, ptr);
415: Vector result1 = db.executeQuery(checkSpeaksQuery, ptr);
416:
417: assertEquals(t.length, result.size());
418: assertEquals(t.length, result1.size());
419:
420: for (int i = 0; i < t.length; i++) {
421: for (int j = 0; j < result.size(); j++) {
422: Dictionary d = (Dictionary) result.elementAt(j);
423: if (d.get("name").equals(t[i])) {
424: for (int k = 0; j < result1.size(); k++)
425: if (((Dictionary) result1.elementAt(k)).get(
426: "col1").equals(d.get("k"))) {
427: result1.removeElementAt(k);
428: break;
429: }
430: result.removeElementAt(j);
431: break;
432: }
433: }
434: }
435: assertEquals(0, result.size());
436: assertEquals(0, result1.size());
437: }
438:
439: public void testSetUpdate() {
440: Dictionary p = new Hashtable();
441: if (db.executeQuery("SELECT l.id FROM test.Language l", null)
442: .size() == 0)
443: for (int i = 0; i < languageData.length; i++) {
444: p.put("name", languageData[i][0]);
445: p.put("isoCode", languageData[i][1]);
446: Pointer italian = db.insert("test.Language", p);
447: }
448: p = new Hashtable();
449:
450: workWithSet(toInsert);
451: }
452:
453: static String[] toInsert2 = { "English", "Italian", "French" };
454:
455: public void testSetUpdate2() {
456: workWithSet(toInsert2);
457: }
458:
459: static String[] toInsert3 = { "English", "German", "French" };
460:
461: public void testSetDelete() {
462: Dictionary p = new Hashtable();
463:
464: Hashtable dt = new Hashtable();
465: dt.put("speaks", new Vector());
466:
467: db.update(ptr, dt);
468: Vector result = db.executeQuery(speaksQuery, ptr);
469: assertEquals(0, result.size());
470:
471: assertEquals(
472: 0,
473: db
474: .executeQuery(
475: "SELECT l.id FROM test.Person p JOIN p.speaks l WHERE p.id=?",
476: ptr).size());
477: workWithSet(toInsert3);
478: //db.delete("test.Language l", "1=1", null);
479: //delete garbage
480:
481: }
482:
483: public void testPtrOneDelete() {
484: db.delete(ptrOne);
485:
486: Dictionary d = db.read(ptr, personFields);
487: assertNotNull(d);
488: assertNull(d.get("extraData"));
489:
490: assertNull(db.read(ptrOne, ptrOneFields));
491: }
492:
493: public void testPtrOneReInsert() {
494: Dictionary p = new Hashtable();
495: p.put("extraData.something", "else2");
496: db.update(ptr, p);
497: Dictionary d = db.read(ptr, personFields);
498: ptrOne = (Pointer) d.get("extraData");
499: assertNotNull(ptrOne);
500: Dictionary read;
501: assertNotNull(read = db.read(ptrOne, ptrOneFields));
502: assertEquals("else2", read.get("something"));
503: }
504:
505: public void testUpdate() {
506: Properties pmod = new Properties();
507: String val = "A completely new guy";
508: pmod.put("indiv.name", val);
509:
510: Vector setintElem = new Vector();
511: setintElem.addElement(new Integer(2));
512:
513: Vector setcharElem = new Vector();
514: setcharElem.addElement("d");
515:
516: pmod.put("intSet", setintElem);
517: pmod.put("charSet", setcharElem);
518:
519: db.update(ptr, pmod);
520:
521: now = new Date();
522: Vector v = db.executeQuery(readPerson, ptr);
523: assertEquals(1, v.size());
524:
525: Dictionary modc = (Dictionary) v.elementAt(0);
526:
527: assertNotNull(modc);
528: create = (Date) modc.get("TS_create");
529: assertEquals(val, modc.get("name"));
530: assertEquals("doe", modc.get("surname"));
531: assertTrue(now.getTime()
532: - ((Date) modc.get("TS_modify")).getTime() < epsilon);
533: assertNotNull(db.read(ptrOne, ptrOneFields));
534:
535: v = db.executeQuery(readIntSet, ptr);
536: assertEquals(1, v.size());
537: assertEquals(new Integer(2), ((Dictionary) v.elementAt(0))
538: .get("member"));
539:
540: v = db.executeQuery(readCharSet, ptr);
541: assertEquals(1, v.size());
542: assertEquals("d", ((Dictionary) v.elementAt(0)).get("member"));
543: }
544:
545: public void testDelete() {
546: Hashtable dt = new Hashtable();
547: dt.put("speaks", new Vector());
548:
549: db.delete(ptr);
550:
551: assertNull(db.read(ptr, personFields));
552: assertNull(db.read(ptrOne, ptrOneFields));
553: assertEquals(0, db.executeQuery(subsetQuery, ptr).size());
554: assertNull(db.read(set1, subsetFields));
555: assertNull(db.read(set2, subsetFields));
556: assertEquals(0, db.executeQuery(speaksQuery, ptr).size());
557: assertEquals(0, db.executeQuery(readIntSet, ptr).size());
558: assertEquals(0, db.executeQuery(readCharSet, ptr).size());
559: assertEquals(
560: 0,
561: db
562: .executeQuery(
563: "SELECT l.id FROM test.Person p JOIN p.speaks l WHERE p.id=?",
564: ptr).size());
565: assertEquals(
566: 0,
567: db
568: .executeQuery(
569: "SELECT l.enum_ FROM test.Person p JOIN p.intSet l WHERE p.id=?",
570: ptr).size());
571: assertEquals(
572: 0,
573: db
574: .executeQuery(
575: "SELECT l.enum_ FROM test.Person p JOIN p.charSet l WHERE p.id=?",
576: ptr).size());
577:
578: /* delete all entries, bug 673:
579: db
580: .delete("test.validMdds.CharWithLength name",
581: "name.name='bla'", null);
582: db.delete("test.validMdds.CharWithLength t", "5=5", null);
583: db.delete("test.validMdds.CharWithLength t", "t.name LIKE \"www\"",
584: null);
585: db.delete("test.validMdds.CharWithLength bla", "'x'=bla.name", null);
586: */
587: }
588:
589: public void testCopy() {
590: Properties p = new Properties();
591: p.put("birthdate", new java.util.GregorianCalendar(1977, 7, 7)
592: .getTime());
593: p.put("indiv.name", "john");
594: p.put("indiv.surname", "Copy");
595: p.put("extraData.something", "else");
596:
597: Calendar c = Calendar.getInstance();
598: c.clear();
599: c.set(1976, 2, 9);
600:
601: Date cr = c.getTime();
602: p.put("TS_create", cr);
603:
604: c.clear();
605: c.set(1976, 2, 10);
606:
607: Date mod = c.getTime();
608: p.put("TS_modify", mod);
609:
610: ptr1 = db.insert("test.Person", p);
611: assertNotNull(ptr1);
612:
613: now = new Date();
614: Vector v = db.executeQuery(readPerson, ptr1);
615: assertEquals(1, v.size());
616:
617: pc1 = (Dictionary) v.elementAt(0);
618: assertNotNull(pc1);
619:
620: assertEquals("john", pc1.get("name"));
621: assertEquals("Copy", pc1.get("surname"));
622: assertEquals(cr, new Date(((Date) pc1.get("TS_create"))
623: .getTime()));
624: assertEquals(mod, new Date(((Date) pc1.get("TS_modify"))
625: .getTime()));
626: db.delete(ptr1);
627: db.delete("test.Individual i", "1=1", null);
628: db.delete("test.Language l", "1=1", null);
629: }
630:
631: public void run(TestResult r) {
632: try {
633: super .run(r);
634:
635: } catch (Throwable t) {
636: t.printStackTrace();
637: }
638:
639: /* very shitty solution, more JUnit should be studied for a better one...
640: * we want to find out whether we just finished the last test
641: * if yes, we do cleanup
642: */
643: if (toString().equals("testCopy(test.table)")) {
644: String nm = tp
645: .getDataSourceName("test/testDatabase.properties");
646:
647: System.out.println("\nworked with: "
648: + MakumbaSystem.getDatabaseProperty(nm,
649: "sql_engine.name")
650: + " version: "
651: + MakumbaSystem.getDatabaseProperty(nm,
652: "sql_engine.version")
653: + "\njdbc driver: "
654: + MakumbaSystem.getDatabaseProperty(nm,
655: "jdbc_driver.name")
656: + " version: "
657: + MakumbaSystem.getDatabaseProperty(nm,
658: "jdbc_driver.version")
659: + "\njdbc connections allocated: "
660: + MakumbaSystem.getDatabaseProperty(nm,
661: "jdbc_connections") + "\ncaches: "
662: + org.makumba.commons.NamedResources.getCacheInfo()
663:
664: );
665: java.util.logging.Logger.getLogger(
666: "org.makumba." + "system").info(
667: "destroying makumba caches");
668: NamedResources.cleanup();
669: }
670: }
671: }
|