[ 
https://issues.apache.org/jira/browse/GEODE-3893?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16215857#comment-16215857
 ] 

ASF GitHub Bot commented on GEODE-3893:
---------------------------------------

upthewaterspout closed pull request #31: GEODE-3893: Replace mocks utility 
class for all examples with per-example mocks
URL: https://github.com/apache/geode-examples/pull/31
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/async/src/test/java/org/apache/geode/examples/async/ExampleAsyncEventListenerTest.java
 
b/async/src/test/java/org/apache/geode/examples/async/ExampleAsyncEventListenerTest.java
index 49b12ea..2a2ca15 100644
--- 
a/async/src/test/java/org/apache/geode/examples/async/ExampleAsyncEventListenerTest.java
+++ 
b/async/src/test/java/org/apache/geode/examples/async/ExampleAsyncEventListenerTest.java
@@ -19,11 +19,12 @@
 import java.util.LinkedList;
 import java.util.List;
 
-import org.geode.examples.util.TestAsyncEvent;
 import org.junit.Test;
 
 import org.apache.geode.cache.Operation;
+import org.apache.geode.cache.Region;
 import org.apache.geode.cache.asyncqueue.AsyncEvent;
+import org.apache.geode.cache.wan.EventSequenceID;
 
 public class ExampleAsyncEventListenerTest {
   @Test
@@ -44,4 +45,58 @@ public void testSpellCheck() {
     assertEquals("will", listener.spellCheck("wil"));
     assertEquals("I", listener.spellCheck("i"));
   }
+
+  public class TestAsyncEvent<K, V> implements AsyncEvent<K, V> {
+    private final Region region;
+    private final Operation operation;
+    private final K key;
+    private final V value;
+
+    public TestAsyncEvent(Region region, Operation operation, K key, V value) {
+      this.region = region;
+      this.operation = operation;
+      this.key = key;
+      this.value = value;
+    }
+
+    @Override
+    public boolean getPossibleDuplicate() {
+      return false;
+    }
+
+    @Override
+    public EventSequenceID getEventSequenceID() {
+      return null;
+    }
+
+    @Override
+    public Region<K, V> getRegion() {
+      return region;
+    }
+
+    @Override
+    public Operation getOperation() {
+      return operation;
+    }
+
+    @Override
+    public Object getCallbackArgument() {
+      return null;
+    }
+
+    @Override
+    public K getKey() {
+      return key;
+    }
+
+    @Override
+    public V getDeserializedValue() {
+      return value;
+    }
+
+    @Override
+    public byte[] getSerializedValue() {
+      return new byte[0];
+    }
+  }
 }
diff --git 
a/async/src/test/java/org/apache/geode/examples/async/ExampleTest.java 
b/async/src/test/java/org/apache/geode/examples/async/ExampleTest.java
index 834c318..3b0117d 100644
--- a/async/src/test/java/org/apache/geode/examples/async/ExampleTest.java
+++ b/async/src/test/java/org/apache/geode/examples/async/ExampleTest.java
@@ -15,6 +15,7 @@
 package org.apache.geode.examples.async;
 
 import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -23,7 +24,6 @@
 
 import org.apache.geode.cache.Region;
 
-import org.geode.examples.util.Mocks;
 import org.junit.Test;
 
 public class ExampleTest {
@@ -31,8 +31,8 @@
   public void testExample() throws Exception {
     Example example = new Example();
 
-    Region<String, String> outgoingRegion = 
Mocks.region(Example.OUTGOING_REGION_NAME);
-    Region<Integer, String> incomingRegion = 
Mocks.region(Example.INCOMING_REGION_NAME);
+    Region<String, String> outgoingRegion = mock(Region.class);
+    Region<Integer, String> incomingRegion = mock(Region.class);
 
     final List<String> words = Arrays.asList(new String[] {"that", "teh"});
     when(outgoingRegion.sizeOnServer()).thenReturn(words.size());
diff --git 
a/functions/src/test/java/org/apache/geode/examples/functions/ExampleTest.java 
b/functions/src/test/java/org/apache/geode/examples/functions/ExampleTest.java
index cfb3ad5..0748b5b 100644
--- 
a/functions/src/test/java/org/apache/geode/examples/functions/ExampleTest.java
+++ 
b/functions/src/test/java/org/apache/geode/examples/functions/ExampleTest.java
@@ -15,6 +15,8 @@
 package org.apache.geode.examples.functions;
 
 import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 import java.util.Arrays;
 import java.util.HashSet;
@@ -22,8 +24,8 @@
 
 import org.apache.geode.cache.Region;
 import org.apache.geode.cache.execute.Execution;
+import org.apache.geode.cache.execute.ResultCollector;
 
-import org.geode.examples.util.Mocks;
 import org.junit.Test;
 
 public class ExampleTest {
@@ -31,9 +33,12 @@
   public void testExample() throws Exception {
     Example example = new Example(10);
 
-    Region<Integer, String> region = Mocks.region("example-region");
+    Region<Integer, String> region = mock(Region.class);
     List<Integer> primes = Arrays.asList(1, 2, 3, 5, 7);
-    Execution execution = Mocks.execution(PrimeNumber.ID, primes);
+    ResultCollector resultCollector = mock(ResultCollector.class);
+    when(resultCollector.getResult()).thenReturn(primes);
+    Execution execution = mock(Execution.class);
+    when(execution.execute(PrimeNumber.ID)).thenReturn(resultCollector);
 
     assertEquals(new HashSet(primes), example.getPrimes(region, execution));
   }
diff --git 
a/listener/src/test/java/org/apache/geode/examples/listener/ExampleTest.java 
b/listener/src/test/java/org/apache/geode/examples/listener/ExampleTest.java
index fccda67..14db282 100644
--- a/listener/src/test/java/org/apache/geode/examples/listener/ExampleTest.java
+++ b/listener/src/test/java/org/apache/geode/examples/listener/ExampleTest.java
@@ -19,8 +19,6 @@
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.geode.cache.Region;
-import org.geode.examples.util.Mocks;
 import org.junit.Test;
 
 public class ExampleTest {
diff --git 
a/loader/src/test/java/org/apache/geode/examples/loader/ExampleTest.java 
b/loader/src/test/java/org/apache/geode/examples/loader/ExampleTest.java
index 2a1f38a..40609de 100644
--- a/loader/src/test/java/org/apache/geode/examples/loader/ExampleTest.java
+++ b/loader/src/test/java/org/apache/geode/examples/loader/ExampleTest.java
@@ -24,8 +24,7 @@
 import java.util.Map;
 
 import org.apache.geode.cache.LoaderHelper;
-import org.apache.geode.cache.Region;
-import org.geode.examples.util.Mocks;
+
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.contrib.java.lang.system.SystemOutRule;
diff --git a/src/test/java/org/geode/examples/util/Mocks.java 
b/src/test/java/org/geode/examples/util/Mocks.java
deleted file mode 100644
index b1e0d8f..0000000
--- a/src/test/java/org/geode/examples/util/Mocks.java
+++ /dev/null
@@ -1,80 +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.geode.examples.util;
-
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.geode.cache.Region;
-import org.apache.geode.cache.execute.Execution;
-import org.apache.geode.cache.execute.ResultCollector;
-
-import org.mockito.invocation.InvocationOnMock;
-
-import org.apache.geode.cache.Region;
-
-public class Mocks {
-  private Mocks() {
-  }
-
-  @SuppressWarnings("unchecked")
-  public static <K, V> Region<K, V> region(String name) throws Exception {
-    Map<K, V> data = new HashMap<>();
-    Region<K, V> region = mock(Region.class);
-
-    when(region.getName()).thenReturn(name);
-    when(region.put(any(), any())).then(inv -> data.put(getKey(inv), 
getValue(inv)));
-    when(region.get(any())).then(inv -> data.get(getKey(inv)));
-    when(region.keySet()).thenReturn(data.keySet());
-    when(region.values()).thenReturn(data.values());
-    when(region.size()).then(inv -> { return data.size(); });
-    when(region.keySetOnServer()).thenReturn(data.keySet());
-    when(region.containsKey(any())).then(inv -> data.containsKey(getKey(inv)));
-    when(region.containsKeyOnServer(any())).then(inv -> 
data.containsKey(getKey(inv)));
-
-    doAnswer(inv -> {
-      data.putAll((Map<? extends K, ? extends V>) inv.getArguments()[0]);
-      return inv.getArguments();
-    }).when(region).putAll(any());
-
-    return region;
-  }
-
-  @SuppressWarnings("unchecked")
-  public static Execution execution(String functionId, Object result) throws 
Exception {
-    ResultCollector resultCollector = mock(ResultCollector.class);
-    when(resultCollector.getResult()).thenReturn(result);
-
-    Execution execution = mock(Execution.class);
-    when(execution.execute(functionId)).thenReturn(resultCollector);
-
-    return execution;
-  }
-
-  @SuppressWarnings("unchecked")
-  private static <K> K getKey(InvocationOnMock inv) {
-    return (K) inv.getArguments()[0];
-  }
-
-  @SuppressWarnings("unchecked")
-  private static <V> V getValue(InvocationOnMock inv) {
-    return (V) inv.getArguments()[1];
-  }
-}
diff --git a/src/test/java/org/geode/examples/util/TestAsyncEvent.java 
b/src/test/java/org/geode/examples/util/TestAsyncEvent.java
deleted file mode 100644
index 1efe693..0000000
--- a/src/test/java/org/geode/examples/util/TestAsyncEvent.java
+++ /dev/null
@@ -1,73 +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.geode.examples.util;
-
-import org.apache.geode.cache.Operation;
-import org.apache.geode.cache.Region;
-import org.apache.geode.cache.asyncqueue.AsyncEvent;
-import org.apache.geode.cache.wan.EventSequenceID;
-
-public class TestAsyncEvent<K, V> implements AsyncEvent<K, V> {
-  private final Region region;
-  private final Operation operation;
-  private final K key;
-  private final V value;
-
-  public TestAsyncEvent(Region region, Operation operation, K key, V value) {
-    this.region = region;
-    this.operation = operation;
-    this.key = key;
-    this.value = value;
-  }
-  @Override
-  public boolean getPossibleDuplicate() {
-    return false;
-  }
-
-  @Override
-  public EventSequenceID getEventSequenceID() {
-    return null;
-  }
-
-  @Override
-  public Region<K, V> getRegion() {
-    return region;
-  }
-
-  @Override
-  public Operation getOperation() {
-    return operation;
-  }
-
-  @Override
-  public Object getCallbackArgument() {
-    return null;
-  }
-
-  @Override
-  public K getKey() {
-    return key;
-  }
-
-  @Override
-  public V getDeserializedValue() {
-    return value;
-  }
-
-  @Override
-  public byte[] getSerializedValue() {
-    return new byte[0];
-  }
-}
diff --git 
a/writer/src/test/java/org/apache/geode/examples/writer/ExampleTest.java 
b/writer/src/test/java/org/apache/geode/examples/writer/ExampleTest.java
index bfa252b..27cc719 100644
--- a/writer/src/test/java/org/apache/geode/examples/writer/ExampleTest.java
+++ b/writer/src/test/java/org/apache/geode/examples/writer/ExampleTest.java
@@ -17,6 +17,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import java.util.Arrays;
@@ -24,7 +25,6 @@
 import org.apache.geode.cache.CacheWriterException;
 import org.apache.geode.cache.Region;
 
-import org.geode.examples.util.Mocks;
 import org.junit.Test;
 
 public class ExampleTest {
@@ -32,7 +32,7 @@
   public void testExample() throws Exception {
     Example example = new Example();
 
-    Region<String, String> region = Mocks.region("example-region");
+    Region<String, String> region = mock(Region.class);
     when(region.put(eq("666-66-6666"), any())).thenThrow(new 
CacheWriterException());
     when(region.put(eq("8675309"), any())).thenThrow(new 
CacheWriterException());
     when(region.put(eq("999-000-0000"), any())).thenThrow(new 
CacheWriterException());


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Replace mocks utility class for all examples with per-example mocks
> -------------------------------------------------------------------
>
>                 Key: GEODE-3893
>                 URL: https://issues.apache.org/jira/browse/GEODE-3893
>             Project: Geode
>          Issue Type: Improvement
>          Components: examples
>            Reporter: Michael Dodge
>            Assignee: Michael Dodge
>             Fix For: 1.4.0
>
>
> org.geode.examples.util.Mocks provides some methods that provide region and 
> execution mocks. This introduces an unfortunate amount of coupling between 
> the different examples. It would be better if each example built its own 
> specific mocks.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to