Gridarta Editor
MapMenuUtilsTest.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 javax.swing.tree.DefaultMutableTreeNode;
24 import javax.swing.tree.MutableTreeNode;
25 import javax.swing.tree.TreeNode;
28 import org.jetbrains.annotations.NotNull;
29 import org.junit.Assert;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 
37 public class MapMenuUtilsTest {
38 
43  @Test
44  public void testRemoveMap1() {
45  final MutableTreeNode dir = new DefaultMutableTreeNode(new MapMenuEntryDir("Test"), true);
46  dir.insert(new DefaultMutableTreeNode(new MapMenuEntryMap(new MapFile(new File("file1")), "title1"), false), 0);
47  dir.insert(new DefaultMutableTreeNode(new MapMenuEntryMap(new MapFile(new File("file2")), "title2"), false), 1);
48  dir.insert(new DefaultMutableTreeNode(new MapMenuEntryMap(new MapFile(new File("file1")), "title3"), false), 2);
49  dir.insert(new DefaultMutableTreeNode(new MapMenuEntryMap(new MapFile(new File("file3")), "title4"), false), 3);
50  dir.insert(new DefaultMutableTreeNode(new MapMenuEntryMap(new MapFile(new File("file2")), "title5"), false), 4);
51  checkFile(dir, "file1", "file2", "file1", "file3", "file2");
52  MapMenuUtils.removeMap(dir, new MapFile(new File("file2")));
53  checkFile(dir, "file1", "file1", "file3");
54  MapMenuUtils.removeMap(dir, new MapFile(new File("file2")));
55  checkFile(dir, "file1", "file1", "file3");
56  MapMenuUtils.removeMap(dir, new MapFile(new File("file1")));
57  checkFile(dir, "file3");
58  MapMenuUtils.removeMap(dir, new MapFile(new File("file3")));
59  checkFile(dir);
60  }
61 
69  private static void checkFile(@NotNull final TreeNode dir, @NotNull final String... files) {
70  Assert.assertEquals(files.length, dir.getChildCount());
71  for (int i = 0; i < files.length; i++) {
72  final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) dir.getChildAt(i);
73  final MapMenuEntry mapMenuEntry = (MapMenuEntry) treeNode.getUserObject();
74  if (files[i] == null) {
75  Assert.assertSame(MapMenuEntryDir.class, mapMenuEntry.getClass());
76  } else {
77  Assert.assertSame(MapMenuEntryMap.class, mapMenuEntry.getClass());
78  final MapMenuEntryMap mapMenuEntryMap = (MapMenuEntryMap) mapMenuEntry;
79  Assert.assertEquals(new MapFile(new File(files[i])), mapMenuEntryMap.getMapFile());
80  }
81  }
82  }
83 
87  @BeforeClass
88  public static void setUp() {
89  System.setProperty("java.util.prefs.PreferencesFactory", "net.sf.gridarta.preferences.FilePreferencesFactory");
90  FilePreferencesFactory.initialize("user_net_sf_gridarta", null);
91  }
92 
93 }
static void setUp()
Initializes the test case.
Regression tests for MapMenuUtils.
A MapMenuEntry that represents a directory.
static void checkFile(@NotNull final TreeNode dir, @NotNull final String... files)
Checks that a MapMenuEntryDir instance contains the given child nodes.
Base package of all Gridarta classes.
Utility class for MapMenu related functions.
void testRemoveMap1()
Checks that MapMenuUtils#removeMap(MutableTreeNode, MapFile) works as expected.
MapFile getMapFile()
Returns the map file.
Abstract base class for recent and bookmark menu entries.
A MapMenuEntry that represents a map.
static void initialize(@NotNull final String defaultName, @Nullable final File file)
Initialize the module.
A PreferencesFactory which creates FilePreferences instances.
The location of a map file with a map directory.
Definition: MapFile.java:31
static void removeMap(@NotNull final MutableTreeNode treeNode, @NotNull final MapFile mapFile)
Removes all entries for a given map file.