-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit test for no resources in the target folder #1478
Open
BoykoAlex
wants to merge
1
commit into
eclipse-m2e:master
Choose a base branch
from
BoykoAlex:no-resources
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+159
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
org.eclipse.m2e.core.tests/resources/projects/demo/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>3.0.8</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>com.example</groupId> | ||
<artifactId>demo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>demo</name> | ||
<description>Demo project for Spring Boot</description> | ||
<properties> | ||
<java.version>17</java.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...2e.core.tests/resources/projects/demo/src/main/java/com/example/demo/DemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class DemoApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(DemoApplication.class, args); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
org.eclipse.m2e.core.tests/resources/projects/demo/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
13 changes: 13 additions & 0 deletions
13
...re.tests/resources/projects/demo/src/test/java/com/example/demo/DemoApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.demo; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class DemoApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
org.eclipse.m2e.core.tests/src/org/eclipse/m2e/core/AppPropsNotCopiedIntoTargetTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Alex Boyko and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
package org.eclipse.m2e.core; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.runtime.IPath; | ||
import org.eclipse.jdt.core.IJavaProject; | ||
import org.eclipse.jdt.core.JavaCore; | ||
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class AppPropsNotCopiedIntoTargetTest extends AbstractMavenProjectTestCase { | ||
|
||
private File projectDirectory; | ||
|
||
@Override | ||
@Before | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
setAutoBuilding(true); | ||
projectDirectory = new File(Files.createTempDirectory("m2e-tests").toFile(), "demo"); | ||
projectDirectory.mkdirs(); | ||
copyDir(new File("resources/projects/demo"), projectDirectory); | ||
} | ||
|
||
@Override | ||
@After | ||
public void tearDown() throws Exception { | ||
FileUtils.deleteDirectory(this.projectDirectory.getParentFile()); | ||
super.tearDown(); | ||
} | ||
|
||
@Test | ||
public void test() throws Exception { | ||
IProject project = importProject(new File(projectDirectory, "pom.xml").toString()); | ||
waitForJobsToComplete(monitor); | ||
|
||
IJavaProject jp = JavaCore.create(project); | ||
|
||
assertTrue("Build Automatically is NOT on!", isAutoBuilding()); | ||
|
||
IPath rootPath = project.getWorkspace().getRoot().getLocation(); | ||
|
||
// Project imported for the first time and built - everything is properly in the target folder | ||
assertTrue("'application.properties' is NOT in the output folder", rootPath.append(jp.getOutputLocation()).append("/application.properties").toFile() | ||
.exists()); | ||
assertTrue("'DemoApplication.class' is NOT in the output folder", rootPath.append(jp.getOutputLocation()) | ||
.append("/com/example/demo/DemoApplication.class").makeAbsolute().toFile().exists()); | ||
|
||
IFile pomXml = project.getFile("pom.xml"); | ||
String content = Files.readString(Path.of(pomXml.getLocationURI())); | ||
pomXml.setContents(new ByteArrayInputStream("Nothing".getBytes()), true, false, null); | ||
|
||
waitForJobsToComplete(monitor); | ||
Thread.sleep(5000); | ||
// Invalid pom file. JavaBuilder built java sources but MavenBuilder wqs unable to do anything hence no resources in the target folder except for compiled classes | ||
assertFalse("'application.properties' hasn't been removed from the output folder", rootPath.append(jp.getOutputLocation()).append("/application.properties").toFile() | ||
.exists()); | ||
assertTrue("'DemoApplication.class' should be created in the output folder by JavaBuilder", rootPath.append(jp.getOutputLocation()) | ||
.append("/com/example/demo/DemoApplication.class").toFile().exists()); | ||
|
||
pomXml.setContents(new ByteArrayInputStream(content.getBytes()), true, false, null); | ||
waitForJobsToComplete(monitor); | ||
Thread.sleep(5000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here replace with a loop that checks regualry with an upper bound instead of a fixed delay. |
||
// Valid pom file. Compiled classes and resources should reappear in the target folder. However, resources are missing which makes the test fail | ||
assertTrue("'DemoApplication.class' hasn't been created in the output folder", rootPath.append(jp.getOutputLocation()) | ||
.append("/com/example/demo/DemoApplication.class").toFile().exists()); | ||
assertTrue("'application.properties' hasn't been copied in the output folder", rootPath.append(jp.getOutputLocation()).append("/application.properties").toFile() | ||
.exists()); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HannesWell can you please advice here? It seems
waitForJobsToComplete
is not enough here or @BoykoAlex maybe can explain why additional wait is required here...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically
waitForJobsToComplete
wasn't enough. RemovingThread.sleep(...)
calls makes it fail at line 76 AFAIR. There are ways to execute stuff async bypassing the Eclipse job queue, i.e.Display.asyncExec(...)
or less likelyCompletableFuture
... I admit I didn't experiment any further with "waiting".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BoykoAlex can you probably replace this then with a loop that check if the file is deleted and if not waits a little time up to 5 seconds?