Merge "Add support for library names."

This commit is contained in:
Bob Badour
2022-02-10 23:44:51 +00:00
committed by Gerrit Code Review
2 changed files with 271 additions and 69 deletions

View File

@@ -39,6 +39,8 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
/** /**
@@ -54,6 +56,7 @@ class LicenseHtmlGeneratorFromXml {
private static final String TAG_FILE_NAME = "file-name"; private static final String TAG_FILE_NAME = "file-name";
private static final String TAG_FILE_CONTENT = "file-content"; private static final String TAG_FILE_CONTENT = "file-content";
private static final String ATTR_CONTENT_ID = "contentId"; private static final String ATTR_CONTENT_ID = "contentId";
private static final String ATTR_LIBRARY_NAME = "lib";
private static final String HTML_HEAD_STRING = private static final String HTML_HEAD_STRING =
"<html><head>\n" "<html><head>\n"
@@ -67,8 +70,12 @@ class LicenseHtmlGeneratorFromXml {
+ "</style>\n" + "</style>\n"
+ "</head>" + "</head>"
+ "<body topmargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" bottommargin=\"0\">\n" + "<body topmargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" bottommargin=\"0\">\n"
+ "<div class=\"toc\">\n" + "<div class=\"toc\">\n";
+ "<ul>"; private static final String LIBRARY_HEAD_STRING =
"<strong>Libraries</strong>\n<ul class=\"libraries\">";
private static final String LIBRARY_TAIL_STRING = "</ul>\n<strong>Files</strong>";
private static final String FILES_HEAD_STRING = "<ul class=\"files\">";
private static final String HTML_MIDDLE_STRING = private static final String HTML_MIDDLE_STRING =
"</ul>\n" "</ul>\n"
@@ -81,12 +88,14 @@ class LicenseHtmlGeneratorFromXml {
private final List<File> mXmlFiles; private final List<File> mXmlFiles;
/* /*
* A map from a file name to a content id (MD5 sum of file content) for its license. * A map from a file name to a library name (may be empty) to a content id (MD5 sum of file
* For example, "/system/priv-app/TeleService/TeleService.apk" maps to * content) for its license.
* For example, "/system/priv-app/TeleService/TeleService.apk" maps to "service/Telephony" to
* "9645f39e9db895a4aa6e02cb57294595". Here "9645f39e9db895a4aa6e02cb57294595" is a MD5 sum * "9645f39e9db895a4aa6e02cb57294595". Here "9645f39e9db895a4aa6e02cb57294595" is a MD5 sum
* of the content of packages/services/Telephony/MODULE_LICENSE_APACHE2. * of the content of packages/services/Telephony/MODULE_LICENSE_APACHE2.
*/ */
private final Map<String, Set<String>> mFileNameToContentIdMap = new HashMap(); private final Map<String, Map<String, Set<String>>> mFileNameToLibraryToContentIdMap =
new HashMap();
/* /*
* A map from a content id (MD5 sum of file content) to a license file content. * A map from a content id (MD5 sum of file content) to a license file content.
@@ -98,7 +107,7 @@ class LicenseHtmlGeneratorFromXml {
static class ContentIdAndFileNames { static class ContentIdAndFileNames {
final String mContentId; final String mContentId;
final List<String> mFileNameList = new ArrayList(); final Map<String, List<String>> mLibraryToFileNameMap = new TreeMap();
ContentIdAndFileNames(String contentId) { ContentIdAndFileNames(String contentId) {
mContentId = contentId; mContentId = contentId;
@@ -120,7 +129,7 @@ class LicenseHtmlGeneratorFromXml {
parse(xmlFile); parse(xmlFile);
} }
if (mFileNameToContentIdMap.isEmpty() || mContentIdToFileContentMap.isEmpty()) { if (mFileNameToLibraryToContentIdMap.isEmpty() || mContentIdToFileContentMap.isEmpty()) {
return false; return false;
} }
@@ -128,7 +137,7 @@ class LicenseHtmlGeneratorFromXml {
try { try {
writer = new PrintWriter(outputFile); writer = new PrintWriter(outputFile);
generateHtml(mFileNameToContentIdMap, mContentIdToFileContentMap, writer, generateHtml(mFileNameToLibraryToContentIdMap, mContentIdToFileContentMap, writer,
noticeHeader); noticeHeader);
writer.flush(); writer.flush();
@@ -157,7 +166,7 @@ class LicenseHtmlGeneratorFromXml {
in = new FileReader(xmlFile); in = new FileReader(xmlFile);
} }
parse(in, mFileNameToContentIdMap, mContentIdToFileContentMap); parse(in, mFileNameToLibraryToContentIdMap, mContentIdToFileContentMap);
in.close(); in.close();
} catch (XmlPullParserException | IOException e) { } catch (XmlPullParserException | IOException e) {
@@ -180,7 +189,8 @@ class LicenseHtmlGeneratorFromXml {
* *
* <licenses> * <licenses>
* <file-name contentId="content_id_of_license1">file1</file-name> * <file-name contentId="content_id_of_license1">file1</file-name>
* <file-name contentId="content_id_of_license2">file2</file-name> * <file-name contentId="content_id_of_license2" lib="name of library">file2</file-name>
* <file-name contentId="content_id_of_license2" lib="another library">file2</file-name>
* ... * ...
* <file-content contentId="content_id_of_license1">license1 file contents</file-content> * <file-content contentId="content_id_of_license1">license1 file contents</file-content>
* <file-content contentId="content_id_of_license2">license2 file contents</file-content> * <file-content contentId="content_id_of_license2">license2 file contents</file-content>
@@ -188,10 +198,12 @@ class LicenseHtmlGeneratorFromXml {
* </licenses> * </licenses>
*/ */
@VisibleForTesting @VisibleForTesting
static void parse(InputStreamReader in, Map<String, Set<String>> outFileNameToContentIdMap, static void parse(InputStreamReader in,
Map<String, Map<String, Set<String>>> outFileNameToLibraryToContentIdMap,
Map<String, String> outContentIdToFileContentMap) Map<String, String> outContentIdToFileContentMap)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
Map<String, Set<String>> fileNameToContentIdMap = new HashMap<String, Set<String>>(); Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap =
new HashMap<String, Map<String, Set<String>>>();
Map<String, String> contentIdToFileContentMap = new HashMap<String, String>(); Map<String, String> contentIdToFileContentMap = new HashMap<String, String>();
XmlPullParser parser = Xml.newPullParser(); XmlPullParser parser = Xml.newPullParser();
@@ -205,12 +217,15 @@ class LicenseHtmlGeneratorFromXml {
if (state == XmlPullParser.START_TAG) { if (state == XmlPullParser.START_TAG) {
if (TAG_FILE_NAME.equals(parser.getName())) { if (TAG_FILE_NAME.equals(parser.getName())) {
String contentId = parser.getAttributeValue("", ATTR_CONTENT_ID); String contentId = parser.getAttributeValue("", ATTR_CONTENT_ID);
String libraryName = parser.getAttributeValue("", ATTR_LIBRARY_NAME);
if (!TextUtils.isEmpty(contentId)) { if (!TextUtils.isEmpty(contentId)) {
String fileName = readText(parser).trim(); String fileName = readText(parser).trim();
if (!TextUtils.isEmpty(fileName)) { if (!TextUtils.isEmpty(fileName)) {
Set<String> contentIds = Map<String, Set<String>> libs =
fileNameToContentIdMap.computeIfAbsent( fileNameToLibraryToContentIdMap.computeIfAbsent(
fileName, k -> new HashSet<>()); fileName, k -> new HashMap<>());
Set<String> contentIds = libs.computeIfAbsent(
libraryName, k -> new HashSet<>());
contentIds.add(contentId); contentIds.add(contentId);
} }
} }
@@ -229,11 +244,17 @@ class LicenseHtmlGeneratorFromXml {
state = parser.next(); state = parser.next();
} }
for (Map.Entry<String, Set<String>> entry : fileNameToContentIdMap.entrySet()) { for (Map.Entry<String, Map<String, Set<String>>> mapEntry :
outFileNameToContentIdMap.merge( fileNameToLibraryToContentIdMap.entrySet()) {
entry.getKey(), entry.getValue(), (s1, s2) -> { outFileNameToLibraryToContentIdMap.merge(
s1.addAll(s2); mapEntry.getKey(), mapEntry.getValue(), (m1, m2) -> {
return s1; for (Map.Entry<String, Set<String>> entry : m2.entrySet()) {
m1.merge(entry.getKey(), entry.getValue(), (s1, s2) -> {
s1.addAll(s2);
return s1;
});
}
return m1;
}); });
} }
outContentIdToFileContentMap.putAll(contentIdToFileContentMap); outContentIdToFileContentMap.putAll(contentIdToFileContentMap);
@@ -251,13 +272,28 @@ class LicenseHtmlGeneratorFromXml {
} }
@VisibleForTesting @VisibleForTesting
static void generateHtml(Map<String, Set<String>> fileNameToContentIdMap, static void generateHtml(Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap,
Map<String, String> contentIdToFileContentMap, PrintWriter writer, Map<String, String> contentIdToFileContentMap, PrintWriter writer,
String noticeHeader) { String noticeHeader) {
List<String> fileNameList = new ArrayList(); List<String> fileNameList = new ArrayList();
fileNameList.addAll(fileNameToContentIdMap.keySet()); fileNameList.addAll(fileNameToLibraryToContentIdMap.keySet());
Collections.sort(fileNameList); Collections.sort(fileNameList);
SortedMap<String, Set<String>> libraryToContentIdMap = new TreeMap();
for (Map<String, Set<String>> libraryToContentValue :
fileNameToLibraryToContentIdMap.values()) {
for (Map.Entry<String, Set<String>> entry : libraryToContentValue.entrySet()) {
if (TextUtils.isEmpty(entry.getKey())) {
continue;
}
libraryToContentIdMap.merge(
entry.getKey(), entry.getValue(), (s1, s2) -> {
s1.addAll(s2);
return s1;
});
}
}
writer.println(HTML_HEAD_STRING); writer.println(HTML_HEAD_STRING);
if (!TextUtils.isEmpty(noticeHeader)) { if (!TextUtils.isEmpty(noticeHeader)) {
@@ -268,21 +304,56 @@ class LicenseHtmlGeneratorFromXml {
Map<String, Integer> contentIdToOrderMap = new HashMap(); Map<String, Integer> contentIdToOrderMap = new HashMap();
List<ContentIdAndFileNames> contentIdAndFileNamesList = new ArrayList(); List<ContentIdAndFileNames> contentIdAndFileNamesList = new ArrayList();
if (!libraryToContentIdMap.isEmpty()) {
writer.println(LIBRARY_HEAD_STRING);
for (Map.Entry<String, Set<String>> entry: libraryToContentIdMap.entrySet()) {
String libraryName = entry.getKey();
for (String contentId : entry.getValue()) {
// Assigns an id to a newly referred license file content.
if (!contentIdToOrderMap.containsKey(contentId)) {
contentIdToOrderMap.put(contentId, count);
// An index in contentIdAndFileNamesList is the order of each element.
contentIdAndFileNamesList.add(new ContentIdAndFileNames(contentId));
count++;
}
int id = contentIdToOrderMap.get(contentId);
writer.format("<li><a href=\"#id%d\">%s</a></li>\n", id, libraryName);
}
}
writer.println(LIBRARY_TAIL_STRING);
}
// Prints all the file list with a link to its license file content. // Prints all the file list with a link to its license file content.
for (String fileName : fileNameList) { for (String fileName : fileNameList) {
for (String contentId : fileNameToContentIdMap.get(fileName)) { for (Map.Entry<String, Set<String>> libToContentId :
// Assigns an id to a newly referred license file content. fileNameToLibraryToContentIdMap.get(fileName).entrySet()) {
if (!contentIdToOrderMap.containsKey(contentId)) { String libraryName = libToContentId.getKey();
contentIdToOrderMap.put(contentId, count); if (libraryName == null) {
libraryName = "";
// An index in contentIdAndFileNamesList is the order of each element.
contentIdAndFileNamesList.add(new ContentIdAndFileNames(contentId));
count++;
} }
for (String contentId : libToContentId.getValue()) {
// Assigns an id to a newly referred license file content.
if (!contentIdToOrderMap.containsKey(contentId)) {
contentIdToOrderMap.put(contentId, count);
int id = contentIdToOrderMap.get(contentId); // An index in contentIdAndFileNamesList is the order of each element.
contentIdAndFileNamesList.get(id).mFileNameList.add(fileName); contentIdAndFileNamesList.add(new ContentIdAndFileNames(contentId));
writer.format("<li><a href=\"#id%d\">%s</a></li>\n", id, fileName); count++;
}
int id = contentIdToOrderMap.get(contentId);
ContentIdAndFileNames elem = contentIdAndFileNamesList.get(id);
List<String> files = elem.mLibraryToFileNameMap.computeIfAbsent(
libraryName, k -> new ArrayList());
files.add(fileName);
if (TextUtils.isEmpty(libraryName)) {
writer.format("<li><a href=\"#id%d\">%s</a></li>\n", id, fileName);
} else {
writer.format("<li><a href=\"#id%d\">%s - %s</a></li>\n",
id, fileName, libraryName);
}
}
} }
} }
@@ -292,19 +363,27 @@ class LicenseHtmlGeneratorFromXml {
// Prints all contents of the license files in order of id. // Prints all contents of the license files in order of id.
for (ContentIdAndFileNames contentIdAndFileNames : contentIdAndFileNamesList) { for (ContentIdAndFileNames contentIdAndFileNames : contentIdAndFileNamesList) {
writer.format("<tr id=\"id%d\"><td class=\"same-license\">\n", count); writer.format("<tr id=\"id%d\"><td class=\"same-license\">\n", count);
writer.println("<div class=\"label\">Notices for file(s):</div>"); for (Map.Entry<String, List<String>> libraryFiles :
writer.println("<div class=\"file-list\">"); contentIdAndFileNames.mLibraryToFileNameMap.entrySet()) {
for (String fileName : contentIdAndFileNames.mFileNameList) { String libraryName = libraryFiles.getKey();
writer.format("%s <br/>\n", fileName); if (TextUtils.isEmpty(libraryName)) {
writer.println("<div class=\"label\">Notices for file(s):</div>");
} else {
writer.format("<div class=\"label\"><strong>%s</strong> used by:</div>\n",
libraryName);
}
writer.println("<div class=\"file-list\">");
for (String fileName : libraryFiles.getValue()) {
writer.format("%s <br/>\n", fileName);
}
writer.println("</div><!-- file-list -->");
count++;
} }
writer.println("</div><!-- file-list -->");
writer.println("<pre class=\"license-text\">"); writer.println("<pre class=\"license-text\">");
writer.println(contentIdToFileContentMap.get( writer.println(contentIdToFileContentMap.get(
contentIdAndFileNames.mContentId)); contentIdAndFileNames.mContentId));
writer.println("</pre><!-- license-text -->"); writer.println("</pre><!-- license-text -->");
writer.println("</td></tr><!-- same-license -->"); writer.println("</td></tr><!-- same-license -->");
count++;
} }
writer.println(HTML_REAR_STRING); writer.println(HTML_REAR_STRING);

View File

@@ -36,7 +36,7 @@ import java.util.Set;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class LicenseHtmlGeneratorFromXmlTest { public class LicenseHtmlGeneratorFromXmlTest {
private static final String VALILD_XML_STRING = private static final String VALID_OLD_XML_STRING =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<licenses>\n" + "<licenses>\n"
+ "<file-name contentId=\"0\">/file0</file-name>\n" + "<file-name contentId=\"0\">/file0</file-name>\n"
@@ -44,7 +44,15 @@ public class LicenseHtmlGeneratorFromXmlTest {
+ "<file-content contentId=\"0\"><![CDATA[license content #0]]></file-content>\n" + "<file-content contentId=\"0\"><![CDATA[license content #0]]></file-content>\n"
+ "</licenses>"; + "</licenses>";
private static final String INVALILD_XML_STRING = private static final String VALID_NEW_XML_STRING =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<licenses>\n"
+ "<file-name contentId=\"0\" lib=\"libA\">/file0</file-name>\n"
+ "<file-name contentId=\"0\" lib=\"libB\">/file1</file-name>\n"
+ "<file-content contentId=\"0\"><![CDATA[license content #0]]></file-content>\n"
+ "</licenses>";
private static final String INVALID_XML_STRING =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<licenses2>\n" + "<licenses2>\n"
+ "<file-name contentId=\"0\">/file0</file-name>\n" + "<file-name contentId=\"0\">/file0</file-name>\n"
@@ -64,13 +72,13 @@ public class LicenseHtmlGeneratorFromXmlTest {
+ "</style>\n" + "</style>\n"
+ "</head>" + "</head>"
+ "<body topmargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" bottommargin=\"0\">\n" + "<body topmargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" bottommargin=\"0\">\n"
+ "<div class=\"toc\">\n" + "<div class=\"toc\">\n";
+ "<ul>\n";
private static final String HTML_CUSTOM_HEADING = "Custom heading"; private static final String HTML_CUSTOM_HEADING = "Custom heading";
private static final String HTML_BODY_STRING = private static final String HTML_OLD_BODY_STRING =
"<li><a href=\"#id0\">/file0</a></li>\n" "<ul class=\"files\">\n"
+ "<li><a href=\"#id0\">/file0</a></li>\n"
+ "<li><a href=\"#id1\">/file0</a></li>\n" + "<li><a href=\"#id1\">/file0</a></li>\n"
+ "<li><a href=\"#id0\">/file1</a></li>\n" + "<li><a href=\"#id0\">/file1</a></li>\n"
+ "</ul>\n" + "</ul>\n"
@@ -97,66 +105,181 @@ public class LicenseHtmlGeneratorFromXmlTest {
+ "</td></tr><!-- same-license -->\n" + "</td></tr><!-- same-license -->\n"
+ "</table></body></html>\n"; + "</table></body></html>\n";
private static final String EXPECTED_HTML_STRING = HTML_HEAD_STRING + HTML_BODY_STRING; private static final String HTML_NEW_BODY_STRING =
"<strong>Libraries</strong>\n"
+ "<ul class=\"libraries\">\n"
+ "<li><a href=\"#id0\">libA</a></li>\n"
+ "<li><a href=\"#id1\">libB</a></li>\n"
+ "</ul>\n"
+ "<strong>Files</strong>\n"
+ "<ul class=\"files\">\n"
+ "<li><a href=\"#id0\">/file0 - libA</a></li>\n"
+ "<li><a href=\"#id1\">/file0 - libB</a></li>\n"
+ "<li><a href=\"#id0\">/file1 - libA</a></li>\n"
+ "</ul>\n"
+ "</div><!-- table of contents -->\n"
+ "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n"
+ "<tr id=\"id0\"><td class=\"same-license\">\n"
+ "<div class=\"label\">Notices for file(s):</div>\n"
+ "<div class=\"file-list\">\n"
+ "/file0 <br/>\n"
+ "/file1 <br/>\n"
+ "</div><!-- file-list -->\n"
+ "<pre class=\"license-text\">\n"
+ "license content #0\n"
+ "</pre><!-- license-text -->\n"
+ "</td></tr><!-- same-license -->\n"
+ "<tr id=\"id1\"><td class=\"same-license\">\n"
+ "<div class=\"label\">Notices for file(s):</div>\n"
+ "<div class=\"file-list\">\n"
+ "/file0 <br/>\n"
+ "</div><!-- file-list -->\n"
+ "<pre class=\"license-text\">\n"
+ "license content #1\n"
+ "</pre><!-- license-text -->\n"
+ "</td></tr><!-- same-license -->\n"
+ "</table></body></html>\n";
private static final String EXPECTED_HTML_STRING_WITH_CUSTOM_HEADING = private static final String EXPECTED_OLD_HTML_STRING = HTML_HEAD_STRING + HTML_OLD_BODY_STRING;
HTML_HEAD_STRING + HTML_CUSTOM_HEADING + "\n" + HTML_BODY_STRING;
private static final String EXPECTED_NEW_HTML_STRING = HTML_HEAD_STRING + HTML_NEW_BODY_STRING;
private static final String EXPECTED_OLD_HTML_STRING_WITH_CUSTOM_HEADING =
HTML_HEAD_STRING + HTML_CUSTOM_HEADING + "\n" + HTML_OLD_BODY_STRING;
private static final String EXPECTED_NEW_HTML_STRING_WITH_CUSTOM_HEADING =
HTML_HEAD_STRING + HTML_CUSTOM_HEADING + "\n" + HTML_NEW_BODY_STRING;
@Test @Test
public void testParseValidXmlStream() throws XmlPullParserException, IOException { public void testParseValidXmlStream() throws XmlPullParserException, IOException {
Map<String, Set<String>> fileNameToContentIdMap = new HashMap<>(); Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>();
Map<String, String> contentIdToFileContentMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>();
LicenseHtmlGeneratorFromXml.parse( LicenseHtmlGeneratorFromXml.parse(
new InputStreamReader(new ByteArrayInputStream(VALILD_XML_STRING.getBytes())), new InputStreamReader(new ByteArrayInputStream(VALID_OLD_XML_STRING.getBytes())),
fileNameToContentIdMap, contentIdToFileContentMap); fileNameToLibraryToContentIdMap, contentIdToFileContentMap);
assertThat(fileNameToContentIdMap.size()).isEqualTo(2); assertThat(fileNameToLibraryToContentIdMap.size()).isEqualTo(1);
assertThat(fileNameToContentIdMap.get("/file0")).containsExactly("0"); assertThat(fileNameToLibraryToContentIdMap.get("").size()).isEqualTo(2);
assertThat(fileNameToContentIdMap.get("/file1")).containsExactly("0"); assertThat(fileNameToLibraryToContentIdMap.get("").get("/file0")).containsExactly("0");
assertThat(fileNameToLibraryToContentIdMap.get("").get("/file1")).containsExactly("0");
assertThat(contentIdToFileContentMap.size()).isEqualTo(1);
assertThat(contentIdToFileContentMap.get("0")).isEqualTo("license content #0");
}
@Test
public void testParseNewValidXmlStream() throws XmlPullParserException, IOException {
Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>();
Map<String, String> contentIdToFileContentMap = new HashMap<>();
LicenseHtmlGeneratorFromXml.parse(
new InputStreamReader(new ByteArrayInputStream(VALID_NEW_XML_STRING.getBytes())),
fileNameToLibraryToContentIdMap, contentIdToFileContentMap);
assertThat(fileNameToLibraryToContentIdMap.size()).isEqualTo(2);
assertThat(fileNameToLibraryToContentIdMap.get("libA").size()).isEqualTo(1);
assertThat(fileNameToLibraryToContentIdMap.get("libB").size()).isEqualTo(1);
assertThat(fileNameToLibraryToContentIdMap.get("libA").get("/file0")).containsExactly("0");
assertThat(fileNameToLibraryToContentIdMap.get("libB").get("/file1")).containsExactly("0");
assertThat(contentIdToFileContentMap.size()).isEqualTo(1); assertThat(contentIdToFileContentMap.size()).isEqualTo(1);
assertThat(contentIdToFileContentMap.get("0")).isEqualTo("license content #0"); assertThat(contentIdToFileContentMap.get("0")).isEqualTo("license content #0");
} }
@Test(expected = XmlPullParserException.class) @Test(expected = XmlPullParserException.class)
public void testParseInvalidXmlStream() throws XmlPullParserException, IOException { public void testParseInvalidXmlStream() throws XmlPullParserException, IOException {
Map<String, Set<String>> fileNameToContentIdMap = new HashMap<>(); Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>();
Map<String, String> contentIdToFileContentMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>();
LicenseHtmlGeneratorFromXml.parse( LicenseHtmlGeneratorFromXml.parse(
new InputStreamReader(new ByteArrayInputStream(INVALILD_XML_STRING.getBytes())), new InputStreamReader(new ByteArrayInputStream(INVALID_XML_STRING.getBytes())),
fileNameToContentIdMap, contentIdToFileContentMap); fileNameToLibraryToContentIdMap, contentIdToFileContentMap);
} }
@Test @Test
public void testGenerateHtml() { public void testGenerateHtml() {
Map<String, Set<String>> fileNameToContentIdMap = new HashMap<>(); Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>();
Map<String, String> contentIdToFileContentMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>();
Map<String, Set<String>> toBoth = new HashMap<>();
Map<String, Set<String>> toOne = new HashMap<>();
fileNameToContentIdMap.put("/file0", new HashSet<String>(Arrays.asList("0", "1"))); toBoth.put("", new HashSet<String>(Arrays.asList("0", "1")));
fileNameToContentIdMap.put("/file1", new HashSet<String>(Arrays.asList("0"))); toOne.put("", new HashSet<String>(Arrays.asList("0")));
fileNameToLibraryToContentIdMap.put("/file0", toBoth);
fileNameToLibraryToContentIdMap.put("/file1", toOne);
contentIdToFileContentMap.put("0", "license content #0"); contentIdToFileContentMap.put("0", "license content #0");
contentIdToFileContentMap.put("1", "license content #1"); contentIdToFileContentMap.put("1", "license content #1");
StringWriter output = new StringWriter(); StringWriter output = new StringWriter();
LicenseHtmlGeneratorFromXml.generateHtml( LicenseHtmlGeneratorFromXml.generateHtml(
fileNameToContentIdMap, contentIdToFileContentMap, new PrintWriter(output), ""); fileNameToLibraryToContentIdMap, contentIdToFileContentMap,
assertThat(output.toString()).isEqualTo(EXPECTED_HTML_STRING); new PrintWriter(output), "");
assertThat(output.toString()).isEqualTo(EXPECTED_OLD_HTML_STRING);
}
@Test
public void testGenerateNewHtml() {
Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>();
Map<String, String> contentIdToFileContentMap = new HashMap<>();
Map<String, Set<String>> toBoth = new HashMap<>();
Map<String, Set<String>> toOne = new HashMap<>();
toBoth.put("libA", new HashSet<String>(Arrays.asList("0")));
toBoth.put("libB", new HashSet<String>(Arrays.asList("1")));
toOne.put("libA", new HashSet<String>(Arrays.asList("0")));
fileNameToLibraryToContentIdMap.put("/file0", toBoth);
fileNameToLibraryToContentIdMap.put("/file1", toOne);
contentIdToFileContentMap.put("0", "license content #0");
contentIdToFileContentMap.put("1", "license content #1");
StringWriter output = new StringWriter();
LicenseHtmlGeneratorFromXml.generateHtml(
fileNameToLibraryToContentIdMap, contentIdToFileContentMap,
new PrintWriter(output), "");
assertThat(output.toString()).isEqualTo(EXPECTED_NEW_HTML_STRING);
} }
@Test @Test
public void testGenerateHtmlWithCustomHeading() { public void testGenerateHtmlWithCustomHeading() {
Map<String, Set<String>> fileNameToContentIdMap = new HashMap<>(); Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>();
Map<String, String> contentIdToFileContentMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>();
Map<String, Set<String>> toBoth = new HashMap<>();
Map<String, Set<String>> toOne = new HashMap<>();
fileNameToContentIdMap.put("/file0", new HashSet<String>(Arrays.asList("0", "1"))); toBoth.put("", new HashSet<String>(Arrays.asList("0", "1")));
fileNameToContentIdMap.put("/file1", new HashSet<String>(Arrays.asList("0"))); toOne.put("", new HashSet<String>(Arrays.asList("0")));
fileNameToLibraryToContentIdMap.put("/file0", toBoth);
fileNameToLibraryToContentIdMap.put("/file1", toOne);
contentIdToFileContentMap.put("0", "license content #0"); contentIdToFileContentMap.put("0", "license content #0");
contentIdToFileContentMap.put("1", "license content #1"); contentIdToFileContentMap.put("1", "license content #1");
StringWriter output = new StringWriter(); StringWriter output = new StringWriter();
LicenseHtmlGeneratorFromXml.generateHtml( LicenseHtmlGeneratorFromXml.generateHtml(
fileNameToContentIdMap, contentIdToFileContentMap, new PrintWriter(output), fileNameToLibraryToContentIdMap, contentIdToFileContentMap,
HTML_CUSTOM_HEADING); new PrintWriter(output), HTML_CUSTOM_HEADING);
assertThat(output.toString()).isEqualTo(EXPECTED_HTML_STRING_WITH_CUSTOM_HEADING); assertThat(output.toString()).isEqualTo(EXPECTED_OLD_HTML_STRING_WITH_CUSTOM_HEADING);
}
@Test
public void testGenerateNewHtmlWithCustomHeading() {
Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>();
Map<String, String> contentIdToFileContentMap = new HashMap<>();
Map<String, Set<String>> toBoth = new HashMap<>();
Map<String, Set<String>> toOne = new HashMap<>();
toBoth.put("libA", new HashSet<String>(Arrays.asList("0")));
toBoth.put("libB", new HashSet<String>(Arrays.asList("1")));
toOne.put("libA", new HashSet<String>(Arrays.asList("0")));
fileNameToLibraryToContentIdMap.put("/file0", toBoth);
fileNameToLibraryToContentIdMap.put("/file1", toOne);
contentIdToFileContentMap.put("0", "license content #0");
contentIdToFileContentMap.put("1", "license content #1");
StringWriter output = new StringWriter();
LicenseHtmlGeneratorFromXml.generateHtml(
fileNameToLibraryToContentIdMap, contentIdToFileContentMap,
new PrintWriter(output), HTML_CUSTOM_HEADING);
assertThat(output.toString()).isEqualTo(EXPECTED_NEW_HTML_STRING_WITH_CUSTOM_HEADING);
} }
} }