Gridarta Editor
MapMenuPreferencesTest.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.mapmenu;
21 
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ResourceBundle;
25 import java.util.prefs.BackingStoreException;
26 import java.util.prefs.Preferences;
27 import javax.swing.tree.DefaultMutableTreeNode;
28 import javax.swing.tree.TreeNode;
29 import net.sf.gridarta.MainControl;
33 import net.sf.japi.swing.action.ActionBuilder;
34 import net.sf.japi.swing.action.ActionBuilderFactory;
35 import net.sf.japi.swing.action.DefaultActionBuilder;
36 import org.jetbrains.annotations.NotNull;
37 import org.junit.Assert;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 
45 public class MapMenuPreferencesTest {
46 
51  @Test
52  public void test1() throws IOException {
53  final PathManager pathManager = new PathManager(new TestProjectSettings());
54  final MapMenuLoader pref = new MapMenuLoader("key", pathManager);
55  Assert.assertEquals(0, pref.loadNumEntries());
56  try {
57  pref.loadEntry(0);
58  Assert.fail();
59  } catch (final IOException ignored) {
60  // ignore
61  }
62  pref.saveNumEntries(2);
63  Assert.assertEquals(2, pref.loadNumEntries());
64  pref.saveEntry(1, "title1", "filename1", "directory1/sub", MapMenuLoader.Type.DIR);
65  pref.saveEntry(0, "title0", "filename0", "directory0/sub", MapMenuLoader.Type.MAP);
66 
67  final MapMenuLoader.Result result0 = pref.loadEntry(0);
68  final MapMenuEntry mapMenuEntry0 = result0.getMapMenuEntry();
69  Assert.assertEquals("directory0/sub", result0.getDirectory());
70  Assert.assertEquals("title0", mapMenuEntry0.getTitle());
71  Assert.assertTrue(mapMenuEntry0 instanceof MapMenuEntryMap);
72  final MapMenuEntryMap mapMenuEntryMap0 = (MapMenuEntryMap) mapMenuEntry0;
73  Assert.assertEquals(pathManager.getMapFile(new File("filename0")), mapMenuEntryMap0.getMapFile());
74 
75  final MapMenuLoader.Result result1 = pref.loadEntry(1);
76  final MapMenuEntry mapMenuEntry1 = result1.getMapMenuEntry();
77  Assert.assertEquals("directory1/sub", result1.getDirectory());
78  Assert.assertEquals("title1", mapMenuEntry1.getTitle());
79  Assert.assertTrue(mapMenuEntry1 instanceof MapMenuEntryDir);
80 
81  try {
82  pref.loadEntry(2);
83  Assert.fail();
84  } catch (final IOException ignored) {
85  // ignore
86  }
87  pref.removeEntry(0);
88  try {
89  pref.loadEntry(0);
90  Assert.fail();
91  } catch (final IOException ignored) {
92  // ignore
93  }
94  pref.loadEntry(1);
95  }
96 
101  @Test
102  public void test2() throws BackingStoreException {
103  final PathManager pathManager = new PathManager(new TestProjectSettings());
104  final ActionBuilder actionBuilder = new DefaultActionBuilder("net.sf.gridarta");
105  ActionBuilderFactory.getInstance().putActionBuilder("net.sf.gridarta", actionBuilder);
106  final ResourceBundle resourceBundle = ResourceBundle.getBundle("net.sf.gridarta.gui.mapmenu.testLoad1");
107  actionBuilder.addBundle(resourceBundle);
108 
109  final Preferences preferences = Preferences.userNodeForPackage(MainControl.class);
110  for (final String key : resourceBundle.keySet()) {
111  final String value = resourceBundle.getString(key);
112  preferences.put(key, value);
113  }
114 
115  final MapMenu mapMenu1 = new MapMenu("test", pathManager);
116  mapMenu1.load();
117  Assert.assertEquals(10, mapMenu1.size());
118 
119  for (final String key : preferences.keys()) {
120  preferences.remove(key);
121  }
122 
123  final MapMenu mapMenu2 = new MapMenu("test", pathManager);
124  mapMenu2.load();
125  Assert.assertEquals(0, mapMenu2.size());
126 
127  mapMenu1.saveAlways();
128 
129  mapMenu2.load();
130  Assert.assertEquals(10, mapMenu2.size());
131 
132  compareTrees(mapMenu1.getRoot(), mapMenu2.getRoot());
133  }
134 
139  @Test
140  public void test3() {
141  final PathManager pathManager = new PathManager(new TestProjectSettings());
142  final ActionBuilder actionBuilder = new DefaultActionBuilder("net.sf.gridarta");
143  ActionBuilderFactory.getInstance().putActionBuilder("net.sf.gridarta", actionBuilder);
144 
145  final MapMenu mapMenu1 = new MapMenu("test", pathManager);
146  mapMenu1.addMapMenuEntry("dir1", new MapMenuEntryMap(pathManager.getMapFile(new File("file1")), "title1"));
147  mapMenu1.addMapMenuEntry("dir2", new MapMenuEntryMap(pathManager.getMapFile(new File("file2")), "title2"));
148  final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) mapMenu1.getRoot().getFirstChild();
149  final MapMenuEntry mapMenuEntry = (MapMenuEntry) treeNode.getUserObject();
150  mapMenuEntry.setTitle("dir2");
151 
152  mapMenu1.saveAlways();
153 
154  final MapMenu mapMenu2 = new MapMenu("test", pathManager);
155  mapMenu2.load();
156 
157  compareTrees(mapMenu1.getRoot(), mapMenu2.getRoot());
158  }
159 
166  private static void compareTrees(@NotNull final TreeNode treeNode1, @NotNull final TreeNode treeNode2) {
167  Assert.assertEquals(treeNode1.getChildCount(), treeNode2.getChildCount());
168  for (int i = 0; i < treeNode1.getChildCount(); i++) {
169  final DefaultMutableTreeNode childTreeNode1 = (DefaultMutableTreeNode) treeNode1.getChildAt(i);
170  final DefaultMutableTreeNode childTreeNode2 = (DefaultMutableTreeNode) treeNode2.getChildAt(i);
171  final MapMenuEntry mapMenuEntry1 = (MapMenuEntry) childTreeNode1.getUserObject();
172  final MapMenuEntry mapMenuEntry2 = (MapMenuEntry) childTreeNode2.getUserObject();
173  Assert.assertSame(mapMenuEntry1.getClass(), mapMenuEntry2.getClass());
174  Assert.assertEquals(mapMenuEntry1.getTitle(), mapMenuEntry2.getTitle());
175  final MapMenuEntryVisitor mapMenuEntryVisitor = new MapMenuEntryVisitor() {
176 
177  @Override
178  public void visit(@NotNull final MapMenuEntryDir mapMenuEntry) {
179  compareTrees(childTreeNode1, childTreeNode2);
180  }
181 
182  @Override
183  public void visit(@NotNull final MapMenuEntryMap mapMenuEntry) {
184  final MapMenuEntryMap mapMenuEntryMap1 = (MapMenuEntryMap) mapMenuEntry1;
185  final MapMenuEntryMap mapMenuEntryMap2 = (MapMenuEntryMap) mapMenuEntry2;
186  Assert.assertEquals(mapMenuEntryMap1.getMapFile(), mapMenuEntryMap2.getMapFile());
187  }
188 
189  };
190  mapMenuEntry1.visit(mapMenuEntryVisitor);
191  }
192  }
193 
197  @BeforeClass
198  public static void setUp() {
199  System.setProperty("java.util.prefs.PreferencesFactory", "net.sf.gridarta.preferences.FilePreferencesFactory");
200  FilePreferencesFactory.initialize("user_net_sf_gridarta", null);
201  }
202 
203 }
void saveEntry(final int index, @NotNull final String title, @NotNull final String filename, @NotNull final String directory, @NotNull final Type type)
Saves an entry to preferences.
This class contains methods for converting relative map paths to absolute map paths and vice versa...
A MapMenuEntry that represents a directory.
void test1()
Checks that saving and re-loading entries works as expected.
Reading and writing of maps, handling of paths.
String getTitle()
Returns the entry&#39;s title.
int size()
Returns the number of entries in this menu.
Definition: MapMenu.java:351
int loadNumEntries()
Returns the number of entries present in the preferences.
void load()
Loads the contents from preferences.
Definition: MapMenu.java:127
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Returns a MapFile instance from an AbsoluteMapPath.
Manages the contents of a recent or bookmark menu.
Definition: MapMenu.java:44
MAP
Preferences value for entries representing map files.
void test3()
Checks that saving and re-loading a MapMenu having directories with the same name works...
static void compareTrees(@NotNull final TreeNode treeNode1, @NotNull final TreeNode treeNode2)
Compares two TreeNode instances.
Result loadEntry(final int index)
Loads an entry from preferences.
Result value consisting of a MapMenuEntry and its location (directory).
void saveAlways()
Saves the contents to preferences.
Definition: MapMenu.java:161
Base package of all Gridarta classes.
DefaultMutableTreeNode getRoot()
Returns the root node.
Definition: MapMenu.java:390
Saves or restores MapMenu contents to Preferences.
abstract void visit(@NotNull MapMenuEntryVisitor visitor)
Calls the.
void saveNumEntries(final int num)
Sets the number of entries present in the preferences.
Interface used as preferences location.
MapFile getMapFile()
Returns the map file.
void addMapMenuEntry(@NotNull final String directory, @NotNull final MapMenuEntry mapMenuEntry)
Adds a MapMenuEntry to this menu.
Definition: MapMenu.java:209
void test2()
Checks that loading a MapMenu instance works as expected.
Abstract base class for recent and bookmark menu entries.
DIR
Preferences value for entries representing directories.
A MapMenuEntry that represents a map.
void removeEntry(final int index)
Removes an entry.
static void initialize(@NotNull final String defaultName, @Nullable final File file)
Initialize the module.
An ProjectSettings implementation for testing purposes.
A PreferencesFactory which creates FilePreferences instances.
void setTitle(@NotNull final String title)
Sets the entry&#39;s title.
static void setUp()
Initializes the test case.
Interface for classes operating on MapMenuEntry instances.