|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Carsten Hammer. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Carsten Hammer |
| 13 | + *******************************************************************************/ |
| 14 | +package org.sandbox.mining.scanner; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | + |
| 18 | +import java.nio.charset.StandardCharsets; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.eclipse.jdt.core.JavaCore; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.io.TempDir; |
| 28 | +import org.sandbox.jdt.triggerpattern.api.GuardFunction; |
| 29 | +import org.sandbox.jdt.triggerpattern.api.GuardFunctionResolverHolder; |
| 30 | +import org.sandbox.jdt.triggerpattern.api.HintFile; |
| 31 | +import org.sandbox.jdt.triggerpattern.internal.BuiltInGuards; |
| 32 | +import org.sandbox.jdt.triggerpattern.internal.HintFileParser; |
| 33 | +import org.sandbox.mining.report.MiningReport; |
| 34 | + |
| 35 | +class SourceScannerTest { |
| 36 | + |
| 37 | + @TempDir |
| 38 | + Path tempDir; |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + void setUp() { |
| 42 | + java.util.HashMap<String, GuardFunction> guards = new java.util.HashMap<>(); |
| 43 | + BuiltInGuards.registerAll(guards); |
| 44 | + GuardFunctionResolverHolder.setResolver(guards::get); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void forwardsCompilerOptionsToSourceVersionGuards() throws Exception { |
| 49 | + Files.writeString(tempDir.resolve("Test.java"), //$NON-NLS-1$ |
| 50 | + "class Test { Object m() { return java.util.Collections.emptyList(); } }", //$NON-NLS-1$ |
| 51 | + StandardCharsets.UTF_8); |
| 52 | + HintFile hintFile = new HintFileParser().parse(""" |
| 53 | + <!id: source-version-test> |
| 54 | + java.util.Collections.emptyList() :: sourceVersionGE(9) |
| 55 | + => java.util.List.of() |
| 56 | + ;; |
| 57 | + """); //$NON-NLS-1$ |
| 58 | + SourceScanner scanner = new SourceScanner(new StandaloneAstParser(), 100); |
| 59 | + |
| 60 | + MiningReport java8Report = scanner.scan("repo", tempDir, List.of(), List.of(hintFile), //$NON-NLS-1$ |
| 61 | + Map.of(JavaCore.COMPILER_SOURCE, "1.8")); //$NON-NLS-1$ |
| 62 | + MiningReport java21Report = scanner.scan("repo", tempDir, List.of(), List.of(hintFile), //$NON-NLS-1$ |
| 63 | + Map.of(JavaCore.COMPILER_SOURCE, "21")); //$NON-NLS-1$ |
| 64 | + |
| 65 | + assertEquals(0, java8Report.getMatches().size()); |
| 66 | + assertEquals(1, java21Report.getMatches().size()); |
| 67 | + assertEquals("source-version-test1", java21Report.getMatches().get(0).ruleName()); //$NON-NLS-1$ |
| 68 | + } |
| 69 | +} |
0 commit comments