Skip to content

Commit

Permalink
fix(bug) build fails on windows with autocrlf=lf
Browse files Browse the repository at this point in the history
Changing end of file and properties String manipulation to use Properties class

close jhipster#10398
  • Loading branch information
Eulbobo committed Jul 31, 2024
1 parent aa2e911 commit ffacdf6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 65 deletions.
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,10 @@
import static java.util.stream.Collectors.joining;

import java.io.IOException;
import java.io.InputStream;
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,16 +15,36 @@

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;
this.properties = new Properties();
createPropertiesIfNotExists(file);
loadPropertiesFromFile(file);
}

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);
}
}

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 updating Spring Factories properties: " + e.getMessage(), e);
}
}

public void append(PropertyKey key, PropertyValue value) {
Expand All @@ -35,61 +57,26 @@ public void append(PropertyKey key, PropertyValue value) {
@ExcludeFromGeneratedCodeCoverage(reason = "Hard to cover IOException")
private void updateFactories(PropertyKey key, PropertyValue value) {
try {
String properties = buildFactories(key, value);
buildFactories(key, value);

Files.writeString(file, properties);
properties.store(Files.newOutputStream(file.toAbsolutePath()), null);
} catch (IOException e) {
throw GeneratorException.technicalError("Error updating 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);
}
return addNewFactory(key, value, currentProperties);
}

private String addNewFactory(PropertyKey key, PropertyValue value, String currentProperties) {
return currentProperties + propertyLine(key, value) + LINE_BREAK;
}

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();
}
private void buildFactories(PropertyKey key, PropertyValue value) throws IOException {
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 newProperties.toString();
}

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

return "";
}

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

0 comments on commit ffacdf6

Please sign in to comment.