Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
Expand Down Expand Up @@ -107,7 +108,8 @@ public void execute() throws MojoExecutionException {
}

StringWriter w = new StringWriter();
String encoding = output != null ? project.getModel().getModelEncoding() : System.getProperty("file.encoding");
String encoding = encodingOrDefault(
output != null ? project.getModel().getModelEncoding() : System.getProperty("file.encoding"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be split into multiple statements for clarity

XMLWriter writer = new PrettyPrintXMLWriter(
w, StringUtils.repeat(" ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE), encoding, null);

Expand Down Expand Up @@ -213,6 +215,13 @@ private static void cleanModel(Model pom) {
pom.setProperties(properties);
}

static String encodingOrDefault(String encoding) {
if (encoding == null || encoding.isEmpty()) {
return Charset.defaultCharset().name();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use the default encoding instead of UTF-8?

}
return encoding;
}

private static class InputLocationStringFormatter extends InputLocation.StringFormatter {
@Override
public String toString(InputLocation location) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.plugins.help;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class EffectivePomMojoTest {

@Test
void encodingFallbackNeverNull() {
String fromNull = EffectivePomMojo.encodingOrDefault(null);
assertNotNull(fromNull);
assertFalse(fromNull.isEmpty());

String fromBlank = EffectivePomMojo.encodingOrDefault("");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is two different tests

assertNotNull(fromBlank);
assertFalse(fromBlank.isEmpty());

assertEquals("UTF-8", EffectivePomMojo.encodingOrDefault("UTF-8"));
}
}
Loading