/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings;
import static com.google.common.truth.Truth.assertThat;
import java.util.HashMap;
import java.util.Map;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.xmlpull.v1.XmlPullParserException;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class LicenseHtmlGeneratorFromXmlTest {
private static final String VAILD_XML_STRING =
"\n" +
"\n" +
"/file0\n" +
"/file1\n" +
"\n" +
"";
private static final String INVAILD_XML_STRING =
"\n" +
"\n" +
"/file0\n" +
"/file1\n" +
"\n" +
"";
private static final String EXPECTED_HTML_STRING =
"
\n" +
"\n" +
"" +
"\n" +
"\n" +
"\n" +
"\n" +
" Notices for file(s): \n" +
"\n" +
"/file0 \n" +
"/file1 \n" +
" \n" +
"\n" +
"license content #0\n" +
" \n" +
" |
\n" +
"
\n";
@Test
public void testParseValidXmlStream() throws XmlPullParserException, IOException {
Map fileNameToContentIdMap = new HashMap();
Map contentIdToFileContentMap = new HashMap();
LicenseHtmlGeneratorFromXml.parse(
new InputStreamReader(new ByteArrayInputStream(VAILD_XML_STRING.getBytes())),
fileNameToContentIdMap, contentIdToFileContentMap);
assertThat(fileNameToContentIdMap.size()).isEqualTo(2);
assertThat(fileNameToContentIdMap.get("/file0")).isEqualTo("0");
assertThat(fileNameToContentIdMap.get("/file1")).isEqualTo("0");
assertThat(contentIdToFileContentMap.size()).isEqualTo(1);
assertThat(contentIdToFileContentMap.get("0")).isEqualTo("license content #0");
}
@Test(expected = XmlPullParserException.class)
public void testParseInvalidXmlStream() throws XmlPullParserException, IOException {
Map fileNameToContentIdMap = new HashMap();
Map contentIdToFileContentMap = new HashMap();
LicenseHtmlGeneratorFromXml.parse(
new InputStreamReader(new ByteArrayInputStream(INVAILD_XML_STRING.getBytes())),
fileNameToContentIdMap, contentIdToFileContentMap);
}
@Test
public void testGenerateHtml() {
Map fileNameToContentIdMap = new HashMap();
Map contentIdToFileContentMap = new HashMap();
fileNameToContentIdMap.put("/file0", "0");
fileNameToContentIdMap.put("/file1", "0");
contentIdToFileContentMap.put("0", "license content #0");
StringWriter output = new StringWriter();
LicenseHtmlGeneratorFromXml.generateHtml(
fileNameToContentIdMap, contentIdToFileContentMap, new PrintWriter(output));
assertThat(output.toString()).isEqualTo(EXPECTED_HTML_STRING);
}
}