From 52f432270deaa5267d0c90e5b838205f4f067d28 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 1 Dec 2010 19:53:03 +0000 Subject: [PATCH] Added a test for using an Enum as a record key, observed brokitude, fixed brokitude, but did not take the opportunity to do so in a non-hacky way. --- .../samskivert/depot/impl/BuildVisitor.java | 3 + .../com/samskivert/depot/EnumKeyRecord.java | 67 +++++++++++++++++++ .../java/com/samskivert/depot/KeyTest.java | 23 ++++++- .../com/samskivert/depot/TestRepository.java | 19 ++++++ 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/samskivert/depot/EnumKeyRecord.java diff --git a/src/main/java/com/samskivert/depot/impl/BuildVisitor.java b/src/main/java/com/samskivert/depot/impl/BuildVisitor.java index 7c9e10b..9ef6e86 100644 --- a/src/main/java/com/samskivert/depot/impl/BuildVisitor.java +++ b/src/main/java/com/samskivert/depot/impl/BuildVisitor.java @@ -964,6 +964,9 @@ public abstract class BuildVisitor implements FragmentVisitor if (value instanceof ByteEnum) { // byte enums require special conversion stmt.setByte(argIx, ((ByteEnum)value).toByte()); + } else if (value instanceof Enum) { + // enums are converted to strings + stmt.setString(argIx, ((Enum)value).name()); } else if (value instanceof int[]) { // int arrays require conversion to byte arrays int[] data = (int[])value; diff --git a/src/test/java/com/samskivert/depot/EnumKeyRecord.java b/src/test/java/com/samskivert/depot/EnumKeyRecord.java new file mode 100644 index 0000000..29644c2 --- /dev/null +++ b/src/test/java/com/samskivert/depot/EnumKeyRecord.java @@ -0,0 +1,67 @@ +// +// $Id$ +// +// Depot library - a Java relational persistence library +// Copyright (C) 2006-2010 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; + +import com.samskivert.depot.annotation.Id; +import com.samskivert.depot.expression.ColumnExp; + +/** + * Tests a record that uses an enum as its key. + */ +public class EnumKeyRecord extends PersistentRecord +{ + // AUTO-GENERATED: FIELDS START + public static final Class _R = EnumKeyRecord.class; + public static final ColumnExp TYPE = colexp(_R, "type"); + public static final ColumnExp NAME = colexp(_R, "name"); + // AUTO-GENERATED: FIELDS END + + public static final int SCHEMA_VERSION = 1; + + public enum Type { A, B, C, D }; + + /** The type is key. */ + @Id public Type type; + + public String name; + + public EnumKeyRecord () {} + + public EnumKeyRecord (Type type, String name) + { + this.type = type; + this.name = name; + } + + // AUTO-GENERATED: METHODS START + /** + * Create and return a primary {@link Key} to identify a {@link EnumKeyRecord} + * with the supplied key values. + */ + public static Key getKey (EnumKeyRecord.Type type) + { + return newKey(_R, type); + } + + /** Register the key fields in an order matching the getKey() factory. */ + static { registerKeyFields(TYPE); } + // AUTO-GENERATED: METHODS END +} diff --git a/src/test/java/com/samskivert/depot/KeyTest.java b/src/test/java/com/samskivert/depot/KeyTest.java index 993b6dc..d33cd60 100644 --- a/src/test/java/com/samskivert/depot/KeyTest.java +++ b/src/test/java/com/samskivert/depot/KeyTest.java @@ -20,6 +20,8 @@ package com.samskivert.depot; +import java.util.EnumSet; + import org.junit.Test; import static org.junit.Assert.*; @@ -29,7 +31,7 @@ import com.samskivert.depot.impl.DepotUtil; /** * Tests some super basic {@link Key} stuff. */ -public class KeyTest +public class KeyTest extends TestBase { @Test public void testSlowConstructor () { @@ -65,4 +67,23 @@ public class KeyTest } assertEquals(recordId, krecordId); } + + @Test public void testEnumKey () + { + EnumKeyRecord a = new EnumKeyRecord(EnumKeyRecord.Type.A, "ayyy"); + EnumKeyRecord b = new EnumKeyRecord(EnumKeyRecord.Type.B, "beee"); + EnumKeyRecord c = new EnumKeyRecord(EnumKeyRecord.Type.C, "ceee"); + EnumKeyRecord d = new EnumKeyRecord(EnumKeyRecord.Type.D, "deee"); + _repo.storeEnum(a); + _repo.storeEnum(b); + _repo.storeEnum(c); + _repo.storeEnum(d); + assertEquals(4, _repo.loadEnums(EnumSet.allOf(EnumKeyRecord.Type.class)).size()); + + assertEquals("beee", _repo.loadEnum(EnumKeyRecord.Type.B).name); + } + + // the HSQL in-memory database persists for the lifetime of the VM, which means we have to + // clean up after ourselves in every test; thus we go ahead and share a repository + protected TestRepository _repo = createTestRepository(); } diff --git a/src/test/java/com/samskivert/depot/TestRepository.java b/src/test/java/com/samskivert/depot/TestRepository.java index c8b091e..b325a97 100644 --- a/src/test/java/com/samskivert/depot/TestRepository.java +++ b/src/test/java/com/samskivert/depot/TestRepository.java @@ -20,8 +20,11 @@ package com.samskivert.depot; +import java.util.List; import java.util.Set; +import com.samskivert.depot.clause.Where; + /** * A test tool for the Depot repository services. */ @@ -37,6 +40,21 @@ public class TestRepository extends DepotRepository return load(TestRecord.getKey(recordId)); } + public EnumKeyRecord loadEnum (EnumKeyRecord.Type type) + { + return load(EnumKeyRecord.getKey(type)); + } + + public List loadEnums (Set types) + { + return findAll(EnumKeyRecord.class, new Where(EnumKeyRecord.TYPE.in(types))); + } + + public void storeEnum (EnumKeyRecord record) + { + store(record); + } + public TestRepository (PersistenceContext perCtx) { super(perCtx); @@ -46,5 +64,6 @@ public class TestRepository extends DepotRepository protected void getManagedRecords (Set> classes) { classes.add(TestRecord.class); + classes.add(EnumKeyRecord.class); } }