-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
devonfw/ide#1090: added AbstractIdeContextTest with better test infra…
…structure
- Loading branch information
Showing
7 changed files
with
207 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
cli/src/main/java/com/devonfw/tools/ide/io/FileCopyMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.devonfw.tools.ide.io; | ||
|
||
/** | ||
* {@link Enum} with the available modes to {@link FileAccess#copy(java.nio.file.Path, java.nio.file.Path, FileCopyMode) | ||
* copy} files and folders. | ||
*/ | ||
public enum FileCopyMode { | ||
|
||
/** | ||
* Copy {@link #isFileOnly() only a single file} and {@link #isFailIfExists() fail if the target-file already exists}. | ||
*/ | ||
COPY_FILE_FAIL_IF_EXISTS, | ||
|
||
/** Copy {@link #isFileOnly() only a single file} and override the target-file if it already exists. */ | ||
COPY_FILE_OVERRIDE, | ||
|
||
/** Copy {@link #isRecursive() recursively} and {@link #isFailIfExists() fail if the target-path already exists}. */ | ||
COPY_TREE_FAIL_IF_EXISTS, | ||
|
||
/** Copy {@link #isRecursive() recursively} and override existing files but merge existing folders. */ | ||
COPY_TREE_OVERRIDE_FILES, | ||
|
||
/** | ||
* Copy {@link #isRecursive() recursively} and {@link FileAccess#delete(java.nio.file.Path) delete} the target-file if | ||
* it exists before copying. | ||
*/ | ||
COPY_TREE_OVERRIDE_TREE; | ||
|
||
/** | ||
* @return {@code true} if only a single file shall be copied. Will fail if a directory is given to copy, | ||
* {@code false} otherwise (to copy folders recursively). | ||
*/ | ||
public boolean isFileOnly() { | ||
|
||
return (this == COPY_FILE_FAIL_IF_EXISTS) || (this == COPY_FILE_OVERRIDE); | ||
} | ||
|
||
/** | ||
* @return {@code true} if files and folders shall be copied recursively, {@code false} otherwise | ||
* ({@link #isFileOnly() copy file copy}). | ||
*/ | ||
public boolean isRecursive() { | ||
|
||
return !isFileOnly(); | ||
} | ||
|
||
/** | ||
* @return {@code true} to fail if the target file or folder already exists, {@code false} otherwise. | ||
*/ | ||
public boolean isFailIfExists() { | ||
|
||
return (this == COPY_FILE_FAIL_IF_EXISTS) || (this == COPY_TREE_FAIL_IF_EXISTS); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
cli/src/test/java/com/devonfw/tools/ide/context/AbstractIdeContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.devonfw.tools.ide.context; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.assertj.core.api.Condition; | ||
import org.assertj.core.api.ListAssert; | ||
|
||
import com.devonfw.tools.ide.io.FileAccess; | ||
import com.devonfw.tools.ide.io.FileAccessImpl; | ||
import com.devonfw.tools.ide.io.FileCopyMode; | ||
import com.devonfw.tools.ide.log.IdeLogLevel; | ||
import com.devonfw.tools.ide.log.IdeTestLogger; | ||
|
||
/** | ||
* Abstract base class for tests that need mocked instances of {@link IdeContext}. | ||
*/ | ||
public abstract class AbstractIdeContextTest extends Assertions { | ||
|
||
/** The source {@link Path} to the test projects. */ | ||
protected static final Path PATH_PROJECTS = Paths.get("src/test/resources/ide-projects"); | ||
|
||
// will not use eclipse-target like done in maven via eclipse profile... | ||
private static final Path PATH_PROJECTS_COPY = Paths.get("target/test-projects/"); | ||
|
||
/** | ||
* @param projectName the (folder)name of the test project in {@link #PATH_PROJECTS}. E.g. "basic". | ||
* @return the {@link IdeContext} pointing to that project. | ||
*/ | ||
protected IdeContext newContext(String projectName) { | ||
|
||
return newContext(projectName, null, true); | ||
} | ||
|
||
/** | ||
* @param projectName the (folder)name of the test project in {@link #PATH_PROJECTS}. E.g. "basic". | ||
* @param projectPath the relative path inside the test project where to create the context. | ||
* @return the {@link IdeContext} pointing to that project. | ||
*/ | ||
protected static IdeContext newContext(String projectName, String projectPath) { | ||
|
||
return newContext(projectName, projectPath, true); | ||
} | ||
|
||
/** | ||
* @param projectName the (folder)name of the test project in {@link #PATH_PROJECTS}. E.g. "basic". | ||
* @param projectPath the relative path inside the test project where to create the context. | ||
* @param copyForMutation - {@code true} to create a copy of the project that can be modified by the test, | ||
* {@code false} otherwise (only to save resources if you are 100% sure that your test never modifies anything | ||
* in that project. | ||
* @return the {@link IdeContext} pointing to that project. | ||
*/ | ||
protected static IdeContext newContext(String projectName, String projectPath, boolean copyForMutation) { | ||
|
||
Path sourceDir = PATH_PROJECTS.resolve(projectName); | ||
Path userDir = sourceDir; | ||
if (projectPath != null) { | ||
userDir = sourceDir.resolve(projectPath); | ||
} | ||
IdeContext context = new IdeTestContext(userDir); | ||
if (copyForMutation) { | ||
Path projectDir = PATH_PROJECTS_COPY.resolve(projectName); | ||
FileAccess fileAccess = new FileAccessImpl(context); | ||
fileAccess.delete(projectDir); | ||
fileAccess.mkdirs(PATH_PROJECTS_COPY); | ||
fileAccess.copy(sourceDir, projectDir, FileCopyMode.COPY_TREE_OVERRIDE_TREE); | ||
fileAccess.copy(PATH_PROJECTS.resolve(IdeContext.FOLDER_IDE), PATH_PROJECTS_COPY.resolve(IdeContext.FOLDER_IDE), | ||
FileCopyMode.COPY_TREE_OVERRIDE_TREE); | ||
context = new IdeTestContext(projectDir.resolve(projectPath)); | ||
} | ||
return context; | ||
} | ||
|
||
/** | ||
* @param context the {@link IdeContext} that was created via the {@link #newContext(String) newContext} method. | ||
* @param level the expected {@link IdeLogLevel}. | ||
* @param message the expected {@link com.devonfw.tools.ide.log.IdeSubLogger#log(String) log message}. | ||
*/ | ||
protected static void assertLogMessage(IdeContext context, IdeLogLevel level, String message) { | ||
|
||
assertLogMessage(context, level, message, false); | ||
} | ||
|
||
/** | ||
* @param context the {@link IdeContext} that was created via the {@link #newContext(String) newContext} method. | ||
* @param level the expected {@link IdeLogLevel}. | ||
* @param message the expected {@link com.devonfw.tools.ide.log.IdeSubLogger#log(String) log message}. | ||
* @param contains - {@code true} if the given {@code message} may only be a sub-string of the log-message to assert, | ||
* {@code false} otherwise (the entire log message including potential parameters being filled in is asserted). | ||
*/ | ||
protected static void assertLogMessage(IdeContext context, IdeLogLevel level, String message, boolean contains) { | ||
|
||
IdeTestLogger logger = (IdeTestLogger) context.level(IdeLogLevel.WARNING); | ||
ListAssert<String> assertion = assertThat(logger.getMessages()).as(level.name() + "-Log messages"); | ||
if (contains) { | ||
Condition<String> condition = new Condition<>() { | ||
public boolean matches(String e) { | ||
|
||
return e.contains(message); | ||
}; | ||
}; | ||
assertion.filteredOn(condition).isNotEmpty(); | ||
} else { | ||
assertion.contains(message); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters