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