All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.izforge.izpack.matcher.MergeMatcher Maven / Gradle / Ivy

package com.izforge.izpack.matcher;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.Is;
import org.hamcrest.core.IsCollectionContaining;

import com.izforge.izpack.api.merge.Mergeable;
import com.izforge.izpack.mock.MockOutputStream;

/**
 * Matcher for mergeable
 *
 * @author Anthonin Bonnefoy
 */
public class MergeMatcher extends TypeSafeMatcher
{
    private static final Logger logger = Logger.getLogger(MergeMatcher.class.getName());

    private Matcher> listMatcher;

    MergeMatcher(Matcher> matcher)
    {
        this.listMatcher = matcher;
    }

    @Override
    public boolean matchesSafely(Mergeable mergeable)
    {
        try
        {
            MockOutputStream outputStream = new MockOutputStream();
            mergeable.merge(outputStream);
            List entryName = outputStream.getListEntryName();

            boolean match = listMatcher.matches(entryName);
            if (logger.isLoggable(Level.FINE) && !match)
            {
                logger.fine("++++++++++++++++++++++++++++++++++++++");
                logger.fine("\nContents of mergeable " + mergeable + ":\n");
                for (String entry : entryName) {
                  logger.fine("\t" + entry);
                }
                logger.fine("\nMATCH: " + match + "\n");
                logger.fine("++++++++++++++++++++++++++++++++++++++");
            }
            return match;
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void describeTo(Description description)
    {
        description.appendText("Expecting mergeable containing ").appendValue(listMatcher);
    }

    @Factory
    public static Matcher isMergeableContainingFile(String fileName)
    {
        return new MergeMatcher(IsCollectionContaining.hasItems(Is.is(fileName)));
    }

    @Factory
    public static Matcher isMergeableMatching(Matcher> matcher)
    {
        return new MergeMatcher(matcher);
    }

    @Factory
    public static Matcher isMergeableContainingFiles(String... files)
    {
        return new MergeMatcher(IsCollectionContaining.hasItems(files));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy