Skip to content

Commit

Permalink
added new test case
Browse files Browse the repository at this point in the history
  • Loading branch information
salimbouch committed Jun 19, 2024
1 parent e2f5c71 commit a2190a2
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 117 deletions.
142 changes: 25 additions & 117 deletions cli/src/test/java/com/devonfw/tools/ide/merge/XmlMergerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.merge.xmlmerger.XmlMerger;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

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

class XmlMergerTest extends AbstractIdeContextTest {

Expand All @@ -25,122 +29,26 @@ class XmlMergerTest extends AbstractIdeContextTest {
private XmlMerger merger = new XmlMerger(context);

@Test
void testMergeStrategyCombine(@TempDir Path tempDir) throws Exception {

// arrange
Path folderPath = TEST_RESOURCES.resolve("combine");
Path sourcePath = folderPath.resolve(SOURCE_XML);
Path targetPath = tempDir.resolve(TARGET_XML);
Path resultPath = folderPath.resolve(RESULT_XML);
Files.copy(folderPath.resolve(TARGET_XML), targetPath);

// act
merger.merge(null, sourcePath, context.getVariables(), targetPath);

// assert
assertThat(targetPath).hasContent(Files.readString(resultPath));
}

@Test
void testMergeStrategyOverride(@TempDir Path tempDir) throws Exception {

// arrange
Path folderPath = TEST_RESOURCES.resolve("override");
Path sourcePath = folderPath.resolve(SOURCE_XML);
Path targetPath = tempDir.resolve(TARGET_XML);
Path resultPath = folderPath.resolve(RESULT_XML);
Files.copy(folderPath.resolve(TARGET_XML), targetPath);

// act
merger.merge(null, sourcePath, context.getVariables(), targetPath);

// assert
assertThat(targetPath).hasContent(Files.readString(resultPath));
}

@Test
void testMergeStrategyKeep(@TempDir Path tempDir) throws Exception {

// arrange
Path folderPath = TEST_RESOURCES.resolve("keep");
Path sourcePath = folderPath.resolve(SOURCE_XML);
Path targetPath = tempDir.resolve(TARGET_XML);
Path resultPath = folderPath.resolve(RESULT_XML);
Files.copy(folderPath.resolve(TARGET_XML), targetPath);

// act
merger.merge(null, sourcePath, context.getVariables(), targetPath);

// assert
assertThat(targetPath).hasContent(Files.readString(resultPath));
}

@Test
void testMergeStrategyAppend(@TempDir Path tempDir) throws Exception {


// arrange
Path folderPath = TEST_RESOURCES.resolve("append");
Path sourcePath = folderPath.resolve(SOURCE_XML);
Path targetPath = tempDir.resolve(TARGET_XML);
Path resultPath = folderPath.resolve(RESULT_XML);
Files.copy(folderPath.resolve(TARGET_XML), targetPath);

// act
merger.merge(null, sourcePath, context.getVariables(), targetPath);

// assert
assertThat(targetPath).hasContent(Files.readString(resultPath));
}

@Test
void testMergeStrategyId(@TempDir Path tempDir) throws Exception {

// arrange
Path folderPath = TEST_RESOURCES.resolve("id");
Path sourcePath = folderPath.resolve(SOURCE_XML);
Path targetPath = tempDir.resolve(TARGET_XML);
Path resultPath = folderPath.resolve(RESULT_XML);
Files.copy(folderPath.resolve(TARGET_XML), targetPath);

// act
merger.merge(null, sourcePath, context.getVariables(), targetPath);

// assert
assertThat(targetPath).hasContent(Files.readString(resultPath));
}

@Test
void testMergeStrategyCombineNested(@TempDir Path tempDir) throws Exception {

// arrange
Path folderPath = TEST_RESOURCES.resolve("combineNested");
Path sourcePath = folderPath.resolve(SOURCE_XML);
Path targetPath = tempDir.resolve(TARGET_XML);
Path resultPath = folderPath.resolve(RESULT_XML);
Files.copy(folderPath.resolve(TARGET_XML), targetPath);

// act
merger.merge(null, sourcePath, context.getVariables(), targetPath);

// assert
assertThat(targetPath).hasContent(Files.readString(resultPath));
}

@Test
void testMergeStrategyOverrideNested(@TempDir Path tempDir) throws Exception {

// arrange
Path folderPath = TEST_RESOURCES.resolve("overrideNested");
Path sourcePath = folderPath.resolve(SOURCE_XML);
Path targetPath = tempDir.resolve(TARGET_XML);
Path resultPath = folderPath.resolve(RESULT_XML);
Files.copy(folderPath.resolve(TARGET_XML), targetPath);

// act
merger.merge(null, sourcePath, context.getVariables(), targetPath);

// assert
assertThat(targetPath).hasContent(Files.readString(resultPath));
void testAllCases(@TempDir Path tempDir) throws Exception {

try(Stream<Path> folders = Files.list(TEST_RESOURCES)) {
// arrange
SoftAssertions softly = new SoftAssertions();
folders.forEach(folder -> {
Path sourcePath = folder.resolve(SOURCE_XML);
Path targetPath = tempDir.resolve(TARGET_XML);
Path resultPath = folder.resolve(RESULT_XML);
try {
Files.copy(folder.resolve(TARGET_XML), targetPath, REPLACE_EXISTING);
// act
merger.merge(null, sourcePath, context.getVariables(), targetPath);
// assert
softly.assertThat(targetPath).hasContent(Files.readString(resultPath));
} catch (IOException e) {
throw new IllegalStateException(e);
}
});
softly.assertAll();
}
}
}
40 changes: 40 additions & 0 deletions cli/src/test/resources/xmlmerger/intellijWorkspace/result.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE"/>
</component>
<component name="FormatOnSaveOptions">
<option name="myRunOnSave" value="true"/>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$"/>
</component>
<component name="MavenImportPreferences">
<option name="generalSettings">
<MavenGeneralSettings>
<option name="mavenHome" value="newMavenHomePath"/>
<option name="threads" value="7"/>
<option name="useMavenConfig" value="true"/>
<option name="userSettingsFile" value="newUserSettingsFilePath"/>
</MavenGeneralSettings>
</option>
</component>
<component name="MavenRunner">
<option name="skipTests" value="false"/>
<option name="vmOptions" value="newValue"/>
</component>
<component name="OptimizeOnSaveOptions">
<option name="myRunOnSave" value="true"/>
</component>
<component name="RunManager">
<configuration default="true" factoryName="JUnit" type="JUnit">
<shortenClasspath name="ARGS_FILE"/>
<option name="METHOD_NAME" value=""/>
<option name="TEST_OBJECT" value="class"/>
<method v="2">
<option enabled="true" name="Make"/>
</method>
</configuration>
</component>
<component CustomDictionaries="0" DefaultDictionary="application-level" Folders="0" RuntimeDictionaries="0" UseSingleDictionary="true" name="SpellCheckerSettings" transferred="true"/>
</project>
16 changes: 16 additions & 0 deletions cli/src/test/resources/xmlmerger/intellijWorkspace/source.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns:merge="https://github.com/devonfw/IDEasy/merge" merge:strategy="combine" version="4">
<component name="MavenImportPreferences" merge:id="@name">
<option name="generalSettings" merge:id="@name">
<MavenGeneralSettings merge:id="name()">
<option name="mavenHome" value="newMavenHomePath"/>
<option name="useMavenConfig" value="true"/>
<option name="userSettingsFile" value="newUserSettingsFilePath"/>
</MavenGeneralSettings>
</option>
</component>
<component name="MavenRunner">
<option name="skipTests" value="false"/>
<option name="vmOptions" value="newValue"/>
</component>
</project>
40 changes: 40 additions & 0 deletions cli/src/test/resources/xmlmerger/intellijWorkspace/target.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE"/>
</component>
<component name="FormatOnSaveOptions">
<option name="myRunOnSave" value="true"/>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$"/>
</component>
<component name="MavenImportPreferences">
<option name="generalSettings">
<MavenGeneralSettings>
<option name="mavenHome" value="oldPath"/>
<option name="threads" value="7"/>
<option name="useMavenConfig" value="true"/>
<option name="userSettingsFile" value="oldPath"/>
</MavenGeneralSettings>
</option>
</component>
<component name="MavenRunner">
<option name="skipTests" value="true"/>
<option name="vmOptions" value="-Xmx8G"/>
</component>
<component name="OptimizeOnSaveOptions">
<option name="myRunOnSave" value="true"/>
</component>
<component name="RunManager">
<configuration default="true" factoryName="JUnit" type="JUnit">
<shortenClasspath name="ARGS_FILE"/>
<option name="METHOD_NAME" value=""/>
<option name="TEST_OBJECT" value="class"/>
<method v="2">
<option enabled="true" name="Make"/>
</method>
</configuration>
</component>
<component CustomDictionaries="0" DefaultDictionary="application-level" Folders="0" RuntimeDictionaries="0" UseSingleDictionary="true" name="SpellCheckerSettings" transferred="true"/>
</project>

0 comments on commit a2190a2

Please sign in to comment.