diff --git a/src/java/com/samskivert/depot/tests/KeyTest.java b/src/java/com/samskivert/depot/tests/KeyTest.java new file mode 100644 index 0000000..c0dfb56 --- /dev/null +++ b/src/java/com/samskivert/depot/tests/KeyTest.java @@ -0,0 +1,72 @@ +// +// $Id$ +// +// Depot library - a Java relational persistence library +// Copyright (C) 2006-2009 Michael Bayne and Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.depot.tests; + +import org.junit.Test; + +import com.samskivert.depot.Key; +import com.samskivert.depot.expression.ColumnExp; +import com.samskivert.depot.impl.DepotUtil; + +import static org.junit.Assert.*; + +/** + * Tests some super basic {@link Key} stuff. + */ +public class KeyTest +{ + @Test public void testSlowConstructor () + { + int species = 10, monkeyId = 15; + Key key = MonkeyRecord.getKey(species, monkeyId); + + // make sure that the arguments we passed in got assigned in the right positions + ColumnExp[] kfs = DepotUtil.getKeyFields(MonkeyRecord.class); + int kspecies = 0, kmonkeyId = 0; + Comparable[] values = new Comparable[kfs.length]; + for (int ii = 0; ii < kfs.length; ii++) { + if (MonkeyRecord.SPECIES.equals(kfs[ii])) { + kspecies = (Integer)(key.getValues()[ii]); + } else if (MonkeyRecord.MONKEY_ID.equals(kfs[ii])) { + kmonkeyId = (Integer)(key.getValues()[ii]); + } + } + assertEquals(species, kspecies); + assertEquals(monkeyId, kmonkeyId); + } + + @Test public void testFastConstructor () + { + int recordId = 10; + Key key = TestRecord.getKey(recordId); + + // make sure that the arguments we passed in got assigned in the right positions + ColumnExp[] kfs = DepotUtil.getKeyFields(TestRecord.class); + int krecordId = 0; + Comparable[] values = new Comparable[kfs.length]; + for (int ii = 0; ii < kfs.length; ii++) { + if (TestRecord.RECORD_ID.equals(kfs[ii])) { + krecordId = (Integer)(key.getValues()[ii]); + } + } + assertEquals(recordId, krecordId); + } +} diff --git a/src/java/com/samskivert/depot/tests/MonkeyRecord.java b/src/java/com/samskivert/depot/tests/MonkeyRecord.java new file mode 100644 index 0000000..3c4e9fe --- /dev/null +++ b/src/java/com/samskivert/depot/tests/MonkeyRecord.java @@ -0,0 +1,61 @@ +// +// $Id$ +// +// Depot library - a Java relational persistence library +// Copyright (C) 2006-2009 Michael Bayne and Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.depot.tests; + +import com.samskivert.depot.Key; +import com.samskivert.depot.PersistentRecord; +import com.samskivert.depot.expression.ColumnExp; +import com.samskivert.depot.annotation.Id; + +/** + * Used for testing. + */ +public class MonkeyRecord extends PersistentRecord +{ + // AUTO-GENERATED: FIELDS START + public static final Class _R = MonkeyRecord.class; + public static final ColumnExp SPECIES = colexp(_R, "species"); + public static final ColumnExp MONKEY_ID = colexp(_R, "monkeyId"); + public static final ColumnExp NAME = colexp(_R, "name"); + // AUTO-GENERATED: FIELDS END + + /** This monkey's species. This is part of our key so that we have a composite key. */ + @Id public int species; + + /** This monkey's unique identifier. */ + @Id public int monkeyId; + + public String name; + + // AUTO-GENERATED: METHODS START + /** + * Create and return a primary {@link Key} to identify a {@link MonkeyRecord} + * with the supplied key values. + */ + public static Key getKey (int species, int monkeyId) + { + return new Key( + MonkeyRecord.class, + new ColumnExp[] { SPECIES, MONKEY_ID }, + new Comparable[] { species, monkeyId }); + } + // AUTO-GENERATED: METHODS END +}