Skip to content

Commit 885d674

Browse files
committed
Clean-up JavaConfigurationTest
1 parent ac2c93c commit 885d674

File tree

5 files changed

+99
-104
lines changed

5 files changed

+99
-104
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/projects/**/.classpath
2+
/projects/**/.project
3+
/projects/**/.settings/

org.eclipse.m2e.jdt.tests/projects/skipAllTest/pom.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5-
https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
65
<modelVersion>4.0.0</modelVersion>
76
<groupId>foo.bar</groupId>
8-
<artifactId>demo</artifactId>
7+
<artifactId>skip-all</artifactId>
98
<version>0.0.1-SNAPSHOT</version>
109
<properties>
1110
<maven.test.skip>true</maven.test.skip>
1211
</properties>
1312
<build>
1413
<pluginManagement>
15-
<plugins>
14+
<plugins>
1615
<plugin>
1716
<groupId>org.apache.maven.plugins</groupId>
1817
<artifactId>maven-compiler-plugin</artifactId>

org.eclipse.m2e.jdt.tests/projects/skipTestCompilation/pom.xml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5-
https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
65
<modelVersion>4.0.0</modelVersion>
76
<groupId>foo.bar</groupId>
8-
<artifactId>demo</artifactId>
7+
<artifactId>skip-compilation</artifactId>
98
<version>0.0.1-SNAPSHOT</version>
109
<build>
11-
<pluginManagement>
12-
<plugins>
13-
<plugin>
14-
<groupId>org.apache.maven.plugins</groupId>
15-
<artifactId>maven-compiler-plugin</artifactId>
16-
<version>3.10.1</version>
17-
</plugin>
18-
</plugins>
19-
</pluginManagement>
2010
<plugins>
2111
<plugin>
2212
<groupId>org.apache.maven.plugins</groupId>
2313
<artifactId>maven-compiler-plugin</artifactId>
14+
<version>3.10.1</version>
2415
<executions>
2516
<execution>
2617
<id>default-testCompile</id>

org.eclipse.m2e.jdt.tests/projects/skipTestResources/pom.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5-
https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
65
<modelVersion>4.0.0</modelVersion>
76
<groupId>foo.bar</groupId>
8-
<artifactId>demo</artifactId>
7+
<artifactId>skip-resources</artifactId>
98
<version>0.0.1-SNAPSHOT</version>
109
<build>
1110
<pluginManagement>
12-
<plugins>
11+
<plugins>
1312
<plugin>
1413
<groupId>org.apache.maven.plugins</groupId>
1514
<artifactId>maven-compiler-plugin</artifactId>
Lines changed: 87 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2020 Red Hat, Inc.
2+
* Copyright (c) 2020, 2023 Red Hat, Inc. and others
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -10,102 +10,105 @@
1010

1111
package org.eclipse.m2e.jdt.tests;
1212

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;
1416

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;
1622
import java.util.Arrays;
23+
import java.util.function.Predicate;
1724

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;
2136
import org.eclipse.m2e.core.MavenPlugin;
2237
import org.eclipse.m2e.core.internal.preferences.MavenConfigurationImpl;
2338
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
39+
import org.junit.Before;
2440
import org.junit.Test;
2541

2642
@SuppressWarnings("restriction")
2743
public class JavaConfigurationTest extends AbstractMavenProjectTestCase {
2844

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+
}
4952

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+
}
6464

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+
}
7774

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+
}
9480

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+
}
111114
}

0 commit comments

Comments
 (0)