From e0e6f20484856279dbbc5ba5af839113388a43ea Mon Sep 17 00:00:00 2001 From: Abel Salgado Romero Date: Sat, 13 Jan 2024 13:35:32 +0100 Subject: [PATCH] [MSHARED-1351] - Fix console message when origin is baseDir Print alternative message replacing the relative origin path by the literal 'base directory'. --- .../DefaultMavenResourcesFiltering.java | 9 +- .../maven/shared/filtering/ConsoleHolder.java | 88 +++++++++++++++++++ .../DefaultMavenResourcesFilteringTest.java | 51 +++++++++++ 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/apache/maven/shared/filtering/ConsoleHolder.java diff --git a/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java index 3713d7c..0c8688f 100644 --- a/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java +++ b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java @@ -36,6 +36,7 @@ import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.maven.model.Resource; import org.codehaus.plexus.util.DirectoryScanner; import org.codehaus.plexus.util.Scanner; @@ -254,9 +255,15 @@ private File getTargetFile(File file) throws MavenFilteringException { Path destination = getDestinationFile(outputDirectory, targetPath, "", mavenResourcesExecution) .getAbsoluteFile() .toPath(); + String origin = basedir.relativize( + resourceDirectory.getAbsoluteFile().toPath()) + .toString(); + if (StringUtils.isEmpty(origin)) { + origin = "."; + } LOGGER.info("Copying " + includedFiles.size() + " resource" + (includedFiles.size() > 1 ? "s" : "") + " from " - + basedir.relativize(resourceDirectory.getAbsoluteFile().toPath()) + + origin + " to " + basedir.relativize(destination)); } catch (Exception e) { diff --git a/src/test/java/org/apache/maven/shared/filtering/ConsoleHolder.java b/src/test/java/org/apache/maven/shared/filtering/ConsoleHolder.java new file mode 100644 index 0000000..bf9a2e9 --- /dev/null +++ b/src/test/java/org/apache/maven/shared/filtering/ConsoleHolder.java @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.shared.filtering; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; + +/** + * Helping class to capture console input and output for tests. + * + * @author abelsromero + * @since 3.3.2 + */ +class ConsoleHolder { + + private PrintStream originalOut; + private PrintStream originalErr; + + private ByteArrayOutputStream newOut; + private ByteArrayOutputStream newErr; + + private ConsoleHolder() {} + + static ConsoleHolder start() { + final ConsoleHolder holder = new ConsoleHolder(); + + holder.originalOut = System.out; + holder.originalErr = System.err; + + holder.newOut = new DoubleOutputStream(holder.originalOut); + holder.newErr = new DoubleOutputStream(holder.originalErr); + + System.setOut(new PrintStream(holder.newOut)); + System.setErr(new PrintStream(holder.newErr)); + + return holder; + } + + void release() { + System.setOut(originalOut); + System.setOut(originalErr); + } + + String getOutput() { + return new String(newOut.toByteArray()); + } + + String getError() { + return new String(newErr.toByteArray()); + } + + static class DoubleOutputStream extends ByteArrayOutputStream { + + final OutputStream other; + + DoubleOutputStream(final OutputStream os) { + other = os; + } + + @Override + public synchronized void write(final byte[] b, final int off, final int len) { + try { + other.write(b, off, len); + } catch (IOException e) { + throw new RuntimeException(e); + } + super.write(b, off, len); + } + } +} diff --git a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java index f2d1a48..228fb66 100644 --- a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java +++ b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java @@ -366,6 +366,57 @@ void noFiltering() throws Exception { assertTrue(filesAreIdentical(initialImageFile, imageFile)); } + @Test + void messageWhenCopyingFromSubDirectory() throws Exception { + + String subDirectory = "src/test/units-files/maven-resources-filtering"; + String unitFilesDir = String.format("%s/%s", getBasedir(), subDirectory); + + assertMessage( + unitFilesDir, + "Copying (\\d)+ resources from " + subDirectory + " to target/DefaultMavenResourcesFilteringTest"); + } + + @Test + void messageWhenCopyingFromBaseDir() throws Exception { + + String unitFilesDir = getBasedir(); + + assertMessage(unitFilesDir, "Copying (\\d)+ resources from . to target/DefaultMavenResourcesFilteringTest"); + } + + private void assertMessage(String directory, String expectedMessagePattern) throws Exception { + Resource resource = new Resource(); + List resources = new ArrayList<>(); + resources.add(resource); + + resource.setDirectory(directory); + resource.setFiltering(false); + + MavenResourcesExecution mre = new MavenResourcesExecution(); + mre.setResources(resources); + mre.setOutputDirectory(outputDirectory); + mre.setEncoding("UTF-8"); + mre.setMavenProject(mavenProject); + mre.setFilters(null); + mre.setNonFilteredFileExtensions(Collections.emptyList()); + mre.setMavenSession(new StubMavenSession()); + + ConsoleHolder console = ConsoleHolder.start(); + + mavenResourcesFiltering.filterResources(mre); + + String output = console.getError(); + String marker = DefaultMavenResourcesFiltering.class.getSimpleName(); + String message = output.substring(output.indexOf(marker) + marker.length() + 3) + .trim() + .replaceAll("\\\\", "/"); + + boolean matches = message.matches(expectedMessagePattern); + assertTrue(matches, "expected: '" + expectedMessagePattern + "' does not match actual: '" + message + "'"); + console.release(); + } + private static boolean filesAreIdentical(File expected, File current) throws IOException { if (expected.length() != current.length()) { return false;