| 1 |
/* |
| 2 |
* Copyright 2015-2020 the original author or authors |
| 3 |
* |
| 4 |
* This software is licensed under the Apache License, Version 2.0, |
| 5 |
* the GNU Lesser General Public License version 2 or later ("LGPL") |
| 6 |
* and the WTFPL. |
| 7 |
* You may choose either license to govern your use of this software only |
| 8 |
* upon the condition that you accept all of the terms of either |
| 9 |
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL. |
| 10 |
*/ |
| 11 |
package org.minidns.integrationtest; |
| 12 |
|
| 13 |
import java.lang.reflect.InvocationTargetException; |
| 14 |
import java.lang.reflect.Method; |
| 15 |
import java.util.ArrayList; |
| 16 |
import java.util.HashSet; |
| 17 |
import java.util.List; |
| 18 |
import java.util.Properties; |
| 19 |
import java.util.Set; |
| 20 |
import java.util.logging.Level; |
| 21 |
import java.util.logging.Logger; |
| 22 |
|
| 23 |
import org.junit.Ignore; |
| 24 |
import org.minidns.dnsname.DnsName; |
| 25 |
import org.minidns.jul.MiniDnsJul; |
| 26 |
import org.minidns.record.Record.TYPE; |
| 27 |
|
| 28 |
public class IntegrationTestHelper { |
| 29 |
|
| 30 |
public static final DnsName DNSSEC_DOMAIN = DnsName.from("verteiltesysteme.net"); |
| 31 |
public static final TYPE RR_TYPE = TYPE.A; |
| 32 |
|
| 33 |
private static Set<Class<?>> testClasses = new HashSet<>(); |
| 34 |
private static Logger LOGGER = Logger.getLogger(IntegrationTestHelper.class.getName()); |
| 35 |
|
| 36 |
enum TestResult { |
| 37 |
Success, |
| 38 |
Failure, |
| 39 |
} |
| 40 |
|
| 41 |
static { |
| 42 |
testClasses.add(CoreTest.class); |
| 43 |
testClasses.add(DnssecTest.class); |
| 44 |
testClasses.add(DaneTest.class); |
| 45 |
testClasses.add(HlaTest.class); |
| 46 |
testClasses.add(NsidTest.class); |
| 47 |
testClasses.add(IterativeDnssecTest.class); |
| 48 |
} |
| 49 |
|
| 50 |
private static final String MINTTEST = "minttest."; |
| 51 |
|
| 52 |
public static void main(String[] args) { |
| 53 |
Properties systemProperties = System.getProperties(); |
| 54 |
String debugString = systemProperties.getProperty(MINTTEST + "debug", Boolean.toString(false)); |
| 55 |
boolean debug = Boolean.parseBoolean(debugString); |
| 56 |
if (debug) { |
| 57 |
LOGGER.info("Enabling debug and trace output"); |
| 58 |
MiniDnsJul.enableMiniDnsTrace(); |
| 59 |
} |
| 60 |
|
| 61 |
int testsRun = 0; |
| 62 |
List<Method> successfulTests = new ArrayList<>(); |
| 63 |
List<Method> failedTests = new ArrayList<>(); |
| 64 |
List<Method> ignoredTests = new ArrayList<>(); |
| 65 |
for (final Class<?> aClass : testClasses) { |
| 66 |
for (final Method method : aClass.getDeclaredMethods()) { |
| 67 |
if (!method.isAnnotationPresent(IntegrationTest.class)) { |
| 68 |
continue; |
| 69 |
} |
| 70 |
if (method.isAnnotationPresent(Ignore.class)) { |
| 71 |
ignoredTests.add(method); |
| 72 |
continue; |
| 73 |
} |
| 74 |
TestResult result = invokeTest(method, aClass); |
| 75 |
testsRun++; |
| 76 |
switch (result) { |
| 77 |
case Success: |
| 78 |
successfulTests.add(method); |
| 79 |
break; |
| 80 |
case Failure: |
| 81 |
failedTests.add(method); |
| 82 |
break; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
StringBuilder resultMessage = new StringBuilder(); |
| 87 |
resultMessage.append("MiniDNS Integration Test Result: [").append(successfulTests.size()).append('/').append(testsRun).append("] "); |
| 88 |
if (!ignoredTests.isEmpty()) { |
| 89 |
resultMessage.append("(Ignored: ").append(ignoredTests.size()).append(") "); |
| 90 |
} |
| 91 |
int exitStatus = 0; |
| 92 |
if (failedTests.isEmpty()) { |
| 93 |
resultMessage.append("SUCCESS \\o/"); |
| 94 |
} else { |
| 95 |
resultMessage.append("FAILURE :("); |
| 96 |
exitStatus = 2; |
| 97 |
} |
| 98 |
LOGGER.info(resultMessage.toString()); |
| 99 |
System.exit(exitStatus); |
| 100 |
} |
| 101 |
|
| 102 |
public static TestResult invokeTest(Method method, Class<?> aClass) { |
| 103 |
Class<?> expected = method.getAnnotation(IntegrationTest.class).expected(); |
| 104 |
if (!Exception.class.isAssignableFrom(expected)) expected = null; |
| 105 |
|
| 106 |
String testClassName = method.getDeclaringClass().getSimpleName(); |
| 107 |
String testMethodName = method.getName(); |
| 108 |
|
| 109 |
LOGGER.logp(Level.INFO, testClassName, testMethodName, "Test start."); |
| 110 |
try { |
| 111 |
method.invoke(null); |
| 112 |
|
| 113 |
if (expected != null) { |
| 114 |
LOGGER.logp(Level.WARNING, testClassName, testMethodName, "Test failed: expected exception " + expected + " was not thrown!"); |
| 115 |
return TestResult.Failure; |
| 116 |
} else { |
| 117 |
LOGGER.logp(Level.INFO, testClassName, testMethodName, "Test suceeded."); |
| 118 |
return TestResult.Success; |
| 119 |
} |
| 120 |
} catch (InvocationTargetException e) { |
| 121 |
if (expected != null && expected.isAssignableFrom(e.getTargetException().getClass())) { |
| 122 |
LOGGER.logp(Level.INFO, testClassName, testMethodName, "Test suceeded."); |
| 123 |
return TestResult.Success; |
| 124 |
} else { |
| 125 |
LOGGER.logp(Level.WARNING, testClassName, testMethodName, "Test failed: unexpected exception was thrown: ", e.getTargetException()); |
| 126 |
return TestResult.Failure; |
| 127 |
} |
| 128 |
} catch (IllegalAccessException | NullPointerException e) { |
| 129 |
LOGGER.logp(Level.SEVERE, testClassName, testMethodName, "Test failed: could not invoke test, is it public static?"); |
| 130 |
return TestResult.Failure; |
| 131 |
} |
| 132 |
} |
| 133 |
} |