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.model.io;
021
022import java.io.BufferedReader;
023import java.io.File;
024import java.io.IOException;
025import java.io.Reader;
026import java.io.StringReader;
027import java.util.ArrayList;
028import java.util.List;
029import net.sf.gridarta.model.archetype.AbstractArchetypeBuilder;
030import net.sf.gridarta.model.archetype.Archetype;
031import net.sf.gridarta.model.archetypeset.ArchetypeSet;
032import net.sf.gridarta.model.errorview.ErrorViewCollector;
033import net.sf.gridarta.model.errorview.TestErrorView;
034import net.sf.gridarta.model.gameobject.GameObject;
035import net.sf.gridarta.model.maparchobject.MapArchObject;
036import org.jetbrains.annotations.NotNull;
037import org.junit.Assert;
038
039/**
040 * Abstract base class for regression tests for {@link ArchetypeParser}.
041 * @author Andreas Kirschbaum
042 */
043public abstract class AbstractArchetypeParserTest<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
044
045    /**
046     * Creates a new archetype parser and parses the given input.
047     * @param input the input to parse
048     * @param hasErrors whether errors are expected
049     * @param hasWarnings whether warnings are expected
050     * @param archetypes the number of archetypes to expect
051     * @throws IOException if parsing fails
052     */
053    protected void check(@NotNull final String input, final boolean hasErrors, final boolean hasWarnings, final int archetypes) throws IOException {
054        final AbstractArchetypeParser<G, A, R, ?> archetypeParser = newArchetypeParser();
055        final TestErrorView errorView = new TestErrorView();
056        final List<G> invObjects = new ArrayList<G>();
057        final Reader reader = new StringReader(input);
058        final BufferedReader bufferedReader = new BufferedReader(reader);
059        try {
060            archetypeParser.parseArchetypeFromStream(bufferedReader, null, null, null, "panel", "folder", "", invObjects, new ErrorViewCollector(errorView, new File("*string*")));
061        } finally {
062            bufferedReader.close();
063        }
064        Assert.assertEquals(hasErrors, errorView.hasErrors());
065        Assert.assertEquals(hasWarnings, errorView.hasWarnings());
066        Assert.assertEquals(archetypes, getArchetypeSet().getArchetypeCount());
067    }
068
069    /**
070     * Creates a new {@link AbstractArchetypeParser} instance.
071     * @return the new instance
072     */
073    @NotNull
074    protected abstract AbstractArchetypeParser<G, A, R, ? extends AbstractArchetypeBuilder<G, A, R>> newArchetypeParser();
075
076    /**
077     * Returns the {@link ArchetypeSet}.
078     * @return the archetype set
079     */
080    @NotNull
081    protected abstract ArchetypeSet<G, A, R> getArchetypeSet();
082
083}