-
Notifications
You must be signed in to change notification settings - Fork 54
DefaultModuleService: change persisting of items #236
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
stelfrich
wants to merge
5
commits into
scijava:master
Choose a base branch
from
stelfrich:disable-persist-if-initialize
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 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
876e9bf
DefaultModuleService: change persisting of items
stelfrich d725de5
Parameter: update initializer() documentation
stelfrich 04e9442
Add test for persistance behavior of ModuleItems
stelfrich 44029b6
ModuleServiceText: clean up
stelfrich b4e8a32
Add ItemPersistence to denote persistence behavior
stelfrich 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
Add test for persistance behavior of ModuleItems
- Loading branch information
commit 04e94429a9c633cf6b979034309163f16faec915
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 |
|---|---|---|
|
|
@@ -34,8 +34,12 @@ | |
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.assertSame; | ||
|
|
||
| import java.security.GeneralSecurityException; | ||
|
|
||
| import org.junit.Assert; | ||
|
||
| import org.junit.Test; | ||
| import org.scijava.Context; | ||
| import org.scijava.prefs.PrefService; | ||
|
|
||
| /** | ||
| * Tests {@link ModuleService}. | ||
|
|
@@ -74,6 +78,38 @@ public void testGetSingleInput() throws ModuleException { | |
| assertSame(info.getInput("double2"), singleDouble); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Test | ||
| public void testPersistingWithInitialize() { | ||
| final Context context = new Context(ModuleService.class, PrefService.class); | ||
| final ModuleService moduleService = context.getService(ModuleService.class); | ||
|
|
||
| // reset the PrefService entries for the test | ||
| final PrefService prefService = context.getService(PrefService.class); | ||
| prefService.clear("persistInteger"); | ||
| prefService.clear("persistDouble"); | ||
|
|
||
| final ModuleInfo info = new FooModuleInfo(); | ||
| final ModuleItem<Double> doubleItem = (ModuleItem<Double>) info.getInput( | ||
| "double1"); | ||
| final ModuleItem<Integer> integerItem = (ModuleItem<Integer>) info.getInput( | ||
| "integer1"); | ||
|
|
||
| // save ModuleItem for which getInitializer() returns "testInitializer" | ||
| moduleService.save(doubleItem, 5d); | ||
|
|
||
| // verify that the item is not persisted | ||
| String persistKey = doubleItem.getPersistKey(); | ||
| Assert.assertNull(prefService.get(persistKey)); | ||
|
|
||
| // save ModuleItem for which getInitializer() returns null | ||
| moduleService.save(integerItem, 5); | ||
|
|
||
| // verify that the item is persisted | ||
| persistKey = integerItem.getPersistKey(); | ||
| Assert.assertEquals(5, prefService.getInt(persistKey, 0)); | ||
| } | ||
|
|
||
| /** A sample module for testing the module service. */ | ||
| public static class FooModule extends AbstractModule { | ||
|
|
||
|
|
@@ -115,16 +151,16 @@ public Module createModule() throws ModuleException { | |
|
|
||
| @Override | ||
| protected void parseParameters() { | ||
| addInput("string", String.class, true); | ||
| addInput("float", Float.class, false); | ||
| addInput("integer1", Integer.class, true); | ||
| addInput("integer2", Integer.class, true); | ||
| addInput("double1", Double.class, false); | ||
| addInput("double2", Double.class, true); | ||
| addInput("string", String.class, true, null, null); | ||
| addInput("float", Float.class, false, null, null); | ||
| addInput("integer1", Integer.class, true, "persistInteger", null); | ||
| addInput("integer2", Integer.class, true, null, null); | ||
| addInput("double1", Double.class, false, "persistDouble", "testInitializer"); | ||
| addInput("double2", Double.class, true, null, null); | ||
| } | ||
|
|
||
| private <T> void addInput(final String name, final Class<T> type, | ||
| final boolean autoFill) | ||
| final boolean autoFill, final String persistKey, final String initializer) | ||
| { | ||
| registerInput(new AbstractModuleItem<T>(this) { | ||
|
|
||
|
|
@@ -143,6 +179,16 @@ public boolean isAutoFill() { | |
| return autoFill; | ||
| } | ||
|
|
||
| @Override | ||
| public String getPersistKey() { | ||
| return persistKey; | ||
| } | ||
|
|
||
| @Override | ||
| public String getInitializer() { | ||
| return initializer; | ||
| } | ||
|
|
||
| }); | ||
| } | ||
|
|
||
|
|
||
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.
???