Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test for persistance behavior of ModuleItems
  • Loading branch information
stelfrich committed Aug 8, 2016
commit 04e94429a9c633cf6b979034309163f16faec915
60 changes: 53 additions & 7 deletions src/test/java/org/scijava/module/ModuleServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

import java.security.GeneralSecurityException;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???


import org.junit.Assert;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typical way to do it is:

import static org.junit.Assert.assertEquals;

And then below, just assertEquals rather than Assert.assertEquals.

import org.junit.Test;
import org.scijava.Context;
import org.scijava.prefs.PrefService;

/**
* Tests {@link ModuleService}.
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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) {

Expand All @@ -143,6 +179,16 @@ public boolean isAutoFill() {
return autoFill;
}

@Override
public String getPersistKey() {
return persistKey;
}

@Override
public String getInitializer() {
return initializer;
}

});
}

Expand Down