-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unit tests #284
Merged
Merged
Add unit tests #284
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
64 changes: 64 additions & 0 deletions
64
multiapps-common/src/test/java/org/cloudfoundry/multiapps/common/util/ListUtilTest.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,64 @@ | ||
package org.cloudfoundry.multiapps.common.util; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class ListUtilTest { | ||
|
||
@Test | ||
void testCast_withValidList() { | ||
int testNumber = 123; | ||
double testDouble = 45.67; | ||
List<Object> originalList = Arrays.asList("String", testNumber, testDouble); | ||
|
||
List<String> castList = ListUtil.cast(originalList); | ||
|
||
assertNotNull(castList); | ||
assertEquals(3, castList.size()); | ||
assertEquals("String", castList.get(0)); | ||
assertEquals(testNumber, castList.get(1)); | ||
assertEquals(testDouble, castList.get(2)); | ||
} | ||
|
||
@Test | ||
void testCast_withNullList() { | ||
List<Object> result = ListUtil.cast(null); | ||
|
||
assertNull(result); | ||
} | ||
|
||
@Test | ||
void testCast_withEmptyList() { | ||
List<Object> emptyList = Collections.emptyList(); | ||
List<Object> result = ListUtil.cast(emptyList); | ||
|
||
assertNotNull(result); | ||
assertTrue(result.isEmpty()); | ||
} | ||
|
||
@Test | ||
void testAsList_withNonNullItem() { | ||
String item = "Test Item"; | ||
List<String> result = ListUtil.asList(item); | ||
|
||
assertNotNull(result); | ||
assertEquals(1, result.size()); | ||
assertEquals(item, result.get(0)); | ||
} | ||
|
||
@Test | ||
void testAsList_withNullItem() { | ||
List<String> result = ListUtil.asList(null); | ||
|
||
assertNotNull(result); | ||
assertTrue(result.isEmpty()); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package org.cloudfoundry.multiapps.common.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
|
@@ -36,4 +40,92 @@ void testParseBooleanFlag() { | |
assertThrows(ContentException.class, () -> MapUtil.parseBooleanFlag(TEST_PARAMETERS, "incorrectTypeFlag1", true)); | ||
assertThrows(ContentException.class, () -> MapUtil.parseBooleanFlag(TEST_PARAMETERS, "incorrectTypeFlag2", true)); | ||
} | ||
|
||
@Test | ||
void testCast_withNullMap() { | ||
Map<Object, Object> result = MapUtil.cast(null); | ||
|
||
assertNull(result); | ||
} | ||
|
||
@Test | ||
void testCast_withEmptyMap() { | ||
Map<Object, Object> emptyMap = Collections.emptyMap(); | ||
|
||
Map<Object, Object> result = MapUtil.cast(emptyMap); | ||
|
||
assertNotNull(result); | ||
assertTrue(result.isEmpty()); | ||
} | ||
|
||
@Test | ||
void testAddNonNull_withNonNullValue() { | ||
Map<String, String> map = new HashMap<>(); | ||
String key = "key1"; | ||
String value = "value1"; | ||
|
||
MapUtil.addNonNull(map, key, value); | ||
|
||
assertEquals(1, map.size()); | ||
assertEquals(value, map.get(key)); | ||
} | ||
|
||
@Test | ||
void testAddNonNull_withNullValue() { | ||
Map<String, String> map = new HashMap<>(); | ||
String key = "key1"; | ||
String value = null; | ||
|
||
MapUtil.addNonNull(map, key, value); | ||
|
||
assertTrue(map.isEmpty()); | ||
} | ||
|
||
@Test | ||
void testMergeSafely_withBothMapsNonNull() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add a the same key to both maps to check if it's overwritten correctly in this test. |
||
Map<String, String> original = new HashMap<>(); | ||
original.put("key1", "value1"); | ||
Map<String, String> override = new HashMap<>(); | ||
override.put("key2", "value2"); | ||
override.put("key1", "value2"); | ||
|
||
Map<String, String> result = MapUtil.mergeSafely(original, override); | ||
|
||
assertNotNull(result); | ||
assertEquals(2, result.size()); | ||
assertEquals("value2", result.get("key1")); | ||
assertEquals("value2", result.get("key2")); | ||
} | ||
|
||
@Test | ||
void testMergeSafely_withOriginalNull() { | ||
Map<String, String> override = new HashMap<>(); | ||
override.put("key1", "value1"); | ||
|
||
Map<String, String> result = MapUtil.mergeSafely(null, override); | ||
|
||
assertNotNull(result); | ||
assertEquals(1, result.size()); | ||
assertEquals("value1", result.get("key1")); | ||
} | ||
|
||
@Test | ||
void testMergeSafely_withOverrideNull() { | ||
Map<String, String> original = new HashMap<>(); | ||
original.put("key1", "value1"); | ||
|
||
Map<String, String> result = MapUtil.mergeSafely(original, null); | ||
|
||
assertNotNull(result); | ||
assertEquals(1, result.size()); | ||
assertEquals("value1", result.get("key1")); | ||
} | ||
|
||
@Test | ||
void testMergeSafely_withBothMapsNull() { | ||
Map<String, String> result = MapUtil.mergeSafely(null, null); | ||
|
||
assertNotNull(result); | ||
assertTrue(result.isEmpty()); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe remove this exception construction and just add a check for equals(Messages.CANNOT...
as everything else seems to be taken from the exception we're checking anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the messages is different is not done this way.