Skip to content

Commit

Permalink
Log warnings instead of throwing exceptions for missing params while …
Browse files Browse the repository at this point in the history
…filling out the VersionInfo defaults (#216)

* CopyrightGenerator adapted to another arg Organization.Name

* Setter for logger for VersionInfo

* Dummy values for missing default props. Warnings instead of Exceptions.

* Deterministic buildYear property inside Unit test

* Readme about new version
  • Loading branch information
koksyn authored Jan 17, 2023
1 parent 875209f commit c475294
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 90 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ The full list of all the parameters is available [here](src/main/resources/MOJO.

# Version Notes

## Version notes 2.3.1 - 2023-01-16
- logs only warnings instead of throwing exception, when cannot fulfill default values for `<VersionInfo>`
(no configuration data required by the formula), see Issue [#213](../../issues/213) and PR [#216](../../pull/216)

## Version notes 2.3.0 - 2023-01-06
- provides default values for plugin configuration, especially for `<VersionInfo>`, see Issue [#98](../../issues/98)
- adds a `disableVersionInfoDefaults` parameter to be able to disable provided defaults, see PR [#205](../../pull/205)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ private void doExecute() throws MojoExecutionException {
if(versionInfo == null) {
versionInfo = new VersionInfo();
}
versionInfo.setLog(getLog());
versionInfo.tryFillOutByDefaults(project, outfile);
} catch (RuntimeException exception) {
throw new MojoExecutionException("Cannot fill out VersionInfo by defaults", exception);
Expand Down
99 changes: 63 additions & 36 deletions src/main/java/com/akathist/maven/plugins/launch4j/VersionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.sf.launch4j.config.LanguageID;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Organization;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

Expand Down Expand Up @@ -115,13 +116,16 @@ public class VersionInfo {
@Parameter
String trademarks;

private Log log;

public VersionInfo() {
}

public VersionInfo(String fileVersion, String txtFileVersion, String fileDescription,
String copyright, String productVersion, String txtProductVersion,
String productName, String companyName, String internalName,
String originalFilename, String language, String trademarks) {
String originalFilename, String language, String trademarks,
Log log) {
this.fileVersion = fileVersion;
this.txtFileVersion = txtFileVersion;
this.fileDescription = fileDescription;
Expand All @@ -134,6 +138,11 @@ public VersionInfo(String fileVersion, String txtFileVersion, String fileDescrip
this.originalFilename = originalFilename;
this.language = language;
this.trademarks = trademarks;
this.log = log;
}

public void setLog(Log log) {
this.log = log;
}

net.sf.launch4j.config.VersionInfo toL4j() {
Expand Down Expand Up @@ -171,65 +180,83 @@ void tryFillOutByDefaults(MavenProject project, File outfile) {
throw new IllegalArgumentException("'outfile' is required, but it is null.");
}

String version = project.getVersion();
String version = getDefaultWhenSourceIsBlankAndLogWarn(project.getVersion(), "1.0.0", "project.version");
Organization organization = project.getOrganization();
String organizationName = "Default organization";
if(organization == null) {
logWarningAboutDummyValue("project.organization.name", organizationName);
} else {
organizationName = getDefaultWhenSourceIsBlankAndLogWarn(organization.getName(), organizationName, "project.organization.name");
}

tryFillOutByDefaultVersionInL4jFormat(version);
tryFillOutCopyrightByDefaults(project.getInceptionYear(), organization);
tryFillOutOrganizationRelatedDefaults(organization);
tryFillOutCopyrightByDefaults(
getDefaultWhenSourceIsBlankAndLogWarn(project.getInceptionYear(), "2020", "project.inceptionYear"),
organizationName
);
tryFillOutOrganizationRelatedDefaults(organizationName);
tryFillOutSimpleValuesByDefaults(
version,
project.getName(),
project.getArtifactId(),
project.getDescription()
getDefaultWhenSourceIsBlankAndLogWarn(project.getName(), "Java Project", "project.name"),
getDefaultWhenSourceIsBlankAndLogWarn(project.getArtifactId(), "java-project", "project.artifactId"),
getDefaultWhenSourceIsBlankAndLogWarn(project.getDescription(), "A Java project.", "project.description")
);

originalFilename = getDefaultWhenOriginalIsBlank(originalFilename, outfile.getName(), "originalFilename", "${project.version}");
String outfileName = getDefaultWhenSourceIsBlankAndLogWarn(outfile.getName(), "app.exe", "outfile");
originalFilename = getDefaultWhenSourceIsBlank(originalFilename, outfileName);
}

private void tryFillOutByDefaultVersionInL4jFormat(String version) {
final String defaultFileVersion = Launch4jFileVersionGenerator.generate(version);
fileVersion = getDefaultWhenOriginalIsBlank(fileVersion, defaultFileVersion, "fileVersion", "${project.version}");
productVersion = getDefaultWhenOriginalIsBlank(productVersion, defaultFileVersion, "productVersion", "${project.version}");
String defaultFileVersion = Launch4jFileVersionGenerator.generate(version);

fileVersion = getDefaultWhenSourceIsBlank(fileVersion, defaultFileVersion);
productVersion = getDefaultWhenSourceIsBlank(productVersion, defaultFileVersion);
}

private void tryFillOutCopyrightByDefaults(String inceptionYear, Organization organization) {
final String defaultCopyright = CopyrightGenerator.generate(inceptionYear, organization);
copyright = getDefaultWhenOriginalIsBlank(copyright, defaultCopyright, "copyright", "${project.inceptionYear},${project.organization.name}");
private void tryFillOutCopyrightByDefaults(String inceptionYear, String organizationName) {
final String defaultCopyright = CopyrightGenerator.generate(inceptionYear, organizationName);
copyright = getDefaultWhenSourceIsBlank(copyright, defaultCopyright);
}

private void tryFillOutOrganizationRelatedDefaults(Organization organization) {
if (organization != null) {
companyName = getDefaultWhenOriginalIsBlank(companyName, organization.getName(), "companyName", "${project.organization.name}");
trademarks = getDefaultWhenOriginalIsBlank(trademarks, organization.getName(), "trademarks", "${project.organization.name}");
}
private void tryFillOutOrganizationRelatedDefaults(String organizationName) {
companyName = getDefaultWhenSourceIsBlank(companyName, organizationName);
trademarks = getDefaultWhenSourceIsBlank(trademarks, organizationName);
}

private void tryFillOutSimpleValuesByDefaults(String version,
String name,
String artifactId,
String description) {
txtFileVersion = getDefaultWhenOriginalIsBlank(txtFileVersion, version, "txtFileVersion", "${project.version}");
txtProductVersion = getDefaultWhenOriginalIsBlank(txtProductVersion, version, "txtProductVersion", "${project.version}");
productName = getDefaultWhenOriginalIsBlank(productName, name, "productName", "${project.name}");
internalName = getDefaultWhenOriginalIsBlank(internalName, artifactId, "internalName", "${project.artifactId}");
fileDescription = getDefaultWhenOriginalIsBlank(fileDescription, description, "fileDescription", "${project.description}");
txtFileVersion = getDefaultWhenSourceIsBlank(txtFileVersion, version);
txtProductVersion = getDefaultWhenSourceIsBlank(txtProductVersion, version);
productName = getDefaultWhenSourceIsBlank(productName, name);
internalName = getDefaultWhenSourceIsBlank(internalName, artifactId);
fileDescription = getDefaultWhenSourceIsBlank(fileDescription, description);
}

private String getDefaultWhenOriginalIsBlank(final String originalValue,
final String defaultValue,
final String originalParameterName,
final String defaultValueFormulaParams) {
if (StringUtils.isBlank(originalValue)) {
if(StringUtils.isNotBlank(defaultValue)) {
return defaultValue;
}

throw new IllegalStateException("Please fill the missing configuration values. " +
"Error when trying to fulfill default value for VersionInfo parameter:'" + originalParameterName + "' with formula params:'" + defaultValueFormulaParams + "'.");
private String getDefaultWhenSourceIsBlank(final String source, final String defaultValue) {
if (StringUtils.isBlank(source)) {
return defaultValue;
}

return originalValue;
return source;
}

private String getDefaultWhenSourceIsBlankAndLogWarn(final String source,
final String defaultValue,
final String sourceParamName) {
if (StringUtils.isBlank(source)) {
logWarningAboutDummyValue(sourceParamName, defaultValue);

return defaultValue;
}

return source;
}

private void logWarningAboutDummyValue(final String sourceParamName, final String dummyValue) {
log.warn("Configuration param ${" + sourceParamName + "} is empty, so a dummy value \"" + dummyValue + "\" " +
"might be used instead to fulfill some of VersionInfo params by defaults.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.akathist.maven.plugins.launch4j.generators;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Organization;

import java.time.LocalDate;

Expand All @@ -12,13 +11,13 @@ private CopyrightGenerator() {
/**
* Parameters should be taken from MavenProject properties:
* @param projectInceptionYear as ${project.inceptionYear}
* @param projectOrganization as ${project.organization}
* @param projectOrganizationName as ${project.organization.name}
* @return a string representing copyrights
*/
public static String generate(String projectInceptionYear, Organization projectOrganization) {
public static String generate(String projectInceptionYear, String projectOrganizationName) {
String inceptionYear = generateInceptionYear(projectInceptionYear);
int buildYear = LocalDate.now().getYear();
String organizationName = generateOrganizationName(projectOrganization);
String organizationName = generateOrganizationName(projectOrganizationName);

return String.format("Copyright © %s%d%s. All rights reserved.", inceptionYear, buildYear, organizationName);
}
Expand All @@ -31,9 +30,9 @@ private static String generateInceptionYear(String projectInceptionYear) {
return "";
}

private static String generateOrganizationName(Organization projectOrganization) {
if(projectOrganization != null && StringUtils.isNotBlank(projectOrganization.getName())) {
return " " + projectOrganization.getName();
private static String generateOrganizationName(String projectOrganizationName) {
if(StringUtils.isNotBlank(projectOrganizationName)) {
return " " + projectOrganizationName;
}

return "";
Expand Down
101 changes: 64 additions & 37 deletions src/test/java/com/akathist/maven/plugins/launch4j/VersionInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

import net.sf.launch4j.config.LanguageID;
import org.apache.maven.model.Organization;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.File;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;
import static org.mockito.Mockito.doReturn;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class VersionInfoTest {
Expand All @@ -37,6 +43,8 @@ public class VersionInfoTest {
MavenProject project;
@Mock
File outfile;
@Mock
Log log;

// Subject
private VersionInfo versionInfo;
Expand All @@ -46,7 +54,8 @@ public void buildVersionInfoFromTestParams() {
versionInfo = new VersionInfo(fileVersion, txtFileVersion, fileDescription,
copyright, productVersion, txtProductVersion,
productName, companyName, internalName,
originalFilename, language, trademarks);
originalFilename, language, trademarks,
log);
}

@Test
Expand Down Expand Up @@ -172,20 +181,6 @@ public void shouldFillOut_Copyright_ByDefault_When_ItWas_Empty() {
assertTrue(versionInfo.copyright.contains(organizationName));
}

@Test
public void should_Not_FillOutByDefaults_From_MavenProject_OrganizationName_When_OrganizationWas_Empty() {
// given
versionInfo.companyName = null;
versionInfo.trademarks = null;

// when
versionInfo.tryFillOutByDefaults(project, outfile);

// then
assertNull(versionInfo.companyName);
assertNull(versionInfo.trademarks);
}

@Test
public void should_Not_FillOutByDefaults_From_OrganizationName_When_VersionInfoPropsWere_Filled() {
// given
Expand Down Expand Up @@ -221,26 +216,6 @@ public void shouldFillOutByDefaults_From_OrganizationName_When_OrganizationWas_F
assertEquals(organizationName, versionInfo.trademarks);
}

@Test(expected = IllegalStateException.class)
public void should_Not_FillOutByDefaults_SimpleValues_From_MavenProject_When_ProjectPropsWere_Empty() {
// given
doReturn(null).when(project).getVersion();
versionInfo.txtFileVersion = null;
versionInfo.txtProductVersion = null;

doReturn(null).when(project).getName();
versionInfo.productName = null;

doReturn(null).when(project).getArtifactId();
versionInfo.internalName = null;

doReturn(null).when(project).getDescription();
versionInfo.fileDescription = null;

// expect throws
versionInfo.tryFillOutByDefaults(project, outfile);
}

@Test
public void should_Not_FillOutByDefaults_SimpleValues_From_MavenProject_When_VersionInfoPropsWere_Filled() {
// given
Expand Down Expand Up @@ -331,6 +306,58 @@ public void shouldFillOut_ByDefault_LastSegmentOfOutfilePath_When_OriginalFilena
assertEquals(outfileName, versionInfo.originalFilename);
}

@Test
public void shouldLogWarningsAboutDummyValues() {
// given
ArgumentCaptor<String> logMessageCaptor = ArgumentCaptor.forClass(String.class);
List<String> missingParamNames = Arrays.asList(
"project.version",
"project.name",
"project.artifactId",
"project.description",
"project.inceptionYear",
"project.organization.name",
"outfile"
);

// when
versionInfo.tryFillOutByDefaults(project, outfile);

// then
verify(log, times(missingParamNames.size())).warn(logMessageCaptor.capture());
List<String> logMessages = logMessageCaptor.getAllValues();


missingParamNames.forEach(missingParamName -> {
assertTrue(logMessages.stream().anyMatch(message -> message.contains(missingParamName)));
});
}

@Test
public void shouldFillOut_ByDummyValues_When_OriginalValues_Empty_And_ProjectParams_Empty() {
// given
final String buildYear = String.valueOf(LocalDate.now().getYear());

VersionInfo emptyValuesVersionInfo = new VersionInfo();
emptyValuesVersionInfo.setLog(log);

// when
emptyValuesVersionInfo.tryFillOutByDefaults(project, outfile);

// then
assertEquals("1.0.0.0", emptyValuesVersionInfo.fileVersion);
assertEquals("1.0.0", emptyValuesVersionInfo.txtFileVersion);
assertEquals("A Java project.", emptyValuesVersionInfo.fileDescription);
assertEquals("Copyright © 2020-" + buildYear + " Default organization. All rights reserved.", emptyValuesVersionInfo.copyright);
assertEquals("1.0.0.0", emptyValuesVersionInfo.productVersion);
assertEquals("1.0.0", emptyValuesVersionInfo.txtProductVersion);
assertEquals("Java Project", emptyValuesVersionInfo.productName);
assertEquals("Default organization", emptyValuesVersionInfo.companyName);
assertEquals("java-project", emptyValuesVersionInfo.internalName);
assertEquals("Default organization", emptyValuesVersionInfo.trademarks);
assertEquals("app.exe", emptyValuesVersionInfo.originalFilename);
}

@Test
public void shouldGenerateString_WithTestParams() {
// when
Expand All @@ -355,4 +382,4 @@ public void shouldGenerateString_WithTestParams() {
private boolean containsParam(String result, String paramName, String paramValue) {
return result.contains(paramName + "='" + paramValue + "'");
}
}
}
Loading

0 comments on commit c475294

Please sign in to comment.