|
| 1 | +import static org.junit.jupiter.api.Assertions.*; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.ByteArrayOutputStream; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.PrintStream; |
| 7 | +import java.lang.reflect.Method; |
| 8 | +import java.nio.file.*; |
| 9 | +import java.util.*; |
| 10 | +import java.util.stream.*; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +class UvaTestRunner { |
| 14 | + |
| 15 | + // Standard input for all tests (customize per problem if needed) |
| 16 | + private static final String TEST_INPUT = "5\n1 2 3 4 5\n"; |
| 17 | + private static final String EXPECTED_OUTPUT = "15\n"; |
| 18 | + |
| 19 | + @Test |
| 20 | + void testAllUvaSolutions() throws Exception { |
| 21 | + List<Class<?>> solutionClasses = discoverUvaSolutionClasses(); |
| 22 | + assertFalse(solutionClasses.isEmpty(), "No UVA solution classes found"); |
| 23 | + |
| 24 | + for (Class<?> clazz : solutionClasses) { |
| 25 | + testSolutionClass(clazz); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + private List<Class<?>> discoverUvaSolutionClasses() throws Exception { |
| 30 | + String basePackage = "com.algorithm.solutions.uva"; |
| 31 | + Path classLocation = Paths.get("target/classes", basePackage.replace('.', '/')); |
| 32 | + |
| 33 | + return Files.walk(classLocation) |
| 34 | + .filter(p -> p.toString().endsWith("Main.class")) |
| 35 | + .map(p -> toClassName(p, classLocation, basePackage)) |
| 36 | + .map(this::loadClass) |
| 37 | + .filter(Objects::nonNull) |
| 38 | + .filter(this::hasMainMethod) |
| 39 | + .collect(Collectors.toList()); |
| 40 | + } |
| 41 | + |
| 42 | + private String toClassName(Path path, Path basePath, String basePackage) { |
| 43 | + return basePackage + "." |
| 44 | + + basePath.relativize(path) |
| 45 | + .toString() |
| 46 | + .replace(".class", "") |
| 47 | + .replace(FileSystems.getDefault().getSeparator(), "."); |
| 48 | + } |
| 49 | + |
| 50 | + private Class<?> loadClass(String className) { |
| 51 | + try { |
| 52 | + return Class.forName(className); |
| 53 | + } catch (ClassNotFoundException e) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private boolean hasMainMethod(Class<?> clazz) { |
| 59 | + try { |
| 60 | + Method main = clazz.getMethod("main", String[].class); |
| 61 | + return main != null; |
| 62 | + } catch (NoSuchMethodException e) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private void testSolutionClass(Class<?> clazz) { |
| 68 | + String className = clazz.getSimpleName(); |
| 69 | + System.out.println("Testing: " + clazz.getName()); |
| 70 | + |
| 71 | + // Redirect I/O |
| 72 | + InputStream originalIn = System.in; |
| 73 | + PrintStream originalOut = System.out; |
| 74 | + |
| 75 | + try { |
| 76 | + System.setIn(new ByteArrayInputStream(TEST_INPUT.getBytes())); |
| 77 | + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
| 78 | + System.setOut(new PrintStream(outContent)); |
| 79 | + |
| 80 | + // Invoke main method |
| 81 | + Method main = clazz.getMethod("main", String[].class); |
| 82 | + main.invoke(null, (Object) new String[0]); |
| 83 | + |
| 84 | + // Verify output |
| 85 | + String actualOutput = outContent.toString(); |
| 86 | + assertTrue( |
| 87 | + actualOutput.contains(EXPECTED_OUTPUT), |
| 88 | + "Failed for " + className + "\nExpected: " + EXPECTED_OUTPUT + "\nActual: " + actualOutput); |
| 89 | + |
| 90 | + } catch (Exception e) { |
| 91 | + fail("Exception in " + className + ": " + e.getMessage()); |
| 92 | + } finally { |
| 93 | + System.setIn(originalIn); |
| 94 | + System.setOut(originalOut); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments