Skip to content

Commit

Permalink
fix: fix NPE on codelocation naming when initial attempt is too long …
Browse files Browse the repository at this point in the history
…an prefix and/or suffix is not set; IDETECT-2034
  • Loading branch information
Steve Billings committed May 29, 2020
1 parent 6d3a5e0 commit 27b2adb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private String createShortenedCodeLocationName(final List<String> namePieces, fi
}

private String shortenPiece(final String piece) {
if (piece.length() <= 40) {
if (piece == null || piece.length() <= 40) {
return piece;
} else {
return piece.substring(0, 19) + "..." + piece.substring(piece.length() - 18);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,38 @@ public void testBomCodeLocationName() {
final File sourcePath = new File("/Users/ekerwin/Documents/source/functional/common-rest");
final File codeLocationPath = new File("/Users/ekerwin/Documents/source/functional/common-rest/child");

final String prefix = "";
final String suffix = "";
final String prefix = null;
final String suffix = null;
final String actual = codeLocationNameGenerator.createBomCodeLocationName(sourcePath, codeLocationPath, "projectName", "projectVersion", detectCodeLocation, prefix, suffix);

assertEquals(expected, actual);
}

@Test
public void testBomCodeLocationNameOversized() {
final String projectNameStart = "really really ";
final String projectName = projectNameStart + "really really really really really really really really really really really really really really really really really really really really really really really really really really long projectName";
final String expected = projectName + "/projectVersion/child/group/name/version npm/bom";
// = path/externalId tool/type

final ExternalIdFactory factory = new ExternalIdFactory();
final ExternalId externalId = factory.createMavenExternalId("group", "name", "version");
final CodeLocationNameGenerator codeLocationNameGenerator = new CodeLocationNameGenerator(null);

final DetectCodeLocation detectCodeLocation = Mockito.mock(DetectCodeLocation.class);
Mockito.when(detectCodeLocation.getExternalId()).thenReturn(externalId);
Mockito.when(detectCodeLocation.getCreatorName()).thenReturn(Optional.of("NPM"));

final File sourcePath = new File("/Users/ekerwin/Documents/source/functional/common-rest");
final File codeLocationPath = new File("/Users/ekerwin/Documents/source/functional/common-rest/child");

final String prefix = null;
final String suffix = null;
final String actual = codeLocationNameGenerator.createBomCodeLocationName(sourcePath, codeLocationPath, projectName, "projectVersion", detectCodeLocation, prefix, suffix);

assertTrue(actual.startsWith(projectNameStart));
}

@Test
public void testLongCodeLocationNames() {
final String expected = "projectName/projectVersion/common-rest-common-...n-rest-common-rest/group/name/version npm/bom";
Expand Down

0 comments on commit 27b2adb

Please sign in to comment.