Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bug) build fails on windows with autocrlf=lf #10463

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
@@ -1,7 +1,7 @@
package tech.jhipster.lite.module.domain.javaproperties;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import tech.jhipster.lite.shared.collection.domain.JHipsterCollections;
Expand Down Expand Up @@ -39,7 +39,7 @@ public Collection<Object> get() {
}

public static PropertyValue merge(PropertyValue v1, PropertyValue v2) {
Collection<Object> mergedValues = new ArrayList<>();
Collection<Object> mergedValues = new LinkedHashSet<>();
mergedValues.addAll(v1.get());
mergedValues.addAll(v2.get());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import static java.util.stream.Collectors.joining;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import tech.jhipster.lite.module.domain.javaproperties.PropertyKey;
import tech.jhipster.lite.module.domain.javaproperties.PropertyValue;
import tech.jhipster.lite.shared.error.domain.Assert;
Expand All @@ -13,83 +16,68 @@

public class PropertiesFileSpringFactoriesHandler {

private static final String EQUAL = "=";
private static final String COLLECTION_SEPARATOR = ",";
private static final String LINE_BREAK = System.lineSeparator();

private final Path file;
private final Properties properties;

public PropertiesFileSpringFactoriesHandler(Path file) {
Assert.notNull("file", file);

this.file = file;
}

public void append(PropertyKey key, PropertyValue value) {
Assert.notNull("key", key);
Assert.notNull("value", value);

updateFactories(key, value);
this.properties = new Properties();
createPropertiesIfNotExists(file);
loadPropertiesFromFile(file);
}

@ExcludeFromGeneratedCodeCoverage(reason = "Hard to cover IOException")
private void updateFactories(PropertyKey key, PropertyValue value) {
try {
String properties = buildFactories(key, value);

Files.writeString(file, properties);
private void loadPropertiesFromFile(Path file) {
try (InputStream inputStream = Files.newInputStream(file)) {
properties.load(inputStream);
} catch (IOException e) {
throw GeneratorException.technicalError("Error updating Spring Factories properties: " + e.getMessage(), e);
throw GeneratorException.technicalError("Error loading Spring Factories properties: " + e.getMessage(), e);
}
}

private String buildFactories(PropertyKey key, PropertyValue value) throws IOException {
String currentProperties = readOrInitFactories();

int propertyIndex = currentProperties.indexOf(propertyId(key));
if (propertyIndex != -1) {
return appendValuesToExistingPropertyKey(propertyIndex, value, currentProperties);
@ExcludeFromGeneratedCodeCoverage(reason = "Hard to cover IOException")
private static void createPropertiesIfNotExists(Path file) {
try {
if (Files.notExists(file)) {
Files.createDirectories(file.getParent());
Files.createFile(file);
}
} catch (IOException e) {
throw GeneratorException.technicalError("Error creating Spring Factories properties file: " + e.getMessage(), e);
}
return addNewFactory(key, value, currentProperties);
}

private String addNewFactory(PropertyKey key, PropertyValue value, String currentProperties) {
return currentProperties + propertyLine(key, value) + LINE_BREAK;
public void append(PropertyKey key, PropertyValue value) {
Assert.notNull("key", key);
Assert.notNull("value", value);
addToProperties(key, value);
saveToFile();
}

private static String appendValuesToExistingPropertyKey(int propertyIndex, PropertyValue value, String currentProperties) {
StringBuilder newProperties = new StringBuilder(currentProperties);
int eolIndex = newProperties.indexOf(LINE_BREAK, propertyIndex);

for (Object propertyValue : value.get()) {
if (!newProperties.substring(propertyIndex, eolIndex).contains(propertyValue.toString())) {
newProperties.insert(eolIndex, COLLECTION_SEPARATOR + propertyValue);
eolIndex = eolIndex + COLLECTION_SEPARATOR.length() + propertyValue.toString().length();
}
@ExcludeFromGeneratedCodeCoverage(reason = "Hard to cover IOException")
private void saveToFile() {
try (OutputStream outputStream = Files.newOutputStream(file)) {
properties.store(outputStream, null);
} catch (IOException e) {
throw GeneratorException.technicalError("Error writing Spring Factories properties: " + e.getMessage(), e);
}
return newProperties.toString();
}

private String readOrInitFactories() throws IOException {
if (Files.notExists(file)) {
Files.createDirectories(file.getParent());
Files.createFile(file);

return "";
private void addToProperties(PropertyKey key, PropertyValue value) {
if (properties.containsKey(key.get())) {
properties.setProperty(
key.get(),
joinedPropertyValues(PropertyValue.merge(PropertyValue.of(new String[] { properties.getProperty(key.get()) }), value))
);
} else {
properties.setProperty(key.get(), joinedPropertyValues(value));
}

return Files.readString(file);
}

private String propertyLine(PropertyKey key, PropertyValue value) {
return propertyId(key) + joinedPropertyValues(value);
}

private static String joinedPropertyValues(PropertyValue value) {
return value.get().stream().map(Object::toString).collect(joining(COLLECTION_SEPARATOR));
}

private String propertyId(PropertyKey key) {
return key.get() + EQUAL;
}
}
11 changes: 10 additions & 1 deletion src/test/java/tech/jhipster/lite/TestFileUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package tech.jhipster.lite;

import static java.nio.file.StandardCopyOption.*;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;
import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder;
Expand Down Expand Up @@ -42,6 +43,14 @@ public static String contentNormalizingNewLines(Path path) {
return content(path).replace("\r\n", "\n");
}

public static List<String> fileLines(Path path) {
try {
return Files.readAllLines(path);
} catch (IOException e) {
throw new AssertionError(e);
}
}

public static void loadDefaultProperties(Path from, Path to) {
try {
Files.createDirectories(to.getParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void shouldBuildModule() {
.hasPrefixedFiles("src/main/docker/cassandra/scripts", "autoMigrate.sh", "execute-cql.sh")
.hasFiles("src/test/java/tech/jhipster/jhlitest/TestCassandraMigrationLoader.java")
.hasFile("src/test/resources/META-INF/spring.factories")
.containing(
.containingInSequence(
"org.springframework.context.ApplicationListener=tech.jhipster.jhlitest.TestCassandraManager,tech.jhipster.jhlitest.TestCassandraMigrationLoader"
)
.and()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void shouldCreateUnknownFile() {

handler.append(propertyKey("o.s.c.ApplicationListener"), propertyValue("c.m.m.MyListener1", "c.m.m.MyListener2"));

assertThat(contentNormalizingNewLines(factoriesFile)).isEqualTo(
assertThat(contentNormalizingNewLines(factoriesFile)).contains(
"""
o.s.c.ApplicationListener=c.m.m.MyListener1,c.m.m.MyListener2
"""
Expand All @@ -39,11 +39,9 @@ void shouldAppendPropertyToFileWithProperties() {

handler.append(propertyKey("o.s.c.ApplicationListener"), propertyValue("c.m.m.MyListener1", "c.m.m.MyListener2"));

assertThat(contentNormalizingNewLines(factoriesFile)).isEqualTo(
"""
org.springframework.test.context.ContextCustomizerFactory=c.m.m.MyContextCustomizerFactory
o.s.c.ApplicationListener=c.m.m.MyListener1,c.m.m.MyListener2
"""
assertThat(fileLines(factoriesFile)).contains(
"org.springframework.test.context.ContextCustomizerFactory=c.m.m.MyContextCustomizerFactory",
"o.s.c.ApplicationListener=c.m.m.MyListener1,c.m.m.MyListener2"
);
}

Expand All @@ -58,7 +56,7 @@ void shouldAppendToExistingProperty() {
propertyValue("c.m.m.MyFactory", "c.m.m.MyFactory2")
);

assertThat(contentNormalizingNewLines(factoriesFile)).isEqualTo(
assertThat(contentNormalizingNewLines(factoriesFile)).contains(
"""
org.springframework.test.context.ContextCustomizerFactory=c.m.m.MyContextCustomizerFactory,c.m.m.MyFactory,c.m.m.MyFactory2
"""
Expand All @@ -77,11 +75,9 @@ void shouldAppendNewAndExistingProperties() {
);
handler.append(propertyKey("o.s.c.ApplicationListener"), propertyValue("c.m.m.MyListener1"));

assertThat(contentNormalizingNewLines(factoriesFile)).isEqualTo(
"""
org.springframework.test.context.ContextCustomizerFactory=c.m.m.MyContextCustomizerFactory,c.m.m.MyNewContextCustomizerFactory
o.s.c.ApplicationListener=c.m.m.MyListener1
"""
assertThat(fileLines(factoriesFile)).contains(
"org.springframework.test.context.ContextCustomizerFactory=c.m.m.MyContextCustomizerFactory,c.m.m.MyNewContextCustomizerFactory",
"o.s.c.ApplicationListener=c.m.m.MyListener1"
);
}

Expand All @@ -100,7 +96,7 @@ void shouldNotAppendExistingValue() {
propertyValue("c.m.m.MyContextCustomizerFactory")
);

assertThat(contentNormalizingNewLines(factoriesFile)).isEqualTo(
assertThat(contentNormalizingNewLines(factoriesFile)).contains(
"""
org.springframework.test.context.ContextCustomizerFactory=c.m.m.MyContextCustomizerFactory
"""
Expand Down