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 javax.swing.tree.DefaultMutableTreeNode;
024import javax.swing.tree.MutableTreeNode;
025import javax.swing.tree.TreeNode;
026import net.sf.gridarta.preferences.FilePreferencesFactory;
027import org.jetbrains.annotations.NotNull;
028import org.junit.Assert;
029import org.junit.BeforeClass;
030import org.junit.Test;
031
032/**
033 * Regression tests for {@link MapMenuUtils}.
034 * @author Andreas Kirschbaum
035 */
036public class MapMenuUtilsTest {
037
038    /**
039     * Checks that {@link MapMenuUtils#removeMap(MutableTreeNode, File)} works
040     * as expected.
041     */
042    @Test
043    public void testRemoveMap1() {
044        final MutableTreeNode dir = new DefaultMutableTreeNode(new MapMenuEntryDir("Test"), true);
045        dir.insert(new DefaultMutableTreeNode(new MapMenuEntryMap(new File("file1"), "title1"), false), 0);
046        dir.insert(new DefaultMutableTreeNode(new MapMenuEntryMap(new File("file2"), "title2"), false), 1);
047        dir.insert(new DefaultMutableTreeNode(new MapMenuEntryMap(new File("file1"), "title3"), false), 2);
048        dir.insert(new DefaultMutableTreeNode(new MapMenuEntryMap(new File("file3"), "title4"), false), 3);
049        dir.insert(new DefaultMutableTreeNode(new MapMenuEntryMap(new File("file2"), "title5"), false), 4);
050        checkFile(dir, "file1", "file2", "file1", "file3", "file2");
051        MapMenuUtils.removeMap(dir, new File("file2"));
052        checkFile(dir, "file1", "file1", "file3");
053        MapMenuUtils.removeMap(dir, new File("file2"));
054        checkFile(dir, "file1", "file1", "file3");
055        MapMenuUtils.removeMap(dir, new File("file1"));
056        checkFile(dir, "file3");
057        MapMenuUtils.removeMap(dir, new File("file3"));
058        checkFile(dir);
059    }
060
061    /**
062     * Checks that a {@link MapMenuEntryDir} instance contains the given child
063     * nodes.
064     * @param dir the map menu entry dir instance
065     * @param files the files of the child nodes
066     * @noinspection TypeMayBeWeakened // IDEA bug
067     */
068    private static void checkFile(@NotNull final TreeNode dir, @NotNull final String... files) {
069        Assert.assertEquals(files.length, dir.getChildCount());
070        for (int i = 0; i < files.length; i++) {
071            final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) dir.getChildAt(i);
072            final MapMenuEntry mapMenuEntry = (MapMenuEntry) treeNode.getUserObject();
073            if (files[i] == null) {
074                Assert.assertSame(MapMenuEntryDir.class, mapMenuEntry.getClass());
075            } else {
076                Assert.assertSame(MapMenuEntryMap.class, mapMenuEntry.getClass());
077                final MapMenuEntryMap mapMenuEntryMap = (MapMenuEntryMap) mapMenuEntry;
078                Assert.assertEquals(new File(files[i]), mapMenuEntryMap.getMapFile());
079            }
080        }
081    }
082
083    /**
084     * Initializes the test case.
085     */
086    @BeforeClass
087    public static void setUp() {
088        System.setProperty("java.util.prefs.PreferencesFactory", "net.sf.gridarta.preferences.FilePreferencesFactory");
089        FilePreferencesFactory.initialize("user_net_sf_gridarta", null);
090    }
091
092}