1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.class2nonsdklist; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.collect.ImmutableMap; 22 23 import org.junit.Before; 24 import org.junit.Test; 25 26 import java.io.ByteArrayOutputStream; 27 import java.util.HashSet; 28 import java.util.Map; 29 import java.util.Set; 30 31 32 public class AnnotationPropertyWriterTest { 33 34 private ByteArrayOutputStream mByteArrayOutputStream; 35 private AnnotationPropertyWriter mAnnotationPropertyWriter; 36 37 @Before setup()38 public void setup() { 39 mByteArrayOutputStream = new ByteArrayOutputStream(); 40 mAnnotationPropertyWriter = new AnnotationPropertyWriter(mByteArrayOutputStream); 41 } 42 43 @Test testExportPropertiesNoEscaping()44 public void testExportPropertiesNoEscaping() { 45 String signature = "foo"; 46 Map<String, String> annotationProperties = ImmutableMap.of( 47 "prop", "val" 48 ); 49 Set<String> parsedFlags = new HashSet<String>(); 50 mAnnotationPropertyWriter.consume(signature, annotationProperties, parsedFlags); 51 mAnnotationPropertyWriter.close(); 52 53 String output = mByteArrayOutputStream.toString(); 54 String expected = "signature,prop\n" 55 + "|foo|,|val|\n"; 56 assertThat(output).isEqualTo(expected); 57 } 58 59 @Test testExportPropertiesEscapeQuotes()60 public void testExportPropertiesEscapeQuotes() { 61 String signature = "foo"; 62 Map<String, String> annotationProperties = ImmutableMap.of( 63 "prop", "val1 | val2 | val3" 64 ); 65 Set<String> parsedFlags = new HashSet<String>(); 66 mAnnotationPropertyWriter.consume(signature, annotationProperties, parsedFlags); 67 mAnnotationPropertyWriter.close(); 68 69 String output = mByteArrayOutputStream.toString(); 70 String expected = "signature,prop\n" 71 + "|foo|,|val1 || val2 || val3|\n"; 72 assertThat(output).isEqualTo(expected); 73 } 74 } 75