b2s.idea.mavenize.AppTest.java Source code

Java tutorial

Introduction

Here is the source code for b2s.idea.mavenize.AppTest.java

Source

/**
 *
 * Copyright to the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */
package b2s.idea.mavenize;

import org.apache.commons.cli.ParseException;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class AppTest {
    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();
    @Mock
    private JarCombiner combiner;
    private ByteArrayOutputStream output;
    private File ideaFolder;
    private PrintStream originalSysOut;
    private File outputFolder;
    private File ideaLibFolder;

    @Before
    public void setUp() throws Exception {
        output = new ByteArrayOutputStream();

        App.jarCombiner = combiner;
        originalSysOut = System.out;
        System.setOut(new PrintStream(output));

        ideaFolder = temporaryFolder.newFolder();
        outputFolder = temporaryFolder.newFolder();
        ideaLibFolder = new File(ideaFolder, "lib");
        IOUtils.write("build-version", new FileOutputStream(new File(ideaFolder, "build.txt")));
    }

    @After
    public void tearDown() throws Exception {
        System.setOut(originalSysOut);
    }

    @Test
    public void shouldPrintAnErrorWhenNoOutputDirectoryIsProvided() throws ParseException {
        ideaLibFolder.mkdirs();

        App.main("-i", ideaFolder.getAbsolutePath());

        assertErrorWasPrinted("Please provide an output directory");
    }

    @Test
    public void shouldStartCombiningTheJarsIfEverythingWasProvided() throws ParseException {
        ideaLibFolder.mkdirs();

        App.main("-i", ideaFolder.getAbsolutePath(), "-o", outputFolder.getAbsolutePath());

        verify(combiner).combineAllJarsIn(ideaLibFolder, new File(outputFolder, "idea-sdk-build-version.jar"));
        assertNoErrorsWerePrinted();
    }

    @Test
    public void shouldPrintAnErrorAboutTheIdeaFolderWhenTheOutputFolderIsProvided() throws ParseException {
        App.main("-o", outputFolder.getAbsolutePath());

        assertErrorWasPrinted("Please provide the IDEA directory path");
    }

    @Test
    public void shouldPrintAnErrorMessageWhenTheIdeaFolderDoesNotHaveALibFolder() throws ParseException {
        App.main("-i", ideaFolder.getAbsolutePath());

        assertErrorWasPrinted("The [lib] folder was not found in the IDEA folder");
    }

    @Test
    public void shouldPrintAnErrorMessageWhenTheIdeaFolderDoesNotExist() throws ParseException {
        App.main("-i", "does-not-exist");

        assertErrorWasPrinted("This path [does-not-exist] can not be found");
    }

    @Test
    public void shouldPrintHelpWhenNoArgsAreProvided() throws ParseException {
        App.main();

        assertHelpWasPrinted();
    }

    private void assertNoErrorsWerePrinted() {
        assertNumberOfErrorsPrinted(0);
    }

    private void assertErrorWasPrinted(String expectedError) {
        assertTrue("Did not find the expected error message\nExpectedToFind=[ERROR:" + expectedError + "]\nActual=["
                + printedOutput() + "]", printedOutput().contains("ERROR:" + expectedError));
        assertNumberOfErrorsPrinted(1);
        assertHelpWasPrinted();
    }

    private void assertNumberOfErrorsPrinted(int expectedCount) {
        String output = printedOutput();
        int count = 0;
        int index = -1;
        while ((index = output.indexOf("ERROR:", index + 1)) != -1) {
            count++;
        }
        assertEquals(
                "we expected only " + expectedCount + " error message to be printed. ActualOutput=[" + output + "]",
                expectedCount, count);
    }

    private void assertHelpWasPrinted() {
        assertTrue(printedOutput().contains("usage: <jar-file>"));
    }

    private String printedOutput() {
        return new String(output.toByteArray());
    }
}