org.mule.test.infrastructure.FileContainsInLine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mule-tests-infrastructure Show documentation
Show all versions of mule-tests-infrastructure Show documentation
Tests infrastructure library for creating mule automated test cases.
/*
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package org.mule.test.infrastructure;
import static java.nio.charset.Charset.defaultCharset;
import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.junit.internal.matchers.TypeSafeMatcher;
public class FileContainsInLine extends TypeSafeMatcher
{
private final Matcher stringMatcher;
@Factory
public static Matcher hasLine(Matcher matcher)
{
return new FileContainsInLine(matcher);
}
private FileContainsInLine(Matcher matcher)
{
stringMatcher = matcher;
}
@Override
public void describeTo(Description description)
{
description.appendText("a file where a line ").appendDescriptionOf(stringMatcher);
}
@Override
public boolean matchesSafely(File file)
{
String line;
try(BufferedReader reader = Files.newBufferedReader(file.toPath(), defaultCharset()))
{
while ((line = reader.readLine()) != null)
{
if (stringMatcher.matches(line))
{
return true;
}
}
}
catch (IOException e)
{
fail(String.format("Exception %s caught while reading the file %s trying to match its line with the matcher %s",e.getMessage(),file.getAbsolutePath(),stringMatcher.toString()));
return false;
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy