001: // $Id: TestSimple.java 16 2008-02-15 06:43:35Z jcamaia $
002:
003: package net.sf.persist.tests.common;
004:
005: import static org.junit.Assert.assertEquals;
006: import static org.junit.Assert.assertNotNull;
007: import static org.junit.Assert.assertNull;
008: import static org.junit.Assert.assertTrue;
009: import static org.junit.Assert.fail;
010:
011: import java.sql.Connection;
012: import java.sql.SQLException;
013: import java.util.ArrayList;
014: import java.util.HashSet;
015: import java.util.Iterator;
016: import java.util.LinkedHashSet;
017: import java.util.List;
018: import java.util.Map;
019: import java.util.Set;
020:
021: import net.sf.persist.DefaultNameGuesser;
022: import net.sf.persist.Persist;
023: import net.sf.persist.PersistException;
024: import net.sf.persist.Result;
025: import net.sf.persist.TableMapping;
026: import net.sf.persist.tests.framework.ConnectionHelper;
027: import net.sf.persist.tests.framework.DynamicBean;
028:
029: import org.apache.log4j.Logger;
030: import org.junit.After;
031: import org.junit.Before;
032: import org.junit.Test;
033:
034: public abstract class TestSimple {
035:
036: protected Connection connection = null;
037: protected Persist persist = null;
038:
039: /**
040: * Must be implemented in subclasses to return the path for the database properties file
041: */
042: public abstract String getProperties();
043:
044: @Before
045: public void setUp() throws SQLException {
046: String properties = getProperties();
047: this .connection = ConnectionHelper.getConnection(properties);
048: this .connection.createStatement().execute("delete from simple");
049: this .persist = new Persist(connection);
050: }
051:
052: @After
053: public void tearDown() throws SQLException {
054: this .connection.close();
055: }
056:
057: public static Simple buildSimple() {
058: Simple simple = new Simple();
059: simple.setIntCol(DynamicBean
060: .randomInt(0, Integer.MAX_VALUE / 2));
061: simple.setStringCol(DynamicBean.randomString(255));
062: return simple;
063: }
064:
065: @Test
066: public void testNoTable() {
067: Simple simple = buildSimple();
068: persist.insert(simple);
069:
070: SimpleNoTable simpleNoTable = persist.read(SimpleNoTable.class,
071: "select * from simple");
072: assertEquals(simple.getIntCol(), simpleNoTable.getIntCol());
073: assertEquals(simple.getStringCol(), simpleNoTable
074: .getStringCol());
075: }
076:
077: @Test
078: public void testCacheName() {
079: Persist persist1 = persist;
080: Persist persist2 = new Persist("my cache", connection);
081:
082: // 2 objects coming from mappings stored in different caches should be equal
083: Simple simple1 = buildSimple();
084: persist1.insert(simple1);
085: Simple simple2 = (Simple) persist2.readList(Simple.class)
086: .get(0);
087:
088: // will not assert ids are equal, since isAutoUpdateGeneratedKeys()==false
089: assertEquals(simple1, simple2);
090:
091: // 2 mappings coming from different caches should be different
092: TableMapping m1 = (TableMapping) persist1
093: .getMapping(Simple.class);
094: TableMapping m2 = (TableMapping) persist2
095: .getMapping(Simple.class);
096: assertTrue(m1 != m2);
097:
098: // make sure that null will return the default cache
099: Persist persist1a = new Persist(null, connection);
100: TableMapping m1a = (TableMapping) persist1a
101: .getMapping(Simple.class);
102: assertEquals(true, m1 == m1a);
103: }
104:
105: @Test
106: public void testExecuteUpdate() {
107:
108: // some data to insert
109: int intCol = DynamicBean.randomInt(0, Integer.MAX_VALUE / 2);
110: String stringCol = DynamicBean.randomString(255);
111:
112: // insert and check count of rows returned
113: int n = persist.executeUpdate(
114: "insert into simple (int_col, string_col) values(?,?)",
115: intCol, stringCol);
116: assertEquals(1, n);
117:
118: // read object and compare with inserted data
119: Simple simpleRead = persist
120: .read(
121: Simple.class,
122: "select * from simple where int_col=? and string_col=?",
123: intCol, stringCol);
124:
125: assertNotNull(simpleRead);
126: assertEquals(intCol, simpleRead.getIntCol());
127: assertEquals(stringCol, simpleRead.getStringCol());
128:
129: // delete object and check it was removed
130: persist.delete(simpleRead);
131: simpleRead = persist
132: .read(
133: Simple.class,
134: "select * from simple where int_col=? and string_col=?",
135: intCol, stringCol);
136: assertNull(simpleRead);
137: }
138:
139: @Test
140: public void testExecuteUpdateAutoGeneratedKeys() {
141:
142: TableMapping mapping = (TableMapping) persist
143: .getMapping(Simple.class);
144: if (mapping.supportsGetGeneratedKeys()) {
145:
146: // some data to insert
147: int intCol = DynamicBean
148: .randomInt(0, Integer.MAX_VALUE / 2);
149: String stringCol = DynamicBean.randomString(255);
150:
151: // insert with explicit auto generated keys and check result object data
152: String[] autoGeneratedKeys = new String[] { "id" };
153: Result result = persist
154: .executeUpdate(
155: Simple.class,
156: "insert into simple (int_col,string_col) values(?,?)",
157: autoGeneratedKeys, intCol, stringCol);
158: assertEquals(1, result.getGeneratedKeys().size());
159: assertEquals(1, result.getRowsModified());
160:
161: // read object and compare with inserted data
162: Simple simpleRead = persist
163: .read(
164: Simple.class,
165: "select * from simple where int_col=? and string_col=?",
166: intCol, stringCol);
167: assertNotNull(simpleRead);
168: assertEquals(intCol, simpleRead.getIntCol());
169: assertEquals(stringCol, simpleRead.getStringCol());
170:
171: // delete object and check it was removed
172: persist.delete(simpleRead);
173: simpleRead = persist
174: .read(
175: Simple.class,
176: "select * from simple where int_col=? and string_col=?",
177: intCol, stringCol);
178: assertNull(simpleRead);
179: } else {
180: Logger
181: .getLogger(this .getClass())
182: .debug(
183: "This database does not support retrieval of auto generated keys");
184: }
185:
186: }
187:
188: @Test
189: public void testSetAutoGeneratedKeys() {
190:
191: TableMapping mapping = (TableMapping) persist
192: .getMapping(Simple.class);
193: if (mapping.supportsGetGeneratedKeys()) {
194:
195: // insert object with setUpdateAutoGeneratedKeys option
196: Simple simpleInsert = TestSimple.buildSimple();
197: simpleInsert.setId(0);
198: persist.setUpdateAutoGeneratedKeys(true);
199: persist.insert(simpleInsert);
200: assertTrue(0 != simpleInsert.getId());
201:
202: int id = persist.read(int.class, "select id from simple");
203: assertEquals(id, simpleInsert.getId());
204:
205: // read object using primary key (auto generated)
206: Simple simpleRead = persist.read(Simple.class,
207: "select * from simple where id=?", simpleInsert
208: .getId());
209: assertEquals(simpleInsert, simpleRead);
210:
211: // delete object by primary key and check it was removed
212: persist.delete(simpleRead);
213: simpleRead = persist.readByPrimaryKey(Simple.class,
214: simpleRead.getId());
215: assertNull(simpleRead);
216:
217: } else {
218: Logger
219: .getLogger(this .getClass())
220: .debug(
221: "This database does not support retrieval of auto generated keys");
222: }
223: }
224:
225: @Test
226: public void testReturnNativeTypes() {
227:
228: Simple simple = buildSimple();
229: persist.insert(simple);
230: int intCol = persist.read(int.class,
231: "select int_col from simple");
232: String stringCol = persist.read(String.class,
233: "select string_col from simple");
234:
235: assertEquals(simple.getIntCol(), intCol);
236: assertEquals(simple.getStringCol(), stringCol);
237: }
238:
239: @Test
240: public void testBatch() {
241:
242: Simple simple1 = buildSimple();
243: Simple simple2 = buildSimple();
244: Simple simple3 = buildSimple();
245: persist.insertBatch(simple1, simple2, simple3);
246:
247: List<Simple> list = persist.readList(Simple.class);
248: List<Simple> s = new ArrayList();
249: s.add(simple1);
250: s.add(simple2);
251: s.add(simple3);
252: assertTrue(s.containsAll(list));
253:
254: Simple s1 = list.get(0);
255: Simple s2 = list.get(1);
256: Simple s3 = list.get(2);
257:
258: s1.setIntCol(simple1.getIntCol() + 1);
259: s1.setStringCol(simple1.getStringCol().toUpperCase());
260: s2.setIntCol(simple2.getIntCol() + 1);
261: s2.setStringCol(simple2.getStringCol().toUpperCase());
262: s3.setIntCol(simple3.getIntCol() + 1);
263: s3.setStringCol(simple3.getStringCol().toUpperCase());
264:
265: s = new ArrayList();
266: s.add(s1);
267: s.add(s2);
268: s.add(s3);
269:
270: persist.updateBatch(s1, s2, s3);
271: list = persist.readList(Simple.class);
272: assertTrue(s.containsAll(list));
273:
274: persist.deleteBatch(s1, s2, s3);
275: list = persist.readList(Simple.class);
276: assertEquals(0, list.size());
277: }
278:
279: @Test
280: public void testObject() {
281:
282: Simple simpleInsert = buildSimple();
283: persist.insert(simpleInsert);
284:
285: int id = persist.read(int.class, "select id from simple");
286:
287: Simple simpleUpdate = persist
288: .readByPrimaryKey(Simple.class, id);
289: assertEquals(simpleInsert, simpleUpdate);
290:
291: simpleUpdate.setIntCol(DynamicBean.randomInt(0,
292: Integer.MAX_VALUE / 2));
293: simpleUpdate.setStringCol(DynamicBean.randomString(255));
294: persist.update(simpleUpdate);
295:
296: Simple simpleRead = persist.readByPrimaryKey(Simple.class,
297: simpleUpdate.getId());
298: assertEquals(simpleUpdate, simpleRead);
299:
300: persist.delete(simpleRead);
301:
302: Simple simpleDeleted = persist.readByPrimaryKey(Simple.class,
303: simpleRead.getId());
304: assertNull(simpleDeleted);
305: }
306:
307: @Test
308: public void testObjectList() {
309:
310: Simple simple1 = buildSimple();
311: Simple simple2 = buildSimple();
312: Simple simple3 = buildSimple();
313: persist.insert(simple1);
314: persist.insert(simple2);
315: persist.insert(simple3);
316:
317: List<Simple> list = persist.readList(Simple.class);
318: List s = new ArrayList();
319: s.add(simple1);
320: s.add(simple2);
321: s.add(simple3);
322: assertTrue(s.containsAll(list));
323:
324: persist.delete(list.get(0));
325: persist.delete(list.get(1));
326: persist.delete(list.get(2));
327: List<Simple> listDeleted = persist.readList(Simple.class);
328: assertTrue(0 == listDeleted.size());
329: }
330:
331: @Test
332: public void testObjectIterator() {
333:
334: Simple simple1 = buildSimple();
335: Simple simple2 = buildSimple();
336: Simple simple3 = buildSimple();
337: persist.insertBatch(simple1, simple2, simple3);
338: List s = new ArrayList();
339: s.add(simple1);
340: s.add(simple2);
341: s.add(simple3);
342:
343: Iterator<Simple> i = persist.readIterator(Simple.class);
344: List si = new ArrayList();
345: while (i.hasNext()) {
346: si.add(i.next());
347: }
348:
349: assertTrue(s.containsAll(si));
350: }
351:
352: @Test
353: public void testMap() {
354:
355: Simple simple = buildSimple();
356: persist.insert(simple);
357:
358: int id = persist.read(int.class, "select id from simple");
359:
360: Map<String, Object> simpleMap1 = persist.readMap(
361: "select * from simple where id=?", id);
362: assertEquals(id, simpleMap1.get("id"));
363: assertEquals(simple.getIntCol(), simpleMap1.get("int_col"));
364: assertEquals(simple.getStringCol(), simpleMap1
365: .get("string_col"));
366:
367: persist.delete(simple);
368: }
369:
370: @Test
371: public void testMapList() {
372:
373: Simple simple1 = buildSimple();
374: Simple simple2 = buildSimple();
375: Simple simple3 = buildSimple();
376:
377: persist.insertBatch(simple1, simple2, simple3);
378:
379: // tests using setAutoUpdateGeneratedKeys do not belong here
380: List<Integer> ids = persist.readList(int.class,
381: "select id from simple order by id");
382: simple1.setId(ids.get(0));
383: simple2.setId(ids.get(1));
384: simple3.setId(ids.get(2));
385:
386: List<Map<String, Object>> simpleList = persist.readMapList(
387: "select * from simple where id in (?,?,?)", simple1
388: .getId(), simple2.getId(), simple3.getId());
389: assertEquals(simple1.getId(), simpleList.get(0).get("id"));
390: assertEquals(simple1.getIntCol(), simpleList.get(0).get(
391: "int_col"));
392: assertEquals(simple1.getStringCol(), simpleList.get(0).get(
393: "string_col"));
394:
395: assertEquals(simple2.getId(), simpleList.get(1).get("id"));
396: assertEquals(simple2.getIntCol(), simpleList.get(1).get(
397: "int_col"));
398: assertEquals(simple2.getStringCol(), simpleList.get(1).get(
399: "string_col"));
400:
401: assertEquals(simple3.getId(), simpleList.get(2).get("id"));
402: assertEquals(simple3.getIntCol(), simpleList.get(2).get(
403: "int_col"));
404: assertEquals(simple3.getStringCol(), simpleList.get(2).get(
405: "string_col"));
406:
407: persist.delete(simple1);
408: persist.delete(simple2);
409: persist.delete(simple3);
410: }
411:
412: @Test
413: public void testMapIterator() {
414:
415: Simple simple1 = buildSimple();
416: Simple simple2 = buildSimple();
417: Simple simple3 = buildSimple();
418: persist.setUpdateAutoGeneratedKeys(false);
419: persist.insertBatch(simple1, simple2, simple3);
420: Simple[] simpleArray = new Simple[] { simple1, simple2, simple3 };
421:
422: Set<Simple> simpleSet = new HashSet();
423:
424: Iterator i = persist.readMapIterator("select * from simple");
425: while (i.hasNext()) {
426: Map<String, Object> m = (Map) i.next();
427: for (int n = 0; n < 3; n++) {
428: if (simpleArray[n].getIntCol() == ((Number) m
429: .get("int_col")).intValue()
430: && simpleArray[n].getStringCol().equals(
431: (String) m.get("string_col"))) {
432: simpleSet.add(simpleArray[n]);
433: }
434: }
435: }
436:
437: assertEquals(3, simpleSet.size());
438: }
439:
440: @Test
441: public void testMapping() {
442: Simple simple = buildSimple();
443: persist.insert(simple);
444:
445: // tests using setAutoUpdateGeneratedKeys do not belong here
446: int id = persist.read(int.class, "select id from simple");
447: simple.setId(id);
448:
449: // Simple01 specifies an invalid column name
450: try {
451: persist.readByPrimaryKey(Simple01.class, simple.getId());
452: fail("Object with invalid column name did not trigger exception");
453: } catch (PersistException e) {
454: assertEquals(
455: e.getMessage(),
456: "Field [intCol] from class [net.sf.persist.tests.common.Simple01] specifies column [hello_world] on table [simple] that does not exist in the database");
457: }
458:
459: // Simple02 specifies an invalid table
460: try {
461: persist.readByPrimaryKey(Simple02.class, simple.getId());
462: fail("Object with invalid table name did not trigger exception");
463: } catch (PersistException e) {
464: assertEquals(
465: e.getMessage(),
466: "Class [net.sf.persist.tests.common.Simple02] specifies table [hello_world] that does not exist in the database");
467: }
468:
469: // Simple03 lacks a field
470: try {
471: persist.readByPrimaryKey(Simple03.class, simple.getId());
472: fail("Object lacking field did not trigger exception");
473: } catch (PersistException e) {
474: assertEquals(
475: e.getMessage(),
476: "Column [int_col] from result set does not have a mapping to a field in [net.sf.persist.tests.common.Simple03]");
477: }
478:
479: // Simple04 has incompatible getter and setter
480: try {
481: persist.readByPrimaryKey(Simple04.class, simple.getId());
482: fail("Object with incompatible getter and setter did not trigger exception");
483: } catch (PersistException e) {
484: assertEquals(
485: e.getMessage(),
486: "Getter [public long net.sf.persist.tests.common.Simple04.getIntCol()] and setter [public void net.sf.persist.tests.common.Simple04.setIntCol(boolean)] have incompatible types");
487: }
488:
489: // Simple05 doesn't specify a table name and guessed names won't work
490: try {
491: persist.readByPrimaryKey(Simple05.class, simple.getId());
492: fail("Object with invalid table name did not trigger exception");
493: } catch (PersistException e) {
494: assertEquals(
495: e.getMessage(),
496: "Class [net.sf.persist.tests.common.Simple05] does not specify a table name through a Table annotation and no guessed table names [simple05, simple05s] exist in the database");
497: }
498:
499: // Simple06 has different annotations for getter and setter
500: try {
501: persist.readByPrimaryKey(Simple06.class, simple.getId());
502: fail("Object with different annotations for getter and setter did not trigger exception");
503: } catch (PersistException e) {
504: assertTrue(e
505: .getMessage()
506: .startsWith(
507: "Annotations for getter [public long net.sf.persist.tests.common.Simple06.getIntCol()] and setter [public void net.sf.persist.tests.common.Simple06.setIntCol(long)] have different annotations"));
508: }
509:
510: // Simple07 doesn't have a getter and setter for string_col
511: try {
512: persist.readByPrimaryKey(Simple07.class, simple.getId());
513: fail("Object without getter and setter did not trigger exception");
514: } catch (PersistException e) {
515: assertEquals(
516: e.getMessage(),
517: "Field [foo] from class [net.sf.persist.tests.common.Simple07] does not specify a column name through a Column annotation and no guessed column names [foo, foos] exist in the database. If this field is not supposed to be associated with the database, please annotate it with @NoColumn");
518: }
519:
520: // Simple08 has conflicting Column and NoColumn annotations
521: try {
522: persist.readByPrimaryKey(Simple08.class, simple.getId());
523: fail("Object with conflicting annotations did not trigger exception");
524: } catch (PersistException e) {
525: assertEquals(
526: e.getMessage(),
527: "Field [intCol] from class [net.sf.persist.tests.common.Simple08] has conflicting NoColumn and Column annotations");
528: }
529:
530: // Simple09 has getter which returns void
531: try {
532: persist.readByPrimaryKey(Simple09.class, simple.getId());
533: fail("Object with getter returning void did not trigger exception");
534: } catch (PersistException e) {
535: assertEquals(
536: e.getMessage(),
537: "Getter [public void net.sf.persist.tests.common.Simple09.getStringCol()] must have a return parameter");
538: }
539:
540: // Simple10 has setter with no parameters
541: try {
542: persist.readByPrimaryKey(Simple10.class, simple.getId());
543: fail("Object with setter having no parameters did not trigger exception");
544: } catch (PersistException e) {
545: assertEquals(
546: e.getMessage(),
547: "Setter [public void net.sf.persist.tests.common.Simple10.setStringCol()] should have a single parameter but has 0");
548: }
549:
550: }
551:
552: @Test
553: public void TestGuessColumn() {
554:
555: DefaultNameGuesser guesser = new DefaultNameGuesser();
556:
557: Set guessed = guesser.guessColumn("name");
558: Set expected = toSet(new String[] { "name", "names" });
559: assertEquals(expected, guessed);
560:
561: guessed = guesser.guessColumn("nameC");
562: expected = toSet(new String[] { "namec", "name_c", "namecs",
563: "name_cs" });
564: assertEquals(expected, guessed);
565:
566: guessed = guesser.guessColumn("nameCo");
567: expected = toSet(new String[] { "nameco", "name_co", "namecos",
568: "name_cos" });
569: assertEquals(expected, guessed);
570:
571: guessed = guesser.guessColumn("n");
572: expected = toSet(new String[] { "n", "ns" });
573: assertEquals(expected, guessed);
574:
575: guessed = guesser.guessColumn("nC");
576: expected = toSet(new String[] { "nc", "ncs", "n_c", "n_cs" });
577: assertEquals(expected, guessed);
578:
579: guessed = guesser.guessColumn("nCMP");
580: expected = toSet(new String[] { "n_c_m_p", "ncmp", "n_c_m_ps",
581: "ncmps", });
582: assertEquals(expected, guessed);
583: }
584:
585: private static Set toSet(String[] values) {
586: Set set = new LinkedHashSet();
587: for (String value : values) {
588: set.add(value);
589: }
590: return set;
591: }
592:
593: }
|