-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] Added Unit tests for AssemblyFileUtils, FileLocation, and Mess…
…ageLevels
- Loading branch information
kdhola
committed
Nov 1, 2023
1 parent
d1efcda
commit a041b1d
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/test/java/org/apache/maven/plugins/assembly/io/FileLocationTest.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,40 @@ | ||
//Testcases written by Kaushik Dhola for io/FileLocation.java | ||
package org.apache.maven.plugins.assembly.io; | ||
import org.junit.Test; | ||
import java.io.File; | ||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class FileLocationTest{ | ||
|
||
|
||
@Test | ||
public void testSetFile_Success() throws Exception { | ||
|
||
FileLocation fileLocation = new FileLocation("testSpecification"); | ||
File file = new File("testFile.txt"); | ||
|
||
// Set file to the location | ||
fileLocation.setFile(file); | ||
|
||
// check id the file is in the given location | ||
File obtainedFile = fileLocation.unsafeGetFile(); | ||
assertEquals(file, obtainedFile); | ||
} | ||
|
||
|
||
@Test(expected = IllegalStateException.class) | ||
public void testSetFile_WhenChannelOpen() throws Exception { | ||
|
||
|
||
// Testing the file when the channel is open | ||
FileLocation fileLocation = mock(FileLocation.class); | ||
doThrow(IllegalStateException.class).when(fileLocation).setFile(any(File.class)); | ||
|
||
// Simulates open file location | ||
fileLocation.open(); | ||
|
||
// Trying to set the file when chennel is open | ||
fileLocation.setFile(new File("testFile.txt")); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/java/org/apache/maven/plugins/assembly/io/TestMessageLevels.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,34 @@ | ||
//Test cases added bby Kaushik Dhola for MessageLevels.java | ||
|
||
package org.apache.maven.plugins.assembly.io; | ||
import org.junit.Test; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
public class TestMessageLevels { | ||
|
||
@Test | ||
public void testGetLevelLabelValid() { | ||
int validLevel = 1; | ||
//getting the label for valid message level | ||
String label = MessageLevels.getLevelLabel(validLevel); | ||
|
||
// Assert the label if matches with "INFO" | ||
assertEquals("INFO", label); | ||
} | ||
|
||
@Test | ||
public void testGetLevelLabelInvalid() { | ||
int invalidLevel = 10; | ||
|
||
try { | ||
//check if an IllegalArgumentException is thrown for the invalid level | ||
MessageLevels.getLevelLabel(invalidLevel); | ||
fail("Expected an IllegalArgumentException to be thrown"); | ||
} catch (IllegalArgumentException e) { | ||
|
||
//Assert the thrown exception | ||
assertEquals("Invalid message level: " + invalidLevel, e.getMessage()); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/java/org/apache/maven/plugins/assembly/utils/TestAssemblyFileUtils.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,54 @@ | ||
// Test cases written by Kaushik Dhola for AssemblyFileUtils.java | ||
|
||
package org.apache.maven.plugins.assembly.utils; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import java.io.File; | ||
|
||
|
||
public class TestAssemblyFileUtils { | ||
|
||
@Test | ||
public void testMakePathRelativeTo() { | ||
|
||
// Test when base directory is null | ||
String path = "/path/to/file"; | ||
String result = AssemblyFileUtils.makePathRelativeTo(path, null); | ||
Assert.assertEquals("basedir is null", path, result); | ||
|
||
// Test when the given path is null | ||
String nullPath = null; | ||
result = AssemblyFileUtils.makePathRelativeTo(nullPath, new File("//base/dir")); | ||
Assert.assertNull("path is null", result); | ||
|
||
// Test when the given path is relative | ||
String basePath = "/base/dir/"; | ||
String relativePath = "/file.txt"; | ||
result = AssemblyFileUtils.makePathRelativeTo(basePath + relativePath, new File(basePath)); | ||
Assert.assertEquals("path is already relative", relativePath, result); | ||
|
||
// Test when path is absolute but starts with the base directory | ||
String absolutePath = "/base/dir/other/directory/file.txt"; | ||
result = AssemblyFileUtils.makePathRelativeTo(absolutePath, new File(basePath)); | ||
Assert.assertEquals("path is absolute but starts with the base directory", "other/directory/file.txt", result); | ||
|
||
// Test when path absolute but not within the base directory | ||
String differentBasePath = "/different/base/dir/"; | ||
String absolutePathNotInBase = "/base/dir/other/file.txt"; | ||
result = AssemblyFileUtils.makePathRelativeTo(absolutePathNotInBase, new File(differentBasePath)); | ||
Assert.assertEquals("path is absolute but not within the base directory", absolutePathNotInBase, result); | ||
|
||
// Test when path is empty | ||
String emptyPath = ""; | ||
result = AssemblyFileUtils.makePathRelativeTo(emptyPath, new File("//base/dir")); | ||
Assert.assertEquals("path is empty", "", result); | ||
|
||
// Test when path is root | ||
String rootPath = "/"; | ||
result = AssemblyFileUtils.makePathRelativeTo(rootPath, new File("/")); | ||
Assert.assertEquals("path is root", ".", result); | ||
} | ||
|
||
} | ||
|