-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#366] Fix NullPointerException when accessing unfilled displayName i…
…n StandaloneConfig (#368) In our user guide, we allow most of the fields under the authors' configuration to be optional other than the GitHub Id. Particularly `Display Name`, which is suppose to use GitHub Id value when it is not provided. However, when the display name is not filled in the standalone config, the program crashes due to a NullPointerException. This is because standalone config adopts the json format. When a field is not filled in, in the json file, it will be initialized as null when deserialized into a java object. This was unforeseen and was left unchecked when accessing the variable. Let's fix this by returning an empty string as the default value of display name in the event of a null case, and also include unit tests for StandaloneConfig parser.
- Loading branch information
Showing
11 changed files
with
194 additions
and
6 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
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
107 changes: 107 additions & 0 deletions
107
src/test/java/reposense/parser/StandaloneConfigJsonParserTest.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,107 @@ | ||
package reposense.parser; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import com.google.gson.JsonSyntaxException; | ||
|
||
import reposense.model.Author; | ||
import reposense.model.RepoConfiguration; | ||
import reposense.model.StandaloneConfig; | ||
import reposense.util.TestUtil; | ||
|
||
public class StandaloneConfigJsonParserTest { | ||
|
||
private static final Path STANDALONE_MALFORMED_CONFIG = new File( | ||
StandaloneConfigJsonParserTest.class.getClassLoader() | ||
.getResource("StandaloneConfigJsonParserTest/standaloneConfig_malformedJson.json").getFile()).toPath(); | ||
|
||
private static final Path STANDALONE_UNKNOWN_PROPERTY_CONFIG = new File( | ||
StandaloneConfigJsonParserTest.class.getClassLoader().getResource( | ||
"StandaloneConfigJsonParserTest/standaloneConfig_unknownPropertyInJson.json").getFile()).toPath(); | ||
|
||
private static final Path STANDALONE_CONFIG_FULL = new File( | ||
StandaloneConfigJsonParserTest.class.getClassLoader().getResource( | ||
"StandaloneConfigJsonParserTest/standaloneConfig_full.json").getFile()).toPath(); | ||
|
||
private static final Path STANDALONE_CONFIG_EMPTY_TEXT_FILE = new File( | ||
StandaloneConfigJsonParserTest.class.getClassLoader().getResource( | ||
"StandaloneConfigJsonParserTest/standaloneConfig_emptyText.json").getFile()).toPath(); | ||
|
||
private static final Path STANDALONE_CONFIG_EMPTY_JSON_FILE = new File( | ||
StandaloneConfigJsonParserTest.class.getClassLoader().getResource( | ||
"StandaloneConfigJsonParserTest/standaloneConfig_emptyJson.json").getFile()).toPath(); | ||
|
||
private static final Path STANDALONE_CONFIG_GITHUBID_ONLY = new File( | ||
StandaloneConfigJsonParserTest.class.getClassLoader().getResource( | ||
"StandaloneConfigJsonParserTest/standaloneConfig_githubId_only.json").getFile()).toPath(); | ||
|
||
private static final String TEST_DUMMY_LOCATION = "https://github.com/reposense/RepoSense.git"; | ||
|
||
private static RepoConfiguration EXPECTED_GITHUBID_ONLY_REPOCONFIG; | ||
private static RepoConfiguration EXPECTED_FULL_REPOCONFIG; | ||
|
||
@BeforeClass | ||
public static void setUp() throws InvalidLocationException { | ||
Author author = new Author("yong24s"); | ||
author.setAuthorAliases(Arrays.asList("Yong Hao TENG")); | ||
author.setIgnoreGlobList(Arrays.asList("**.css", "**.html", "**.jade", "**.js")); | ||
|
||
EXPECTED_GITHUBID_ONLY_REPOCONFIG = new RepoConfiguration(TEST_DUMMY_LOCATION); | ||
EXPECTED_GITHUBID_ONLY_REPOCONFIG.setFormats(ArgsParser.DEFAULT_FORMATS); | ||
EXPECTED_GITHUBID_ONLY_REPOCONFIG.setAuthorList(Arrays.asList(new Author("yong24s"))); | ||
|
||
EXPECTED_FULL_REPOCONFIG = new RepoConfiguration(TEST_DUMMY_LOCATION); | ||
EXPECTED_FULL_REPOCONFIG.setFormats(Arrays.asList("gradle", "jade", "java", "js", "md", "scss", "yml")); | ||
EXPECTED_FULL_REPOCONFIG.setIgnoreCommitList(Arrays.asList("7b96c563eb2d3612aa5275364333664a18f01491")); | ||
EXPECTED_FULL_REPOCONFIG.setIgnoreGlobList(Arrays.asList("**.adoc", "collate**")); | ||
EXPECTED_FULL_REPOCONFIG.setAuthorList(Arrays.asList(author)); | ||
EXPECTED_FULL_REPOCONFIG.setAuthorDisplayName(author, "Yong Hao"); | ||
EXPECTED_FULL_REPOCONFIG.addAuthorAliases(author, Arrays.asList(author.getGitId())); | ||
EXPECTED_FULL_REPOCONFIG.addAuthorAliases(author, author.getAuthorAliases()); | ||
} | ||
|
||
@Test | ||
public void standaloneConfig_parseEmptyTextFile_success() throws IOException { | ||
new StandaloneConfigJsonParser().parse(STANDALONE_CONFIG_EMPTY_TEXT_FILE); | ||
} | ||
|
||
@Test | ||
public void standaloneConfig_parseEmptyJsonFile_success() throws IOException { | ||
new StandaloneConfigJsonParser().parse(STANDALONE_CONFIG_EMPTY_JSON_FILE); | ||
} | ||
|
||
@Test | ||
public void standaloneConfig_ignoresUnknownProperty_success() throws IOException { | ||
new StandaloneConfigJsonParser().parse(STANDALONE_UNKNOWN_PROPERTY_CONFIG); | ||
} | ||
|
||
@Test | ||
public void standaloneConfig_correctConfig_success() throws IOException, InvalidLocationException { | ||
StandaloneConfig config = new StandaloneConfigJsonParser().parse(STANDALONE_CONFIG_FULL); | ||
assertSameConfig(EXPECTED_FULL_REPOCONFIG, config); | ||
} | ||
|
||
@Test | ||
public void standaloneConfig_githubIdOnlyConfig_success() throws IOException, InvalidLocationException { | ||
StandaloneConfig config = new StandaloneConfigJsonParser().parse(STANDALONE_CONFIG_GITHUBID_ONLY); | ||
assertSameConfig(EXPECTED_GITHUBID_ONLY_REPOCONFIG, config); | ||
} | ||
|
||
@Test(expected = JsonSyntaxException.class) | ||
public void standaloneConfig_malformedJsonFile_throwsJsonSyntaxException() throws IOException { | ||
new StandaloneConfigJsonParser().parse(STANDALONE_MALFORMED_CONFIG); | ||
} | ||
|
||
private void assertSameConfig(RepoConfiguration expectedRepoConfig, StandaloneConfig actualStandaloneConfig) | ||
throws InvalidLocationException { | ||
RepoConfiguration actualRepoConfig = new RepoConfiguration(TEST_DUMMY_LOCATION); | ||
actualRepoConfig.update(actualStandaloneConfig); | ||
TestUtil.compareRepoConfig(expectedRepoConfig, actualRepoConfig); | ||
} | ||
} |
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
3 changes: 3 additions & 0 deletions
3
src/test/resources/StandaloneConfigJsonParserTest/standaloneConfig_emptyJson.json
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,3 @@ | ||
{ | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
src/test/resources/StandaloneConfigJsonParserTest/standaloneConfig_emptyText.json
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 @@ | ||
|
14 changes: 14 additions & 0 deletions
14
src/test/resources/StandaloneConfigJsonParserTest/standaloneConfig_full.json
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,14 @@ | ||
{ | ||
"ignoreGlobList": ["**.adoc", "collate**"], | ||
"formats": ["gradle", "jade", "java", "js", "md", "scss", "yml"], | ||
"ignoreCommitList": ["7b96c563eb2d3612aa5275364333664a18f01491"], | ||
"authors": | ||
[ | ||
{ | ||
"githubId": "yong24s", | ||
"displayName": "Yong Hao", | ||
"authorNames": ["Yong Hao TENG"], | ||
"ignoreGlobList": ["**.css", "**.html", "**.jade", "**.js"] | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
src/test/resources/StandaloneConfigJsonParserTest/standaloneConfig_githubId_only.json
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,8 @@ | ||
{ | ||
"authors": | ||
[ | ||
{ | ||
"githubId": "yong24s" | ||
} | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
src/test/resources/StandaloneConfigJsonParserTest/standaloneConfig_malformedJson.json
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,7 @@ | ||
{ | ||
"authors": | ||
[ | ||
{ | ||
"githubId": "yong24s" | ||
}, | ||
] |
9 changes: 9 additions & 0 deletions
9
...test/resources/StandaloneConfigJsonParserTest/standaloneConfig_unknownPropertyInJson.json
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,9 @@ | ||
{ | ||
"authors": | ||
[ | ||
{ | ||
"githubId": "yong24s", | ||
"level": "1" | ||
}, | ||
] | ||
} |