Skip to content

Check null to avoid null pointer exception #334

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
38 changes: 31 additions & 7 deletions src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,25 @@ public static String locateOriginalText(String fullLicenseText, int startToken,
}
// ignore
}

/*
* @param text text to test
* @return the first token in the license text

/**
* Return the first license token found in the given text
* <p>
* The method normalizes the input text, removes comment characters,
* and splits it into tokens
* using {@link LicenseTextHelper#TOKEN_SPLIT_PATTERN}.
* It returns the first non-empty token found,
* or {@code null} if no such token exists.
* </p>
*
* @param text The license text to extract the first token from.
* @return The first non-empty token as a {@link String},
* or {@code null} if none is found.
*/
public static String getFirstLicenseToken(String text) {
if (text == null) {
return null;
}
String textToTokenize = LicenseTextHelper.normalizeText(LicenseTextHelper.replaceMultWord(LicenseTextHelper.replaceSpaceComma(
LicenseTextHelper.removeLineSeparators(removeCommentChars(text))))).toLowerCase();
Matcher m = LicenseTextHelper.TOKEN_SPLIT_PATTERN.matcher(textToTokenize);
Expand All @@ -197,12 +210,23 @@ public static String getFirstLicenseToken(String text) {
}
return null;
}

/**
* @param text text to test
* @return true if the text contains a single token
* Check whether the given text contains only a single token
* <p>
* A single token string is defined as a non-null string that does not
* contain any newline characters and contains exactly one non-empty token
* as identified by the {@link LicenseTextHelper#TOKEN_SPLIT_PATTERN}.
* </p>
*
* @param text The text to test.
* @return {@code true} if the text contains a single token,
* {@code false} otherwise.
*/
public static boolean isSingleTokenString(String text) {
if (text == null) {
return false;
}
if (text.contains("\n")) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,18 +539,19 @@ public void testLicenseEqualsNoneLicense() throws InvalidSPDXAnalysisException,
Map<String, String> xlationMap = new HashMap<>();;
assertTrue(LicenseCompareHelper.isLicenseEqual(lic3, lic4, xlationMap));
assertFalse(LicenseCompareHelper.isLicenseEqual(lic4, lic2, xlationMap));
}


}

public void testisSingleTokenString() {
assertTrue(LicenseCompareHelper.isSingleTokenString(""));
assertTrue(LicenseCompareHelper.isSingleTokenString(" token "));
assertTrue(LicenseCompareHelper.isSingleTokenString("'"));
assertTrue(LicenseCompareHelper.isSingleTokenString(" '"));
assertTrue(LicenseCompareHelper.isSingleTokenString("' "));
assertFalse(LicenseCompareHelper.isSingleTokenString("a and"));
assertFalse(LicenseCompareHelper.isSingleTokenString("a\nand"));
assertFalse(LicenseCompareHelper.isSingleTokenString(null));
}

public void regressionTestMatchingGpl20Only() throws IOException, InvalidSPDXAnalysisException, SpdxCompareException {
String compareText = UnitTestHelper.fileToText(GPL_2_TEXT);
DifferenceDescription result = LicenseCompareHelper.isTextStandardLicense(LicenseInfoFactory.getListedLicenseById("GPL-2.0-only"), compareText);
Expand All @@ -568,11 +569,16 @@ public void testMatchingStandardLicenseIds() throws IOException, InvalidSPDXAnal
assertTrue(result[3].startsWith("GPL-2"));
}
}

public void testFirstLicenseToken() {
assertEquals("first", LicenseCompareHelper.getFirstLicenseToken(" first,token that is needed\nnext"));
assertEquals("first", LicenseCompareHelper.getFirstLicenseToken("// first,second"));
assertNull(LicenseCompareHelper.getFirstLicenseToken(null));
assertNull(LicenseCompareHelper.getFirstLicenseToken(""));
assertNull(LicenseCompareHelper.getFirstLicenseToken(" <!-- -->"));
assertNull(LicenseCompareHelper.getFirstLicenseToken("# "));
}

@SuppressWarnings("unused")
private String stringCharToUnicode(String s, int location) {
return "\\u" + Integer.toHexString(s.charAt(location) | 0x10000).substring(1);
Expand Down