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.
This commit is contained in:
@@ -964,6 +964,9 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
|
||||
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;
|
||||
|
||||
@@ -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<EnumKeyRecord> _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<EnumKeyRecord> 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
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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<EnumKeyRecord> loadEnums (Set<EnumKeyRecord.Type> 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<Class<? extends PersistentRecord>> classes)
|
||||
{
|
||||
classes.add(TestRecord.class);
|
||||
classes.add(EnumKeyRecord.class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user