From 17b2d4e9bf9c82d3bcee00ee439b1ae70535f698 Mon Sep 17 00:00:00 2001 From: aboyko Date: Tue, 18 Jul 2023 13:24:20 -0400 Subject: [PATCH] Unit test for no resources in the target folder --- .../resources/projects/demo/pom.xml | 41 +++++++++ .../com/example/demo/DemoApplication.java | 13 +++ .../src/main/resources/application.properties | 1 + .../example/demo/DemoApplicationTests.java | 13 +++ .../core/AppPropsNotCopiedIntoTargetTest.java | 91 +++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 org.eclipse.m2e.core.tests/resources/projects/demo/pom.xml create mode 100644 org.eclipse.m2e.core.tests/resources/projects/demo/src/main/java/com/example/demo/DemoApplication.java create mode 100644 org.eclipse.m2e.core.tests/resources/projects/demo/src/main/resources/application.properties create mode 100644 org.eclipse.m2e.core.tests/resources/projects/demo/src/test/java/com/example/demo/DemoApplicationTests.java create mode 100644 org.eclipse.m2e.core.tests/src/org/eclipse/m2e/core/AppPropsNotCopiedIntoTargetTest.java diff --git a/org.eclipse.m2e.core.tests/resources/projects/demo/pom.xml b/org.eclipse.m2e.core.tests/resources/projects/demo/pom.xml new file mode 100644 index 0000000000..4cdf713548 --- /dev/null +++ b/org.eclipse.m2e.core.tests/resources/projects/demo/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.0.8 + + + com.example + demo + 0.0.1-SNAPSHOT + demo + Demo project for Spring Boot + + 17 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/org.eclipse.m2e.core.tests/resources/projects/demo/src/main/java/com/example/demo/DemoApplication.java b/org.eclipse.m2e.core.tests/resources/projects/demo/src/main/java/com/example/demo/DemoApplication.java new file mode 100644 index 0000000000..64b538a17c --- /dev/null +++ b/org.eclipse.m2e.core.tests/resources/projects/demo/src/main/java/com/example/demo/DemoApplication.java @@ -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); + } + +} diff --git a/org.eclipse.m2e.core.tests/resources/projects/demo/src/main/resources/application.properties b/org.eclipse.m2e.core.tests/resources/projects/demo/src/main/resources/application.properties new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/org.eclipse.m2e.core.tests/resources/projects/demo/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/org.eclipse.m2e.core.tests/resources/projects/demo/src/test/java/com/example/demo/DemoApplicationTests.java b/org.eclipse.m2e.core.tests/resources/projects/demo/src/test/java/com/example/demo/DemoApplicationTests.java new file mode 100644 index 0000000000..2778a6a7ea --- /dev/null +++ b/org.eclipse.m2e.core.tests/resources/projects/demo/src/test/java/com/example/demo/DemoApplicationTests.java @@ -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() { + } + +} diff --git a/org.eclipse.m2e.core.tests/src/org/eclipse/m2e/core/AppPropsNotCopiedIntoTargetTest.java b/org.eclipse.m2e.core.tests/src/org/eclipse/m2e/core/AppPropsNotCopiedIntoTargetTest.java new file mode 100644 index 0000000000..b4a1afd532 --- /dev/null +++ b/org.eclipse.m2e.core.tests/src/org/eclipse/m2e/core/AppPropsNotCopiedIntoTargetTest.java @@ -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); + // 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()); + } + +} \ No newline at end of file