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 android.processor.compat.changeid; 18 19 import static org.hamcrest.core.StringStartsWith.startsWith; 20 import static org.junit.Assert.assertThat; 21 22 import org.junit.Test; 23 24 import java.io.ByteArrayOutputStream; 25 import java.io.OutputStream; 26 27 public class XmlWriterTest { 28 29 private static final String HEADER = 30 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"; 31 32 private OutputStream mOutputStream = new ByteArrayOutputStream(); 33 34 @Test testNoChanges()35 public void testNoChanges() throws Exception { 36 XmlWriter writer = new XmlWriter(); 37 writer.write(mOutputStream); 38 39 String expected = HEADER + "<config/>"; 40 41 assertThat(mOutputStream.toString(), startsWith(expected)); 42 } 43 44 @Test testOneChange()45 public void testOneChange() throws Exception { 46 XmlWriter writer = new XmlWriter(); 47 Change c = new Change.Builder() 48 .id(123456789L) 49 .name("change-name") 50 .build(); 51 52 writer.addChange(c); 53 writer.write(mOutputStream); 54 55 String expected = HEADER + "<config>" 56 + "<compat-change id=\"123456789\" name=\"change-name\"/>" 57 + "</config>"; 58 59 assertThat(mOutputStream.toString(), startsWith(expected)); 60 } 61 62 @Test testSomeChanges()63 public void testSomeChanges() throws Exception { 64 XmlWriter writer = new XmlWriter(); 65 Change c = new Change.Builder() 66 .id(111L) 67 .name("change-name1") 68 .description("my nice change") 69 .build(); 70 Change disabled = new Change.Builder() 71 .id(222L) 72 .name("change-name2") 73 .disabled() 74 .build(); 75 Change sdkRestricted = new Change.Builder() 76 .id(333L) 77 .name("change-name3") 78 .enabledAfter(28) 79 .build(); 80 Change both = new Change.Builder() 81 .id(444L) 82 .name("change-name4") 83 .disabled() 84 .enabledAfter(29) 85 .build(); 86 Change loggingOnly = new Change.Builder() 87 .id(555L) 88 .name("change-name5") 89 .loggingOnly() 90 .build(); 91 92 writer.addChange(c); 93 writer.addChange(disabled); 94 writer.addChange(sdkRestricted); 95 writer.addChange(both); 96 writer.addChange(loggingOnly); 97 writer.write(mOutputStream); 98 99 String expected = HEADER + "<config>" 100 + "<compat-change description=\"my nice change\" id=\"111\" name=\"change-name1\"/>" 101 + "<compat-change disabled=\"true\" id=\"222\" name=\"change-name2\"/>" 102 + "<compat-change enableAfterTargetSdk=\"28\" id=\"333\" " 103 + "name=\"change-name3\"/>" 104 + "<compat-change disabled=\"true\" enableAfterTargetSdk=\"29\" id=\"444\" " 105 + "name=\"change-name4\"/>" 106 + "<compat-change id=\"555\" loggingOnly=\"true\" name=\"change-name5\"/>" 107 + "</config>"; 108 109 assertThat(mOutputStream.toString(), startsWith(expected)); 110 } 111 } 112