|
| 1 | +package example; |
| 2 | + |
| 3 | +import com.marklogic.client.DatabaseClient; |
| 4 | +import com.marklogic.client.DatabaseClientFactory; |
| 5 | +import com.marklogic.test.unit.TestManager; |
| 6 | +import com.marklogic.test.unit.TestModule; |
| 7 | +import com.marklogic.test.unit.TestResult; |
| 8 | +import com.marklogic.test.unit.TestSuiteResult; |
| 9 | +import org.junit.AfterClass; |
| 10 | +import org.junit.Assert; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | +import org.junit.runners.Parameterized; |
| 14 | + |
| 15 | +import java.io.FileReader; |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Properties; |
| 19 | + |
| 20 | +/** |
| 21 | + * Example of running each test module as a separate JUnit test. |
| 22 | + */ |
| 23 | +@RunWith(Parameterized.class) |
| 24 | +public class ParameterizedTest extends Assert { |
| 25 | + |
| 26 | + private TestModule testModule; |
| 27 | + |
| 28 | + private static TestManager testManager; |
| 29 | + private static DatabaseClient databaseClient; |
| 30 | + |
| 31 | + public ParameterizedTest(TestModule testModule) { |
| 32 | + this.testModule = testModule; |
| 33 | + } |
| 34 | + |
| 35 | + @AfterClass |
| 36 | + public static void releaseDatabaseClient() { |
| 37 | + if (databaseClient != null) { |
| 38 | + databaseClient.release(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * This sets up the parameters for our test by getting a list of the test modules. |
| 44 | + * <p> |
| 45 | + * Also creates a DatabaseClient based on the values in gradle.properties. Typically, properties will be retrieved via |
| 46 | + * a more robust mechanism, like Spring's test framework support. |
| 47 | + * |
| 48 | + * @return |
| 49 | + * @throws Exception |
| 50 | + */ |
| 51 | + @Parameterized.Parameters(name = "{index}: {0}") |
| 52 | + public static List<TestModule> getTestModules() throws IOException { |
| 53 | + Properties props = new Properties(); |
| 54 | + props.load(new FileReader("gradle.properties")); |
| 55 | + final String host = props.getProperty("mlHost"); |
| 56 | + final int port = Integer.parseInt(props.getProperty("mlTestRestPort")); |
| 57 | + final String username = props.getProperty("mlUsername"); |
| 58 | + final String password = props.getProperty("mlPassword"); |
| 59 | + |
| 60 | + databaseClient = DatabaseClientFactory.newClient(host, port, |
| 61 | + new DatabaseClientFactory.DigestAuthContext(username, password)); |
| 62 | + testManager = new TestManager(databaseClient); |
| 63 | + return testManager.list(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void test() { |
| 68 | + TestSuiteResult result = testManager.run(this.testModule); |
| 69 | + for (TestResult testResult : result.getTestResults()) { |
| 70 | + String failureXml = testResult.getFailureXml(); |
| 71 | + if (failureXml != null) { |
| 72 | + fail(String.format("Test %s in suite %s failed, cause: %s", testResult.getName(), testModule.getSuite(), failureXml)); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments