You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by jc...@apache.org on 2013/11/29 13:55:06 UTC

git commit: ISIS-619: extend IsisAction with method to instantiate classes

Updated Branches:
  refs/heads/master d852a683b -> 325638a65


ISIS-619: extend IsisAction with method to instantiate classes


Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/325638a6
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/325638a6
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/325638a6

Branch: refs/heads/master
Commit: 325638a655d6b6a6281dd7cc47d11ac65c7c84c0
Parents: d852a68
Author: Jeroen van der Wal <je...@stromboli.it>
Authored: Fri Nov 29 13:53:39 2013 +0100
Committer: Jeroen van der Wal <je...@stromboli.it>
Committed: Fri Nov 29 13:53:39 2013 +0100

----------------------------------------------------------------------
 .../unittestsupport/jmocking/IsisActions.java   | 18 ++++
 ...sActionsTest_returnNewTransientInstance.java | 93 ++++++++++++++++++++
 2 files changed, 111 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/isis/blob/325638a6/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jmocking/IsisActions.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jmocking/IsisActions.java b/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jmocking/IsisActions.java
index 33f5886..99afeb9 100644
--- a/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jmocking/IsisActions.java
+++ b/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jmocking/IsisActions.java
@@ -18,7 +18,9 @@
  */
 package org.apache.isis.core.unittestsupport.jmocking;
 
+import org.hamcrest.Description;
 import org.jmock.api.Action;
+import org.jmock.api.Invocation;
 
 public final class IsisActions {
     
@@ -37,5 +39,21 @@ public final class IsisActions {
         return JMockActions.returnArgument(i);
     }
 
+    public static Action returnNewTransientInstance() {
+        return new Action(){
+    
+            @Override
+            public void describeTo(Description description) {
+                description.appendText("new transient instance");
+            }
+    
+            @Override
+            public Object invoke(Invocation invocation) throws Throwable {
+                Class<?> cls = (Class<?>) invocation.getParameter(0);
+                return cls.newInstance();
+            }
+        };
+    }
+
 
 }

http://git-wip-us.apache.org/repos/asf/isis/blob/325638a6/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/IsisActionsTest_returnNewTransientInstance.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/IsisActionsTest_returnNewTransientInstance.java b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/IsisActionsTest_returnNewTransientInstance.java
new file mode 100644
index 0000000..4bbdc8b
--- /dev/null
+++ b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/IsisActionsTest_returnNewTransientInstance.java
@@ -0,0 +1,93 @@
+/*
+ *
+ *  Copyright 2012-2013 Eurocommercial Properties NV
+ *
+ *
+ *  Licensed 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.unittestsupport.jmocking;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+import org.jmock.Expectations;
+import org.jmock.auto.Mock;
+import org.junit.Rule;
+import org.junit.Test;
+
+import org.apache.isis.core.unittestsupport.jmocking.JUnitRuleMockery2.Mode;
+
+public class IsisActionsTest_returnNewTransientInstance {
+
+    // we can't use the 'real' DomainObjectConainter because applib depends on this module, not vice versa
+    // but it doesn't matter; we are just testing the action (of the expectation), not the object on which 
+    // we add the expectation 
+    public static interface MyDomainObjectContainer {
+        <T> T newTransientInstance(Class<T> t);
+
+        void persistIfNotAlready(Object o);
+    }
+
+    public static class MyCustomer  {
+    }
+
+    @Mock
+    private MyDomainObjectContainer mockContainer;
+
+    @Rule
+    public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES);
+
+
+    @Test
+    public void testIt() {
+
+        context.checking(new Expectations() {
+            {
+                allowing(mockContainer).newTransientInstance(with(anySubclassOf(Object.class)));
+                will(IsisActions.returnNewTransientInstance());
+                ignoring(mockContainer);
+            }
+        });
+        
+        // is allowed (and executed)
+        MyCustomer o = mockContainer.newTransientInstance(MyCustomer.class);
+        assertThat(o, is(not(nullValue())));
+        
+        // is ignored
+        mockContainer.persistIfNotAlready(o);
+    }
+    
+
+    private static <X> Matcher<Class<X>> anySubclassOf(final Class<X> cls) {
+        return new TypeSafeMatcher<Class<X>>() {
+
+            @Override
+            public void describeTo(final Description arg0) {
+                arg0.appendText("is subclass of ").appendText(cls.getName());
+            }
+
+            @Override
+            public boolean matchesSafely(final Class<X> item) {
+                return cls.isAssignableFrom(item);
+            }
+        };
+    }
+
+
+}