2008-06-09 14:41:06 -04:00
|
|
|
|
|
|
|
package net.sourceforge.tuned;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.prefs.Preferences;
|
|
|
|
|
|
|
|
import net.sourceforge.filebot.web.MovieDescriptor;
|
|
|
|
import net.sourceforge.tuned.PreferencesMap.SerializableAdapter;
|
2009-03-08 14:41:11 -04:00
|
|
|
import net.sourceforge.tuned.PreferencesMap.SimpleAdapter;
|
2008-06-09 14:41:06 -04:00
|
|
|
|
|
|
|
import org.junit.AfterClass;
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
|
|
public class PreferencesMapTest {
|
|
|
|
|
|
|
|
private static Preferences root;
|
|
|
|
private static Preferences strings;
|
|
|
|
private static Preferences numbers;
|
|
|
|
private static Preferences temp;
|
|
|
|
private static Preferences sequence;
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
public static void setUpBeforeClass() throws Exception {
|
2008-06-29 13:38:57 -04:00
|
|
|
root = Preferences.userRoot().node("junit-test");
|
2008-06-09 14:41:06 -04:00
|
|
|
|
|
|
|
strings = root.node("strings");
|
|
|
|
strings.put("1", "Firefly");
|
|
|
|
strings.put("2", "Roswell");
|
|
|
|
strings.put("3", "Angel");
|
|
|
|
strings.put("4", "Dead like me");
|
|
|
|
strings.put("5", "Babylon");
|
|
|
|
|
|
|
|
numbers = root.node("numbers");
|
|
|
|
numbers.putInt("M", 4);
|
|
|
|
numbers.putInt("A", 5);
|
|
|
|
numbers.putInt("X", 2);
|
|
|
|
|
|
|
|
sequence = root.node("sequence");
|
|
|
|
sequence.putInt("1", 1);
|
|
|
|
sequence.putInt("2", 2);
|
|
|
|
sequence.putInt("3", 3);
|
|
|
|
|
|
|
|
temp = root.node("temp");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
public static void tearDownAfterClass() throws Exception {
|
|
|
|
root.removeNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void get() {
|
2009-03-08 14:41:11 -04:00
|
|
|
Map<String, String> stringMap = PreferencesMap.map(strings);
|
2008-06-09 14:41:06 -04:00
|
|
|
|
|
|
|
assertEquals("Firefly", stringMap.get("1"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void put() {
|
2009-03-08 14:41:11 -04:00
|
|
|
Map<String, String> stringMap = PreferencesMap.map(temp);
|
2008-06-09 14:41:06 -04:00
|
|
|
|
|
|
|
stringMap.put("key", "snake");
|
|
|
|
|
|
|
|
assertEquals("snake", temp.get("key", null));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void remove() throws Exception {
|
2009-03-08 14:41:11 -04:00
|
|
|
Map<String, Integer> map = PreferencesMap.map(numbers, SimpleAdapter.forClass(Integer.class));
|
2008-06-09 14:41:06 -04:00
|
|
|
|
|
|
|
map.remove("A");
|
|
|
|
|
|
|
|
assertFalse(Arrays.asList(numbers.keys()).contains("A"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void clear() throws Exception {
|
2009-03-08 14:41:11 -04:00
|
|
|
Map<String, Integer> map = PreferencesMap.map(temp, SimpleAdapter.forClass(Integer.class));
|
2008-06-09 14:41:06 -04:00
|
|
|
|
|
|
|
map.put("X", 42);
|
|
|
|
|
|
|
|
map.clear();
|
|
|
|
|
|
|
|
assertTrue(temp.keys().length == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void containsKey() {
|
|
|
|
temp.put("name", "kaya");
|
|
|
|
|
2009-03-08 14:41:11 -04:00
|
|
|
Map<String, String> map = PreferencesMap.map(temp);
|
2008-06-09 14:41:06 -04:00
|
|
|
|
|
|
|
assertTrue(map.containsKey("name"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void values() {
|
|
|
|
|
2009-03-08 14:41:11 -04:00
|
|
|
Map<String, Integer> map = PreferencesMap.map(sequence, SimpleAdapter.forClass(Integer.class));
|
2008-06-09 14:41:06 -04:00
|
|
|
|
|
|
|
Collection<Integer> list = map.values();
|
|
|
|
|
|
|
|
assertTrue(list.contains(1));
|
|
|
|
assertTrue(list.contains(2));
|
|
|
|
assertTrue(list.contains(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void containsValue() {
|
2009-03-08 14:41:11 -04:00
|
|
|
Map<String, String> map = PreferencesMap.map(strings);
|
2008-06-09 14:41:06 -04:00
|
|
|
|
|
|
|
assertTrue(map.containsValue("Firefly"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void entrySet() {
|
2009-03-08 14:41:11 -04:00
|
|
|
Map<String, Integer> map = PreferencesMap.map(numbers, SimpleAdapter.forClass(Integer.class));
|
2008-06-09 14:41:06 -04:00
|
|
|
|
|
|
|
for (Entry<String, Integer> entry : map.entrySet()) {
|
|
|
|
Integer v = entry.getValue();
|
|
|
|
entry.setValue(v + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(5, numbers.getInt("M", -1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = NumberFormatException.class)
|
|
|
|
public void adapterException() {
|
2009-03-08 14:41:11 -04:00
|
|
|
PreferencesMap.map(strings, SimpleAdapter.forClass(Integer.class)).values();
|
2008-06-09 14:41:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void containsKeyWithObjectKey() throws Exception {
|
2009-03-08 14:41:11 -04:00
|
|
|
Map<String, String> map = PreferencesMap.map(strings);
|
2008-06-09 14:41:06 -04:00
|
|
|
|
|
|
|
assertFalse(map.containsKey(new Object()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void getWithObjectKey() throws Exception {
|
2009-03-08 14:41:11 -04:00
|
|
|
Map<String, String> map = PreferencesMap.map(strings);
|
2008-06-09 14:41:06 -04:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
assertEquals(null, map.get(new Object()));
|
2008-06-09 14:41:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void serializableAdapter() {
|
|
|
|
Map<String, MovieDescriptor> map = PreferencesMap.map(temp, new SerializableAdapter<MovieDescriptor>());
|
|
|
|
|
|
|
|
MovieDescriptor movie = new MovieDescriptor("The Hitchhiker's Guide to the Galaxy", 42);
|
|
|
|
|
|
|
|
map.put("movie", movie);
|
|
|
|
|
|
|
|
MovieDescriptor retrieved = map.get("movie");
|
|
|
|
|
|
|
|
assertEquals(movie.getImdbId(), retrieved.getImdbId());
|
|
|
|
}
|
|
|
|
}
|