From 48545c1e8400f7eaf8cd1d27a139375dfb43266c Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Mon, 30 Oct 2017 15:21:43 -0400 Subject: [PATCH] Finish implementation of proxy model objects. --- .../net/helenus/core/AbstractUnitOfWork.java | 14 ++-- .../core/reflect/MapperInvocationHandler.java | 14 ++-- .../core/draft/EntityDraftBuilderTest.java | 83 +++++++++++++------ 3 files changed, 75 insertions(+), 36 deletions(-) diff --git a/src/main/java/net/helenus/core/AbstractUnitOfWork.java b/src/main/java/net/helenus/core/AbstractUnitOfWork.java index ea0d005..1b4454d 100644 --- a/src/main/java/net/helenus/core/AbstractUnitOfWork.java +++ b/src/main/java/net/helenus/core/AbstractUnitOfWork.java @@ -86,7 +86,9 @@ public abstract class AbstractUnitOfWork implements UnitOfW @Override public synchronized UnitOfWork begin() { - elapsedTime = Stopwatch.createStarted(); + if (LOG.isInfoEnabled()) { + elapsedTime = Stopwatch.createStarted(); + } // log.record(txn::start) return this; } @@ -317,13 +319,11 @@ public abstract class AbstractUnitOfWork implements UnitOfW }); // log.record(txn::abort) // cache.invalidateSince(txn::start time) - if (!hasAborted()) { - committed = false; - aborted = true; - elapsedTime.stop(); - if (LOG.isInfoEnabled()) { - LOG.info(logTimers("aborted")); + if (LOG.isInfoEnabled()) { + if (elapsedTime.isRunning()) { + elapsedTime.stop(); } + LOG.info(logTimers("aborted")); } } diff --git a/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java b/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java index fdfa0b1..cbff40b 100644 --- a/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java +++ b/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java @@ -26,6 +26,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.HashMap; import java.util.Map; +import java.util.Set; import net.helenus.core.Helenus; import net.helenus.mapping.annotation.Transient; @@ -64,7 +65,7 @@ public class MapperInvocationHandler implements InvocationHandler, Serializab } private Object writeReplace() { - return new SerializationProxy(this); + return new SerializationProxy(this); } private void readObject(ObjectInputStream stream) throws InvalidObjectException { throw new InvalidObjectException("Proxy required."); @@ -152,25 +153,28 @@ public class MapperInvocationHandler implements InvocationHandler, Serializab return value; } - static class SerializationProxy implements Serializable { + static class SerializationProxy implements Serializable { private static final long serialVersionUID = -5617583940055969353L; - private final Class iface; + private final Class iface; private final Map src; public SerializationProxy(MapperInvocationHandler mapper) { this.iface = mapper.iface; if (mapper.src instanceof ValueProviderMap) { this.src = new HashMap(mapper.src.size()); - this.src.putAll(src); + Set keys = mapper.src.keySet(); + for (String key : keys) { + this.src.put(key, mapper.src.get(key)); + } } else { this.src = mapper.src; } } Object readResolve() throws ObjectStreamException { - return Helenus.map(iface, src); + return new MapperInvocationHandler(iface, src); } } diff --git a/src/test/java/net/helenus/test/integration/core/draft/EntityDraftBuilderTest.java b/src/test/java/net/helenus/test/integration/core/draft/EntityDraftBuilderTest.java index f21c50b..72c088e 100644 --- a/src/test/java/net/helenus/test/integration/core/draft/EntityDraftBuilderTest.java +++ b/src/test/java/net/helenus/test/integration/core/draft/EntityDraftBuilderTest.java @@ -15,9 +15,11 @@ */ package net.helenus.test.integration.core.draft; +import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.concurrent.TimeoutException; import org.junit.Assert; import org.junit.BeforeClass; @@ -27,41 +29,48 @@ import net.helenus.core.Helenus; import net.helenus.core.HelenusSession; import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest; +import static net.helenus.core.Query.eq; + public class EntityDraftBuilderTest extends AbstractEmbeddedCassandraTest { static Supply supply; static HelenusSession session; + static Supply.Draft draft = null; @BeforeClass - public static void beforeTest() { + public static void beforeTest() throws TimeoutException { session = Helenus.init(getSession()).showCql().add(Supply.class).autoCreateDrop().get(); - supply = session.dsl(Supply.class); - } + supply = session.dsl(Supply.class); + + draft = Supply.draft("APAC").code("WIDGET-002").description("Our second Widget!") + .demand(new HashMap() { + { + put("APAC", 100L); + put("EMEA", 10000L); + put("NORAM", 2000000L); + } + }).shipments(new HashSet() { + { + add("HMS Puddle in transit to APAC, 100 units."); + add("Frigate Jimmy in transit to EMEA, 10000 units."); + } + }).suppliers(new ArrayList() { + { + add("Puddle, Inc."); + add("Jimmy Town, LTD."); + } + }); + + Supply s1 = session.insert(draft).sync(); + } @Test public void testFoo() throws Exception { - Supply.Draft draft = null; - draft = Supply.draft("APAC").code("WIDGET-002").description("Our second Widget!") - .demand(new HashMap() { - { - put("APAC", 100L); - put("EMEA", 10000L); - put("NORAM", 2000000L); - } - }).shipments(new HashSet() { - { - add("HMS Puddle in transit to APAC, 100 units."); - add("Frigate Jimmy in transit to EMEA, 10000 units."); - } - }).suppliers(new ArrayList() { - { - add("Puddle, Inc."); - add("Jimmy Town, LTD."); - } - }); - - Supply s1 = session.insert(draft).sync(); + Supply s1 = session.select(Supply.class).where(supply::id, eq(draft.id())) + .single() + .sync() + .orElse(null); // List Supply s2 = session.update(s1.update()).prepend(supply::suppliers, "Pignose Supply, LLC.").sync(); @@ -76,4 +85,30 @@ public class EntityDraftBuilderTest extends AbstractEmbeddedCassandraTest { Supply s4 = session.update(s3.update()).put(supply::demand, "NORAM", 10L).sync(); Assert.assertEquals((long) s4.demand().get("NORAM"), 10L); } + + @Test + public void testSerialization() throws Exception { + Supply s1, s2; + + s1 = session.select(Supply.class).where(supply::id, eq(draft.id())) + .single() + .sync() + .orElse(null); + + byte[] data; + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutput out = new ObjectOutputStream(bos)) { + out.writeObject(s1); + out.flush(); + data = bos.toByteArray(); + } + + try (ByteArrayInputStream bis = new ByteArrayInputStream(data); + ObjectInput in = new ObjectInputStream(bis)) { + s2 = (Supply)in.readObject(); + } + + Assert.assertEquals(s2.id(), s1.id()); + Assert.assertEquals(s2, s1); + } }