001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2010 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.utils;
021
022import java.util.Collections;
023import java.util.Enumeration;
024import java.util.HashMap;
025import java.util.Map;
026import java.util.ResourceBundle;
027import java.util.regex.Pattern;
028import net.sf.japi.swing.action.ActionBuilder;
029import net.sf.japi.swing.action.ActionBuilderFactory;
030import net.sf.japi.util.IteratorEnumeration;
031import org.jetbrains.annotations.NotNull;
032import org.jetbrains.annotations.Nullable;
033
034/**
035 * Utility class for initializing the {@link ActionBuilder} infrastructure for
036 * regression tests.
037 * @author Andreas Kirschbaum
038 */
039public class TestActionBuilder {
040
041    /**
042     * The {@link Pattern} for matching validator keys for default values.
043     */
044    @NotNull
045    private static final Pattern PATTERN_DEFAULT_KEY = Pattern.compile("Validator\\..*\\.default|MapValidator\\.All\\.default");
046
047    /**
048     * Private constructor to prevent instantiation.
049     */
050    private TestActionBuilder() {
051    }
052
053    /**
054     * Initializes the {@link ActionBuilder} infrastructure for regression
055     * tests.
056     */
057    public static void initialize() {
058        final ActionBuilder actionBuilder = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
059        final ResourceBundle resourceBundle = new ResourceBundle() {
060
061            /**
062             * Maps key to associated object.
063             */
064            @NotNull
065            private final Map<String, Object> objects = new HashMap<String, Object>();
066
067            @Nullable
068            @Override
069            protected Object handleGetObject(@NotNull final String key) {
070                final Object existingObject = objects.get(key);
071                if (existingObject != null) {
072                    return existingObject;
073                }
074
075                final Object object;
076                if (PATTERN_DEFAULT_KEY.matcher(key).matches()) {
077                    object = "true";
078                } else {
079                    return null;
080                }
081                objects.put(key, object);
082                return object;
083            }
084
085            @NotNull
086            @Override
087            public Enumeration<String> getKeys() {
088                return new IteratorEnumeration<String>(Collections.unmodifiableSet(objects.keySet()).iterator());
089            }
090
091        };
092        actionBuilder.addBundle(resourceBundle);
093    }
094
095}