|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Microsoft Corporation and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Microsoft Corporation - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | + |
| 12 | +package com.microsoft.java.test.plugin.util; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | +import static org.junit.Assert.assertNotNull; |
| 16 | +import static org.junit.Assert.assertNull; |
| 17 | +import static org.junit.Assert.assertTrue; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.eclipse.core.resources.IProject; |
| 23 | +import org.eclipse.core.runtime.NullProgressMonitor; |
| 24 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 25 | +import org.eclipse.jdt.core.IJavaProject; |
| 26 | +import org.eclipse.jdt.core.IType; |
| 27 | +import org.eclipse.jdt.core.JavaCore; |
| 28 | +import org.eclipse.jdt.core.compiler.IProblem; |
| 29 | +import org.eclipse.jdt.core.dom.AST; |
| 30 | +import org.eclipse.jdt.core.dom.ASTParser; |
| 31 | +import org.eclipse.jdt.core.dom.CompilationUnit; |
| 32 | +import org.eclipse.jdt.core.dom.TypeDeclaration; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +import com.microsoft.java.test.plugin.AbstractProjectsManagerBasedTest; |
| 36 | +import com.microsoft.java.test.plugin.model.JavaTestItem; |
| 37 | + |
| 38 | +public class TestSearchUtilsTest extends AbstractProjectsManagerBasedTest { |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testDiscoverJUnit5TestsWithOlderPreviewSource() throws Exception { |
| 42 | + final IProject project = importProjects("preview-junit").get(0); |
| 43 | + final IJavaProject javaProject = JavaCore.create(project); |
| 44 | + final IType type = javaProject.findType("example.MiniTest"); |
| 45 | + assertNotNull(type); |
| 46 | + final ICompilationUnit unit = type.getCompilationUnit(); |
| 47 | + javaProject.setOption(JavaCore.COMPILER_SOURCE, "17"); |
| 48 | + javaProject.setOption(JavaCore.COMPILER_COMPLIANCE, "17"); |
| 49 | + javaProject.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); |
| 50 | + |
| 51 | + final ASTParser parser = ASTParser.newParser(AST.getJLSLatest()); |
| 52 | + parser.setSource(unit); |
| 53 | + parser.setCompilerOptions(javaProject.getOptions(true)); |
| 54 | + parser.setResolveBindings(true); |
| 55 | + final CompilationUnit invalid = (CompilationUnit) parser.createAST(new NullProgressMonitor()); |
| 56 | + assertTrue(Arrays.stream(invalid.getProblems()) |
| 57 | + .anyMatch(problem -> problem.getID() == IProblem.PreviewFeaturesNotAllowed)); |
| 58 | + assertNull(((TypeDeclaration) invalid.types().get(0)).resolveBinding()); |
| 59 | + |
| 60 | + final List<JavaTestItem> fileTests = TestSearchUtils.findTestTypesAndMethods( |
| 61 | + Arrays.asList(unit.getResource().getLocationURI().toString()), new NullProgressMonitor()); |
| 62 | + assertEquals(2, fileTests.size()); |
| 63 | + assertEquals("MiniTest", fileTests.get(0).getLabel()); |
| 64 | + assertEquals(2, fileTests.get(0).getChildren().size()); |
| 65 | + assertEquals("SiblingTest", fileTests.get(1).getLabel()); |
| 66 | + assertEquals(1, fileTests.get(1).getChildren().size()); |
| 67 | + |
| 68 | + final ICompilationUnit helper = javaProject.findType("example.Helper").getCompilationUnit(); |
| 69 | + assertTrue(TestSearchUtils.findTestTypesAndMethods( |
| 70 | + Arrays.asList(helper.getResource().getLocationURI().toString()), new NullProgressMonitor()).isEmpty()); |
| 71 | + |
| 72 | + final IType nestedType = javaProject.findType("example.NestedOnlyTest"); |
| 73 | + final List<JavaTestItem> nestedTests = TestSearchUtils.findTestTypesAndMethods( |
| 74 | + Arrays.asList(nestedType.getResource().getLocationURI().toString()), new NullProgressMonitor()); |
| 75 | + assertEquals("Child", nestedTests.get(0).getChildren().get(0).getLabel()); |
| 76 | + |
| 77 | + final List<JavaTestItem> packages = TestSearchUtils.findTestPackagesAndTypes( |
| 78 | + Arrays.asList(javaProject.getHandleIdentifier()), new NullProgressMonitor()); |
| 79 | + assertEquals(1, packages.size()); |
| 80 | + assertEquals(3, packages.get(0).getChildren().size()); |
| 81 | + assertTrue(packages.get(0).getChildren().stream().anyMatch(item -> "MiniTest".equals(item.getLabel()))); |
| 82 | + assertTrue(packages.get(0).getChildren().stream().anyMatch(item -> "SiblingTest".equals(item.getLabel()))); |
| 83 | + assertTrue(packages.get(0).getChildren().stream().anyMatch(item -> "NestedOnlyTest".equals(item.getLabel()))); |
| 84 | + |
| 85 | + final List<JavaTestItem> methods = TestSearchUtils.findDirectTestChildrenForClass( |
| 86 | + Arrays.asList(type.getHandleIdentifier()), new NullProgressMonitor()); |
| 87 | + assertEquals(2, methods.size()); |
| 88 | + final IType siblingType = unit.getType("SiblingTest"); |
| 89 | + final List<JavaTestItem> siblingMethods = TestSearchUtils.findDirectTestChildrenForClass( |
| 90 | + Arrays.asList(siblingType.getHandleIdentifier()), new NullProgressMonitor()); |
| 91 | + assertEquals(1, siblingMethods.size()); |
| 92 | + final List<JavaTestItem> nestedChildren = TestSearchUtils.findDirectTestChildrenForClass( |
| 93 | + Arrays.asList(nestedType.getHandleIdentifier()), new NullProgressMonitor()); |
| 94 | + assertEquals("Child", nestedChildren.get(0).getLabel()); |
| 95 | + assertEquals(JavaCore.ENABLED, javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true)); |
| 96 | + } |
| 97 | +} |
0 commit comments