|
1 | 1 | /*******************************************************************************
|
2 |
| - * Copyright (c) 2020 Red Hat, Inc. |
| 2 | + * Copyright (c) 2020, 2023 Red Hat, Inc. and others |
3 | 3 | * All rights reserved. This program and the accompanying materials
|
4 | 4 | * are made available under the terms of the Eclipse Public License 2.0
|
5 | 5 | * which accompanies this distribution, and is available at
|
|
10 | 10 |
|
11 | 11 | package org.eclipse.m2e.jdt.tests;
|
12 | 12 |
|
13 |
| -import static org.junit.Assert.*; |
| 13 | +import static org.junit.Assert.assertArrayEquals; |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | +import static org.junit.Assert.assertTrue; |
14 | 16 |
|
15 |
| -import java.io.*; |
| 17 | +import java.io.ByteArrayInputStream; |
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
16 | 22 | import java.util.Arrays;
|
| 23 | +import java.util.function.Predicate; |
17 | 24 |
|
18 |
| -import org.eclipse.core.resources.*; |
19 |
| -import org.eclipse.core.runtime.*; |
20 |
| -import org.eclipse.jdt.core.*; |
| 25 | +import org.eclipse.core.resources.IFile; |
| 26 | +import org.eclipse.core.resources.IMarker; |
| 27 | +import org.eclipse.core.resources.IProject; |
| 28 | +import org.eclipse.core.resources.IResource; |
| 29 | +import org.eclipse.core.resources.IncrementalProjectBuilder; |
| 30 | +import org.eclipse.core.runtime.CoreException; |
| 31 | +import org.eclipse.core.runtime.FileLocator; |
| 32 | +import org.eclipse.jdt.core.IClasspathEntry; |
| 33 | +import org.eclipse.jdt.core.IJavaProject; |
| 34 | +import org.eclipse.jdt.core.JavaCore; |
| 35 | +import org.eclipse.jdt.core.JavaModelException; |
21 | 36 | import org.eclipse.m2e.core.MavenPlugin;
|
22 | 37 | import org.eclipse.m2e.core.internal.preferences.MavenConfigurationImpl;
|
23 | 38 | import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
|
| 39 | +import org.junit.Before; |
24 | 40 | import org.junit.Test;
|
25 | 41 |
|
26 | 42 | @SuppressWarnings("restriction")
|
27 | 43 | public class JavaConfigurationTest extends AbstractMavenProjectTestCase {
|
28 | 44 |
|
29 |
| - @Test |
30 |
| - public void testFileChangeUpdatesJDTSettings() throws CoreException, IOException, InterruptedException { |
31 |
| - ((MavenConfigurationImpl) MavenPlugin.getMavenConfiguration()).setAutomaticallyUpdateConfiguration(true); |
32 |
| - setAutoBuilding(true); |
33 |
| - File pomFileFS = new File(FileLocator.toFileURL(getClass().getResource("/projects/compilerSettings/pom.xml")).getFile()); |
34 |
| - IProject project = importProject(pomFileFS.getAbsolutePath()); |
35 |
| - waitForJobsToComplete(); |
36 |
| - IJavaProject javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID); |
37 |
| - assertEquals("1.8", javaProject.getOption(JavaCore.COMPILER_SOURCE, false)); |
38 |
| - IFile pomFileWS = project.getFile("pom.xml"); |
39 |
| - byte[] bytes = new byte[(int) pomFileFS.length()]; |
40 |
| - try (InputStream stream = pomFileWS.getContents()) { |
41 |
| - stream.read(bytes); |
42 |
| - } |
43 |
| - String contents = new String(bytes); |
44 |
| - contents = contents.replace("1.8", "11"); |
45 |
| - pomFileWS.setContents(new ByteArrayInputStream(contents.getBytes()), true, false, null); |
46 |
| - waitForJobsToComplete(); |
47 |
| - assertEquals("11", javaProject.getOption(JavaCore.COMPILER_SOURCE, false)); |
48 |
| - } |
| 45 | + @Override |
| 46 | + @Before |
| 47 | + public void setUp() throws Exception { |
| 48 | + super.setUp(); |
| 49 | + ((MavenConfigurationImpl) MavenPlugin.getMavenConfiguration()).setAutomaticallyUpdateConfiguration(true); |
| 50 | + setAutoBuilding(true); |
| 51 | + } |
49 | 52 |
|
50 |
| - @Test |
51 |
| - public void testJDTWarnings() throws CoreException, IOException, InterruptedException { |
52 |
| - ((MavenConfigurationImpl) MavenPlugin.getMavenConfiguration()).setAutomaticallyUpdateConfiguration(true); |
53 |
| - setAutoBuilding(true); |
54 |
| - File pomFileFS = new File( |
55 |
| - FileLocator.toFileURL(getClass().getResource("/projects/compilerWarnings/pom.xml")).getFile()); |
56 |
| - waitForJobsToComplete(); |
57 |
| - IProject project = importProject(pomFileFS.getAbsolutePath()); |
58 |
| - IFile file = project.getFile("src/main/java/A.java"); |
59 |
| - project.build(IncrementalProjectBuilder.FULL_BUILD, null); |
60 |
| - waitForJobsToComplete(); |
61 |
| - IMarker[] findMarkers = file.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_ZERO); |
62 |
| - assertArrayEquals(new IMarker[0], findMarkers); |
63 |
| - } |
| 53 | + @Test |
| 54 | + public void testFileChangeUpdatesJDTSettings() throws CoreException, IOException, InterruptedException { |
| 55 | + IJavaProject project = importResourceProject("/projects/compilerSettings/pom.xml"); |
| 56 | + assertEquals("1.8", project.getOption(JavaCore.COMPILER_SOURCE, false)); |
| 57 | + IFile pomFileWS = project.getProject().getFile("pom.xml"); |
| 58 | + String pomContent = Files.readString(Path.of(pomFileWS.getLocationURI())); |
| 59 | + pomContent = pomContent.replace("1.8", "11"); |
| 60 | + pomFileWS.setContents(new ByteArrayInputStream(pomContent.getBytes()), true, false, null); |
| 61 | + waitForJobsToComplete(); |
| 62 | + assertEquals("11", project.getOption(JavaCore.COMPILER_SOURCE, false)); |
| 63 | + } |
64 | 64 |
|
65 |
| - @Test |
66 |
| - public void testSkipAllTest() throws CoreException, IOException, InterruptedException { |
67 |
| - ((MavenConfigurationImpl) MavenPlugin.getMavenConfiguration()).setAutomaticallyUpdateConfiguration(true); |
68 |
| - setAutoBuilding(true); |
69 |
| - File pomFileFS = new File( |
70 |
| - FileLocator.toFileURL(getClass().getResource("/projects/skipAllTest/pom.xml")).getFile()); |
71 |
| - waitForJobsToComplete(); |
72 |
| - IProject project = importProject(pomFileFS.getAbsolutePath()); |
73 |
| - waitForJobsToComplete(); |
74 |
| - IJavaProject jproject = JavaCore.create(project); |
75 |
| - assertTrue(Arrays.stream(jproject.getRawClasspath()).noneMatch(IClasspathEntry::isTest)); |
76 |
| - } |
| 65 | + @Test |
| 66 | + public void testJDTWarnings() throws CoreException, IOException, InterruptedException { |
| 67 | + IJavaProject project = importResourceProject("/projects/compilerWarnings/pom.xml"); |
| 68 | + IFile file = project.getProject().getFile("src/main/java/A.java"); |
| 69 | + project.getProject().build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 70 | + waitForJobsToComplete(); |
| 71 | + IMarker[] findMarkers = file.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_ZERO); |
| 72 | + assertArrayEquals(new IMarker[0], findMarkers); |
| 73 | + } |
77 | 74 |
|
78 |
| - @Test |
79 |
| - public void testSkipTestCompilation() throws CoreException, IOException, InterruptedException { |
80 |
| - ((MavenConfigurationImpl) MavenPlugin.getMavenConfiguration()).setAutomaticallyUpdateConfiguration(true); |
81 |
| - setAutoBuilding(true); |
82 |
| - File pomFileFS = new File( |
83 |
| - FileLocator.toFileURL(getClass().getResource("/projects/skipTestCompilation/pom.xml")).getFile()); |
84 |
| - waitForJobsToComplete(); |
85 |
| - IProject project = importProject(pomFileFS.getAbsolutePath()); |
86 |
| - waitForJobsToComplete(); |
87 |
| - IJavaProject jproject = JavaCore.create(project); |
88 |
| - assertTrue(Arrays.stream(jproject.getRawClasspath()) |
89 |
| - .noneMatch(cp -> cp.isTest() && cp.getPath().toString().contains("test/java"))); |
90 |
| - assertEquals(1, Arrays.stream(jproject.getRawClasspath()) |
91 |
| - .filter(cp -> cp.isTest() && cp.getPath().toString().contains("test/resources")) |
92 |
| - .count()); |
93 |
| - } |
| 75 | + @Test |
| 76 | + public void testSkipAllTest() throws CoreException, IOException, InterruptedException { |
| 77 | + IJavaProject project = importResourceProject("/projects/skipAllTest/pom.xml"); |
| 78 | + assertTrue(Arrays.stream(project.getRawClasspath()).noneMatch(IClasspathEntry::isTest)); |
| 79 | + } |
94 | 80 |
|
95 |
| - @Test |
96 |
| - public void testSkipTestResources() throws CoreException, IOException, InterruptedException { |
97 |
| - ((MavenConfigurationImpl) MavenPlugin.getMavenConfiguration()).setAutomaticallyUpdateConfiguration(true); |
98 |
| - setAutoBuilding(true); |
99 |
| - File pomFileFS = new File( |
100 |
| - FileLocator.toFileURL(getClass().getResource("/projects/skipTestResources/pom.xml")).getFile()); |
101 |
| - waitForJobsToComplete(); |
102 |
| - IProject project = importProject(pomFileFS.getAbsolutePath()); |
103 |
| - waitForJobsToComplete(); |
104 |
| - IJavaProject jproject = JavaCore.create(project); |
105 |
| - assertTrue(Arrays.stream(jproject.getRawClasspath()) |
106 |
| - .noneMatch(cp -> cp.isTest() && cp.getPath().toString().contains("test/resources"))); |
107 |
| - assertEquals(1, Arrays.stream(jproject.getRawClasspath()) |
108 |
| - .filter(cp -> cp.isTest() && cp.getPath().toString().contains("test/java")) |
109 |
| - .count()); |
110 |
| - } |
| 81 | + @Test |
| 82 | + public void testSkipTestCompilation() throws CoreException, IOException, InterruptedException { |
| 83 | + IJavaProject project = importResourceProject("/projects/skipTestCompilation/pom.xml"); |
| 84 | + assertEquals(0, classpathEntriesCount(project, TEST_SOURCES)); |
| 85 | + assertEquals(1, classpathEntriesCount(project, TEST_RESOURCES)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testSkipTestResources() throws CoreException, IOException, InterruptedException { |
| 90 | + IJavaProject project = importResourceProject("/projects/skipTestResources/pom.xml"); |
| 91 | + assertEquals(1, classpathEntriesCount(project, TEST_SOURCES)); |
| 92 | + assertEquals(0, classpathEntriesCount(project, TEST_RESOURCES)); |
| 93 | + } |
| 94 | + |
| 95 | + // --- utility methods --- |
| 96 | + |
| 97 | + private static final Predicate<IClasspathEntry> TEST_SOURCES = cp -> cp.isTest() |
| 98 | + && cp.getPath().toString().contains("test/java"); |
| 99 | + private static final Predicate<IClasspathEntry> TEST_RESOURCES = cp -> cp.isTest() |
| 100 | + && cp.getPath().toString().contains("test/resources"); |
| 101 | + |
| 102 | + private long classpathEntriesCount(IJavaProject project, Predicate<IClasspathEntry> testSources) |
| 103 | + throws JavaModelException { |
| 104 | + return Arrays.stream(project.getRawClasspath()).filter(testSources).count(); |
| 105 | + } |
| 106 | + |
| 107 | + private IJavaProject importResourceProject(String name) throws IOException, InterruptedException, CoreException { |
| 108 | + File pomFile = new File(FileLocator.toFileURL(JavaConfigurationTest.class.getResource(name)).getFile()); |
| 109 | + waitForJobsToComplete(); |
| 110 | + IProject project = importProject(pomFile.getAbsolutePath()); |
| 111 | + waitForJobsToComplete(); |
| 112 | + return JavaCore.create(project); |
| 113 | + } |
111 | 114 | }
|
0 commit comments