Gridarta Editor
MapMenuTest.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 javax.swing.tree.DefaultMutableTreeNode;
23 import javax.swing.tree.MutableTreeNode;
27 import org.jetbrains.annotations.NotNull;
28 import org.junit.Assert;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 
36 public class MapMenuTest {
37 
42  @Test
43  public void testSubDir1() {
44  final PathManager pathManager = new PathManager(new TestProjectSettings());
45  final MapMenu mapMenu = new MapMenu("test", pathManager);
46  final MutableTreeNode dir = mapMenu.getRoot();
47  checkDir(dir);
48  mapMenu.getOrCreateDirectory(dir, "dir1");
49  checkDir(dir, "dir1");
50  mapMenu.getOrCreateDirectory(dir, "dir2");
51  mapMenu.getOrCreateDirectory(dir, "dir3");
52  checkDir(dir, "dir1", "dir2", "dir3");
53  mapMenu.getOrCreateDirectory(dir, "dir2");
54  checkDir(dir, "dir1", "dir2", "dir3");
55  }
56 
61  @Test
62  public void testSubDir2() {
63  final PathManager pathManager = new PathManager(new TestProjectSettings());
64  final MapMenu mapMenu = new MapMenu("test", pathManager);
65  final MutableTreeNode dir = mapMenu.getRoot();
66  checkDir(dir);
67  mapMenu.getOrCreateDirectory(dir, "dir1");
68  mapMenu.getOrCreateDirectory(dir, "dir2");
69  mapMenu.getOrCreateDirectory(dir, "dir3");
70  checkDir(dir, "dir1", "dir2", "dir3");
71  try {
72  mapMenu.getOrCreateDirectory(dir, "dir2/sub");
73  Assert.fail("IllegalArgumentException expected");
74  } catch (final IllegalArgumentException ignored) {
75  }
76  try {
77  mapMenu.getOrCreateDirectory(dir, "dir4/sub");
78  Assert.fail("IllegalArgumentException expected");
79  } catch (final IllegalArgumentException ignored) {
80  }
81  try {
82  mapMenu.getOrCreateDirectory(dir, "");
83  Assert.fail("IllegalArgumentException expected");
84  } catch (final IllegalArgumentException ignored) {
85  }
86  checkDir(dir, "dir1", "dir2", "dir3");
87  }
88 
96  private static void checkDir(@NotNull final MutableTreeNode dir, @NotNull final String... paths) {
97  Assert.assertEquals(paths.length, dir.getChildCount());
98  for (int i = 0; i < paths.length; i++) {
99  final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) dir.getChildAt(i);
100  final MapMenuEntry mapMenuEntry = (MapMenuEntry) treeNode.getUserObject();
101  Assert.assertEquals(paths[i], mapMenuEntry.getTitle());
102  }
103  }
104 
108  @BeforeClass
109  public static void setUp() {
110  System.setProperty("java.util.prefs.PreferencesFactory", "net.sf.gridarta.preferences.FilePreferencesFactory");
111  FilePreferencesFactory.initialize("user_net_sf_gridarta", null);
112  }
113 
114 }
static void setUp()
Initializes the test case.
This class contains methods for converting relative map paths to absolute map paths and vice versa...
Reading and writing of maps, handling of paths.
String getTitle()
Returns the entry&#39;s title.
void testSubDir2()
Checks that MapMenu#getOrCreateDirectory(MutableTreeNode, String) rejects invalid path names...
Manages the contents of a recent or bookmark menu.
Definition: MapMenu.java:44
Regression tests for MapMenu.
Base package of all Gridarta classes.
DefaultMutableTreeNode getRoot()
Returns the root node.
Definition: MapMenu.java:390
DefaultMutableTreeNode getOrCreateDirectory(@NotNull final MutableTreeNode this2, @NotNull final String path)
Returns the MapMenuEntryDir for a given path.
Definition: MapMenu.java:244
Abstract base class for recent and bookmark menu entries.
void testSubDir1()
Checks that MapMenu#getOrCreateDirectory(MutableTreeNode, String) works as expected.
static void checkDir(@NotNull final MutableTreeNode dir, @NotNull final String... paths)
Checks that a MapMenuEntryDir instance contains the given child nodes.
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.