001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2011 The Gridarta Developers.
004 *
005 * This program is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 2 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License along
016 * with this program; if not, write to the Free Software Foundation, Inc.,
017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
018 */
019
020package net.sf.gridarta.gui.mapmenu;
021
022import java.io.File;
023import java.io.IOException;
024import java.util.ResourceBundle;
025import java.util.prefs.BackingStoreException;
026import java.util.prefs.Preferences;
027import javax.swing.tree.DefaultMutableTreeNode;
028import javax.swing.tree.TreeNode;
029import net.sf.gridarta.MainControl;
030import net.sf.gridarta.preferences.FilePreferencesFactory;
031import net.sf.japi.swing.action.ActionBuilder;
032import net.sf.japi.swing.action.ActionBuilderFactory;
033import net.sf.japi.swing.action.DefaultActionBuilder;
034import org.jetbrains.annotations.NotNull;
035import org.junit.Assert;
036import org.junit.BeforeClass;
037import org.junit.Test;
038
039/**
040 * Regression tests for {@link MapMenuLoader}.
041 * @author Andreas Kirschbaum
042 */
043public class MapMenuPreferencesTest {
044
045    /**
046     * Checks that saving and re-loading entries works as expected.
047     * @throws IOException if the test fails
048     */
049    @Test
050    public void test1() throws IOException {
051        final MapMenuLoader pref = new MapMenuLoader("key");
052        Assert.assertEquals(0, pref.loadNumEntries());
053        try {
054            pref.loadEntry(0);
055            Assert.fail();
056        } catch (final IOException ignored) {
057            // ignore
058        }
059        pref.saveNumEntries(2);
060        Assert.assertEquals(2, pref.loadNumEntries());
061        pref.saveEntry(1, "title1", "filename1", "directory1/sub", MapMenuLoader.Type.DIR);
062        pref.saveEntry(0, "title0", "filename0", "directory0/sub", MapMenuLoader.Type.MAP);
063
064        final MapMenuLoader.Result result0 = pref.loadEntry(0);
065        final MapMenuEntry mapMenuEntry0 = result0.getMapMenuEntry();
066        Assert.assertEquals("directory0/sub", result0.getDirectory());
067        Assert.assertEquals("title0", mapMenuEntry0.getTitle());
068        Assert.assertTrue(mapMenuEntry0 instanceof MapMenuEntryMap);
069        final MapMenuEntryMap mapMenuEntryMap0 = (MapMenuEntryMap) mapMenuEntry0;
070        Assert.assertEquals(new File("filename0"), mapMenuEntryMap0.getMapFile());
071
072        final MapMenuLoader.Result result1 = pref.loadEntry(1);
073        final MapMenuEntry mapMenuEntry1 = result1.getMapMenuEntry();
074        Assert.assertEquals("directory1/sub", result1.getDirectory());
075        Assert.assertEquals("title1", mapMenuEntry1.getTitle());
076        Assert.assertTrue(mapMenuEntry1 instanceof MapMenuEntryDir);
077
078        try {
079            pref.loadEntry(2);
080            Assert.fail();
081        } catch (final IOException ignored) {
082            // ignore
083        }
084        pref.removeEntry(0);
085        try {
086            pref.loadEntry(0);
087            Assert.fail();
088        } catch (final IOException ignored) {
089            // ignore
090        }
091        pref.loadEntry(1);
092    }
093
094    /**
095     * Checks that loading a {@link MapMenu} instance works as expected.
096     * @throws BackingStoreException if the test fails
097     */
098    @Test
099    public void test2() throws BackingStoreException {
100        final ActionBuilder actionBuilder = new DefaultActionBuilder("net.sf.gridarta");
101        ActionBuilderFactory.getInstance().putActionBuilder("net.sf.gridarta", actionBuilder);
102        final ResourceBundle resourceBundle = ResourceBundle.getBundle("net.sf.gridarta.gui.mapmenu.testLoad1");
103        actionBuilder.addBundle(resourceBundle);
104
105        final Preferences preferences = Preferences.userNodeForPackage(MainControl.class);
106        for (final String key : resourceBundle.keySet()) {
107            final String value = resourceBundle.getString(key);
108            preferences.put(key, value);
109        }
110
111        final MapMenu mapMenu1 = new MapMenu("test");
112        mapMenu1.load();
113        Assert.assertEquals(10, mapMenu1.size());
114
115        for (final String key : preferences.keys()) {
116            preferences.remove(key);
117        }
118
119        final MapMenu mapMenu2 = new MapMenu("test");
120        mapMenu2.load();
121        Assert.assertEquals(0, mapMenu2.size());
122
123        mapMenu1.saveAlways();
124
125        mapMenu2.load();
126        Assert.assertEquals(10, mapMenu2.size());
127
128        compareTrees(mapMenu1.getRoot(), mapMenu2.getRoot());
129    }
130
131    /**
132     * Checks that saving and re-loading a {@link MapMenu} having directories
133     * with the same name works.
134     */
135    @Test
136    public void test3() {
137        final ActionBuilder actionBuilder = new DefaultActionBuilder("net.sf.gridarta");
138        ActionBuilderFactory.getInstance().putActionBuilder("net.sf.gridarta", actionBuilder);
139
140        final MapMenu mapMenu1 = new MapMenu("test");
141        mapMenu1.addMapMenuEntry("dir1", new MapMenuEntryMap(new File("file1"), "title1"));
142        mapMenu1.addMapMenuEntry("dir2", new MapMenuEntryMap(new File("file2"), "title2"));
143        final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) mapMenu1.getRoot().getFirstChild();
144        final MapMenuEntry mapMenuEntry = (MapMenuEntry) treeNode.getUserObject();
145        mapMenuEntry.setTitle("dir2");
146
147        mapMenu1.saveAlways();
148
149        final MapMenu mapMenu2 = new MapMenu("test");
150        mapMenu2.load();
151
152        compareTrees(mapMenu1.getRoot(), mapMenu2.getRoot());
153    }
154
155    /**
156     * Compares two {@link TreeNode} instances. Fails the test case if the nodes
157     * are not equal.
158     * @param treeNode1 the first tree node instance
159     * @param treeNode2 the second tree node instance
160     */
161    private static void compareTrees(@NotNull final TreeNode treeNode1, @NotNull final TreeNode treeNode2) {
162        Assert.assertEquals(treeNode1.getChildCount(), treeNode2.getChildCount());
163        for (int i = 0; i < treeNode1.getChildCount(); i++) {
164            final DefaultMutableTreeNode childTreeNode1 = (DefaultMutableTreeNode) treeNode1.getChildAt(i);
165            final DefaultMutableTreeNode childTreeNode2 = (DefaultMutableTreeNode) treeNode2.getChildAt(i);
166            final MapMenuEntry mapMenuEntry1 = (MapMenuEntry) childTreeNode1.getUserObject();
167            final MapMenuEntry mapMenuEntry2 = (MapMenuEntry) childTreeNode2.getUserObject();
168            Assert.assertSame(mapMenuEntry1.getClass(), mapMenuEntry2.getClass());
169            Assert.assertEquals(mapMenuEntry1.getTitle(), mapMenuEntry2.getTitle());
170            final MapMenuEntryVisitor mapMenuEntryVisitor = new MapMenuEntryVisitor() {
171
172                @Override
173                public void visit(@NotNull final MapMenuEntryDir mapMenuEntry) {
174                    compareTrees(childTreeNode1, childTreeNode2);
175                }
176
177                @Override
178                public void visit(@NotNull final MapMenuEntryMap mapMenuEntry) {
179                    final MapMenuEntryMap mapMenuEntryMap1 = (MapMenuEntryMap) mapMenuEntry1;
180                    final MapMenuEntryMap mapMenuEntryMap2 = (MapMenuEntryMap) mapMenuEntry2;
181                    Assert.assertEquals(mapMenuEntryMap1.getMapFile(), mapMenuEntryMap2.getMapFile());
182                }
183
184            };
185            mapMenuEntry1.visit(mapMenuEntryVisitor);
186        }
187    }
188
189    /**
190     * Initializes the test case.
191     */
192    @BeforeClass
193    public static void setUp() {
194        System.setProperty("java.util.prefs.PreferencesFactory", "net.sf.gridarta.preferences.FilePreferencesFactory");
195        FilePreferencesFactory.initialize("user_net_sf_gridarta", null);
196    }
197
198}