blob: 3bc8e2b210965526299a5a56eb9cbd3a9ea0b2ab [file] [log] [blame]
/*
* Copyright (C) 2019 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.compat.annotation;
import static org.mockito.Mockito.when;
import static org.junit.Assert.assertThat;
import static org.hamcrest.core.StringStartsWith.startsWith;
import org.junit.Test;
import org.mockito.Mock;
import javax.annotation.processing.Filer;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import org.junit.Before;
import org.junit.Rule;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
public class XmlWriterTest {
private static final String HEADER =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
@Mock private Filer filer;
@Mock private FileObject fileObject;
private OutputStream outputStream = new ByteArrayOutputStream();
@Rule public MockitoRule rule = MockitoJUnit.rule();
@Before
public void setUp() throws Exception {
when(filer.createResource(StandardLocation.CLASS_OUTPUT, "package", "name")).thenReturn(
fileObject);
when(fileObject.openOutputStream()).thenReturn(outputStream);
}
@Test
public void testNoChanges() throws Exception {
XmlWriter writer = new XmlWriter();
writer.write("package", "name", filer);
String expected = HEADER + "<config/>";
assertThat(outputStream.toString(), startsWith(expected));
}
@Test
public void testOneChange() throws Exception {
XmlWriter writer = new XmlWriter();
Change c = new Change(123456789L, "change-name", false, null);
writer.addChange(c);
writer.write("package", "name", filer);
String expected = HEADER + "<config>"
+ "<compat-change id=\"123456789\" name=\"change-name\"/>"
+ "</config>";
assertThat(outputStream.toString(), startsWith(expected));
}
@Test
public void testSomeChanges() throws Exception {
XmlWriter writer = new XmlWriter();
Change c = new Change(111L, "change-name1", false, null);
Change disabled = new Change(222L, "change-name2", true, null);
Change sdkRestricted = new Change(333L, "change-name3", false, 28);
Change both = new Change(444L, "change-name4", true, 29);
writer.addChange(c);
writer.addChange(disabled);
writer.addChange(sdkRestricted);
writer.addChange(both);
writer.write("package", "name", filer);
String expected = HEADER + "<config>"
+ "<compat-change id=\"111\" name=\"change-name1\"/>"
+ "<compat-change disabled=\"true\" id=\"222\" name=\"change-name2\"/>"
+ "<compat-change enableAfterTargetSdk=\"28\" id=\"333\" name=\"change-name3\"/>"
+ "<compat-change disabled=\"true\" enableAfterTargetSdk=\"29\" id=\"444\" "
+ "name=\"change-name4\"/>"
+ "</config>";
assertThat(outputStream.toString(), startsWith(expected));
}
}