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 Change overridable = new Change.Builder() 92 .id(666L) 93 .name("change-name6") 94 .disabled() 95 .overridable() 96 .build(); 97 98 writer.addChange(c); 99 writer.addChange(disabled); 100 writer.addChange(sdkRestricted); 101 writer.addChange(both); 102 writer.addChange(loggingOnly); 103 writer.addChange(overridable); 104 writer.write(mOutputStream); 105 106 String expected = HEADER + "<config>" 107 + "<compat-change description=\"my nice change\" id=\"111\" name=\"change-name1\"/>" 108 + "<compat-change disabled=\"true\" id=\"222\" name=\"change-name2\"/>" 109 + "<compat-change enableAfterTargetSdk=\"28\" id=\"333\" " 110 + "name=\"change-name3\"/>" 111 + "<compat-change disabled=\"true\" enableAfterTargetSdk=\"29\" id=\"444\" " 112 + "name=\"change-name4\"/>" 113 + "<compat-change id=\"555\" loggingOnly=\"true\" name=\"change-name5\"/>" 114 + "<compat-change disabled=\"true\" id=\"666\" name=\"change-name6\" " 115 + "overridable=\"true\"/>" 116 + "</config>"; 117 118 assertThat(mOutputStream.toString(), startsWith(expected)); 119 } 120 } 121