001/* 002 * Copyright 2015-2018 the original author or authors 003 * 004 * This software is licensed under the Apache License, Version 2.0, 005 * the GNU Lesser General Public License version 2 or later ("LGPL") 006 * and the WTFPL. 007 * You may choose either license to govern your use of this software only 008 * upon the condition that you accept all of the terms of either 009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL. 010 */ 011package org.minidns.integrationtest; 012 013import java.lang.reflect.InvocationTargetException; 014import java.lang.reflect.Method; 015import java.util.ArrayList; 016import java.util.HashSet; 017import java.util.List; 018import java.util.Properties; 019import java.util.Set; 020import java.util.logging.Level; 021import java.util.logging.Logger; 022 023import org.junit.Ignore; 024import org.minidns.dnsname.DnsName; 025import org.minidns.jul.MiniDnsJul; 026import org.minidns.record.Record.TYPE; 027 028public class IntegrationTestHelper { 029 030 public static final DnsName DNSSEC_DOMAIN = DnsName.from("verteiltesysteme.net"); 031 public static final TYPE RR_TYPE = TYPE.A; 032 033 private static Set<Class<?>> testClasses = new HashSet<>(); 034 private static Logger LOGGER = Logger.getLogger(IntegrationTestHelper.class.getName()); 035 036 enum TestResult { 037 Success, 038 Failure, 039 ; 040 } 041 042 static { 043 testClasses.add(CoreTest.class); 044 testClasses.add(DnssecTest.class); 045 testClasses.add(DaneTest.class); 046 testClasses.add(HlaTest.class); 047 testClasses.add(NsidTest.class); 048 testClasses.add(IterativeDnssecTest.class); 049 } 050 051 private static final String MINTTEST = "minttest."; 052 053 public static void main(String[] args) { 054 Properties systemProperties = System.getProperties(); 055 String debugString = systemProperties.getProperty(MINTTEST + "debug", Boolean.toString(false)); 056 boolean debug = Boolean.parseBoolean(debugString); 057 if (debug) { 058 LOGGER.info("Enabling debug and trace output"); 059 MiniDnsJul.enableMiniDnsTrace(); 060 } 061 062 int testsRun = 0; 063 List<Method> successfulTests = new ArrayList<>(); 064 List<Method> failedTests = new ArrayList<>(); 065 List<Method> ignoredTests = new ArrayList<>(); 066 for (final Class<?> aClass : testClasses) { 067 for (final Method method : aClass.getDeclaredMethods()) { 068 if (!method.isAnnotationPresent(IntegrationTest.class)) { 069 continue; 070 } 071 if (method.isAnnotationPresent(Ignore.class)) { 072 ignoredTests.add(method); 073 continue; 074 } 075 TestResult result = invokeTest(method, aClass); 076 testsRun++; 077 switch (result) { 078 case Success: 079 successfulTests.add(method); 080 break; 081 case Failure: 082 failedTests.add(method); 083 break; 084 } 085 } 086 } 087 StringBuilder resultMessage = new StringBuilder(); 088 resultMessage.append("MiniDNS Integration Test Result: [").append(successfulTests.size()).append('/').append(testsRun).append("] "); 089 if (!ignoredTests.isEmpty()) { 090 resultMessage.append("(Ignored: ").append(ignoredTests.size()).append(") "); 091 } 092 int exitStatus = 0; 093 if (failedTests.isEmpty()) { 094 resultMessage.append("SUCCESS \\o/"); 095 } else { 096 resultMessage.append("FAILURE :("); 097 exitStatus = 2; 098 } 099 LOGGER.info(resultMessage.toString()); 100 System.exit(exitStatus); 101 } 102 103 public static TestResult invokeTest(Method method, Class<?> aClass) { 104 Class<?> expected = method.getAnnotation(IntegrationTest.class).expected(); 105 if (!Exception.class.isAssignableFrom(expected)) expected = null; 106 107 String testClassName = method.getDeclaringClass().getSimpleName(); 108 String testMethodName = method.getName(); 109 110 LOGGER.logp(Level.INFO, testClassName, testMethodName, "Test start."); 111 try { 112 method.invoke(null); 113 114 if (expected != null) { 115 LOGGER.logp(Level.WARNING, testClassName, testMethodName, "Test failed: expected exception " + expected + " was not thrown!"); 116 return TestResult.Failure; 117 } else { 118 LOGGER.logp(Level.INFO, testClassName, testMethodName, "Test suceeded."); 119 return TestResult.Success; 120 } 121 } catch (InvocationTargetException e) { 122 if (expected != null && expected.isAssignableFrom(e.getTargetException().getClass())) { 123 LOGGER.logp(Level.INFO, testClassName, testMethodName, "Test suceeded."); 124 return TestResult.Success; 125 } else { 126 LOGGER.logp(Level.WARNING, testClassName, testMethodName, "Test failed: unexpected exception was thrown: ", e.getTargetException()); 127 return TestResult.Failure; 128 } 129 } catch (IllegalAccessException | NullPointerException e) { 130 LOGGER.logp(Level.SEVERE, testClassName, testMethodName, "Test failed: could not invoke test, is it public static?"); 131 return TestResult.Failure; 132 } 133 } 134}