forked from objectify/objectify
-
Notifications
You must be signed in to change notification settings - Fork 0
Ignore no-op load/save/delete actions properly #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nbali
wants to merge
1
commit into
master
Choose a base branch
from
tweaks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,4 @@ objectify.iml | |
| .svn | ||
| .gwt | ||
| .gradle | ||
| .factorypath | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 38 additions & 22 deletions
60
src/main/java/com/googlecode/objectify/util/ResultProxy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,57 @@ | ||
| package com.googlecode.objectify.util; | ||
|
|
||
| import com.google.common.collect.ForwardingList; | ||
| import com.google.common.collect.ForwardingMap; | ||
| import com.googlecode.objectify.Result; | ||
|
|
||
| import java.io.ObjectStreamException; | ||
| import java.io.Serializable; | ||
| import java.lang.reflect.InvocationHandler; | ||
| import java.lang.reflect.Method; | ||
| import java.lang.reflect.Proxy; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| /** | ||
| * A dynamic proxy that wraps a Result<?> value. For example, if you had a Result<List<String>>, the | ||
| * A utility class that wraps a Result<?> value. For example, if you had a Result<List<String>>, the | ||
| * proxy would implement List<String> and call through to the inner object. | ||
| */ | ||
| public class ResultProxy<T> implements InvocationHandler, Serializable | ||
| public class ResultProxy<T> | ||
| { | ||
| /** | ||
| * Create a ResultProxy for the given interface. | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public static <S> S create(Class<? super S> interf, Result<S> result) { | ||
| return (S)Proxy.newProxyInstance(result.getClass().getClassLoader(), new Class[] { interf }, new ResultProxy<>(result)); | ||
| public static <K, V> Map<K, V> createMap(final Result<Map<K, V>> result) { | ||
| return new ResultProxyMap<K, V>(result); | ||
| } | ||
|
|
||
| Result<T> result; | ||
| public static <E> List<E> createList(final Result<List<E>> result) { | ||
| return new ResultProxyList<E>(result); | ||
| } | ||
|
|
||
| @RequiredArgsConstructor | ||
| private static class ResultProxyList<E> extends ForwardingList<E> implements Serializable { | ||
|
|
||
| private final Result<List<E>> result; | ||
|
|
||
| @Override | ||
| protected List<E> delegate() { | ||
| return result.now(); | ||
| } | ||
|
|
||
| private ResultProxy(Result<T> result) { | ||
| this.result = result; | ||
| private Object writeReplace() throws ObjectStreamException { | ||
| return delegate(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Object invoke(Object obj, Method meth, Object[] params) throws Throwable { | ||
| return meth.invoke(result.now(), params); | ||
| @RequiredArgsConstructor | ||
| private static class ResultProxyMap<K, V> extends ForwardingMap<K, V> implements Serializable { | ||
|
|
||
| private final Result<Map<K, V>> result; | ||
|
|
||
| @Override | ||
| protected Map<K, V> delegate() { | ||
| return result.now(); | ||
| } | ||
|
|
||
| private Object writeReplace() throws ObjectStreamException { | ||
| return delegate(); | ||
| } | ||
| } | ||
|
|
||
| private Object writeReplace() throws ObjectStreamException { | ||
| return new NowProxy<>(result.now()); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/test/java/com/googlecode/objectify/test/IgnoreEmptyOperationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package com.googlecode.objectify.test; | ||
|
|
||
| import static com.googlecode.objectify.ObjectifyService.factory; | ||
| import static com.googlecode.objectify.ObjectifyService.ofy; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| import org.mockito.Mockito; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import com.google.appengine.api.datastore.DatastoreServiceConfig; | ||
| import com.google.common.collect.Lists; | ||
| import com.googlecode.objectify.Key; | ||
| import com.googlecode.objectify.test.entity.Trivial; | ||
| import com.googlecode.objectify.test.util.TestBase; | ||
| import com.googlecode.objectify.test.util.TestObjectifyFactory; | ||
|
|
||
| public class IgnoreEmptyOperationTests extends TestBase { | ||
|
|
||
| @Override | ||
| protected void setUpObjectifyFactory(TestObjectifyFactory factory) { | ||
| factory.register(Trivial.class); | ||
| super.setUpObjectifyFactory(Mockito.spy(factory)); | ||
| Mockito.verify(factory(), Mockito.times(1)).begin(); | ||
| } | ||
|
|
||
| // engines are responsible for datastore actions, and because all of them requires an AsyncDatastoreService | ||
| // it seems like a good idea to verify if it has been created or not | ||
| private void verifyNoRemoteCall() { | ||
| Mockito.verify(factory(), Mockito.never()) | ||
| .createAsyncDatastoreService(Mockito.any(DatastoreServiceConfig.class), Mockito.anyBoolean()); | ||
| Mockito.verifyNoMoreInteractions(factory()); | ||
| } | ||
|
|
||
| private void verifyThatRemoteCallWasExecuted(int times) { | ||
| Mockito.verify(factory(), Mockito.times(times)) | ||
| .createAsyncDatastoreService(Mockito.any(DatastoreServiceConfig.class), Mockito.anyBoolean()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptySave() { | ||
| ofy().save().entities(Lists.newArrayList()).now().size(); | ||
|
|
||
| verifyNoRemoteCall(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNotEmptySave() { | ||
| ofy().save().entities(Arrays.asList(new Trivial())).now().size(); | ||
|
|
||
| verifyThatRemoteCallWasExecuted(1); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyDelete() { | ||
| ofy().delete().entities(Lists.newArrayList()).now(); | ||
| ofy().delete().keys(Lists.<Key<Trivial>> newArrayList()).now(); | ||
| ofy().delete().type(Trivial.class).ids(Lists.newArrayList()).now(); | ||
|
|
||
| verifyNoRemoteCall(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNotEmptyDelete() { | ||
| final Trivial trivial = new Trivial(1L, "", 1L); | ||
| ofy().delete().entities(Arrays.asList(trivial)).now(); | ||
| ofy().delete().keys(Arrays.asList(Key.create(trivial))).now(); | ||
| ofy().delete().type(Trivial.class).ids(Arrays.asList(trivial.getId())).now(); | ||
|
|
||
| verifyThatRemoteCallWasExecuted(3); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyLoad() { | ||
| ofy().load().entities(Lists.newArrayList()).size(); | ||
| ofy().load().keys(Lists.<Key<Trivial>> newArrayList()).size(); | ||
| ofy().load().type(Trivial.class).ids(Lists.newArrayList()).size(); | ||
|
|
||
| verifyNoRemoteCall(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNotEmptyLoad() { | ||
| final Trivial trivial = new Trivial(1L, "", 1L); | ||
| ofy().load().entities(Arrays.asList(trivial)).size(); | ||
| ofy().load().keys(Arrays.asList(Key.<Trivial> create(trivial))).size(); | ||
| ofy().load().type(Trivial.class).ids(Arrays.asList(trivial.getId())).size(); | ||
|
|
||
| verifyThatRemoteCallWasExecuted(3); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Collections.emptyMap();? Or you want it to be writable?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH there is no proper consistency in the objectify itself, for example
some uses empty map, some new hashmap, but this method returned this
originally, so seemed like a good idea to keep it.
2015.10.19. 13:18 ezt írta ("Peter Farkas" notifications@github.com):