Skip to content

Commit cdeb735

Browse files
ctruedenhinerm
authored andcommitted
Add down-the-middle unit tests for PrefService
1 parent 963d4eb commit cdeb735

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.prefs;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertFalse;
36+
import static org.junit.Assert.assertNull;
37+
import static org.junit.Assert.assertTrue;
38+
39+
import org.junit.After;
40+
import org.junit.Before;
41+
import org.junit.Test;
42+
import org.scijava.Context;
43+
import org.scijava.prefs.PrefService;
44+
45+
/**
46+
* Tests {@link PrefService}.
47+
*
48+
* @author Curtis Rueden
49+
*/
50+
public class PrefServiceTest {
51+
52+
private PrefService prefService;
53+
54+
@Before
55+
public void setUp() {
56+
final Context context = new Context(PrefService.class);
57+
prefService = context.getService(PrefService.class);
58+
}
59+
60+
@After
61+
public void tearDown() {
62+
prefService.clear(getClass());
63+
prefService.getContext().dispose();
64+
}
65+
66+
/**
67+
* Tests {@link PrefService#put(Class, String, String)} and
68+
* {@link PrefService#get(Class, String)}.
69+
*/
70+
@Test
71+
public void testString() {
72+
assertNull(prefService.get(getClass(), "animal"));
73+
prefService.put(getClass(), "animal", "kraken");
74+
assertEquals("kraken", prefService.get(getClass(), "animal"));
75+
}
76+
77+
/**
78+
* Tests {@link PrefService#put(Class, String, boolean)} and
79+
* {@link PrefService#getBoolean(Class, String, boolean)}.
80+
*/
81+
@Test
82+
public void testBoolean() {
83+
assertFalse(prefService.getBoolean(getClass(), "awesome", false));
84+
prefService.put(getClass(), "awesome", true);
85+
assertTrue(prefService.getBoolean(getClass(), "awesome", false));
86+
}
87+
88+
/**
89+
* Tests {@link PrefService#put(Class, String, double)} and
90+
* {@link PrefService#getDouble(Class, String, double)}.
91+
*/
92+
@Test
93+
public void testDouble() {
94+
final String key = "real";
95+
final double dv = 0, value = 123.456;
96+
assertEquals(dv, prefService.getDouble(getClass(), key, dv), 0);
97+
prefService.put(getClass(), key, value);
98+
assertEquals(value, prefService.getDouble(getClass(), key, dv), 0);
99+
}
100+
101+
/**
102+
* Tests {@link PrefService#put(Class, String, float)} and
103+
* {@link PrefService#getFloat(Class, String, float)}.
104+
*/
105+
@Test
106+
public void testFloat() {
107+
final String key = "real";
108+
final float dv = 0, value = 654.321f;
109+
assertEquals(dv, prefService.getFloat(getClass(), key, dv), 0);
110+
prefService.put(getClass(), key, value);
111+
assertEquals(value, prefService.getFloat(getClass(), key, dv), 0);
112+
}
113+
114+
/**
115+
* Tests {@link PrefService#put(Class, String, int)} and
116+
* {@link PrefService#getInt(Class, String, int)}.
117+
*/
118+
@Test
119+
public void testInt() {
120+
final String key = "integer";
121+
final int dv = 0, value = 1234;
122+
assertEquals(dv, prefService.getInt(getClass(), key, dv));
123+
prefService.put(getClass(), key, value);
124+
assertEquals(value, prefService.getInt(getClass(), key, dv));
125+
}
126+
127+
/**
128+
* Tests {@link PrefService#put(Class, String, long)} and
129+
* {@link PrefService#getLong(Class, String, long)}.
130+
*/
131+
@Test
132+
public void testLong() {
133+
final long dv = 0L, value = 9999999999999L;
134+
assertEquals(dv, prefService.getLong(getClass(), "power level", dv));
135+
prefService.put(getClass(), "power level", value);
136+
assertTrue(prefService.getLong(getClass(), "power level", dv) > 9000);
137+
}
138+
139+
}

0 commit comments

Comments
 (0)