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 javax.swing.tree.DefaultMutableTreeNode;
023import javax.swing.tree.MutableTreeNode;
024import net.sf.gridarta.preferences.FilePreferencesFactory;
025import org.jetbrains.annotations.NotNull;
026import org.junit.Assert;
027import org.junit.BeforeClass;
028import org.junit.Test;
029
030/**
031 * Regression tests for {@link MapMenu}.
032 * @author Andreas Kirschbaum
033 */
034public class MapMenuTest {
035
036    /**
037     * Checks that {@link MapMenu#getOrCreateDirectory(MutableTreeNode, String)}
038     * works as expected.
039     */
040    @Test
041    public void testSubDir1() {
042        final MapMenu mapMenu = new MapMenu("test");
043        final MutableTreeNode dir = mapMenu.getRoot();
044        checkDir(dir);
045        mapMenu.getOrCreateDirectory(dir, "dir1");
046        checkDir(dir, "dir1");
047        mapMenu.getOrCreateDirectory(dir, "dir2");
048        mapMenu.getOrCreateDirectory(dir, "dir3");
049        checkDir(dir, "dir1", "dir2", "dir3");
050        mapMenu.getOrCreateDirectory(dir, "dir2");
051        checkDir(dir, "dir1", "dir2", "dir3");
052    }
053
054    /**
055     * Checks that {@link MapMenu#getOrCreateDirectory(MutableTreeNode, String)}
056     * rejects invalid path names.
057     */
058    @Test
059    public void testSubDir2() {
060        final MapMenu mapMenu = new MapMenu("test");
061        final MutableTreeNode dir = mapMenu.getRoot();
062        checkDir(dir);
063        mapMenu.getOrCreateDirectory(dir, "dir1");
064        mapMenu.getOrCreateDirectory(dir, "dir2");
065        mapMenu.getOrCreateDirectory(dir, "dir3");
066        checkDir(dir, "dir1", "dir2", "dir3");
067        try {
068            mapMenu.getOrCreateDirectory(dir, "dir2/sub");
069            Assert.fail("IllegalArgumentException expected");
070        } catch (final IllegalArgumentException ignored) {
071        }
072        try {
073            mapMenu.getOrCreateDirectory(dir, "dir4/sub");
074            Assert.fail("IllegalArgumentException expected");
075        } catch (final IllegalArgumentException ignored) {
076        }
077        try {
078            mapMenu.getOrCreateDirectory(dir, "");
079            Assert.fail("IllegalArgumentException expected");
080        } catch (final IllegalArgumentException ignored) {
081        }
082        checkDir(dir, "dir1", "dir2", "dir3");
083    }
084
085    /**
086     * Checks that a {@link MapMenuEntryDir} instance contains the given child
087     * nodes.
088     * @param dir the map menu entry dir instance
089     * @param paths the paths of the child nodes
090     * @noinspection TypeMayBeWeakened // IDEA bug
091     */
092    private static void checkDir(@NotNull final MutableTreeNode dir, @NotNull final String... paths) {
093        Assert.assertEquals(paths.length, dir.getChildCount());
094        for (int i = 0; i < paths.length; i++) {
095            final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) dir.getChildAt(i);
096            final MapMenuEntry mapMenuEntry = (MapMenuEntry) treeNode.getUserObject();
097            Assert.assertEquals(paths[i], mapMenuEntry.getTitle());
098        }
099    }
100
101    /**
102     * Initializes the test case.
103     */
104    @BeforeClass
105    public static void setUp() {
106        System.setProperty("java.util.prefs.PreferencesFactory", "net.sf.gridarta.preferences.FilePreferencesFactory");
107        FilePreferencesFactory.initialize("user_net_sf_gridarta", null);
108    }
109
110}