001/* 
002 * Copyright (C) 2016 Hobrasoft s.r.o.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU Affero General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU Affero General Public License for more details.
013 *
014 * You should have received a copy of the GNU Affero General Public License
015 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
016 */
017package cz.hobrasoft.pdfmu;
018
019import com.tngtech.java.junit.dataprovider.DataProvider;
020import com.tngtech.java.junit.dataprovider.DataProviderRunner;
021import com.tngtech.java.junit.dataprovider.UseDataProvider;
022import cz.hobrasoft.pdfmu.error.ErrorType;
023import org.junit.Assert;
024import org.junit.Rule;
025import org.junit.Test;
026import org.junit.contrib.java.lang.system.Assertion;
027import org.junit.contrib.java.lang.system.SystemOutRule;
028import org.junit.runner.RunWith;
029
030/**
031 * @author Filip Bartek
032 */
033@RunWith(DataProviderRunner.class)
034public class MainBasicTest extends MainTest {
035
036    @Rule
037    public final SystemOutRule systemOutRule
038            = new SystemOutRule().mute().enableLog();
039
040    @DataProvider
041    public static Object[][] dataProviderParserError() {
042        return new Object[][]{
043            new Object[]{new String[]{"--unrecognized-argument"}, ErrorType.PARSER_UNRECOGNIZED_ARGUMENT.getCode()},
044            new Object[]{new String[]{"--output-format", "invalid-format"}, ErrorType.PARSER_INVALID_CHOICE.getCode()},
045            new Object[]{new String[]{"unrecognized-command"}, ErrorType.PARSER_UNRECOGNIZED_COMMAND.getCode()},
046            new Object[]{new String[]{}, ErrorType.PARSER_TOO_FEW_ARGUMENTS.getCode()},
047            new Object[]{new String[]{"--output-format"}, ErrorType.PARSER_EXPECTED_ONE_ARGUMENT.getCode()}
048        };
049    }
050
051    @Test
052    @UseDataProvider
053    public void testParserError(String[] args, int exitStatus) {
054        exit.expectSystemExitWithStatus(exitStatus);
055        Main.main(args);
056        assert false;
057    }
058
059    @Test
060    public void testVersion() {
061        exit.expectSystemExitWithStatus(0);
062        exit.checkAssertionAfterwards(new Assertion() {
063            @Override
064            public void checkAssertion() {
065                // The terminating line break is introduced by Argparse4j
066                Assert.assertEquals(String.format("%1$s\n", Main.getProjectVersion()),
067                        systemOutRule.getLogWithNormalizedLineSeparator());
068            }
069        });
070        Main.main(new String[]{"--version"});
071        assert false;
072    }
073
074    @Test
075    public void testHelp() {
076        exit.expectSystemExitWithStatus(0);
077        Main.main(new String[]{"--help"});
078        assert false;
079    }
080
081    @Test
082    public void testLegalNotice() {
083        exit.expectSystemExitWithStatus(0);
084        exit.checkAssertionAfterwards(new Assertion() {
085            @Override
086            public void checkAssertion() {
087                Assert.assertEquals(Main.getLegalNotice(),
088                        systemOutRule.getLogWithNormalizedLineSeparator());
089            }
090        });
091        Main.main(new String[]{"--legal-notice"});
092        assert false;
093    }
094
095}