Skip to content

Commit ab134c1

Browse files
BoykoAlexHannesWell
authored andcommitted
Unit test for no resources in the target folder
1 parent 8789e4c commit ab134c1

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.0.8</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.example</groupId>
12+
<artifactId>demo</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>demo</name>
15+
<description>Demo project for Spring Boot</description>
16+
<properties>
17+
<java.version>17</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter</artifactId>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-test</artifactId>
28+
<scope>test</scope>
29+
</dependency>
30+
</dependencies>
31+
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-maven-plugin</artifactId>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
41+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DemoApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(DemoApplication.class, args);
11+
}
12+
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.demo;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class DemoApplicationTests {
8+
9+
@Test
10+
void contextLoads() {
11+
}
12+
13+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 Alex Boyko and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*******************************************************************************/
10+
package org.eclipse.m2e.core;
11+
12+
import static org.junit.Assert.assertFalse;
13+
import static org.junit.Assert.assertTrue;
14+
15+
import java.io.ByteArrayInputStream;
16+
import java.io.File;
17+
import java.nio.file.Files;
18+
import java.nio.file.Path;
19+
20+
import org.apache.commons.io.FileUtils;
21+
import org.eclipse.core.resources.IFile;
22+
import org.eclipse.core.resources.IProject;
23+
import org.eclipse.core.runtime.IPath;
24+
import org.eclipse.jdt.core.IJavaProject;
25+
import org.eclipse.jdt.core.JavaCore;
26+
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
31+
public class AppPropsNotCopiedIntoTargetTest extends AbstractMavenProjectTestCase {
32+
33+
private File projectDirectory;
34+
35+
@Override
36+
@Before
37+
public void setUp() throws Exception {
38+
super.setUp();
39+
setAutoBuilding(true);
40+
projectDirectory = new File(Files.createTempDirectory("m2e-tests").toFile(), "demo");
41+
projectDirectory.mkdirs();
42+
copyDir(new File("resources/projects/demo"), projectDirectory);
43+
}
44+
45+
@Override
46+
@After
47+
public void tearDown() throws Exception {
48+
FileUtils.deleteDirectory(this.projectDirectory.getParentFile());
49+
super.tearDown();
50+
}
51+
52+
@Test
53+
public void test() throws Exception {
54+
IProject project = importProject(new File(projectDirectory, "pom.xml").toString());
55+
waitForJobsToComplete(monitor);
56+
57+
IJavaProject jp = JavaCore.create(project);
58+
59+
assertTrue("Build Automatically is NOT on!", isAutoBuilding());
60+
61+
IPath rootPath = project.getWorkspace().getRoot().getLocation();
62+
63+
// Project imported for the first time and built - everything is properly in the target folder
64+
assertTrue("'application.properties' is NOT in the output folder", rootPath.append(jp.getOutputLocation()).append("/application.properties").toFile()
65+
.exists());
66+
assertTrue("'DemoApplication.class' is NOT in the output folder", rootPath.append(jp.getOutputLocation())
67+
.append("/com/example/demo/DemoApplication.class").makeAbsolute().toFile().exists());
68+
69+
IFile pomXml = project.getFile("pom.xml");
70+
String content = Files.readString(Path.of(pomXml.getLocationURI()));
71+
pomXml.setContents(new ByteArrayInputStream("Nothing".getBytes()), true, false, null);
72+
73+
waitForJobsToComplete(monitor);
74+
Thread.sleep(5000);
75+
// 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
76+
assertFalse("'application.properties' hasn't been removed from the output folder", rootPath.append(jp.getOutputLocation()).append("/application.properties").toFile()
77+
.exists());
78+
assertTrue("'DemoApplication.class' should be created in the output folder by JavaBuilder", rootPath.append(jp.getOutputLocation())
79+
.append("/com/example/demo/DemoApplication.class").toFile().exists());
80+
81+
pomXml.setContents(new ByteArrayInputStream(content.getBytes()), true, false, null);
82+
waitForJobsToComplete(monitor);
83+
Thread.sleep(5000);
84+
// Valid pom file. Compiled classes and resources should reappear in the target folder. However, resources are missing which makes the test fail
85+
assertTrue("'DemoApplication.class' hasn't been created in the output folder", rootPath.append(jp.getOutputLocation())
86+
.append("/com/example/demo/DemoApplication.class").toFile().exists());
87+
assertTrue("'application.properties' hasn't been copied in the output folder", rootPath.append(jp.getOutputLocation()).append("/application.properties").toFile()
88+
.exists());
89+
}
90+
91+
}

0 commit comments

Comments
 (0)