Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Allow TestNG suite creation only with tests filter
Browse files Browse the repository at this point in the history
  • Loading branch information
extsoft committed Oct 2, 2017
1 parent 397ad06 commit 4c90cfd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,22 @@
@SuppressWarnings("WeakerAccess")
public final class LoadableTestNGSuite implements TestNGSuite {

private static final String SUITE_NAME = "Sunshine suite";
private static final String SUITE_XML_FILE_NAME = "sunshine-suite.xml";
private final SunshineSuite artifacts;
private final File suiteXml;

/**
* Construct the new instance with the specified tests filter. All tests will be loaded from the classpath.
*
* @param filter the filter to be used to select desired tests
* @see #LoadableTestNGSuite(FileSystem, Condition)
* @since 0.2
*/
public LoadableTestNGSuite(Condition filter) {
this(new FileSystemOfClasspathClasses(), filter);
}

/**
* Constructs new instance with the specified file system and tests filter. All filtered tests
* will be printed to {@link System#out}.
Expand Down Expand Up @@ -93,22 +106,22 @@ public LoadableTestNGSuite(FileSystem fileSystem, Directory xmlSuiteDirectory, C
* @since 0.1
*/
public LoadableTestNGSuite(SunshineSuite suite, Directory xmlSuiteDirectory) {
artifacts = suite;
suiteXml = new FileBase(xmlSuiteDirectory, "sunshine-suite.xml");
this.artifacts = suite;
this.suiteXml = new FileBase(xmlSuiteDirectory, SUITE_XML_FILE_NAME);
}

@Override
public File tests() throws SuiteException {
XmlSuite xmlSuite = new XmlSuite();
xmlSuite.setName("Sunshine suite");
xmlSuite.setName(SUITE_NAME);
try {
for (SunshineTest sunshineTest : this.artifacts.tests()) {
XmlTest test = new TestNGTest(sunshineTest).object();
test.setSuite(xmlSuite);
xmlSuite.addTest(test);
}
suiteXml.write(xmlSuite.toXml());
return suiteXml;
this.suiteXml.write(xmlSuite.toXml());
return this.suiteXml;
} catch (TestException | IOException e) {
throw new SuiteException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public static void main(String[] args) {
new Sun(
new TestNGKernel(
new LoadableTestNGSuite(
new FileSystemOfClasspathClasses(),
new RegexCondition(
new AttributeWithPrintableValue(
"The following pattern will be used for classes filtering:",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package io.github.tatools.sunshine.testng;

import io.github.tatools.sunshine.core.Condition.Fake;
import io.github.tatools.sunshine.core.SuiteException;
import io.github.tatools.sunshine.core.*;
import org.hamcrest.CustomMatcher;
import org.hamcrest.MatcherAssert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.util.ArrayList;


/**
* @author Dmytro Serdiuk ([email protected])
Expand All @@ -21,21 +20,51 @@ public class LoadableTestNGSuiteTest {

@Test
public void testAutomaticSuiteDirectoryCreation() throws SuiteException {
new LoadableTestNGSuite(
ArrayList::new,
this.testFolder.getRoot().getAbsolutePath() + "/custom",
new Fake(true)
).tests();
MatcherAssert.assertThat(
new LoadableTestNGSuite(
new FileSystem.Fake(),
this.testFolder.getRoot().getAbsolutePath() + "/custom",
new Condition.Fake(true)
).tests(),
new SuiteFileMatcher()
);
}

@Test
public void testDefaultSuiteDirectoryCreation() throws SuiteException {
new LoadableTestNGSuite(ArrayList::new).tests();
MatcherAssert.assertThat(
new LoadableTestNGSuite(new SunshineSuite.Fake()).tests(),
new SuiteFileMatcher()
);
}


@Test
public void testFileSystemFilteringWithDefaultSuiteFolder() throws SuiteException {
new LoadableTestNGSuite(ArrayList::new, new Fake(true)).tests();
MatcherAssert.assertThat(
new LoadableTestNGSuite(new FileSystem.Fake(), new Condition.Fake(true)).tests(),
new SuiteFileMatcher()
);
}

@Test
public void testDefaultTestsFiltering() throws SuiteException {
MatcherAssert.assertThat(
new LoadableTestNGSuite(new Condition.Fake(false)).tests(),
new SuiteFileMatcher()
);
}

private static class SuiteFileMatcher extends CustomMatcher<File> {

public SuiteFileMatcher() {
super("Check existence of a suite file");
}

@Override
public boolean matches(Object item) {
final File file = (File) item;
return file.exist();
}
}
}

0 comments on commit 4c90cfd

Please sign in to comment.