Skip to content

Commit

Permalink
devonfw#78: devonfw#106: added test for saving changes to ide.propert…
Browse files Browse the repository at this point in the history
…ies (devonfw#107)
  • Loading branch information
MattesMrzik authored and hohwille committed Oct 24, 2023
1 parent 4ee2297 commit 7fca954
Showing 1 changed file with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.devonfw.tools.ide.environment;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -37,4 +41,75 @@ public void testLoad() {
assertThat(variables.getVariables()).hasSize(4);
}

}
@Test
void testSave() throws Exception {

// arrange
List<String> linesToWrite = new ArrayList<>();
linesToWrite.add("# first comment");
linesToWrite.add("# second comment");
linesToWrite.add("var0=0");
linesToWrite.add("var1=1");
linesToWrite.add("var2=2");
linesToWrite.add("export var3=3");
linesToWrite.add("export var4=4");
linesToWrite.add("export var5=5");
linesToWrite.add("export var6=6");
linesToWrite.add("# third comment");
linesToWrite.add("var7=7");
linesToWrite.add("var8=8");
linesToWrite.add("# 4th comment");
linesToWrite.add("# 5th comment");
linesToWrite.add("var9=9");

Path propertiesFilePath = Path.of("target/tmp-EnvironmentVariablesPropertiesFileTest-ide.properties");
Files.write(propertiesFilePath, linesToWrite, StandardOpenOption.CREATE_NEW);
// check if this writing was correct
List<String> lines = Files.readAllLines(propertiesFilePath);
assertThat(lines).containsExactlyElementsOf(linesToWrite);

AbstractEnvironmentVariables parent = null;
EnvironmentVariablesType type = EnvironmentVariablesType.SETTINGS;

EnvironmentVariablesPropertiesFile variables = new EnvironmentVariablesPropertiesFile(parent, type,
propertiesFilePath, LOGGER);

// act
variables.set("var5", "5", true);
variables.set("var1", "1.0", false);
variables.set("var10", "10", false);
variables.set("var11", "11", true); // var11 must be set after var 10, the other lines can be shuffled
variables.set("var3", "3", false);
variables.set("var7", "7", true);
variables.set("var6", "6.0", true);
variables.set("var4", "4.0", false);
variables.set("var8", "8.0", true);

variables.save();

// assert
List<String> linesAfterSave = new ArrayList<>();
linesAfterSave.add("# first comment");
linesAfterSave.add("# second comment");
linesAfterSave.add("var0=0");
linesAfterSave.add("var1=1.0");
linesAfterSave.add("var2=2");
linesAfterSave.add("var3=3");
linesAfterSave.add("var4=4.0");
linesAfterSave.add("export var5=5");
linesAfterSave.add("export var6=6.0");
linesAfterSave.add("# third comment");
linesAfterSave.add("export var7=7");
linesAfterSave.add("export var8=8.0");
linesAfterSave.add("# 4th comment");
linesAfterSave.add("# 5th comment");
linesAfterSave.add("var9=9");
linesAfterSave.add("var10=10");
linesAfterSave.add("export var11=11");

lines = Files.readAllLines(propertiesFilePath);
assertThat(lines).containsExactlyElementsOf(linesAfterSave);
// clean up
Files.delete(propertiesFilePath);
}
}

0 comments on commit 7fca954

Please sign in to comment.