You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2015/07/25 12:39:38 UTC
[2/3] isis git commit: ISIS-1150: mothballed the in-memory
objectstore.
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/core/runtime/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_instances.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_instances.java b/core/runtime/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_instances.java
deleted file mode 100644
index 4274613..0000000
--- a/core/runtime/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_instances.java
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.isis.core.objectstore.internal;
-
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
-import org.jmock.Expectations;
-import org.jmock.Mockery;
-import org.jmock.integration.junit4.JMock;
-import org.jmock.integration.junit4.JUnit4Mockery;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import org.apache.isis.core.metamodel.spec.ObjectSpecId;
-
-@RunWith(JMock.class)
-public class ObjectStorePersistedObjectsDefault_instances {
-
- private ObjectStorePersistedObjects persistedObjects;
-
- private final Mockery context = new JUnit4Mockery();
-
- private ObjectSpecId mockSpec;
-
- @Before
- public void setUp() throws Exception {
- persistedObjects = new ObjectStorePersistedObjects();
- mockSpec = context.mock(ObjectSpecId.class);
- }
-
- @Test
- public void instancesLazilyPopulatedWhenAskForThem() throws Exception {
- neverInteractsWithSpec();
-
- // no instances
- final Iterable<ObjectStoreInstances> instancesBefore = persistedObjects.instances();
- assertThat(instancesBefore.iterator().hasNext(), is(false));
-
- ensureThereAreSomeInstances();
-
- // now there are
- final Iterable<ObjectStoreInstances> instancesAfter = persistedObjects.instances();
- assertThat(instancesAfter.iterator().hasNext(), is(true));
- }
-
- @Test
- public void clearZapsTheInstances() throws Exception {
- neverInteractsWithSpec();
-
- ensureThereAreSomeInstances();
- final Iterable<ObjectStoreInstances> instancesAfter = persistedObjects.instances();
- assertThat(instancesAfter.iterator().hasNext(), is(true));
-
- persistedObjects.clear();
-
- // now there are no more instances
- final Iterable<ObjectStoreInstances> instancesBefore = persistedObjects.instances();
- assertThat(instancesBefore.iterator().hasNext(), is(false));
- }
-
- private void ensureThereAreSomeInstances() {
- persistedObjects.instancesFor(mockSpec);
- }
-
- private void neverInteractsWithSpec() {
- context.checking(new Expectations() {
- {
- never(mockSpec);
- }
- });
- }
-
-}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/core/runtime/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_savesOidGeneratorAsMemento.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_savesOidGeneratorAsMemento.java b/core/runtime/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_savesOidGeneratorAsMemento.java
deleted file mode 100644
index 8514db2..0000000
--- a/core/runtime/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_savesOidGeneratorAsMemento.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.isis.core.objectstore.internal;
-
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.apache.isis.core.runtime.system.persistence.IdentifierGeneratorDefault;
-import org.apache.isis.core.unittestsupport.jmocking.JUnitRuleMockery2;
-
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertThat;
-
-public class ObjectStorePersistedObjectsDefault_savesOidGeneratorAsMemento {
-
- private ObjectStorePersistedObjects persistedObjects;
-
- @Rule
- public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(JUnitRuleMockery2.Mode.INTERFACES_AND_CLASSES);
-
- private IdentifierGeneratorDefault.Memento mockMemento;
-
- @Before
- public void setUp() throws Exception {
- persistedObjects = new ObjectStorePersistedObjects();
- mockMemento = context.mock(IdentifierGeneratorDefault.Memento.class);
- }
-
- @Test
- public void noOidGeneratorInitially() throws Exception {
- final IdentifierGeneratorDefault.Memento oidGeneratorMemento = persistedObjects.getOidGeneratorMemento();
- assertThat(oidGeneratorMemento, is(nullValue()));
- }
-
- @Test
- public void oidGeneratorStoredOnceSaved() throws Exception {
- persistedObjects.saveOidGeneratorMemento(mockMemento);
- final IdentifierGeneratorDefault.Memento oidGeneratorMemento = persistedObjects.getOidGeneratorMemento();
- assertThat(oidGeneratorMemento, is(mockMemento));
- }
-
-}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/core/runtime/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_services.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_services.java b/core/runtime/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_services.java
deleted file mode 100644
index 83bf511..0000000
--- a/core/runtime/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_services.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.isis.core.objectstore.internal;
-
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertThat;
-
-import org.jmock.auto.Mock;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-
-import org.apache.isis.core.metamodel.adapter.oid.Oid;
-import org.apache.isis.core.metamodel.spec.ObjectSpecId;
-import org.apache.isis.core.unittestsupport.jmocking.JUnitRuleMockery2;
-import org.apache.isis.core.unittestsupport.jmocking.JUnitRuleMockery2.Mode;
-
-public class ObjectStorePersistedObjectsDefault_services {
-
- @Rule
- public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES);
-
- @Mock
- private Oid mockOidForFooService;
- @Mock
- private Oid mockOidForBarService;
-
- private ObjectStorePersistedObjects persistedObjects;
-
- @Before
- public void setUp() throws Exception {
- persistedObjects = new ObjectStorePersistedObjects();
- }
-
- @Test
- public void noServicesInitially() throws Exception {
- final Oid service = persistedObjects.getService(ObjectSpecId.of("fooService"));
- assertThat(service, is(nullValue()));
- }
-
- @Test
- public void registerServicesMakesAvailable() throws Exception {
- persistedObjects.registerService(ObjectSpecId.of("fooService"), mockOidForFooService);
-
- final Oid service = persistedObjects.getService(ObjectSpecId.of("fooService"));
- assertThat(service, is(mockOidForFooService));
- }
-
- @Test
- public void registerServicesWhenMoreThanOnePullsOutTheCorrectOne() throws Exception {
- persistedObjects.registerService(ObjectSpecId.of("fooService"), mockOidForFooService);
- persistedObjects.registerService(ObjectSpecId.of("barService"), mockOidForBarService);
-
- final Oid service = persistedObjects.getService(ObjectSpecId.of("fooService"));
- assertThat(service, is(mockOidForFooService));
- }
-
-}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/core/schema/pom.xml
----------------------------------------------------------------------
diff --git a/core/schema/pom.xml b/core/schema/pom.xml
index fbfc74b..5c0eef7 100644
--- a/core/schema/pom.xml
+++ b/core/schema/pom.xml
@@ -29,7 +29,7 @@
<artifactId>isis-core-schema</artifactId>
<packaging>jar</packaging>
- <name>Isis Core Schemas</name>
+ <name>Isis Schemas</name>
<description>
Apache Isis schemas, for conversion into canonical DTOs (for use in integration scenarios).
</description>
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/core/security-noop/pom.xml
----------------------------------------------------------------------
diff --git a/core/security-noop/pom.xml b/core/security-noop/pom.xml
index 12336f2..ddf96a4 100644
--- a/core/security-noop/pom.xml
+++ b/core/security-noop/pom.xml
@@ -27,7 +27,7 @@
</parent>
<artifactId>isis-core-security</artifactId>
- <name>Isis Core (Bypass) Security</name>
+ <name>Isis Security Bypass</name>
<properties>
<siteBaseDir>..</siteBaseDir>
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/core/security-shiro/pom.xml
----------------------------------------------------------------------
diff --git a/core/security-shiro/pom.xml b/core/security-shiro/pom.xml
index 69496bc..57ce933 100644
--- a/core/security-shiro/pom.xml
+++ b/core/security-shiro/pom.xml
@@ -30,7 +30,7 @@
<artifactId>isis-core-security-shiro</artifactId>
<version>1.9.0-SNAPSHOT</version>
- <name>Isis Core Shiro Security</name>
+ <name>Isis Security Shiro</name>
<properties>
<siteBaseDir>..</siteBaseDir>
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/core/specsupport/pom.xml
----------------------------------------------------------------------
diff --git a/core/specsupport/pom.xml b/core/specsupport/pom.xml
index 81c8f95..e9a8f7e 100644
--- a/core/specsupport/pom.xml
+++ b/core/specsupport/pom.xml
@@ -19,7 +19,7 @@
</parent>
<artifactId>isis-core-specsupport</artifactId>
- <name>Isis Core Spec Support</name>
+ <name>Isis Spec Support</name>
<properties>
<siteBaseDir>..</siteBaseDir>
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/core/unittestsupport/pom.xml
----------------------------------------------------------------------
diff --git a/core/unittestsupport/pom.xml b/core/unittestsupport/pom.xml
index 6fcb864..3fc1e0c 100644
--- a/core/unittestsupport/pom.xml
+++ b/core/unittestsupport/pom.xml
@@ -27,7 +27,7 @@
</parent>
<artifactId>isis-core-unittestsupport</artifactId>
- <name>Isis Core Unit Test Support</name>
+ <name>Isis Unit Test Support</name>
<description>
Support for writing unit tests; should be added as a dependency
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/core/viewer-restfulobjects-applib/pom.xml
----------------------------------------------------------------------
diff --git a/core/viewer-restfulobjects-applib/pom.xml b/core/viewer-restfulobjects-applib/pom.xml
index d8b60b8..0ec02b4 100644
--- a/core/viewer-restfulobjects-applib/pom.xml
+++ b/core/viewer-restfulobjects-applib/pom.xml
@@ -27,7 +27,7 @@
</parent>
<artifactId>isis-core-viewer-restfulobjects-applib</artifactId>
- <name>Isis Core RestfulObjects Viewer AppLib</name>
+ <name>Isis RestfulObjects Viewer AppLib</name>
<properties>
<siteBaseDir>..</siteBaseDir>
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/core/viewer-restfulobjects-rendering/pom.xml
----------------------------------------------------------------------
diff --git a/core/viewer-restfulobjects-rendering/pom.xml b/core/viewer-restfulobjects-rendering/pom.xml
index fdc1563..9bcab14 100644
--- a/core/viewer-restfulobjects-rendering/pom.xml
+++ b/core/viewer-restfulobjects-rendering/pom.xml
@@ -28,7 +28,7 @@
<artifactId>isis-core-viewer-restfulobjects-rendering</artifactId>
- <name>Isis Core RestfulObjects Viewer Rendering</name>
+ <name>Isis RestfulObjects Viewer Rendering</name>
<properties>
<siteBaseDir>..</siteBaseDir>
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/core/viewer-restfulobjects-server/pom.xml
----------------------------------------------------------------------
diff --git a/core/viewer-restfulobjects-server/pom.xml b/core/viewer-restfulobjects-server/pom.xml
index 648347e..a0ba630 100644
--- a/core/viewer-restfulobjects-server/pom.xml
+++ b/core/viewer-restfulobjects-server/pom.xml
@@ -28,7 +28,7 @@
<artifactId>isis-core-viewer-restfulobjects-server</artifactId>
- <name>Isis Core RestfulObjects Viewer Server</name>
+ <name>Isis RestfulObjects Viewer Server</name>
<properties>
<siteBaseDir>..</siteBaseDir>
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/core/webserver/pom.xml
----------------------------------------------------------------------
diff --git a/core/webserver/pom.xml b/core/webserver/pom.xml
index a9b2850..92b5ceb 100644
--- a/core/webserver/pom.xml
+++ b/core/webserver/pom.xml
@@ -27,7 +27,7 @@
</parent>
<artifactId>isis-core-webserver</artifactId>
- <name>Isis Core WebServer</name>
+ <name>Isis WebServer</name>
<properties>
<siteBaseDir>..</siteBaseDir>
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/core/wrapper/pom.xml
----------------------------------------------------------------------
diff --git a/core/wrapper/pom.xml b/core/wrapper/pom.xml
index b17deeb..d7286b2 100644
--- a/core/wrapper/pom.xml
+++ b/core/wrapper/pom.xml
@@ -29,7 +29,7 @@
<artifactId>isis-core-wrapper</artifactId>
<name>Isis Core Wrapper Service</name>
- <description>Isis Core Wrapper Service</description>
+ <description>Isis Wrapper Service</description>
<properties>
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/InMemoryObjectStore.java
----------------------------------------------------------------------
diff --git a/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/InMemoryObjectStore.java b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/InMemoryObjectStore.java
new file mode 100644
index 0000000..53e3775
--- /dev/null
+++ b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/InMemoryObjectStore.java
@@ -0,0 +1,502 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.objectstore;
+
+import java.text.MessageFormat;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Vector;
+import com.google.common.collect.Lists;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.apache.isis.core.commons.debug.DebugBuilder;
+import org.apache.isis.core.commons.debug.DebugUtils;
+import org.apache.isis.core.commons.exceptions.IsisException;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.adapter.mgr.AdapterManager;
+import org.apache.isis.core.metamodel.adapter.oid.Oid;
+import org.apache.isis.core.metamodel.adapter.oid.RootOid;
+import org.apache.isis.core.metamodel.adapter.oid.TypedOid;
+import org.apache.isis.core.metamodel.adapter.version.Version;
+import org.apache.isis.core.metamodel.facets.collections.modify.CollectionFacet;
+import org.apache.isis.core.metamodel.facets.collections.modify.CollectionFacetUtils;
+import org.apache.isis.core.metamodel.spec.ObjectSpecId;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+import org.apache.isis.core.metamodel.spec.SpecificationLoader;
+import org.apache.isis.core.metamodel.spec.feature.Contributed;
+import org.apache.isis.core.metamodel.spec.feature.ObjectAssociation;
+import org.apache.isis.core.objectstore.commands.InMemoryCreateObjectCommand;
+import org.apache.isis.core.objectstore.commands.InMemoryDestroyObjectCommand;
+import org.apache.isis.core.objectstore.commands.InMemorySaveObjectCommand;
+import org.apache.isis.core.objectstore.internal.ObjectStoreInstances;
+import org.apache.isis.core.objectstore.internal.ObjectStorePersistedObjects;
+import org.apache.isis.core.runtime.persistence.ObjectNotFoundException;
+import org.apache.isis.core.runtime.persistence.ObjectPersistenceException;
+import org.apache.isis.core.runtime.persistence.UnsupportedFindException;
+import org.apache.isis.core.runtime.persistence.objectstore.transaction.CreateObjectCommand;
+import org.apache.isis.core.runtime.persistence.objectstore.transaction.DestroyObjectCommand;
+import org.apache.isis.core.runtime.persistence.objectstore.transaction.PersistenceCommand;
+import org.apache.isis.core.runtime.persistence.objectstore.transaction.SaveObjectCommand;
+import org.apache.isis.core.runtime.persistence.query.PersistenceQueryBuiltIn;
+import org.apache.isis.core.runtime.system.context.IsisContext;
+import org.apache.isis.core.runtime.system.persistence.ObjectStore;
+import org.apache.isis.core.runtime.system.persistence.PersistenceQuery;
+import org.apache.isis.core.runtime.system.persistence.PersistenceSession;
+import org.apache.isis.core.runtime.system.persistence.PersistenceSessionFactory;
+
+public class InMemoryObjectStore implements ObjectStore {
+
+ private final static Logger LOG = LoggerFactory.getLogger(InMemoryObjectStore.class);
+
+ protected ObjectStorePersistedObjects persistedObjects;
+
+ public InMemoryObjectStore() {
+ LOG.info("creating memory object store");
+ }
+
+ // ///////////////////////////////////////////////////////
+ // Name
+ // ///////////////////////////////////////////////////////
+
+ @Override
+ public String name() {
+ return "In-Memory Object Store";
+ }
+
+ // ///////////////////////////////////////////////////////
+ // open, close, shutdown
+ // ///////////////////////////////////////////////////////
+
+ @Override
+ public void open() {
+ // TODO: all a bit hacky, but is to keep tests running. Should really
+ // sort out using mocks.
+ final InMemoryPersistenceSessionFactory inMemoryPersistenceSessionFactory = getInMemoryPersistenceSessionFactory();
+ persistedObjects = inMemoryPersistenceSessionFactory == null ? null : inMemoryPersistenceSessionFactory.getPersistedObjects();
+ if (persistedObjects == null) {
+ if (inMemoryPersistenceSessionFactory != null) {
+ persistedObjects = inMemoryPersistenceSessionFactory.createPersistedObjects();
+ } else {
+ persistedObjects = new ObjectStorePersistedObjects();
+ }
+ } else {
+ recreateAdapters();
+ }
+ }
+
+ protected void recreateAdapters() {
+ for (final ObjectSpecId noSpec : persistedObjects.specifications()) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("recreating adapters for: " + noSpec.asString());
+ }
+ recreateAdapters(persistedObjects.instancesFor(noSpec));
+ }
+ }
+
+ private void recreateAdapters(final ObjectStoreInstances objectStoreInstances) {
+ for (final Oid oid : objectStoreInstances.getOids()) {
+
+ // it's important not to "touch" the pojo, not even in log messages.
+ // That's because
+ // the toString() will cause bytecode enhancement to try to resolve
+ // references.
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("recreating adapter: oid=" + oid);
+ }
+ final Object pojo = objectStoreInstances.getPojo(oid);
+
+ final ObjectAdapter existingAdapterLookedUpByPojo = getAdapterManager().getAdapterFor(pojo);
+ if (existingAdapterLookedUpByPojo != null) {
+ // this could happen if we rehydrate a persisted object that
+ // depends on another persisted object
+ // not yet rehydrated.
+ getPersistenceSession().getAdapterManager().removeAdapter(existingAdapterLookedUpByPojo);
+ }
+
+ final ObjectAdapter existingAdapterLookedUpByOid = getAdapterManager().getAdapterFor(oid);
+ if (existingAdapterLookedUpByOid != null) {
+ throw new IsisException("A mapping already exists for " + oid + ": " + existingAdapterLookedUpByOid);
+ }
+
+ final ObjectAdapter recreatedAdapter = getPersistenceSession().getAdapterManager().mapRecreatedPojo(oid, pojo);
+
+ final Version version = objectStoreInstances.getVersion(oid);
+ recreatedAdapter.setVersion(version);
+ }
+ }
+
+ @Override
+ public void close() {
+ final InMemoryPersistenceSessionFactory inMemoryPersistenceSessionFactory = getInMemoryPersistenceSessionFactory();
+ // TODO: this is hacky, only here to keep tests running. Should sort out
+ // using mocks
+ if (inMemoryPersistenceSessionFactory != null) {
+ inMemoryPersistenceSessionFactory.attach(getPersistenceSession(), persistedObjects);
+ persistedObjects = null;
+ }
+ }
+
+ // ///////////////////////////////////////////////////////
+ // fixtures
+ // ///////////////////////////////////////////////////////
+
+ /**
+ * No permanent persistence, so must always install fixtures.
+ */
+ @Override
+ public boolean isFixturesInstalled() {
+ return false;
+ }
+
+ // ///////////////////////////////////////////////////////
+ // reset
+ // ///////////////////////////////////////////////////////
+
+ @Override
+ public void reset() {
+ }
+
+ // ///////////////////////////////////////////////////////
+ // Transaction management
+ // ///////////////////////////////////////////////////////
+
+ @Override
+ public void startTransaction() {
+ }
+
+ @Override
+ public void endTransaction() {
+ }
+
+ @Override
+ public void abortTransaction() {
+ }
+
+ // ///////////////////////////////////////////////////////
+ // Command Creation
+ // ///////////////////////////////////////////////////////
+
+ @Override
+ public CreateObjectCommand createCreateObjectCommand(final ObjectAdapter object) {
+ if (object.getSpecification().isParented()) {
+ return null;
+ }
+ return new InMemoryCreateObjectCommand(object, persistedObjects);
+ }
+
+ @Override
+ public SaveObjectCommand createSaveObjectCommand(final ObjectAdapter object) {
+ if (object.getSpecification().isParented()) {
+ return null;
+ }
+ return new InMemorySaveObjectCommand(object, persistedObjects);
+ }
+
+ @Override
+ public DestroyObjectCommand createDestroyObjectCommand(final ObjectAdapter object) {
+ return new InMemoryDestroyObjectCommand(object, persistedObjects);
+ }
+
+ // ///////////////////////////////////////////////////////
+ // Command Execution
+ // ///////////////////////////////////////////////////////
+
+ @Override
+ public void execute(final List<PersistenceCommand> commands) throws ObjectPersistenceException {
+ if (LOG.isInfoEnabled()) {
+ LOG.info("execute commands");
+ }
+ for (final PersistenceCommand command : commands) {
+ command.execute(null);
+ }
+ LOG.info("end execution");
+ }
+
+ // ///////////////////////////////////////////////////////
+ // getObject, resolveField, resolveImmediately
+ // ///////////////////////////////////////////////////////
+
+ @Override
+ public ObjectAdapter loadInstanceAndAdapt(final TypedOid oid) throws ObjectNotFoundException, ObjectPersistenceException {
+ if(LOG.isDebugEnabled()) {
+ LOG.debug("getObject " + oid);
+ }
+ final ObjectSpecification objectSpec = getSpecificationLookup().lookupBySpecId(oid.getObjectSpecId());
+ final ObjectStoreInstances ins = instancesFor(objectSpec.getSpecId());
+ final ObjectAdapter adapter = ins.getObjectAndMapIfRequired(oid);
+ if (adapter == null) {
+ throw new ObjectNotFoundException(oid);
+ }
+ return adapter;
+ }
+
+ @Override
+ public void resolveImmediately(final ObjectAdapter adapter) throws ObjectPersistenceException {
+
+ // these diagnostics are because, even though this method is called by
+ // PersistenceSessionObjectStore#resolveImmediately which has a check,
+ // seem to be hitting a race condition with another thread that is
+ // resolving the object before I get here.
+ if (adapter.canTransitionToResolving()) {
+ if(LOG.isDebugEnabled()) {
+ LOG.debug("resolve " + adapter);
+ }
+ } else {
+ LOG.warn("resolveImmediately ignored, " + "adapter's current state is: " + adapter.getResolveState() + " ; oid: " + adapter.getOid());
+ }
+
+ adapter.markAsResolvedIfPossible();
+ }
+
+
+ // ///////////////////////////////////////////////////////
+ // getInstances, hasInstances
+ // ///////////////////////////////////////////////////////
+
+ @Override
+ public List<ObjectAdapter> loadInstancesAndAdapt(final PersistenceQuery persistenceQuery) throws ObjectPersistenceException, UnsupportedFindException {
+
+ if (!(persistenceQuery instanceof PersistenceQueryBuiltIn)) {
+ throw new IllegalArgumentException(MessageFormat.format("Provided PersistenceQuery not supported; was {0}; " + "the in-memory object store only supports {1}", persistenceQuery.getClass().getName(), PersistenceQueryBuiltIn.class.getName()));
+ }
+ final PersistenceQueryBuiltIn builtIn = (PersistenceQueryBuiltIn) persistenceQuery;
+
+ final List<ObjectAdapter> instances = Lists.newArrayList();
+ final ObjectSpecification spec = persistenceQuery.getSpecification();
+ findInstances(spec, builtIn, instances);
+ return resolved(instances);
+ }
+
+ @Override
+ public boolean hasInstances(final ObjectSpecification spec) {
+ if (instancesFor(spec.getSpecId()).hasInstances()) {
+ return true;
+ }
+
+ // includeSubclasses
+ final List<ObjectSpecification> subclasses = spec.subclasses();
+ for (int i = 0; i < subclasses.size(); i++) {
+ if (hasInstances(subclasses.get(i))) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private void findInstances(final ObjectSpecification spec, final PersistenceQueryBuiltIn persistenceQuery, final List<ObjectAdapter> foundInstances) {
+
+ instancesFor(spec.getSpecId()).findInstancesAndAdd(persistenceQuery, foundInstances);
+
+ // include subclasses
+ final List<ObjectSpecification> subclasses = spec.subclasses();
+ for (int i = 0; i < subclasses.size(); i++) {
+ findInstances(subclasses.get(i), persistenceQuery, foundInstances);
+ }
+ }
+
+ private static List<ObjectAdapter> resolved(final List<ObjectAdapter> instances) {
+ for (ObjectAdapter adapter: instances) {
+ adapter.markAsResolvedIfPossible();
+ }
+ return instances;
+ }
+
+ // ///////////////////////////////////////////////////////
+ // Services
+ // ///////////////////////////////////////////////////////
+
+ @Override
+ public RootOid getOidForService(ObjectSpecification serviceSpec) {
+ return (RootOid) persistedObjects.getService(serviceSpec.getSpecId());
+ }
+
+ @Override
+ public void registerService(final RootOid rootOid) {
+ persistedObjects.registerService(rootOid.getObjectSpecId(), rootOid);
+ }
+
+ private ObjectStoreInstances instancesFor(final ObjectSpecId spec) {
+ return persistedObjects.instancesFor(spec);
+ }
+
+ // ///////////////////////////////////////////////////////
+ // Debugging
+ // ///////////////////////////////////////////////////////
+
+ @Override
+ public String debugTitle() {
+ return name();
+ }
+
+ @Override
+ public void debugData(final DebugBuilder debug) {
+ debug.appendTitle("Domain Objects");
+ for (final ObjectSpecId specId : persistedObjects.specifications()) {
+ debug.appendln(specId.asString());
+ final ObjectStoreInstances instances = instancesFor(specId);
+ instances.debugData(debug);
+ }
+ debug.unindent();
+ debug.appendln();
+ }
+
+ private String debugCollectionGraph(final ObjectAdapter collection, final int level, final Vector<ObjectAdapter> recursiveElements) {
+ final StringBuffer s = new StringBuffer();
+
+ if (recursiveElements.contains(collection)) {
+ s.append("*\n");
+ } else {
+ recursiveElements.addElement(collection);
+
+ final CollectionFacet facet = CollectionFacetUtils.getCollectionFacetFromSpec(collection);
+ final Iterator<ObjectAdapter> e = facet.iterator(collection);
+
+ while (e.hasNext()) {
+ indent(s, level);
+
+ ObjectAdapter element;
+ try {
+ element = e.next();
+ } catch (final ClassCastException ex) {
+ LOG.error(ex.getMessage(), ex);
+ return s.toString();
+ }
+
+ s.append(element);
+ s.append(debugGraph(element, level + 1, recursiveElements));
+ }
+ }
+
+ return s.toString();
+ }
+
+ private String debugGraph(final ObjectAdapter object, final int level, final Vector<ObjectAdapter> recursiveElements) {
+ if (level > 3) {
+ return "...\n"; // only go 3 levels?
+ }
+
+ Vector<ObjectAdapter> elements;
+ if (recursiveElements == null) {
+ elements = new Vector<ObjectAdapter>(25, 10);
+ } else {
+ elements = recursiveElements;
+ }
+
+ if (object.getSpecification().isParentedOrFreeCollection()) {
+ return "\n" + debugCollectionGraph(object, level, elements);
+ } else {
+ return "\n" + debugObjectGraph(object, level, elements);
+ }
+ }
+
+ private String debugObjectGraph(final ObjectAdapter object, final int level, final Vector<ObjectAdapter> recursiveElements) {
+ final StringBuffer s = new StringBuffer();
+
+ recursiveElements.addElement(object);
+
+ // work through all its fields
+ final List<ObjectAssociation> fields = object.getSpecification().getAssociations(Contributed.EXCLUDED);
+
+ for (int i = 0; i < fields.size(); i++) {
+ final ObjectAssociation field = fields.get(i);
+ final Object obj = field.get(object);
+
+ final String id = field.getId();
+ indent(s, level);
+
+ if (field.isOneToManyAssociation()) {
+ s.append(id + ": \n" + debugCollectionGraph((ObjectAdapter) obj, level + 1, recursiveElements));
+ } else {
+ if (recursiveElements.contains(obj)) {
+ s.append(id + ": " + obj + "*\n");
+ } else {
+ s.append(id + ": " + obj);
+ s.append(debugGraph((ObjectAdapter) obj, level + 1, recursiveElements));
+ }
+ }
+ }
+
+ return s.toString();
+ }
+
+ private void indent(final StringBuffer s, final int level) {
+ for (int indent = 0; indent < level; indent++) {
+ s.append(DebugUtils.indentString(4) + "|");
+ }
+
+ s.append(DebugUtils.indentString(4) + "+--");
+ }
+
+ // ///////////////////////////////////////////////////////
+ // Dependencies (from context)
+ // ///////////////////////////////////////////////////////
+
+ /**
+ * Must use {@link IsisContext context}, because although this object is
+ * recreated with each {@link PersistenceSession session}, the persisted
+ * objects that get attached to
+ * it span multiple sessions.
+ *
+ * <p>
+ * The alternative design would be to laboriously inject the session into
+ * not only this object but also the {@link ObjectStoreInstances} that do
+ * the work.
+ */
+ protected PersistenceSession getPersistenceSession() {
+ return IsisContext.getPersistenceSession();
+ }
+
+ /**
+ * Must use {@link IsisContext context}, because although this object is
+ * recreated with each {@link PersistenceSession session}, the persisted
+ * objects that get attached to it span multiple sessions.
+ *
+ * <p>
+ * The alternative design would be to laboriously inject the session into
+ * not only this object but also the {@link ObjectStoreInstances} that do
+ * the work.
+ */
+ protected AdapterManager getAdapterManager() {
+ return getPersistenceSession().getAdapterManager();
+ }
+
+ protected SpecificationLoader getSpecificationLookup() {
+ return IsisContext.getSpecificationLoader();
+ }
+
+
+ /**
+ * Downcasts the {@link org.apache.isis.core.runtime.system.persistence.PersistenceSessionFactory} to
+ * {@link InMemoryPersistenceSessionFactory}.
+ */
+ protected InMemoryPersistenceSessionFactory getInMemoryPersistenceSessionFactory() {
+ final PersistenceSessionFactory persistenceSessionFactory = getPersistenceSession().getPersistenceSessionFactory();
+
+ if (!(persistenceSessionFactory instanceof InMemoryPersistenceSessionFactory)) {
+ // for testing support
+ return null;
+ }
+ return (InMemoryPersistenceSessionFactory) persistenceSessionFactory;
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/InMemoryPersistenceMechanismInstaller.java
----------------------------------------------------------------------
diff --git a/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/InMemoryPersistenceMechanismInstaller.java b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/InMemoryPersistenceMechanismInstaller.java
new file mode 100644
index 0000000..cc48c06
--- /dev/null
+++ b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/InMemoryPersistenceMechanismInstaller.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.objectstore;
+
+import org.apache.isis.core.commons.config.IsisConfiguration;
+import org.apache.isis.core.runtime.installerregistry.installerapi.PersistenceMechanismInstallerAbstract;
+import org.apache.isis.core.runtime.system.DeploymentType;
+import org.apache.isis.core.runtime.system.persistence.ObjectStore;
+import org.apache.isis.core.runtime.system.persistence.PersistenceSessionFactory;
+
+/**
+ * Installs the in-memory object store.
+ */
+public class InMemoryPersistenceMechanismInstaller extends PersistenceMechanismInstallerAbstract {
+
+ public static final String NAME = "in-memory";
+
+ public InMemoryPersistenceMechanismInstaller() {
+ super(NAME);
+ }
+
+ // ///////////////////////////////////////////////////////////////
+ // createPersistenceSessionFactory
+ // ///////////////////////////////////////////////////////////////
+
+ @Override
+ public PersistenceSessionFactory createPersistenceSessionFactory(final DeploymentType deploymentType) {
+ return new InMemoryPersistenceSessionFactory(deploymentType, getConfiguration(), this);
+ }
+
+ // ///////////////////////////////////////////////////////////////
+ // Hook methods
+ // ///////////////////////////////////////////////////////////////
+
+ /**
+ * Hook method to return {@link ObjectStore}.
+ */
+ @Override
+ public ObjectStore createObjectStore(
+ final IsisConfiguration configuration) {
+ return new InMemoryObjectStore();
+ }
+
+
+
+
+}
+
+
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/InMemoryPersistenceSessionFactory.java
----------------------------------------------------------------------
diff --git a/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/InMemoryPersistenceSessionFactory.java b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/InMemoryPersistenceSessionFactory.java
new file mode 100644
index 0000000..455ca6a
--- /dev/null
+++ b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/InMemoryPersistenceSessionFactory.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.objectstore;
+
+import org.apache.isis.core.commons.config.IsisConfiguration;
+import org.apache.isis.core.objectstore.internal.ObjectStoreInstances;
+import org.apache.isis.core.objectstore.internal.ObjectStorePersistedObjects;
+import org.apache.isis.core.runtime.persistence.ObjectStoreFactory;
+import org.apache.isis.core.runtime.system.persistence.PersistenceSessionFactory;
+import org.apache.isis.core.runtime.system.DeploymentType;
+import org.apache.isis.core.runtime.system.persistence.IdentifierGenerator;
+import org.apache.isis.core.runtime.system.persistence.IdentifierGeneratorDefault;
+import org.apache.isis.core.runtime.system.persistence.OidGenerator;
+import org.apache.isis.core.runtime.system.persistence.PersistenceSession;
+
+public class InMemoryPersistenceSessionFactory extends PersistenceSessionFactory {
+
+ private ObjectStorePersistedObjects persistedObjects;
+
+ public InMemoryPersistenceSessionFactory(final DeploymentType deploymentType, final IsisConfiguration configuration, final ObjectStoreFactory objectStoreFactory) {
+ super(deploymentType, configuration, objectStoreFactory);
+ }
+
+ ObjectStorePersistedObjects getPersistedObjects() {
+ return persistedObjects;
+ }
+
+ @Override
+ public PersistenceSession createPersistenceSession() {
+ final PersistenceSession persistenceSession = super.createPersistenceSession();
+ if (persistedObjects != null) {
+ final OidGenerator oidGenerator = persistenceSession.getOidGenerator();
+ final IdentifierGenerator identifierGenerator = oidGenerator.getIdentifierGenerator();
+ final IdentifierGeneratorDefault identifierGeneratorDefault = identifierGenerator.underlying(IdentifierGeneratorDefault.class);
+ if(identifierGeneratorDefault != null) {
+ identifierGeneratorDefault.resetTo(persistedObjects.getOidGeneratorMemento());
+ }
+ }
+
+ return persistenceSession;
+ }
+
+ /**
+ * Not API - called when {@link InMemoryObjectStore} first
+ * {@link InMemoryObjectStore#open() open}ed.
+ */
+ public ObjectStorePersistedObjects createPersistedObjects() {
+ return new ObjectStorePersistedObjects();
+ }
+
+ /**
+ * Not API - called when {@link InMemoryObjectStore} is
+ * {@link InMemoryObjectStore#close() close}d.
+ */
+ public void attach(final PersistenceSession persistenceSession, final ObjectStorePersistedObjects persistedObjects) {
+ final OidGenerator oidGenerator = persistenceSession.getOidGenerator();
+ final IdentifierGenerator identifierGenerator = oidGenerator.getIdentifierGenerator();
+
+ final IdentifierGeneratorDefault identifierGeneratorDefault = identifierGenerator.underlying(IdentifierGeneratorDefault.class);
+ if(identifierGeneratorDefault != null) {
+ identifierGeneratorDefault.resetTo(persistedObjects.getOidGeneratorMemento());
+ persistedObjects.saveOidGeneratorMemento(identifierGeneratorDefault.getMemento());
+ }
+
+ this.persistedObjects = persistedObjects;
+ }
+
+ @Override
+ protected void doShutdown() {
+ if (persistedObjects != null) {
+ for (final ObjectStoreInstances inst : persistedObjects.instances()) {
+ inst.shutdown();
+ }
+ persistedObjects.clear();
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/AbstractInMemoryPersistenceCommand.java
----------------------------------------------------------------------
diff --git a/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/AbstractInMemoryPersistenceCommand.java b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/AbstractInMemoryPersistenceCommand.java
new file mode 100644
index 0000000..9c5602c
--- /dev/null
+++ b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/AbstractInMemoryPersistenceCommand.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.objectstore.commands;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.spec.ObjectSpecId;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+import org.apache.isis.core.objectstore.internal.ObjectStoreInstances;
+import org.apache.isis.core.objectstore.internal.ObjectStorePersistedObjects;
+import org.apache.isis.core.runtime.persistence.ObjectPersistenceException;
+import org.apache.isis.core.runtime.persistence.objectstore.transaction.PersistenceCommandAbstract;
+
+public abstract class AbstractInMemoryPersistenceCommand extends PersistenceCommandAbstract {
+
+ private final static Logger LOG = LoggerFactory.getLogger(AbstractInMemoryPersistenceCommand.class);
+
+ private final ObjectStorePersistedObjects persistedObjects;
+
+ public AbstractInMemoryPersistenceCommand(final ObjectAdapter adapter, final ObjectStorePersistedObjects persistedObjects) {
+ super(adapter);
+ this.persistedObjects = persistedObjects;
+ }
+
+ protected void save(final ObjectAdapter adapter) throws ObjectPersistenceException {
+ final ObjectSpecification specification = adapter.getSpecification();
+ if (LOG.isDebugEnabled()) {
+ LOG.debug(" saving object " + adapter + " as instance of " + specification.getShortIdentifier());
+ }
+ final ObjectStoreInstances ins = instancesFor(specification.getSpecId());
+ ins.save(adapter); // also sets the version
+ }
+
+ protected void destroy(final ObjectAdapter adapter) {
+ final ObjectSpecification specification = adapter.getSpecification();
+ if (LOG.isDebugEnabled()) {
+ LOG.debug(" destroy object " + adapter + " as instance of " + specification.getShortIdentifier());
+ }
+ final ObjectStoreInstances ins = instancesFor(specification.getSpecId());
+ ins.remove(adapter.getOid());
+ }
+
+ private ObjectStoreInstances instancesFor(final ObjectSpecId spec) {
+ return persistedObjects.instancesFor(spec);
+ }
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/InMemoryCreateObjectCommand.java
----------------------------------------------------------------------
diff --git a/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/InMemoryCreateObjectCommand.java b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/InMemoryCreateObjectCommand.java
new file mode 100644
index 0000000..af2d363
--- /dev/null
+++ b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/InMemoryCreateObjectCommand.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.objectstore.commands;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.objectstore.internal.ObjectStorePersistedObjects;
+import org.apache.isis.core.runtime.persistence.ObjectPersistenceException;
+import org.apache.isis.core.runtime.persistence.objectstore.transaction.CreateObjectCommand;
+import org.apache.isis.core.runtime.persistence.objectstore.transaction.PersistenceCommandContext;
+
+public final class InMemoryCreateObjectCommand extends AbstractInMemoryPersistenceCommand implements CreateObjectCommand {
+ private final static Logger LOG = LoggerFactory.getLogger(InMemoryCreateObjectCommand.class);
+
+ public InMemoryCreateObjectCommand(final ObjectAdapter object, final ObjectStorePersistedObjects persistedObjects) {
+ super(object, persistedObjects);
+ }
+
+ @Override
+ public void execute(final PersistenceCommandContext context) throws ObjectPersistenceException {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug(" create object " + onAdapter());
+ }
+ save(onAdapter());
+ }
+
+ @Override
+ public String toString() {
+ return "CreateObjectCommand [object=" + onAdapter() + "]";
+ }
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/InMemoryDestroyObjectCommand.java
----------------------------------------------------------------------
diff --git a/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/InMemoryDestroyObjectCommand.java b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/InMemoryDestroyObjectCommand.java
new file mode 100644
index 0000000..16093ab
--- /dev/null
+++ b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/InMemoryDestroyObjectCommand.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.objectstore.commands;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.objectstore.internal.ObjectStorePersistedObjects;
+import org.apache.isis.core.runtime.persistence.ObjectPersistenceException;
+import org.apache.isis.core.runtime.persistence.objectstore.transaction.DestroyObjectCommand;
+import org.apache.isis.core.runtime.persistence.objectstore.transaction.PersistenceCommandContext;
+
+public final class InMemoryDestroyObjectCommand extends AbstractInMemoryPersistenceCommand implements DestroyObjectCommand {
+ private final static Logger LOG = LoggerFactory.getLogger(InMemoryDestroyObjectCommand.class);
+
+ public InMemoryDestroyObjectCommand(final ObjectAdapter adapter, final ObjectStorePersistedObjects persistedObjects) {
+ super(adapter, persistedObjects);
+ }
+
+ @Override
+ public void execute(final PersistenceCommandContext context) throws ObjectPersistenceException {
+ if (LOG.isInfoEnabled()) {
+ LOG.info(" delete object '" + onAdapter() + "'");
+ }
+ destroy(onAdapter());
+ }
+
+ @Override
+ public String toString() {
+ return "DestroyObjectCommand [object=" + onAdapter() + "]";
+ }
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/InMemorySaveObjectCommand.java
----------------------------------------------------------------------
diff --git a/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/InMemorySaveObjectCommand.java b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/InMemorySaveObjectCommand.java
new file mode 100644
index 0000000..4b48f61
--- /dev/null
+++ b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/commands/InMemorySaveObjectCommand.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.objectstore.commands;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.objectstore.internal.ObjectStorePersistedObjects;
+import org.apache.isis.core.runtime.persistence.ObjectPersistenceException;
+import org.apache.isis.core.runtime.persistence.objectstore.transaction.PersistenceCommandContext;
+import org.apache.isis.core.runtime.persistence.objectstore.transaction.SaveObjectCommand;
+
+public final class InMemorySaveObjectCommand extends AbstractInMemoryPersistenceCommand implements SaveObjectCommand {
+
+ @SuppressWarnings("unused")
+ private final static Logger LOG = LoggerFactory.getLogger(InMemorySaveObjectCommand.class);
+
+ public InMemorySaveObjectCommand(final ObjectAdapter object, final ObjectStorePersistedObjects persistedObjects) {
+ super(object, persistedObjects);
+ }
+
+ @Override
+ public void execute(final PersistenceCommandContext context) throws ObjectPersistenceException {
+ save(onAdapter());
+ }
+
+ @Override
+ public String toString() {
+ return "SaveObjectCommand [object=" + onAdapter() + "]";
+ }
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances.java
----------------------------------------------------------------------
diff --git a/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances.java b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances.java
new file mode 100644
index 0000000..b24f332
--- /dev/null
+++ b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances.java
@@ -0,0 +1,246 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.objectstore.internal;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.isis.applib.clock.Clock;
+import org.apache.isis.core.commons.authentication.AuthenticationSession;
+import org.apache.isis.core.commons.debug.DebugBuilder;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.adapter.mgr.AdapterManager;
+import org.apache.isis.core.metamodel.adapter.oid.Oid;
+import org.apache.isis.core.metamodel.adapter.version.SerialNumberVersion;
+import org.apache.isis.core.metamodel.adapter.version.Version;
+import org.apache.isis.core.metamodel.spec.ObjectSpecId;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+import org.apache.isis.core.objectstore.InMemoryObjectStore;
+import org.apache.isis.core.runtime.persistence.query.PersistenceQueryBuiltIn;
+import org.apache.isis.core.runtime.system.context.IsisContext;
+import org.apache.isis.core.runtime.system.persistence.PersistenceSession;
+
+/*
+ * The objects need to store in a repeatable sequence so the elements and instances method return the same data for any repeated
+ * call, and so that one subset of instances follows on the previous. This is done by keeping the objects in the order that they
+ * where created.
+ */
+public class ObjectStoreInstances {
+
+ private final Map<Oid, Object> pojoByOidMap = new HashMap<Oid, Object>();
+ private final Map<Oid, Version> versionByOidMap = new HashMap<Oid, Version>();
+
+ @SuppressWarnings("unused")
+ private final ObjectSpecId spec;
+
+ // ///////////////////////////////////////////////////////
+ // Constructors
+ // ///////////////////////////////////////////////////////
+
+ public ObjectStoreInstances(final ObjectSpecId spec) {
+ this.spec = spec;
+ }
+
+ // ///////////////////////////////////////////////////////
+ // Object Instances
+ // ///////////////////////////////////////////////////////
+
+ /**
+ * TODO: shouldn't really be exposing this directly.
+ */
+ public Map<Oid, Object> getObjectInstances() {
+ return pojoByOidMap;
+ }
+
+ public Set<Oid> getOids() {
+ return Collections.unmodifiableSet(pojoByOidMap.keySet());
+ }
+
+ public Object getPojo(final Oid oid) {
+ return pojoByOidMap.get(oid);
+ }
+
+ public Version getVersion(final Oid oid) {
+ return versionByOidMap.get(oid);
+ }
+
+ // ///////////////////////////////////////////////////////
+ // shutdown
+ // ///////////////////////////////////////////////////////
+
+ public void shutdown() {
+ pojoByOidMap.clear();
+ versionByOidMap.clear();
+ }
+
+ // ///////////////////////////////////////////////////////
+ // save, remove
+ // ///////////////////////////////////////////////////////
+
+ public void save(final ObjectAdapter adapter) {
+ pojoByOidMap.put(adapter.getOid(), adapter.getObject());
+
+ final Version version = versionByOidMap.get(adapter.getOid());
+ final Version nextVersion = nextVersion(version);
+ versionByOidMap.put(adapter.getOid(), nextVersion);
+ adapter.setVersion(nextVersion);
+ }
+
+ private synchronized Version nextVersion(final Version version) {
+ final long sequence = (version != null ? version.getSequence() : 0) + 1;
+ return SerialNumberVersion.create(sequence, getAuthenticationSession().getUserName(), new Date(Clock.getTime()));
+ }
+
+ public void remove(final Oid oid) {
+ pojoByOidMap.remove(oid);
+ versionByOidMap.remove(oid);
+ }
+
+ // ///////////////////////////////////////////////////////
+ // retrieveObject
+ // ///////////////////////////////////////////////////////
+
+ /**
+ * If the pojo exists in the object store, then looks up the
+ * {@link ObjectAdapter adapter} from the {@link org.apache.isis.core.runtime.persistence.adaptermanager.AdapterManagerDefault}, and only
+ * if none found does it recreates a new {@link ObjectAdapter adapter}.
+ */
+ public ObjectAdapter getObjectAndMapIfRequired(final Oid oid) {
+ final Object pojo = getObjectInstances().get(oid);
+ if (pojo == null) {
+ return null;
+ }
+ final ObjectAdapter adapterLookedUpByPojo = getAdapterManager().getAdapterFor(pojo);
+ if (adapterLookedUpByPojo != null) {
+ return adapterLookedUpByPojo;
+ }
+ final ObjectAdapter adapterLookedUpByOid = getAdapterManager().getAdapterFor(oid);
+ if (adapterLookedUpByOid != null) {
+ return adapterLookedUpByOid;
+ }
+ return getPersistenceSession().getAdapterManager().mapRecreatedPojo(oid, pojo);
+ }
+
+ // ///////////////////////////////////////////////////////
+ // instances, numberOfInstances, hasInstances
+ // ///////////////////////////////////////////////////////
+
+ /**
+ * Not API, but <tt>public</tt> so can be called by
+ * {@link InMemoryObjectStore}.
+ */
+ public void findInstancesAndAdd(final PersistenceQueryBuiltIn persistenceQuery, final List<ObjectAdapter> foundInstances) {
+ for (final ObjectAdapter element : elements()) {
+ if (persistenceQuery.matches(element)) {
+ foundInstances.add(element);
+ }
+ }
+ }
+
+ public int numberOfInstances() {
+ return getObjectInstances().size();
+ }
+
+ public boolean hasInstances() {
+ return numberOfInstances() > 0;
+ }
+
+ private List<ObjectAdapter> elements() {
+ final List<ObjectAdapter> v = new ArrayList<ObjectAdapter>(getObjectInstances().size());
+ for (final Oid oid : getObjectInstances().keySet()) {
+ v.add(getObjectAndMapIfRequired(oid));
+ }
+ return v;
+ }
+
+ // ///////////////////////////////////////////////////////
+ // Debugging
+ // ///////////////////////////////////////////////////////
+
+ public void debugData(final DebugBuilder debug) {
+ debug.indent();
+ if (getObjectInstances().size() == 0) {
+ debug.appendln("no instances");
+ }
+ for (final Oid oid : getObjectInstances().keySet()) {
+ final ObjectAdapter objectAdapter = getObjectAndMapIfRequired(oid);
+ final String title = objectAdapter.titleString();
+ final Object object = getObjectInstances().get(oid);
+ debug.appendln(oid.toString(), object + " (" + title + ")");
+ }
+ debug.appendln();
+ debug.unindent();
+ }
+
+ // ///////////////////////////////////////////////////////
+ // Dependencies (from context)
+ // ///////////////////////////////////////////////////////
+
+ /**
+ * Must use {@link IsisContext context}, because although this object is
+ * recreated with each {@link PersistenceSession session}, the persisted
+ * objects that get
+ * {@link #attachPersistedObjects(MemoryObjectStorePersistedObjects)
+ * attached} to it span multiple sessions.
+ *
+ * <p>
+ * The alternative design would be to laboriously inject this object via the
+ * {@link InMemoryObjectStore}.
+ */
+ protected PersistenceSession getPersistenceSession() {
+ return IsisContext.getPersistenceSession();
+ }
+
+ /**
+ * Must use {@link IsisContext context}, because although this object is
+ * recreated with each {@link PersistenceSession session}, the persisted
+ * objects that get
+ * {@link #attachPersistedObjects(MemoryObjectStorePersistedObjects)
+ * attached} to it span multiple sessions.
+ *
+ * <p>
+ * The alternative design would be to laboriously inject this object via the
+ * {@link InMemoryObjectStore}.
+ */
+ protected AdapterManager getAdapterManager() {
+ return getPersistenceSession().getAdapterManager();
+ }
+
+ /**
+ * Must use {@link IsisContext context}, because although this object is
+ * recreated with each {@link PersistenceSession session}, the persisted
+ * objects that get
+ * {@link #attachPersistedObjects(MemoryObjectStorePersistedObjects)
+ * attached} to it span multiple sessions.
+ *
+ * <p>
+ * The alternative design would be to laboriously inject this object via the
+ * {@link InMemoryObjectStore}.
+ */
+ protected AuthenticationSession getAuthenticationSession() {
+ return IsisContext.getAuthenticationSession();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjects.java
----------------------------------------------------------------------
diff --git a/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjects.java b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjects.java
new file mode 100644
index 0000000..4e3cf35
--- /dev/null
+++ b/mothballed/objectstore-inmemory/src/main/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjects.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.objectstore.internal;
+
+import java.util.Map;
+import java.util.Set;
+
+import com.google.common.collect.Maps;
+
+import org.apache.isis.core.commons.exceptions.IsisException;
+import org.apache.isis.core.metamodel.adapter.oid.Oid;
+import org.apache.isis.core.metamodel.spec.ObjectSpecId;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+import org.apache.isis.core.runtime.system.persistence.IdentifierGeneratorDefault;
+
+/**
+ * Represents the persisted objects.
+ *
+ * Attached and detached to each session.
+ */
+public class ObjectStorePersistedObjects {
+
+ private final Map<ObjectSpecId, ObjectStoreInstances> instancesBySpecMap = Maps.newHashMap();
+ private final Map<ObjectSpecId, Oid> serviceOidByIdMap = Maps.newHashMap();
+
+ private IdentifierGeneratorDefault.Memento oidGeneratorMemento;
+
+
+ public IdentifierGeneratorDefault.Memento getOidGeneratorMemento() {
+ return oidGeneratorMemento;
+ }
+
+ public void saveOidGeneratorMemento(final IdentifierGeneratorDefault.Memento memento) {
+ this.oidGeneratorMemento = memento;
+ }
+
+ public Oid getService(final ObjectSpecId objectSpecId) {
+ return serviceOidByIdMap.get(objectSpecId);
+ }
+
+ public void registerService(final ObjectSpecId objectSpecId, final Oid oid) {
+ final Oid oidLookedUpByName = serviceOidByIdMap.get(objectSpecId);
+ if (oidLookedUpByName != null) {
+ if (oidLookedUpByName.equals(oid)) {
+ throw new IsisException("Already another service registered as name: " + objectSpecId + " (existing Oid: " + oidLookedUpByName + ", " + "intended: " + oid + ")");
+ }
+ } else {
+ serviceOidByIdMap.put(objectSpecId, oid);
+ }
+ }
+
+ // TODO: this is where the clever logic needs to go to determine how to save
+ // into our custom Map.
+ // also think we shouldn't surface the entire Map, just the API we require
+ // (keySet, values etc).
+ public ObjectStoreInstances instancesFor(final ObjectSpecId specId) {
+ ObjectStoreInstances ins = instancesBySpecMap.get(specId);
+ if (ins == null) {
+ ins = new ObjectStoreInstances(specId);
+ instancesBySpecMap.put(specId, ins);
+ }
+ return ins;
+ }
+
+ public Iterable<ObjectSpecId> specifications() {
+ return instancesBySpecMap.keySet();
+ }
+
+ public void clear() {
+ instancesBySpecMap.clear();
+ }
+
+ public Iterable<ObjectStoreInstances> instances() {
+ return instancesBySpecMap.values();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances_findInstancesAndAdd.java
----------------------------------------------------------------------
diff --git a/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances_findInstancesAndAdd.java b/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances_findInstancesAndAdd.java
new file mode 100644
index 0000000..45294aa
--- /dev/null
+++ b/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances_findInstancesAndAdd.java
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.objectstore.internal;
+
+import java.util.List;
+import com.google.common.collect.Lists;
+import org.jmock.Expectations;
+import org.jmock.auto.Mock;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.apache.isis.core.commons.authentication.AuthenticationSession;
+import org.apache.isis.core.commons.matchers.IsisMatchers;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.spec.ObjectSpecId;
+import org.apache.isis.core.runtime.persistence.adaptermanager.AdapterManagerDefault;
+import org.apache.isis.core.runtime.persistence.query.PersistenceQueryBuiltIn;
+import org.apache.isis.core.unittestsupport.jmocking.IsisActions;
+import org.apache.isis.core.unittestsupport.jmocking.JUnitRuleMockery2;
+import org.apache.isis.core.unittestsupport.jmocking.JUnitRuleMockery2.Mode;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class ObjectStoreInstances_findInstancesAndAdd {
+
+ private ObjectStoreInstances instances;
+
+ @Rule
+ public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES);
+
+ @Mock
+ private ObjectSpecId mockSpecId;
+ @Mock
+ private PersistenceQueryBuiltIn mockPersistenceQueryBuiltIn;
+ @Mock
+ private AuthenticationSession mockAuthSession;
+ @Mock
+ private AdapterManagerDefault mockAdapterManager;
+
+ @Mock
+ private ObjectAdapter mockAdapter1;
+ @Mock
+ private ObjectAdapter mockAdapter2;
+
+ @Before
+ public void setUp() throws Exception {
+ instances = new ObjectStoreInstances(mockSpecId) {
+ @Override
+ protected AuthenticationSession getAuthenticationSession() {
+ return mockAuthSession;
+ }
+ @Override
+ protected AdapterManagerDefault getAdapterManager() {
+ return mockAdapterManager;
+ }
+ };
+ context.ignoring(mockAuthSession);
+ }
+
+ @Test
+ public void findInstancesAndAdd_whenEmpty() throws Exception {
+ context.never(mockPersistenceQueryBuiltIn);
+ final List<ObjectAdapter> foundInstances = Lists.newArrayList();
+ instances.findInstancesAndAdd(mockPersistenceQueryBuiltIn, foundInstances);
+ }
+
+ @Test
+ public void findInstancesAndAdd_whenNotEmpty() throws Exception {
+ context.ignoring(mockAdapter1, mockAdapter2);
+ context.checking(new Expectations() {
+ {
+ one(mockPersistenceQueryBuiltIn).matches(mockAdapter1);
+ will(returnValue(false));
+
+ one(mockPersistenceQueryBuiltIn).matches(mockAdapter2);
+ will(returnValue(true));
+
+ allowing(mockAdapterManager).getAdapterFor(with(any(Object.class)));
+ will(IsisActions.returnEach(mockAdapter1, mockAdapter2));
+ }
+ });
+
+ instances.save(mockAdapter1);
+ instances.save(mockAdapter2);
+
+ final List<ObjectAdapter> foundInstances = Lists.newArrayList();
+ instances.findInstancesAndAdd(mockPersistenceQueryBuiltIn, foundInstances);
+
+ assertThat(foundInstances.size(), is(1));
+ assertThat(foundInstances, IsisMatchers.listContaining(mockAdapter2));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances_init.java
----------------------------------------------------------------------
diff --git a/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances_init.java b/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances_init.java
new file mode 100644
index 0000000..cd2c384
--- /dev/null
+++ b/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances_init.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.objectstore.internal;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.apache.isis.core.metamodel.adapter.oid.Oid;
+import org.apache.isis.core.metamodel.spec.ObjectSpecId;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+
+/**
+ * Tested in style of <i>Working Effectively with Legacy Code</i> (Feathers) and
+ * <i>Growing Object-Oriented Software</i> (Freeman & Pryce).
+ */
+@RunWith(JMock.class)
+public class ObjectStoreInstances_init {
+
+ private ObjectStoreInstances instances;
+
+ private final Mockery context = new JUnit4Mockery();
+
+ private ObjectSpecId mockSpecId;
+
+ @Before
+ public void setUp() throws Exception {
+ mockSpecId = context.mock(ObjectSpecId.class);
+ instances = new ObjectStoreInstances(mockSpecId);
+ }
+
+ @Test
+ public void initiallyEmpty() throws Exception {
+ final Map<Oid, Object> objectInstances = instances.getObjectInstances();
+ assertThat(objectInstances.size(), is(0));
+
+ final Set<Oid> oids = instances.getOids();
+ assertThat(oids.size(), is(0));
+
+ assertThat(instances.hasInstances(), is(false));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances_save.java
----------------------------------------------------------------------
diff --git a/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances_save.java b/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances_save.java
new file mode 100644
index 0000000..9d93d54
--- /dev/null
+++ b/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStoreInstances_save.java
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.objectstore.internal;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.apache.isis.core.commons.authentication.AuthenticationSession;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.adapter.oid.Oid;
+import org.apache.isis.core.metamodel.adapter.version.Version;
+import org.apache.isis.core.metamodel.spec.ObjectSpecId;
+
+@RunWith(JMock.class)
+public class ObjectStoreInstances_save {
+
+ private ObjectStoreInstances instances;
+
+ private final Mockery context = new JUnit4Mockery();
+
+ private ObjectSpecId mockSpecId;
+ private ObjectAdapter mockAdapter;
+ private AuthenticationSession mockAuthSession;
+
+ @Before
+ public void setUp() throws Exception {
+ mockSpecId = context.mock(ObjectSpecId.class);
+ mockAdapter = context.mock(ObjectAdapter.class);
+ mockAuthSession = context.mock(AuthenticationSession.class);
+ instances = new ObjectStoreInstances(mockSpecId) {
+ @Override
+ protected AuthenticationSession getAuthenticationSession() {
+ return mockAuthSession;
+ }
+ };
+ ignoreAuthenticationSession();
+ }
+
+ private void ignoreAuthenticationSession() {
+ context.checking(new Expectations() {
+ {
+ ignoring(mockAuthSession);
+ }
+ });
+ }
+
+ @Test
+ public void saveUpdatesTheOptimisticLock() throws Exception {
+ allowingGetOidAndGetObjectAndTitleStringFromAdapter();
+ context.checking(new Expectations() {
+ {
+ one(mockAdapter).setVersion(with(any(Version.class)));
+ }
+ });
+ instances.save(mockAdapter);
+
+ }
+
+ @Test
+ public void saveStoresObject() throws Exception {
+ allowingGetOidAndGetObjectAndTitleStringFromAdapter();
+ ignoringInteractionsWithAdapter();
+ instances.save(mockAdapter);
+
+ final Map<Oid, Object> objectInstances = instances.getObjectInstances();
+ assertThat(objectInstances.size(), is(1));
+
+ final Set<Oid> oids = instances.getOids();
+ assertThat(oids.size(), is(1));
+
+ assertThat(instances.hasInstances(), is(true));
+
+ }
+
+ private void ignoringInteractionsWithAdapter() {
+ context.checking(new Expectations() {
+ {
+ ignoring(mockAdapter);
+ }
+ });
+ }
+
+ private void allowingGetOidAndGetObjectAndTitleStringFromAdapter() {
+ context.checking(new Expectations() {
+ {
+ allowing(mockAdapter).getOid();
+ allowing(mockAdapter).getObject();
+ allowing(mockAdapter).titleString();
+ }
+ });
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/a466fb50/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_instances.java
----------------------------------------------------------------------
diff --git a/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_instances.java b/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_instances.java
new file mode 100644
index 0000000..4274613
--- /dev/null
+++ b/mothballed/objectstore-inmemory/src/test/java/org/apache/isis/core/objectstore/internal/ObjectStorePersistedObjectsDefault_instances.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.objectstore.internal;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.apache.isis.core.metamodel.spec.ObjectSpecId;
+
+@RunWith(JMock.class)
+public class ObjectStorePersistedObjectsDefault_instances {
+
+ private ObjectStorePersistedObjects persistedObjects;
+
+ private final Mockery context = new JUnit4Mockery();
+
+ private ObjectSpecId mockSpec;
+
+ @Before
+ public void setUp() throws Exception {
+ persistedObjects = new ObjectStorePersistedObjects();
+ mockSpec = context.mock(ObjectSpecId.class);
+ }
+
+ @Test
+ public void instancesLazilyPopulatedWhenAskForThem() throws Exception {
+ neverInteractsWithSpec();
+
+ // no instances
+ final Iterable<ObjectStoreInstances> instancesBefore = persistedObjects.instances();
+ assertThat(instancesBefore.iterator().hasNext(), is(false));
+
+ ensureThereAreSomeInstances();
+
+ // now there are
+ final Iterable<ObjectStoreInstances> instancesAfter = persistedObjects.instances();
+ assertThat(instancesAfter.iterator().hasNext(), is(true));
+ }
+
+ @Test
+ public void clearZapsTheInstances() throws Exception {
+ neverInteractsWithSpec();
+
+ ensureThereAreSomeInstances();
+ final Iterable<ObjectStoreInstances> instancesAfter = persistedObjects.instances();
+ assertThat(instancesAfter.iterator().hasNext(), is(true));
+
+ persistedObjects.clear();
+
+ // now there are no more instances
+ final Iterable<ObjectStoreInstances> instancesBefore = persistedObjects.instances();
+ assertThat(instancesBefore.iterator().hasNext(), is(false));
+ }
+
+ private void ensureThereAreSomeInstances() {
+ persistedObjects.instancesFor(mockSpec);
+ }
+
+ private void neverInteractsWithSpec() {
+ context.checking(new Expectations() {
+ {
+ never(mockSpec);
+ }
+ });
+ }
+
+}