diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2021-11-11 13:07:00 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2021-11-11 13:07:00 +0000 |
commit | de47e225a2e8580025b5ce784a9d3d0d56f2086d (patch) | |
tree | 5e0ce514c085822a9a9326b7738894643a452c63 | |
parent | 61e780823a0b62fda397be8acd0a3f141fcf460c (diff) | |
parent | 8a12961f0238786c7aa3b6c15f8faf78688408c4 (diff) | |
download | libcore-android-mainline-12.0.0_r49.tar.gz |
Snap for 7905862 from 8a12961f0238786c7aa3b6c15f8faf78688408c4 to mainline-networking-releaseandroid-mainline-12.0.0_r49android12-mainline-networkstack-release
Change-Id: Ie28cb35dec11e01793c049cd20fd3f1011e512f9
69 files changed, 6041 insertions, 32 deletions
diff --git a/luni/src/test/java/libcore/android/compat/CompatibilityTest.java b/luni/src/test/java/libcore/android/compat/CompatibilityTest.java new file mode 100644 index 00000000000..40ed9e6cf4f --- /dev/null +++ b/luni/src/test/java/libcore/android/compat/CompatibilityTest.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2021 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 libcore.android.compat; + +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import android.compat.Compatibility; +import android.compat.Compatibility.BehaviorChangeDelegate; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CompatibilityTest { + + @Test + public void testBehaviorChangeDelegate() { + long changeId = 1L; + BehaviorChangeDelegate defaultDelegate = new BehaviorChangeDelegate() {}; + + // default implementation should not throw exception + defaultDelegate.onChangeReported(changeId); + assertTrue(defaultDelegate.isChangeEnabled(changeId)); + } + + @Test + public void testReportUnconditionalChange() { + long changeId = 1L; + BehaviorChangeDelegate delegate = mock(BehaviorChangeDelegate.class); + Compatibility.setBehaviorChangeDelegate(delegate); + + Compatibility.reportUnconditionalChange(changeId); + verify(delegate).onChangeReported(changeId); + } + + @Test + public void testClearBehaviorChangeDelegate() { + long changeId = 1L; + BehaviorChangeDelegate delegate = mock(BehaviorChangeDelegate.class); + Compatibility.setBehaviorChangeDelegate(delegate); + + Compatibility.clearBehaviorChangeDelegate(); + Compatibility.reportUnconditionalChange(changeId); + verify(delegate, never()).onChangeReported(anyLong()); + } +} diff --git a/luni/src/test/java/libcore/java/io/ConsoleTest.java b/luni/src/test/java/libcore/java/io/ConsoleTest.java new file mode 100644 index 00000000000..36d382f08dd --- /dev/null +++ b/luni/src/test/java/libcore/java/io/ConsoleTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2008 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 libcore.java.io; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.Console; +import java.io.IOError; +import java.io.InputStream; +import java.io.OutputStream; +import java.lang.reflect.Constructor; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class ConsoleTest { + + private Console createConsole(InputStream inStream, OutputStream outStream) throws Exception { + Constructor<Console> constructor = + Console.class.getDeclaredConstructor(InputStream.class, OutputStream.class); + constructor.setAccessible(true); + return constructor.newInstance(inStream, outStream); + } + + private boolean isTty() { + // It is important for some tests to know if it is running in an environment where tty is + // available or not. This is because some API calls end up calling the native echo() + // function which throws an exception if tty is not available. + // The Console.isTty() method is private, however an option to check, without reflection, if + // tty is available is to see if console() return null or not. The function will always + // return an instance of Console if isTty() is true, or null otherwise. + return (Console.console() != null); + } + + @Test + public void testReadPassword() throws Exception { + final byte[] bytes = "secret password\n".getBytes(); + ByteArrayInputStream inStream = new ByteArrayInputStream(bytes); + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); + Console console = createConsole(inStream, outStream); + try { + String password = String.valueOf(console.readPassword()); + // Due to readPassword depending on echo, which depends on having stdin as a tty, it is + // expected that it will throw an IOError if tty is not available + assertTrue("readPassword succeeded unexpectedly", isTty()); + assertEquals("secret password", password); + } catch(IOError e) { + assertFalse("readPassword threw unexpected IOError", isTty()); + assertTrue("readPassword exception not as expected", + e.getMessage().contains("Inappropriate ioctl for device")); + } + } + + @Test + public void testReadPasswordWithPrompt() throws Exception { + final byte[] bytes = "secret password\n".getBytes(); + ByteArrayInputStream inStream = new ByteArrayInputStream(bytes); + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); + Console console = createConsole(inStream, outStream); + String username = "Alice"; + try { + String password = String.valueOf( + console.readPassword("%s, please enter your password:", username)); + // Due to readPassword depending on echo, which depends on having stdin as a tty, it is + // expected that it will throw an IOError if tty is not available + assertTrue("readPassword succeeded unexpectedly", isTty()); + assertEquals("secret password", password); + String prompt = new String(((ByteArrayOutputStream) outStream).toByteArray()); + assertEquals("Alice, please enter your password:", prompt); + } catch(IOError e) { + assertFalse("readPassword threw unexpected IOError", isTty()); + assertTrue("readPassword exception not as expected", + e.getMessage().contains("Inappropriate ioctl for device")); + } + } + +} diff --git a/luni/src/test/java/libcore/java/io/FileDescriptorTest.java b/luni/src/test/java/libcore/java/io/FileDescriptorTest.java index 390cfd650bc..f4ed41d0de2 100644 --- a/luni/src/test/java/libcore/java/io/FileDescriptorTest.java +++ b/luni/src/test/java/libcore/java/io/FileDescriptorTest.java @@ -72,4 +72,11 @@ public class FileDescriptorTest extends TestCase { Os.close(target); } } + + public void testGetSetInt$() { + FileDescriptor fd = new FileDescriptor(); + assertEquals(-1, fd.getInt$()); + fd.setInt$(42); + assertEquals(42, fd.getInt$()); + } } diff --git a/luni/src/test/java/libcore/java/io/FilePermissionTest.java b/luni/src/test/java/libcore/java/io/FilePermissionTest.java new file mode 100644 index 00000000000..cae7b06e8c4 --- /dev/null +++ b/luni/src/test/java/libcore/java/io/FilePermissionTest.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2008 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 libcore.java.io; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.FilePermission; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class FilePermissionTest { + + @Test + public void testImplies() { + FilePermission permission = new FilePermission("none", null); + // Implementation is stubbed in Android + assertTrue(permission.implies(null)); + } + + @Test + public void testGetActions() { + FilePermission permission = new FilePermission("none", null); + // Implementation is stubbed in Android + assertNull(permission.getActions()); + } +} diff --git a/luni/src/test/java/libcore/java/io/OldFileWriterTest.java b/luni/src/test/java/libcore/java/io/OldFileWriterTest.java index 1db9a3eb288..c68c92e4b68 100644 --- a/luni/src/test/java/libcore/java/io/OldFileWriterTest.java +++ b/luni/src/test/java/libcore/java/io/OldFileWriterTest.java @@ -20,6 +20,7 @@ package libcore.java.io; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; @@ -77,7 +78,7 @@ public class OldFileWriterTest extends TestCase { } } - public void test_handleEarlyEOFChar_1() { + public void test_handleEarlyEOFChar_1() throws IOException { String str = "All work and no play makes Jack a dull boy\n"; int NUMBER = 2048; int j = 0; @@ -91,27 +92,23 @@ public class OldFileWriterTest extends TestCase { } File f = null; FileWriter fw = null; - try { - f = File.createTempFile("ony", "by_one"); - fw = new FileWriter(f); - fw.write(strChars); - fw.close(); - InputStreamReader in = null; - FileInputStream fis = new FileInputStream(f); - in = new InputStreamReader(fis); - int b; - int errors = 0; - for (int offset = 0; offset < strChars.length; ++offset) { - b = in.read(); - if (b == -1) { - fail("Early EOF at offset " + offset + "\n"); - return; - } + f = File.createTempFile("ony", "by_one"); + fw = new FileWriter(f); + fw.write(strChars); + fw.close(); + InputStreamReader in = null; + FileInputStream fis = new FileInputStream(f); + in = new InputStreamReader(fis); + int b; + int errors = 0; + for (int offset = 0; offset < strChars.length; ++offset) { + b = in.read(); + if (b == -1) { + fail("Early EOF at offset " + offset + "\n"); + return; } - assertEquals(0, errors); - } catch (IOException e) { - e.printStackTrace(); } + assertEquals(0, errors); } public void test_handleEarlyEOFChar_2() throws IOException { @@ -141,6 +138,40 @@ public class OldFileWriterTest extends TestCase { assertEquals(inputStr, outStr); } + public void test_handleExternalFileDescriptor() throws IOException { + String str = "All work and no play makes Jack a dull boy\n"; + int NUMBER = 2048; + int j = 0; + int len = str.length() * NUMBER; + /* == 88064 *//* NUMBER compulsively written copies of the same string */ + char[] strChars = new char[len]; + for (int i = 0; i < NUMBER; ++i) { + for (int k = 0; k < str.length(); ++k) { + strChars[j++] = str.charAt(k); + } + } + File f = null; + FileWriter fw = null; + f = File.createTempFile("ony", "by_one"); + FileOutputStream stream = new FileOutputStream(f); + fw = new FileWriter(stream.getFD()); + fw.write(strChars); + fw.close(); + InputStreamReader in = null; + FileInputStream fis = new FileInputStream(f); + in = new InputStreamReader(fis); + int b; + int errors = 0; + for (int offset = 0; offset < strChars.length; ++offset) { + b = in.read(); + if (b == -1) { + fail("Early EOF at offset " + offset + "\n"); + return; + } + } + assertEquals(0, errors); + } + protected void setUp() throws Exception { f = File.createTempFile("writer", ".tst"); diff --git a/luni/src/test/java/libcore/java/io/PrintWriterTest.java b/luni/src/test/java/libcore/java/io/PrintWriterTest.java new file mode 100644 index 00000000000..cf3b86ae5ca --- /dev/null +++ b/luni/src/test/java/libcore/java/io/PrintWriterTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2008 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 libcore.java.io; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintWriter; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class PrintWriterTest { + + private class Writer extends PrintWriter { + + Writer(File f) throws FileNotFoundException { + super(f); + } + + public void setError() { + super.setError(); + } + + public void clearError() { + super.clearError(); + } + } + + @Test + public void testSetError() throws IOException { + File f = File.createTempFile("tmp", "tst"); + Writer writer = new Writer(f); + assertFalse(writer.checkError()); + writer.setError(); + assertTrue(writer.checkError()); + writer.clearError(); + assertFalse(writer.checkError()); + } +} diff --git a/luni/src/test/java/libcore/java/io/SerializablePermissionTest.java b/luni/src/test/java/libcore/java/io/SerializablePermissionTest.java new file mode 100644 index 00000000000..29c0b961b94 --- /dev/null +++ b/luni/src/test/java/libcore/java/io/SerializablePermissionTest.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2008 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 libcore.java.io; + +import static org.junit.Assert.assertEquals; + +import java.io.SerializablePermission; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class SerializablePermissionTest { + + @Test + public void testConstructorWithActions() { + SerializablePermission permission = new SerializablePermission("none", "no action"); + // Implementation is stubbed in Android + assertEquals("", permission.getName()); + } +} diff --git a/luni/src/test/java/libcore/java/io/StreamCorruptedExceptionTest.java b/luni/src/test/java/libcore/java/io/StreamCorruptedExceptionTest.java new file mode 100644 index 00000000000..61307f81559 --- /dev/null +++ b/luni/src/test/java/libcore/java/io/StreamCorruptedExceptionTest.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2008 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 libcore.java.io; + +import static org.junit.Assert.assertNull; + +import java.io.StreamCorruptedException; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class StreamCorruptedExceptionTest { + + @Test + public void testEmptyConstructor() { + StreamCorruptedException exception = new StreamCorruptedException(); + assertNull(exception.getMessage()); + assertNull(exception.getCause()); + } +} diff --git a/luni/src/test/java/libcore/java/io/UTFDataFormatExceptionTest.java b/luni/src/test/java/libcore/java/io/UTFDataFormatExceptionTest.java new file mode 100644 index 00000000000..f3102a2c7c2 --- /dev/null +++ b/luni/src/test/java/libcore/java/io/UTFDataFormatExceptionTest.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2008 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 libcore.java.io; + +import static org.junit.Assert.assertNull; + +import java.io.UTFDataFormatException; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class UTFDataFormatExceptionTest { + + @Test + public void testEmptyConstructor() { + UTFDataFormatException exception = new UTFDataFormatException(); + assertNull(exception.getMessage()); + assertNull(exception.getCause()); + } +} diff --git a/luni/src/test/java/libcore/java/io/UnsupportedEncodingExceptionTest.java b/luni/src/test/java/libcore/java/io/UnsupportedEncodingExceptionTest.java new file mode 100644 index 00000000000..d79116730c8 --- /dev/null +++ b/luni/src/test/java/libcore/java/io/UnsupportedEncodingExceptionTest.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2008 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 libcore.java.io; + +import static org.junit.Assert.assertNull; + +import java.io.UnsupportedEncodingException; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class UnsupportedEncodingExceptionTest { + + @Test + public void testEmptyConstructor() { + UnsupportedEncodingException exception = new UnsupportedEncodingException(); + assertNull(exception.getMessage()); + assertNull(exception.getCause()); + } +} diff --git a/luni/src/test/java/libcore/java/lang/invoke/CallSitesTest.java b/luni/src/test/java/libcore/java/lang/invoke/CallSitesTest.java index 5debdcb1ffe..559eb99143e 100644 --- a/luni/src/test/java/libcore/java/lang/invoke/CallSitesTest.java +++ b/luni/src/test/java/libcore/java/lang/invoke/CallSitesTest.java @@ -31,14 +31,22 @@ import java.lang.invoke.WrongMethodTypeException; import static java.lang.invoke.MethodHandles.Lookup.*; public class CallSitesTest extends TestCase { - public void test_ConstantCallSite() throws Throwable { - final MethodType type = MethodType.methodType(int.class, int.class, int.class); - final MethodHandle mh = - MethodHandles.lookup().findStatic(CallSitesTest.class, "add2", type); - final ConstantCallSite site = new ConstantCallSite(mh); - assertEquals(mh, site.getTarget()); - assertEquals(type, site.type()); + public void test_ConstantCallSiteConstructorNullMethodHandle() throws Throwable { + try { + ConstantCallSite site = new ConstantCallSite((MethodHandle) null); + fail(); + } catch (NullPointerException e) { + } + } + + private static MethodHandle methodHandleForAdd2() throws Throwable { + final MethodType mt = MethodType.methodType(int.class, int.class, int.class); + return MethodHandles.lookup().findStatic(CallSitesTest.class, "add2", mt); + } + public void test_ConstantCallSite() throws Throwable { + final MethodHandle mh = methodHandleForAdd2(); + ConstantCallSite site = new ConstantCallSite(mh); int n = (int) site.dynamicInvoker().invokeExact(7, 37); assertEquals(44, n); try { @@ -48,6 +56,147 @@ public class CallSitesTest extends TestCase { } } + static class OurConstantCallSite extends ConstantCallSite { + OurConstantCallSite(MethodType targetType, MethodHandle createTargetHook) throws Throwable { + super(targetType, createTargetHook); + } + + static MethodHandle createTargetHook(OurConstantCallSite callSite) throws Throwable { + final MethodType add2MethodType = + MethodType.methodType(int.class, int.class, int.class); + return MethodHandles.lookup().findStatic(CallSitesTest.class, "add2", add2MethodType); + } + } + + public void test_ConstantCallSiteWithHook() throws Throwable { + final MethodType targetType = + MethodType.methodType(int.class, int.class, int.class); + MethodHandle createTargetHook = + MethodHandles.lookup().findStatic(OurConstantCallSite.class, "createTargetHook", + MethodType.methodType(MethodHandle.class, + OurConstantCallSite.class)); + OurConstantCallSite callSite = new OurConstantCallSite(targetType, createTargetHook); + int x = (int) callSite.getTarget().invoke(1, 2); + assertEquals(3, x); + } + + public void test_MutableCallSiteConstructorNullMethodType() throws Throwable { + try { + MutableCallSite callSite = new MutableCallSite((MethodType) null); + fail(); + } catch (NullPointerException e) { + } + } + + public void test_MutableCallSiteConstructorNullMethodHandle() throws Throwable { + try { + MutableCallSite callSite = new MutableCallSite((MethodHandle) null); + fail(); + } catch (NullPointerException e) { + } + } + + public void test_MutableCallsiteNoMethodHandle() throws Throwable { + try { + MutableCallSite callSite = + new MutableCallSite(MethodType.methodType(int.class, int.class, int.class)); + int result = (int) callSite.getTarget().invokeExact(1, 2); + fail(); + } catch (IllegalStateException e) { + } + } + + public void test_MutableCallSite() throws Throwable { + final MethodHandle mh = methodHandleForAdd2(); + final MutableCallSite site = new MutableCallSite(mh); + + // Invocation test + int n = (int) site.getTarget().invokeExact(7, 37); + assertEquals(44, n); + + // setTarget() tests + try { + final MethodType mt_add3 = + MethodType.methodType(int.class, int.class, int.class, int.class); + final MethodHandle mh_add3 = + MethodHandles.lookup().findStatic(CallSitesTest.class, "add3", mt_add3); + + site.setTarget(mh_add3); + fail(); + } catch (WrongMethodTypeException e) { + } + try { + site.setTarget(null); + fail(); + } catch (NullPointerException e) { + } + final MethodHandle mh_sub2 = + MethodHandles.lookup().findStatic(CallSitesTest.class, "sub2", + MethodType.methodType(int.class, int.class, int.class)); + site.setTarget(mh_sub2); + n = (int) site.getTarget().invokeExact(7, 37); + assertEquals(-30, n); + } + + public void test_VolatileCallSiteConstructorNullMethodType() throws Throwable { + try { + VolatileCallSite vc = new VolatileCallSite((MethodType) null); + fail(); + } catch (NullPointerException e) { + } + } + + public void test_VolatileCallSiteConstructorNullMethodHandle() throws Throwable { + try { + VolatileCallSite vc = new VolatileCallSite((MethodHandle) null); + fail(); + } catch (NullPointerException e) { + } + } + + public void test_VolatileCallsiteNoMethodHandle() throws Throwable { + try { + VolatileCallSite vc = + new VolatileCallSite(MethodType.methodType(int.class, int.class, int.class)); + int result = (int) vc.getTarget().invokeExact(1, 2); + fail(); + } catch (IllegalStateException e) { + } + } + + public void test_VolatileCallSite() throws Throwable { + final MethodHandle mh = methodHandleForAdd2(); + final VolatileCallSite site = new VolatileCallSite(mh); + + // Invocation test + int n = (int) site.getTarget().invokeExact(7, 37); + assertEquals(44, n); + + // setTarget() tests + try { + final MethodType mt_add3 = + MethodType.methodType(int.class, int.class, int.class, int.class); + final MethodHandle mh_add3 = + MethodHandles.lookup().findStatic(CallSitesTest.class, "add3", mt_add3); + site.setTarget(mh_add3); + fail(); + } catch (WrongMethodTypeException e) { + } + try { + site.setTarget(null); + fail(); + } catch (NullPointerException e) { + } + + // One last invocation test + final MethodHandle mh_sub2 = + MethodHandles.lookup().findStatic(CallSitesTest.class, "sub2", + MethodType.methodType(int.class, int.class, int.class)); + site.setTarget(mh_sub2); + n = (int) site.getTarget().invokeExact(7, 37); + assertEquals(-30, n); + } + public void test_EarlyBoundMutableCallSite() throws Throwable { final MethodType type = MethodType.methodType(int.class, int.class, int.class); final MethodHandle add2 = @@ -97,7 +246,7 @@ public class CallSitesTest extends TestCase { } private static void commonMutableCallSitesTest(CallSite site, - MethodHandle firstTarget) throws Throwable{ + MethodHandle firstTarget) throws Throwable { site.setTarget(firstTarget); site.setTarget(firstTarget); @@ -133,15 +282,15 @@ public class CallSitesTest extends TestCase { assertEquals(100, (int) site.dynamicInvoker().invokeExact(147, 47)); } - private static int add2(int x, int y) { + public static int add2(int x, int y) { return x + y; } - private static int add3(int x, int y, int z) { + public static int add3(int x, int y, int z) { return x + y + z; } - private static int sub2(int x, int y) { + public static int sub2(int x, int y) { return x - y; } } diff --git a/luni/src/test/java/libcore/java/lang/invoke/LambdaConversionExceptionTest.java b/luni/src/test/java/libcore/java/lang/invoke/LambdaConversionExceptionTest.java new file mode 100644 index 00000000000..06a923aca41 --- /dev/null +++ b/luni/src/test/java/libcore/java/lang/invoke/LambdaConversionExceptionTest.java @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2021 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 libcore.java.lang.invoke; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.lang.invoke.LambdaConversionException; + +@RunWith(JUnit4.class) +public class LambdaConversionExceptionTest { + private static final String MESSAGE = "message"; + + @Test + public void constructor() { + try { + throw new LambdaConversionException(); + } catch (LambdaConversionException e) { + assertNull(e.getMessage()); + assertNull(e.getCause()); + } + } + + @Test + public void constructorLString() { + try { + throw new LambdaConversionException(MESSAGE); + } catch (LambdaConversionException e) { + assertEquals(MESSAGE, e.getMessage()); + assertNull(e.getCause()); + } + } + + @Test + public void constructorLStringLThrowable() { + try { + try { + throw new IllegalArgumentException(); + } catch (Throwable t) { + throw new LambdaConversionException(MESSAGE, t); + } + } catch (LambdaConversionException e) { + assertEquals(MESSAGE, e.getMessage()); + assertTrue(e.getCause() instanceof IllegalArgumentException); + } + } + + @Test + public void constructorLThrowable() { + try { + try { + throw new IllegalArgumentException(); + } catch (Throwable t) { + throw new LambdaConversionException(t); + } + } catch (LambdaConversionException e) { + assertEquals("java.lang.IllegalArgumentException", e.getMessage()); + assertTrue(e.getCause() instanceof IllegalArgumentException); + } + + try { + throw new LambdaConversionException((Throwable) null); + } catch (LambdaConversionException e) { + assertNull(e.getMessage()); + assertNull(e.getCause()); + } + } + + @Test + public void constructorWithSuppressionsAndWritableStackTraces() { + boolean trueAndFalse[] = { true, false }; + + for (boolean enableSuppression : trueAndFalse) { + for (boolean writableStackTrace : trueAndFalse) { + try { + throw new LambdaConversionException(MESSAGE, null, enableSuppression, + writableStackTrace); + } catch (LambdaConversionException e) { + assertEquals(MESSAGE, e.getMessage()); + + // Check if exceptions can be suppressed. + e.addSuppressed(new LambdaConversionException()); + boolean haveSuppressed = (e.getSuppressed().length == 1); + assertEquals(enableSuppression, haveSuppressed); + + // Check if stack trace is writable. + boolean stackTraceHasElements = (0 != e.getStackTrace().length); + assertEquals(writableStackTrace, stackTraceHasElements); + if (stackTraceHasElements) { + // Only a writable stack trace has elements present. Now re-write it. + e.setStackTrace(new StackTraceElement[0]); + boolean wroteStackTrace = (e.getStackTrace().length == 0); + assertEquals(writableStackTrace, wroteStackTrace); + } + } + } + } + } +} diff --git a/luni/src/test/java/libcore/java/lang/invoke/MethodHandleInfoTest.java b/luni/src/test/java/libcore/java/lang/invoke/MethodHandleInfoTest.java index 12b82b05f60..a4e62f00f1e 100644 --- a/luni/src/test/java/libcore/java/lang/invoke/MethodHandleInfoTest.java +++ b/luni/src/test/java/libcore/java/lang/invoke/MethodHandleInfoTest.java @@ -54,6 +54,25 @@ public class MethodHandleInfoTest extends TestCase { } } + public void test_constants() { + final int [] REF_CONSTANTS = { + 0, // Not used + MethodHandleInfo.REF_getField, // 1 + MethodHandleInfo.REF_getStatic, // 2 + MethodHandleInfo.REF_putField, // 3 + MethodHandleInfo.REF_putStatic, // 4 + MethodHandleInfo.REF_invokeVirtual, // 5 + MethodHandleInfo.REF_invokeStatic, // 6 + MethodHandleInfo.REF_invokeSpecial, // 7 + MethodHandleInfo.REF_newInvokeSpecial, // 8 + MethodHandleInfo.REF_invokeInterface, // 9 + }; + assertEquals(10, REF_CONSTANTS.length); + for (int i = 0; i < REF_CONSTANTS.length; ++i) { + assertEquals(i, REF_CONSTANTS[i]); + } + } + public void test_referenceKindToString() { assertEquals("getField", referenceKindToString(REF_getField)); assertEquals("getStatic", referenceKindToString(REF_getStatic)); @@ -77,4 +96,46 @@ public class MethodHandleInfoTest extends TestCase { } catch (IllegalArgumentException expected) { } } + + public void test_refKindIsField() { + assertTrue(MethodHandleInfo.refKindIsField(MethodHandleInfo.REF_getField)); + assertTrue(MethodHandleInfo.refKindIsField(MethodHandleInfo.REF_getStatic)); + assertTrue(MethodHandleInfo.refKindIsField(MethodHandleInfo.REF_putField)); + assertTrue(MethodHandleInfo.refKindIsField(MethodHandleInfo.REF_putStatic)); + + assertFalse(MethodHandleInfo.refKindIsField(MethodHandleInfo.REF_invokeVirtual)); + assertFalse(MethodHandleInfo.refKindIsField(MethodHandleInfo.REF_invokeStatic)); + assertFalse(MethodHandleInfo.refKindIsField(MethodHandleInfo.REF_invokeSpecial)); + assertFalse(MethodHandleInfo.refKindIsField(MethodHandleInfo.REF_newInvokeSpecial)); + assertFalse(MethodHandleInfo.refKindIsField(MethodHandleInfo.REF_invokeInterface)); + } + + public void test_refKindIsValid() { + assertTrue(MethodHandleInfo.refKindIsValid(MethodHandleInfo.REF_getField)); + assertTrue(MethodHandleInfo.refKindIsValid(MethodHandleInfo.REF_getStatic)); + assertTrue(MethodHandleInfo.refKindIsValid(MethodHandleInfo.REF_putField)); + assertTrue(MethodHandleInfo.refKindIsValid(MethodHandleInfo.REF_putStatic)); + assertTrue(MethodHandleInfo.refKindIsValid(MethodHandleInfo.REF_invokeVirtual)); + assertTrue(MethodHandleInfo.refKindIsValid(MethodHandleInfo.REF_invokeStatic)); + assertTrue(MethodHandleInfo.refKindIsValid(MethodHandleInfo.REF_invokeSpecial)); + assertTrue(MethodHandleInfo.refKindIsValid(MethodHandleInfo.REF_newInvokeSpecial)); + assertTrue(MethodHandleInfo.refKindIsValid(MethodHandleInfo.REF_invokeInterface)); + } + + public void test_refKindName() { + assertEquals("getField", MethodHandleInfo.refKindName(MethodHandleInfo.REF_getField)); + assertEquals("getStatic", MethodHandleInfo.refKindName(MethodHandleInfo.REF_getStatic)); + assertEquals("putField", MethodHandleInfo.refKindName(MethodHandleInfo.REF_putField)); + assertEquals("putStatic", MethodHandleInfo.refKindName(MethodHandleInfo.REF_putStatic)); + assertEquals("invokeVirtual", + MethodHandleInfo.refKindName(MethodHandleInfo.REF_invokeVirtual)); + assertEquals("invokeStatic", + MethodHandleInfo.refKindName(MethodHandleInfo.REF_invokeStatic)); + assertEquals("invokeSpecial", + MethodHandleInfo.refKindName(MethodHandleInfo.REF_invokeSpecial)); + assertEquals("newInvokeSpecial", + MethodHandleInfo.refKindName(MethodHandleInfo.REF_newInvokeSpecial)); + assertEquals("invokeInterface", + MethodHandleInfo.refKindName(MethodHandleInfo.REF_invokeInterface)); + } } diff --git a/luni/src/test/java/libcore/java/lang/invoke/WrongMethodTypeExceptionTest.java b/luni/src/test/java/libcore/java/lang/invoke/WrongMethodTypeExceptionTest.java new file mode 100644 index 00000000000..d9288d28b62 --- /dev/null +++ b/luni/src/test/java/libcore/java/lang/invoke/WrongMethodTypeExceptionTest.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2021 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 libcore.java.lang.invoke; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.lang.invoke.WrongMethodTypeException; + +@RunWith(JUnit4.class) +public class WrongMethodTypeExceptionTest { + private static final String MESSAGE = "message"; + + @Test + public void constructor() { + try { + throw new WrongMethodTypeException(); + } catch (WrongMethodTypeException e) { + assertNull(e.getCause()); + assertNull(e.getMessage()); + } + } + + @Test + public void constructorLString() { + try { + throw new WrongMethodTypeException(MESSAGE); + } catch (WrongMethodTypeException e) { + assertNull(e.getCause()); + assertEquals(MESSAGE, e.getMessage()); + } + } +} diff --git a/luni/src/test/java/libcore/java/lang/reflect/GenericSignatureFormatErrorTest.java b/luni/src/test/java/libcore/java/lang/reflect/GenericSignatureFormatErrorTest.java new file mode 100644 index 00000000000..8324aa4735a --- /dev/null +++ b/luni/src/test/java/libcore/java/lang/reflect/GenericSignatureFormatErrorTest.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2021 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 libcore.java.lang.reflect; + +import java.lang.reflect.GenericSignatureFormatError; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GenericSignatureFormatErrorTest { + private static final String MESSAGE = "message"; + + @Test + public void constructor() { + try { + throw new GenericSignatureFormatError(); + } catch (GenericSignatureFormatError e) { + assertNull(e.getCause()); + assertNull(e.getMessage()); + } + } + + @Test + public void constructorLString() { + try { + throw new GenericSignatureFormatError(MESSAGE); + } catch (GenericSignatureFormatError e) { + assertNull(e.getCause()); + assertEquals(MESSAGE, e.getMessage()); + } + } +} diff --git a/luni/src/test/java/libcore/java/lang/reflect/ReflectPermissionTest.java b/luni/src/test/java/libcore/java/lang/reflect/ReflectPermissionTest.java new file mode 100644 index 00000000000..1f788fea50f --- /dev/null +++ b/luni/src/test/java/libcore/java/lang/reflect/ReflectPermissionTest.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2021 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 libcore.java.lang.reflect; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.lang.reflect.ReflectPermission; + +@RunWith(JUnit4.class) +public class ReflectPermissionTest { + private static final String NAME = "message"; + private static final String ACTIONS = "No action"; + + private void checkFieldsAreEmpty(ReflectPermission permission) { + // The super-class of ReflectPermission overrides the name and actions provided to + // the ReflectPermission constructors with empty strings. + // + // This is legacy code that is not intended for use. + assertTrue(permission.getName().isEmpty()); + assertTrue(permission.getActions().isEmpty()); + } + + @Test + public void reflectPermissionWithName() { + ReflectPermission permission = new ReflectPermission(NAME); + checkFieldsAreEmpty(permission); + } + + @Test + public void reflectPermissionWithNameAndActions() { + ReflectPermission permission = new ReflectPermission(NAME, ACTIONS); + checkFieldsAreEmpty(permission); + } +} diff --git a/luni/src/test/java/libcore/java/net/AbstractCookiesTest.java b/luni/src/test/java/libcore/java/net/AbstractCookiesTest.java index 7c472063f70..1cc25bf390c 100644 --- a/luni/src/test/java/libcore/java/net/AbstractCookiesTest.java +++ b/luni/src/test/java/libcore/java/net/AbstractCookiesTest.java @@ -726,6 +726,13 @@ public abstract class AbstractCookiesTest extends TestCase { assertNotEquals(createCookie("theme", "light", "a.com", "/path"), baseCookie); } + public void testIsHttpOnly() throws Exception { + HttpCookie cookie = createCookie("a", "android", "a.com", "/"); + assertFalse(cookie.isHttpOnly()); + cookie.setHttpOnly(true); + assertTrue(cookie.isHttpOnly()); + } + private static void assertNotEquals(HttpCookie one, HttpCookie two) { assertFalse(one.equals(two)); assertFalse(two.equals(one)); diff --git a/luni/src/test/java/libcore/java/net/AuthenticatorTest.java b/luni/src/test/java/libcore/java/net/AuthenticatorTest.java new file mode 100644 index 00000000000..6531107cac7 --- /dev/null +++ b/luni/src/test/java/libcore/java/net/AuthenticatorTest.java @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2021 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 libcore.java.net; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.net.Authenticator; +import java.net.InetAddress; +import java.net.PasswordAuthentication; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class AuthenticatorTest { + + private class MockAuthenticator extends Authenticator { + private int requests = 0; + private String userName; + private String password; + + public void setUserName(String userName) { + this.userName = userName; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + protected PasswordAuthentication getPasswordAuthentication() { + ++requests; + return new PasswordAuthentication(userName, password.toCharArray()); + } + + public int getRequests() { + return requests; + } + + public InetAddress getAddr() { + return getRequestingSite(); + } + + public int getPort() { + return getRequestingPort(); + } + + public String getProtocol() { + return getRequestingProtocol(); + } + + public String getPrompt() { + return getRequestingPrompt(); + } + + public String getScheme() { + return getRequestingScheme(); + } + } + + @Test + public void testRequestPasswordAuthentication() throws Exception { + final InetAddress addr = InetAddress.getByName("localhost"); + final int port = 42; + final String protocol = "HTTP"; + final String prompt = "Please enter your password"; + final String scheme = "scheme"; + final String userName = "007"; + final String password = "super secret"; + + MockAuthenticator auth = new MockAuthenticator(); + auth.setUserName(userName); + auth.setPassword(password); + Authenticator.setDefault(auth); + PasswordAuthentication passAuth = Authenticator.requestPasswordAuthentication( + addr, port, protocol, prompt, scheme); + + assertNotNull(passAuth); + assertEquals(userName, passAuth.getUserName()); + assertEquals(password, String.valueOf(passAuth.getPassword())); + + assertEquals(1, auth.getRequests()); + assertEquals(addr, auth.getAddr()); + assertEquals(port, auth.getPort()); + assertEquals(protocol, auth.getProtocol()); + assertEquals(prompt, auth.getPrompt()); + assertEquals(scheme, auth.getScheme()); + } + + @Test + public void testRequestPasswordAuthenticationWithNullAuthenticator() throws Exception { + final InetAddress addr = InetAddress.getByName("localhost"); + final int port = 42; + final String protocol = "HTTP"; + final String prompt = "Please enter your password"; + final String scheme = "scheme"; + + Authenticator.setDefault(null); + assertNull(Authenticator.requestPasswordAuthentication( + addr, port, protocol, prompt, scheme)); + } +} diff --git a/luni/src/test/java/libcore/java/net/DatagramSocketTest.java b/luni/src/test/java/libcore/java/net/DatagramSocketTest.java index fabed977af5..f6c3a275a05 100644 --- a/luni/src/test/java/libcore/java/net/DatagramSocketTest.java +++ b/luni/src/test/java/libcore/java/net/DatagramSocketTest.java @@ -16,6 +16,7 @@ package libcore.java.net; +import java.io.FileDescriptor; import java.lang.reflect.Field; import java.net.DatagramPacket; import java.net.DatagramSocket; @@ -219,4 +220,19 @@ public class DatagramSocketTest extends TestCaseWithRules { assertSame(packetAddr, newPacketAddr); } } + + public void testGetFileDescriptor$() throws Exception { + final int port = 9999; + + try(DatagramSocket s = new DatagramSocket()) { + s.connect(InetAddress.getLocalHost(), port); + + FileDescriptor fd = s.getFileDescriptor$(); + assertTrue(fd.valid()); + + s.disconnect(); + fd = s.getFileDescriptor$(); + assertTrue(fd.valid()); + } + } } diff --git a/luni/src/test/java/libcore/java/nio/channels/AsynchronousFileChannelTest.java b/luni/src/test/java/libcore/java/nio/channels/AsynchronousFileChannelTest.java index 3b072f2fe94..43f5b7d8aea 100644 --- a/luni/src/test/java/libcore/java/nio/channels/AsynchronousFileChannelTest.java +++ b/luni/src/test/java/libcore/java/nio/channels/AsynchronousFileChannelTest.java @@ -27,8 +27,10 @@ import java.nio.ByteBuffer; import java.nio.channels.AsynchronousFileChannel; import java.nio.channels.ClosedChannelException; import java.nio.channels.CompletionHandler; +import java.nio.channels.FileLock; import java.nio.channels.NonReadableChannelException; import java.nio.channels.NonWritableChannelException; +import java.nio.channels.OverlappingFileLockException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.NoSuchFileException; @@ -701,4 +703,67 @@ public class AsynchronousFileChannelTest { } catch(ClosedChannelException expected) {} } + static class LockHandler implements CompletionHandler<FileLock, Integer> { + public FileLock fileLock; + public Throwable exc; + + private final CountDownLatch cdl = new CountDownLatch(1); + + @Override + public void completed(FileLock fileLock, Integer attachment) { + this.fileLock = fileLock; + + cdl.countDown(); + } + + @Override + public void failed(Throwable exc, Integer attachment) { + this.exc = exc; + } + + public boolean awaitCompletion() throws InterruptedException { + return cdl.await(10, TimeUnit.SECONDS); + } + } + + + @Test + public void testLock() throws Exception { + File temp = createTemporaryFile(256); + assertEquals(256, temp.length()); + AsynchronousFileChannel afc = AsynchronousFileChannel.open(temp.toPath(), + StandardOpenOption.WRITE); + LockHandler handler = new LockHandler(); + + afc.lock(null, handler); + assertTrue(handler.awaitCompletion()); + assertNotNull(handler.fileLock); + assertNull(handler.exc); + assertTrue(handler.fileLock.isValid()); + assertFalse(handler.fileLock.isShared()); + + AsynchronousFileChannel otherAfc = AsynchronousFileChannel.open(temp.toPath(), + StandardOpenOption.WRITE); + LockHandler otherHandler = new LockHandler(); + try { + otherAfc.lock(null, otherHandler); + fail(); + } catch (OverlappingFileLockException expected) { + } + + handler.fileLock.release(); + + otherHandler = new LockHandler(); + otherAfc.lock(null, otherHandler); + assertTrue(otherHandler.awaitCompletion()); + assertNotNull(otherHandler.fileLock); + assertNull(otherHandler.exc); + assertTrue(otherHandler.fileLock.isValid()); + + otherHandler.fileLock.release(); + + afc.close(); + otherAfc.close(); + } + } diff --git a/luni/src/test/java/libcore/java/nio/channels/FileLockTest.java b/luni/src/test/java/libcore/java/nio/channels/FileLockTest.java new file mode 100644 index 00000000000..dbf72f9ba6f --- /dev/null +++ b/luni/src/test/java/libcore/java/nio/channels/FileLockTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2021 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 libcore.java.nio.channels; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.nio.channels.FileChannel; +import java.nio.channels.FileLock; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class FileLockTest { + + @Test + public void testToString() throws Exception { + File tmp = File.createTempFile("FileLockTest", "tmp"); + + try(FileOutputStream fos = new FileOutputStream(tmp)) { + try(FileChannel fc = fos.getChannel()) { + FileLock lock = fc.lock(0, 42, false); + String strLock = lock.toString(); + assertTrue(strLock.contains("0")); + assertTrue(strLock.contains("42")); + assertTrue(strLock.contains("exclusive")); + assertTrue(strLock.contains("valid")); + assertFalse(strLock.contains("invalid")); + + lock.release(); + strLock = lock.toString(); + assertTrue(strLock.contains("0")); + assertTrue(strLock.contains("42")); + assertTrue(strLock.contains("exclusive")); + assertTrue(strLock.contains("invalid")); + } + } + + try(FileInputStream fis = new FileInputStream(tmp)) { + try(FileChannel fc = fis.getChannel()) { + FileLock lock = fc.lock(0, 42, true); + String strLock = lock.toString(); + assertTrue(strLock.contains("0")); + assertTrue(strLock.contains("42")); + assertTrue(strLock.contains("shared")); + assertTrue(strLock.contains("valid")); + assertFalse(strLock.contains("invalid")); + + lock.release(); + strLock = lock.toString(); + assertTrue(strLock.contains("0")); + assertTrue(strLock.contains("42")); + assertTrue(strLock.contains("shared")); + assertTrue(strLock.contains("invalid")); + } + } + } +} diff --git a/luni/src/test/java/libcore/java/nio/file/ClosedFileSystemExceptionTest.java b/luni/src/test/java/libcore/java/nio/file/ClosedFileSystemExceptionTest.java new file mode 100644 index 00000000000..f20989963ae --- /dev/null +++ b/luni/src/test/java/libcore/java/nio/file/ClosedFileSystemExceptionTest.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2021 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 libcore.java.nio.file; + +import static org.junit.Assert.assertNull; + +import java.nio.file.ClosedFileSystemException; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class ClosedFileSystemExceptionTest { + + @Test + public void testEmptyConstructor() { + ClosedFileSystemException exception = new ClosedFileSystemException(); + assertNull(exception.getMessage()); + assertNull(exception.getCause()); + } +} diff --git a/luni/src/test/java/libcore/java/nio/file/ProviderNotFoundExceptionTest.java b/luni/src/test/java/libcore/java/nio/file/ProviderNotFoundExceptionTest.java index 277db4f146e..480d9b4943f 100644 --- a/luni/src/test/java/libcore/java/nio/file/ProviderNotFoundExceptionTest.java +++ b/luni/src/test/java/libcore/java/nio/file/ProviderNotFoundExceptionTest.java @@ -22,6 +22,12 @@ import java.nio.file.ProviderNotFoundException; public class ProviderNotFoundExceptionTest extends TestCase { + public void test_constructor$() { + ProviderNotFoundException exception = new ProviderNotFoundException(); + assertNull(exception.getMessage()); + assertNull(exception.getCause()); + } + public void test_constructor$String() { String message = "message"; ProviderNotFoundException exception = new ProviderNotFoundException(message); diff --git a/luni/src/test/java/libcore/java/nio/file/ReadOnlyFileSystemExceptionTest.java b/luni/src/test/java/libcore/java/nio/file/ReadOnlyFileSystemExceptionTest.java new file mode 100644 index 00000000000..b89eccd835c --- /dev/null +++ b/luni/src/test/java/libcore/java/nio/file/ReadOnlyFileSystemExceptionTest.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2021 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 libcore.java.nio.file; + +import static org.junit.Assert.assertNull; + +import java.nio.file.ReadOnlyFileSystemException; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class ReadOnlyFileSystemExceptionTest { + + @Test + public void testEmptyConstructor() { + ReadOnlyFileSystemException exception = new ReadOnlyFileSystemException(); + assertNull(exception.getMessage()); + assertNull(exception.getCause()); + } +} diff --git a/luni/src/test/java/libcore/java/nio/file/attribute/AclEntryTest.java b/luni/src/test/java/libcore/java/nio/file/attribute/AclEntryTest.java index 6e0ccd8a037..0464faff16d 100644 --- a/luni/src/test/java/libcore/java/nio/file/attribute/AclEntryTest.java +++ b/luni/src/test/java/libcore/java/nio/file/attribute/AclEntryTest.java @@ -55,4 +55,31 @@ public class AclEntryTest { assertEquals(1, flags.size()); assertTrue(flags.contains(AclEntryFlag.INHERIT_ONLY)); } + + @Test + public void testBuilder() throws Exception { + UserPrincipal user = Files.getOwner(Paths.get(".")); + + AclEntry aclEntry = AclEntry.newBuilder() + .setType(AclEntryType.ALLOW) + .setPrincipal(user) + .setFlags(AclEntryFlag.INHERIT_ONLY) + .setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.READ_ATTRIBUTES) + .build(); + + AclEntry.Builder builder = AclEntry.newBuilder(aclEntry); + aclEntry = builder.build(); + + assertEquals(AclEntryType.ALLOW, aclEntry.type()); + assertEquals(user, aclEntry.principal()); + + Set<AclEntryPermission> permissions = aclEntry.permissions(); + assertEquals(2, permissions.size()); + assertTrue(permissions.contains(AclEntryPermission.READ_DATA)); + assertTrue(permissions.contains(AclEntryPermission.READ_ATTRIBUTES)); + + Set<AclEntryFlag> flags = aclEntry.flags(); + assertEquals(1, flags.size()); + assertTrue(flags.contains(AclEntryFlag.INHERIT_ONLY)); + } } diff --git a/luni/src/test/java/libcore/java/nio/file/spi/FileSystemProviderTest.java b/luni/src/test/java/libcore/java/nio/file/spi/FileSystemProviderTest.java new file mode 100644 index 00000000000..82c58a134ef --- /dev/null +++ b/luni/src/test/java/libcore/java/nio/file/spi/FileSystemProviderTest.java @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2021 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 libcore.java.nio.file.spi; + +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.net.URI; +import java.nio.channels.SeekableByteChannel; +import java.nio.file.AccessMode; +import java.nio.file.CopyOption; +import java.nio.file.DirectoryStream; +import java.nio.file.FileStore; +import java.nio.file.FileSystem; +import java.nio.file.LinkOption; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.attribute.FileAttribute; +import java.nio.file.attribute.FileAttributeView; +import java.nio.file.spi.FileSystemProvider; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class FileSystemProviderTest { + + class MockFileSystemProvider extends FileSystemProvider { + @Override + public String getScheme() { + return "mock"; + } + + @Override + public FileSystem newFileSystem(URI uri, Map<String,?> env) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public FileSystem getFileSystem(URI uri) { + throw new UnsupportedOperationException(); + } + + @Override + public Path getPath(URI uri) { + throw new UnsupportedOperationException(); + } + + @Override + public SeekableByteChannel newByteChannel(Path path, + Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public DirectoryStream<Path> newDirectoryStream(Path dir, + DirectoryStream.Filter<? super Path> filter) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void delete(Path path) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void copy(Path source, Path target, CopyOption... options) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void move(Path source, Path target, CopyOption... options) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isSameFile(Path path, Path path2) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isHidden(Path path) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public FileStore getFileStore(Path path) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void checkAccess(Path path, AccessMode... modes) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public <V extends FileAttributeView> V getFileAttributeView( + Path path, Class<V> type, LinkOption... options) { + throw new UnsupportedOperationException(); + } + + @Override + public <A extends BasicFileAttributes> A readAttributes( + Path path, Class<A> type, LinkOption... options) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public Map<String,Object> readAttributes( + Path path, String attributes, LinkOption... options) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void setAttribute(Path path, String attribute, + Object value, LinkOption... options) throws IOException { + throw new UnsupportedOperationException(); + } + } + + @Test + public void testCreateLink() throws Exception { + MockFileSystemProvider provider = new MockFileSystemProvider(); + + Path link = Paths.get("testdir"); + Path existing = Paths.get("testfile"); + + try { + provider.createLink(link, existing); + fail("Expected UnsupportedOperationException"); + } catch(UnsupportedOperationException expected) { + } + } + + @Test + public void testNewAsynchronousFileChannel() throws Exception { + MockFileSystemProvider provider = new MockFileSystemProvider(); + + Path path = Paths.get("testfile"); + Set<OpenOption> options = new TreeSet<OpenOption>(); + + try { + provider.newAsynchronousFileChannel(path, options, null); + fail("Expected UnsupportedOperationException"); + } catch(UnsupportedOperationException expected) { + } + } +} diff --git a/luni/src/test/java/libcore/java/time/chrono/ChronoZonedDateTimeTest.java b/luni/src/test/java/libcore/java/time/chrono/ChronoZonedDateTimeTest.java index a31998003ed..d6a0de44dbc 100644 --- a/luni/src/test/java/libcore/java/time/chrono/ChronoZonedDateTimeTest.java +++ b/luni/src/test/java/libcore/java/time/chrono/ChronoZonedDateTimeTest.java @@ -16,6 +16,7 @@ package libcore.java.time.chrono; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -24,14 +25,22 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +import java.time.Instant; import java.time.ZoneId; import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.time.chrono.ChronoLocalDateTime; import java.time.chrono.ChronoZonedDateTime; +import java.time.chrono.JapaneseChronology; +import java.time.chrono.JapaneseDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.FormatStyle; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; import java.time.temporal.TemporalField; import java.time.temporal.TemporalUnit; +import java.util.Locale; @RunWith(JUnit4.class) public class ChronoZonedDateTimeTest { @@ -58,6 +67,21 @@ public class ChronoZonedDateTimeTest { verify(temporalUnit).isSupportedBy(chronoZonedDateTime); } + @Test + public void testFormat() { + JapaneseChronology chrono = JapaneseChronology.INSTANCE; + DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder() + .appendLocalized(FormatStyle.MEDIUM, null) + .toFormatter(Locale.US); + ZonedDateTime epochTime = ZonedDateTime.ofInstant(Instant.EPOCH, ZoneId.of("UTC")); + + ChronoZonedDateTime<JapaneseDate> chronoZonedDateTime = chrono.zonedDateTime(epochTime); + assertEquals("Jan 1, 45 Shōwa", chronoZonedDateTime.format(dateFormatter)); + + ChronoLocalDateTime<JapaneseDate> chronoLocalDateTime = chrono.localDateTime(epochTime); + assertEquals("Jan 1, 45 Shōwa", chronoLocalDateTime.format(dateFormatter)); + } + /* Used to test default methods. */ private static class TestChronoZonedDateTime implements ChronoZonedDateTime { diff --git a/luni/src/test/java/libcore/java/util/AbstractMapTest.java b/luni/src/test/java/libcore/java/util/AbstractMapTest.java new file mode 100644 index 00000000000..4b6801853f6 --- /dev/null +++ b/luni/src/test/java/libcore/java/util/AbstractMapTest.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2021 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 libcore.java.util; + +import static org.junit.Assert.assertEquals; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.util.AbstractMap; + +@RunWith(JUnit4.class) +public class AbstractMapTest { + + + @Test + public void testSimpleEntrySetValue() { + AbstractMap.SimpleEntry<String, Integer> entry = new AbstractMap.SimpleEntry<>("abc", 1); + assertEquals(Integer.valueOf(1), entry.getValue()); + entry.setValue(-1); + assertEquals(Integer.valueOf(-1), entry.getValue()); + + AbstractMap.SimpleImmutableEntry<String, Integer> immutableEntry = + new AbstractMap.SimpleImmutableEntry<>("abc", 1); + assertEquals(Integer.valueOf(1), immutableEntry.getValue()); + Assert.assertThrows(UnsupportedOperationException.class, () -> immutableEntry.setValue(-1)); + } +} diff --git a/luni/src/test/java/libcore/java/util/SpliteratorsTest.java b/luni/src/test/java/libcore/java/util/SpliteratorsTest.java index 639ebf1f449..5e01ee028c2 100644 --- a/luni/src/test/java/libcore/java/util/SpliteratorsTest.java +++ b/luni/src/test/java/libcore/java/util/SpliteratorsTest.java @@ -26,6 +26,10 @@ import java.util.List; import java.util.PrimitiveIterator; import java.util.Spliterator; import java.util.Spliterators; +import java.util.Spliterators.AbstractDoubleSpliterator; +import java.util.Spliterators.AbstractIntSpliterator; +import java.util.Spliterators.AbstractLongSpliterator; +import java.util.Spliterators.AbstractSpliterator; import java.util.function.Consumer; import java.util.function.DoubleConsumer; import java.util.function.IntConsumer; @@ -863,4 +867,302 @@ public class SpliteratorsTest extends TestCase { assertFalse(it.hasNext()); } + + private static class IncrementalDoubleSequenceSpliterator extends AbstractDoubleSpliterator { + private int current = 0; + private final int size; + + IncrementalDoubleSequenceSpliterator(int size, int additionalCharacteristics) { + super(size, additionalCharacteristics); + this.size = size; + } + + @Override + public boolean tryAdvance(DoubleConsumer action) { + if (current < size) { + action.accept(current++); + return true; + } else { + return false; + } + } + } + + public void testAbstractDoubleSpliterator_smallSize() { + int size = 10; + int characteristics = Spliterator.ORDERED; + AbstractDoubleSpliterator spliterator = new IncrementalDoubleSequenceSpliterator(size, + characteristics); + assertEquals(spliterator.estimateSize(), size); + assertEquals(spliterator.characteristics(), characteristics); + + Spliterator.OfDouble resultSpliterator = spliterator.trySplit(); + // The parent spliterator should have no remaining elements + assertEquals(spliterator.estimateSize(), 0); + assertFalse(spliterator.tryAdvance((DoubleConsumer) value -> { + throw new IllegalArgumentException("Unexpected call with value: " + value); + })); + // The result spliterator should have the same size because the size is small. + assertEquals(resultSpliterator.estimateSize(), size); + assertTrue(resultSpliterator.tryAdvance(((DoubleConsumer) value -> { + assertEquals(0d, value); + }))); + assertTrue(resultSpliterator.tryAdvance(((DoubleConsumer) value -> { + assertEquals(1d, value); + }))); + } + + public void testAbstractDoubleSpliterator_largeSize() { + // AbstractDoubleSpliterator.MAX_BATCH is package-private, and so assume it's 1 << 10. + int maxBatch = 1 << 10; + int size = maxBatch * 2; + int characteristics = Spliterator.ORDERED | Spliterator.SIZED; + AbstractDoubleSpliterator spliterator = new IncrementalDoubleSequenceSpliterator(size, + characteristics); + assertEquals(spliterator.estimateSize(), size); + assertEquals(spliterator.characteristics(), characteristics | Spliterator.SUBSIZED); + + Spliterator.OfDouble resultSpliterator = spliterator.trySplit(); + // The total size is not changed + assertEquals(size, spliterator.estimateSize() + resultSpliterator.estimateSize()); + + // The result spliterator should have the max batch size. + assertEquals(resultSpliterator.estimateSize(), maxBatch); + assertTrue(resultSpliterator.tryAdvance(((DoubleConsumer) value -> { + assertEquals(0d, value); + }))); + assertTrue(resultSpliterator.tryAdvance(((DoubleConsumer) value -> { + assertEquals(1d, value); + })));; + + // The parent spliterator should be able to advance. + assertTrue(spliterator.tryAdvance(((DoubleConsumer) value -> { + assertEquals((double) maxBatch, value); + }))); + + } + + private static class IncrementalIntSequenceSpliterator extends AbstractIntSpliterator { + private int current = 0; + private final int size; + + IncrementalIntSequenceSpliterator(int size, int additionalCharacteristics) { + super(size, additionalCharacteristics); + this.size = size; + } + + @Override + public boolean tryAdvance(IntConsumer action) { + if (current < size) { + action.accept(current++); + return true; + } else { + return false; + } + } + } + + public void testAbstractIntSpliterator_smallSize() { + int size = 10; + int characteristics = Spliterator.ORDERED; + AbstractIntSpliterator spliterator = new IncrementalIntSequenceSpliterator(size, + characteristics); + assertEquals(spliterator.estimateSize(), size); + assertEquals(spliterator.characteristics(), characteristics); + + Spliterator.OfInt resultSpliterator = spliterator.trySplit(); + // The parent spliterator should have no remaining elements + assertEquals(spliterator.estimateSize(), 0); + assertFalse(spliterator.tryAdvance((IntConsumer) value -> { + throw new IllegalArgumentException("Unexpected call with value: " + value); + })); + // The result spliterator should have the same size because the size is small. + assertEquals(resultSpliterator.estimateSize(), size); + assertTrue(resultSpliterator.tryAdvance(((IntConsumer) value -> { + assertEquals(0, value); + }))); + assertTrue(resultSpliterator.tryAdvance(((IntConsumer) value -> { + assertEquals(1, value); + }))); + } + + public void testAbstractIntSpliterator_largeSize() { + // AbstractIntSpliterator.MAX_BATCH is package-private, and so assume it's 1 << 10. + int maxBatch = 1 << 10; + int size = maxBatch * 2; + int characteristics = Spliterator.ORDERED | Spliterator.SIZED; + AbstractIntSpliterator spliterator = new IncrementalIntSequenceSpliterator(size, + characteristics); + assertEquals(spliterator.estimateSize(), size); + assertEquals(spliterator.characteristics(), characteristics | Spliterator.SUBSIZED); + + Spliterator.OfInt resultSpliterator = spliterator.trySplit(); + // The total size is not changed + assertEquals(size, spliterator.estimateSize() + resultSpliterator.estimateSize()); + + // The result spliterator should have the max batch size. + assertEquals(resultSpliterator.estimateSize(), maxBatch); + assertTrue(resultSpliterator.tryAdvance(((IntConsumer) value -> { + assertEquals(0, value); + }))); + assertTrue(resultSpliterator.tryAdvance(((IntConsumer) value -> { + assertEquals(1, value); + }))); + + // The parent spliterator should be able to advance. + assertTrue(spliterator.tryAdvance(((IntConsumer) value -> { + assertEquals(maxBatch, value); + }))); + } + + private static class IncrementalLongSequenceSpliterator extends AbstractLongSpliterator { + private int current = 0; + private final int size; + + IncrementalLongSequenceSpliterator(int size, int additionalCharacteristics) { + super(size, additionalCharacteristics); + this.size = size; + } + + @Override + public boolean tryAdvance(LongConsumer action) { + if (current < size) { + action.accept(current++); + return true; + } else { + return false; + } + } + } + + public void testAbstractLongSpliterator_smallSize() { + int size = 10; + int characteristics = Spliterator.ORDERED; + AbstractLongSpliterator spliterator = new IncrementalLongSequenceSpliterator(size, + characteristics); + assertEquals(spliterator.estimateSize(), size); + assertEquals(spliterator.characteristics(), characteristics); + + Spliterator.OfLong resultSpliterator = spliterator.trySplit(); + // The parent spliterator should have no remaining elements + assertEquals(spliterator.estimateSize(), 0L); + assertFalse(spliterator.tryAdvance((LongConsumer) value -> { + throw new IllegalArgumentException("Unexpected call with value: " + value); + })); + // The result spliterator should have the same size because the size is small. + assertEquals(resultSpliterator.estimateSize(), size); + assertTrue(resultSpliterator.tryAdvance(((LongConsumer) value -> { + assertEquals(0L, value); + }))); + assertTrue(resultSpliterator.tryAdvance(((LongConsumer) value -> { + assertEquals(1L, value); + }))); + } + + public void testAbstractLongSpliterator_largeSize() { + // AbstractLongSpliterator.MAX_BATCH is package-private, and so assume it's 1 << 10. + int maxBatch = 1 << 10; + int size = maxBatch * 2; + int characteristics = Spliterator.ORDERED | Spliterator.SIZED; + AbstractLongSpliterator spliterator = new IncrementalLongSequenceSpliterator(size, + characteristics); + assertEquals(spliterator.estimateSize(), size); + assertEquals(spliterator.characteristics(), characteristics | Spliterator.SUBSIZED); + + Spliterator.OfLong resultSpliterator = spliterator.trySplit(); + // The total size is not changed + assertEquals(size, spliterator.estimateSize() + resultSpliterator.estimateSize()); + + // The result spliterator should have the max batch size. + assertEquals(resultSpliterator.estimateSize(), maxBatch); + assertTrue(resultSpliterator.tryAdvance(((LongConsumer) value -> { + assertEquals(0, value); + }))); + assertTrue(resultSpliterator.tryAdvance(((LongConsumer) value -> { + assertEquals(1, value); + })));; + + // The parent spliterator should be able to advance. + assertTrue(spliterator.tryAdvance(((LongConsumer) value -> { + assertEquals(maxBatch, value); + }))); + + } + + private static class IncrementalIntegerSequenceSpliterator extends + AbstractSpliterator<Integer> { + private int current = 0; + private final int size; + + IncrementalIntegerSequenceSpliterator(int size, int additionalCharacteristics) { + super(size, additionalCharacteristics); + this.size = size; + } + + @Override + public boolean tryAdvance(Consumer<? super Integer> action) { + if (current < size) { + action.accept(current++); + return true; + } else { + return false; + } + } + } + + public void testAbstractSpliterator_smallSize() { + int size = 10; + int characteristics = Spliterator.ORDERED; + AbstractSpliterator<Integer> spliterator = new IncrementalIntegerSequenceSpliterator(size, + characteristics); + assertEquals(spliterator.estimateSize(), size); + assertEquals(spliterator.characteristics(), characteristics); + + Spliterator<Integer> resultSpliterator = spliterator.trySplit(); + // The parent spliterator should have no remaining elements + assertEquals(spliterator.estimateSize(), 0L); + assertFalse(spliterator.tryAdvance(value -> { + throw new IllegalArgumentException("Unexpected call with value: " + value); + })); + // The result spliterator should have the same size because the size is small. + assertEquals(resultSpliterator.estimateSize(), size); + assertTrue(resultSpliterator.tryAdvance((value -> { + assertEquals(Integer.valueOf(0), value); + }))); + assertTrue(resultSpliterator.tryAdvance((value -> { + assertEquals(Integer.valueOf(1), value); + }))); + } + + public void testAbstractSpliterator_largeSize() { + // AbstractSpliterator.MAX_BATCH is package-private, and so assume it's 1 << 10. + int maxBatch = 1 << 10; + int size = maxBatch * 2; + int characteristics = Spliterator.ORDERED | Spliterator.SIZED; + AbstractSpliterator<Integer> spliterator = new IncrementalIntegerSequenceSpliterator(size, + characteristics); + assertEquals(spliterator.estimateSize(), size); + assertEquals(spliterator.characteristics(), characteristics | Spliterator.SUBSIZED); + + Spliterator<Integer> resultSpliterator = spliterator.trySplit(); + // The total size is not changed + assertEquals(size, spliterator.estimateSize() + resultSpliterator.estimateSize()); + + // The result spliterator should have the max batch size. + assertEquals(resultSpliterator.estimateSize(), maxBatch); + assertTrue(resultSpliterator.tryAdvance((value -> { + assertEquals(Integer.valueOf(0), value); + }))); + assertTrue(resultSpliterator.tryAdvance((value -> { + assertEquals(Integer.valueOf(1), value); + }))); + + ; + + // The parent spliterator should be able to advance. + assertTrue(spliterator.tryAdvance((value -> { + assertEquals(Integer.valueOf(maxBatch), value); + }))); + + } } diff --git a/luni/src/test/java/libcore/javax/security/auth/AuthPermissionTest.java b/luni/src/test/java/libcore/javax/security/auth/AuthPermissionTest.java new file mode 100644 index 00000000000..e4715d02389 --- /dev/null +++ b/luni/src/test/java/libcore/javax/security/auth/AuthPermissionTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2021 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 libcore.javax.security.auth; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import javax.security.auth.AuthPermission; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Android does not support {@link java.lang.SecurityManager} and has its implementations stubbed. + * See comments in {@link java.lang.SecurityManager} for details. + * + * This test is added primarily for code coverage. + */ +@RunWith(JUnit4.class) +public class AuthPermissionTest { + + @Test + public void constructorWithString() { + AuthPermission permission = new AuthPermission("name"); + assertNotNull(permission); + assertEquals("", permission.getName()); + } + + @Test + public void constructorWithStringAndString() { + AuthPermission permission = new AuthPermission("name", "actions"); + assertNotNull(permission); + assertEquals("", permission.getName()); + assertEquals("", permission.getActions()); + } +} diff --git a/luni/src/test/java/libcore/javax/security/auth/PrivateCredentialPermissionTest.java b/luni/src/test/java/libcore/javax/security/auth/PrivateCredentialPermissionTest.java new file mode 100644 index 00000000000..8ecb49b0ff5 --- /dev/null +++ b/luni/src/test/java/libcore/javax/security/auth/PrivateCredentialPermissionTest.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2021 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 libcore.javax.security.auth; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.security.AllPermission; +import javax.security.auth.AuthPermission; +import javax.security.auth.PrivateCredentialPermission; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Android does not support {@link java.lang.SecurityManager} and has its implementations stubbed. + * See comments in {@link java.lang.SecurityManager} for details. + * + * This test is added primarily for code coverage. + */ +@RunWith(JUnit4.class) +public class PrivateCredentialPermissionTest { + + private PrivateCredentialPermission permission; + + @Before + public void setUp() { + permission = new PrivateCredentialPermission("name", "actions"); + } + + @Test + public void constructorWithStringAndString() { + assertNotNull(permission); + assertEquals("", permission.getName()); + assertNull(permission.getActions()); + } + + @Test + public void getActions() { + assertNull(permission.getActions()); + } + + @Test + public void getCredentialClass() { + assertNull(permission.getCredentialClass()); + } + + @Test + public void getPrincipals() { + assertNull(permission.getPrincipals()); + } + + @Test + public void implies() { + assertTrue(permission.implies(new AuthPermission(""))); + assertTrue(permission.implies(new AllPermission())); + } +} diff --git a/luni/src/test/java/libcore/javax/security/auth/SubjectDomainCombinerTest.java b/luni/src/test/java/libcore/javax/security/auth/SubjectDomainCombinerTest.java new file mode 100644 index 00000000000..a88670c258d --- /dev/null +++ b/luni/src/test/java/libcore/javax/security/auth/SubjectDomainCombinerTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2021 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 libcore.javax.security.auth; + +import static org.junit.Assert.assertNull; + +import javax.security.auth.Subject; +import javax.security.auth.SubjectDomainCombiner; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Android does not support {@link java.lang.SecurityManager} and has its implementations stubbed. + * See comments in {@link java.lang.SecurityManager} for details. + * + * This test is added primarily for code coverage. + */ +@RunWith(JUnit4.class) +public class SubjectDomainCombinerTest { + + @Test + public void combine() { + SubjectDomainCombiner combiner = new SubjectDomainCombiner(null); + assertNull(combiner.combine(null, null)); + } + + @Test + public void getSubject() { + SubjectDomainCombiner combiner = new SubjectDomainCombiner(null); + assertNull(combiner.getSubject()); + + combiner = new SubjectDomainCombiner(new Subject()); + assertNull(combiner.getSubject()); + } +} diff --git a/luni/src/test/java/libcore/javax/security/auth/SubjectTest.java b/luni/src/test/java/libcore/javax/security/auth/SubjectTest.java new file mode 100644 index 00000000000..26efed79028 --- /dev/null +++ b/luni/src/test/java/libcore/javax/security/auth/SubjectTest.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2021 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 libcore.javax.security.auth; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.security.Principal; +import java.util.Set; +import javax.security.auth.Subject; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Android does not support {@link java.lang.SecurityManager} and has its implementations stubbed. + * See comments in {@link java.lang.SecurityManager} for details. + * + * This test is added primarily for code coverage. + */ +@RunWith(JUnit4.class) +public class SubjectTest { + + private Set<Principal> set; + private Subject subject; + + @Before + public void setUp() { + set = Set.of(new PrincipalImpl()); + subject = new Subject(true, set, set, set); + } + + @Test + public void getPrincipals() { + assertEquals(set, subject.getPrincipals()); + } + + @Test + public void getPrivateCredentials() { + assertEquals(set, subject.getPrivateCredentials()); + } + + @Test + public void getPublicCredentials() { + assertEquals(set, subject.getPublicCredentials()); + } + + @Test + public void isReadOnly() { + assertTrue(subject.isReadOnly()); + + subject = new Subject(false, set, set, set); + assertFalse(subject.isReadOnly()); + } + + @Test + public void setReadOnly() { + subject = new Subject(false, set, set, set); + assertFalse(subject.isReadOnly()); + + subject.setReadOnly(); + assertTrue(subject.isReadOnly()); + } + + private static final class PrincipalImpl implements Principal { + + @Override + public String getName() { + return ""; + } + } +} diff --git a/luni/src/test/java/libcore/javax/xml/datatype/DatatypeConfigurationExceptionTest.java b/luni/src/test/java/libcore/javax/xml/datatype/DatatypeConfigurationExceptionTest.java new file mode 100644 index 00000000000..3b6708d6b36 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/datatype/DatatypeConfigurationExceptionTest.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.datatype; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import javax.xml.datatype.DatatypeConfigurationException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class DatatypeConfigurationExceptionTest { + + @Test + public void constructor() { + DatatypeConfigurationException e = new DatatypeConfigurationException(); + assertNull(e.getMessage()); + assertNull(e.getCause()); + } + + @Test + public void constructorWithStringAndThrowable() { + Throwable t = new Throwable(); + DatatypeConfigurationException e = new DatatypeConfigurationException("message", t); + assertEquals("message", e.getMessage()); + assertEquals(t, e.getCause()); + } + + @Test + public void constructorWithThrowable() { + Throwable t = new Throwable(); + DatatypeConfigurationException e = new DatatypeConfigurationException(t); + assertEquals("java.lang.Throwable", e.getMessage()); + assertEquals(t, e.getCause()); + } +} diff --git a/luni/src/test/java/libcore/javax/xml/datatype/DatatypeConstantsTest.java b/luni/src/test/java/libcore/javax/xml/datatype/DatatypeConstantsTest.java new file mode 100644 index 00000000000..f1797d1fabd --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/datatype/DatatypeConstantsTest.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.datatype; + +import javax.xml.datatype.DatatypeConstants; +import junit.framework.TestCase; + +public class DatatypeConstantsTest extends TestCase { + + public void testField_getId() { + assertEquals(0, DatatypeConstants.YEARS.getId()); + assertEquals(1, DatatypeConstants.MONTHS.getId()); + assertEquals(2, DatatypeConstants.DAYS.getId()); + assertEquals(3, DatatypeConstants.HOURS.getId()); + assertEquals(4, DatatypeConstants.MINUTES.getId()); + assertEquals(5, DatatypeConstants.SECONDS.getId()); + } +} diff --git a/luni/src/test/java/libcore/javax/xml/datatype/DatatypeFactoryTest.java b/luni/src/test/java/libcore/javax/xml/datatype/DatatypeFactoryTest.java new file mode 100644 index 00000000000..8a48eca8f80 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/datatype/DatatypeFactoryTest.java @@ -0,0 +1,222 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.datatype; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.GregorianCalendar; +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeConstants; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.Duration; +import javax.xml.datatype.XMLGregorianCalendar; +import junit.framework.TestCase; + +public class DatatypeFactoryTest extends TestCase { + + private DatatypeFactory factory; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + factory = new ExtendsDatatypeFactory(); + } + + public void testConstructor() { + DatatypeFactory f = new ExtendsDatatypeFactory(); + } + + public void testNewDuration_biiiiii() { + Duration duration = factory.newDuration(true, 1, 2, 3, 4, 5, 6); + assertEquals(1, duration.getYears()); + assertEquals(2, duration.getMonths()); + assertEquals(3, duration.getDays()); + assertEquals(4, duration.getHours()); + assertEquals(5, duration.getMinutes()); + assertEquals(6, duration.getSeconds()); + } + + public void testNewDurationDayTime_biiii() { + Duration duration = factory.newDurationDayTime(true, 3, 4, 5, 6); + assertEquals(0, duration.getYears()); + assertEquals(0, duration.getMonths()); + assertEquals(3, duration.getDays()); + assertEquals(4, duration.getHours()); + assertEquals(5, duration.getMinutes()); + assertEquals(6, duration.getSeconds()); + } + + public void testNewDurationDayTime_bBigInteger() { + Duration duration = factory.newDuration(true, BigInteger.valueOf(1), BigInteger.valueOf(2), + BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), + BigDecimal.valueOf(6)); + assertEquals(1, duration.getYears()); + assertEquals(2, duration.getMonths()); + assertEquals(3, duration.getDays()); + assertEquals(4, duration.getHours()); + assertEquals(5, duration.getMinutes()); + assertEquals(6, duration.getSeconds()); + } + + public void testNewDurationDayTime_String() { + Duration duration = factory.newDuration(""); + assertNull(duration); + } + + public void testNewDurationDayTime_long() { + Duration duration = factory.newDuration(1000L); + assertEquals(0, duration.getYears()); + assertEquals(0, duration.getMonths()); + assertEquals(0, duration.getDays()); + assertEquals(0, duration.getHours()); + assertEquals(0, duration.getMinutes()); + assertEquals(1, duration.getSeconds()); + } + + public void testNewDurationYearMonth_bii() { + Duration duration = factory.newDurationYearMonth(true, 1, 2); + assertEquals(1, duration.getYears()); + assertEquals(2, duration.getMonths()); + assertEquals(0, duration.getDays()); + assertEquals(0, duration.getHours()); + assertEquals(0, duration.getMinutes()); + assertEquals(0, duration.getSeconds()); + } + + public void testNewDurationYearMonth_bBigInteger() { + Duration duration = factory.newDurationYearMonth(true, BigInteger.valueOf(1), BigInteger.valueOf(2)); + assertEquals(1, duration.getYears()); + assertEquals(2, duration.getMonths()); + assertEquals(0, duration.getDays()); + assertEquals(0, duration.getHours()); + assertEquals(0, duration.getMinutes()); + assertEquals(0, duration.getSeconds()); + } + + public void testNewDurationYearMonth_String() { + Duration duration = factory.newDurationYearMonth(""); + assertNull(duration); + } + + public void testNewDurationYearMonth_long() { + Duration duration = factory.newDurationYearMonth(1000); + assertEquals(0, duration.getYears()); + assertEquals(0, duration.getMonths()); + assertEquals(0, duration.getDays()); + assertEquals(0, duration.getHours()); + assertEquals(0, duration.getMinutes()); + assertEquals(1, duration.getSeconds()); + } + + public void testNewInstance() { + try { + DatatypeFactory.newInstance(); + fail("Unexpectedly created new instance"); + } catch (DatatypeConfigurationException expected) { + // no default implementation in Android + } + } + + public void testNewInstance_customClass() { + try { + DatatypeFactory.newInstance(null, null); + fail("Unexpectedly created new instance"); + } catch (DatatypeConfigurationException expected) { + // class loading disabled + } + } + + public void testNewXMLGregorianCalendar_iiiiiii() { + XMLGregorianCalendar calendar = factory.newXMLGregorianCalendar( + 1, 2, 3, 4, 5, 6, 7, 0); + assertNull(calendar); + } + + public void testNewXMLGregorianCalendarDate_iiii() { + XMLGregorianCalendar calendar = factory.newXMLGregorianCalendarDate(1, 2, 3, 0); + assertNull(calendar); + } + + public void testNewXMLGregorianCalendarTime_iiii() { + XMLGregorianCalendar calendar = factory.newXMLGregorianCalendarTime(4, 5, 6, 0); + assertNull(calendar); + } + + public void testNewXMLGregorianCalendarTime_iiiii() { + XMLGregorianCalendar calendar = factory.newXMLGregorianCalendarTime(4, 5, 6, 7, 0); + assertNull(calendar); + } + + public void testNewXMLGregorianCalendarTime_BigDecimal() { + XMLGregorianCalendar calendar = factory.newXMLGregorianCalendarTime(4, 5, 6, BigDecimal.valueOf(7), 0); + assertNull(calendar); + } + + private static class ExtendsDatatypeFactory extends DatatypeFactory { + + protected ExtendsDatatypeFactory() { + super(); + } + + @Override + public Duration newDuration(String lexicalRepresentation) { + return null; + } + + @Override + public Duration newDuration(long durationInMilliSeconds) { + return new DurationImpl(durationInMilliSeconds); + } + + @Override + public Duration newDuration(boolean isPositive, BigInteger years, + BigInteger months, BigInteger days, BigInteger hours, + BigInteger minutes, BigDecimal seconds) { + int y = years == null ? DatatypeConstants.FIELD_UNDEFINED : years.intValue(); + int m = months == null ? DatatypeConstants.FIELD_UNDEFINED : months.intValue(); + int d = days == null ? DatatypeConstants.FIELD_UNDEFINED : days.intValue(); + int h = hours == null ? DatatypeConstants.FIELD_UNDEFINED : hours.intValue(); + int mn = minutes == null ? DatatypeConstants.FIELD_UNDEFINED : minutes.intValue(); + int s = seconds == null ? DatatypeConstants.FIELD_UNDEFINED : seconds.intValue(); + return new DurationImpl(isPositive ? 1 : -1, y, m, d, h, mn, s); + } + + @Override + public XMLGregorianCalendar newXMLGregorianCalendar() { + return null; + } + + @Override + public XMLGregorianCalendar newXMLGregorianCalendar(String lexicalRepresentation) { + return null; + } + + @Override + public XMLGregorianCalendar newXMLGregorianCalendar(GregorianCalendar cal) { + return null; + } + + @Override + public XMLGregorianCalendar newXMLGregorianCalendar(BigInteger year, int month, + int day, int hour, int minute, int second, + BigDecimal fractionalSecond, int timezone) { + return null; + } + + } +} diff --git a/luni/src/test/java/libcore/javax/xml/datatype/DurationImpl.java b/luni/src/test/java/libcore/javax/xml/datatype/DurationImpl.java new file mode 100644 index 00000000000..ae89cbcbe54 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/datatype/DurationImpl.java @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.datatype; + +import java.math.BigDecimal; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; +import javax.xml.datatype.DatatypeConstants; +import javax.xml.datatype.DatatypeConstants.Field; +import javax.xml.datatype.Duration; + +/** + * Avoid depending on {@link javax.xml.datatype.DatatypeFactory}, and + * throws {@link UnsupportedOperationException} on abstract methods, + * since proper implementation does not exist in Android. + */ +public class DurationImpl extends javax.xml.datatype.Duration { + + private final int sign; + private final Map<Field, Number> fields; + + DurationImpl(long millis) { + fields = new HashMap<>(); + if (millis > 0) { + sign = 1; + } else if (millis == 0) { + sign = 0; + return; + } else { + sign = -1; + millis = -millis; + } + long d = millis / 86400000L; + millis %= 86400000L; + if (d > 0) { + fields.put(DatatypeConstants.DAYS, d); + } + long h = millis / 3600000L; + millis %= 3600000L; + if (h > 0) { + fields.put(DatatypeConstants.HOURS, h); + } + long m = millis / 60000L; + millis %= 60000L; + if (m > 0) { + fields.put(DatatypeConstants.MINUTES, m); + } + fields.put(DatatypeConstants.SECONDS, (float)millis / 1000); + } + + DurationImpl(int sgn, int y, int months, int d, int h, int m, float s) { + sign = sgn; + fields = new HashMap<>(); + if (y >= 0) { fields.put(DatatypeConstants.YEARS, y); } + if (months >= 0) { fields.put(DatatypeConstants.MONTHS, months); } + if (d >= 0) { fields.put(DatatypeConstants.DAYS, d); } + if (h >= 0) { fields.put(DatatypeConstants.HOURS, h); } + if (m >= 0) { fields.put(DatatypeConstants.MINUTES, m); } + if (s >= 0) { fields.put(DatatypeConstants.SECONDS, s); } + } + + @Override + public int getSign() { + return sign; + } + + @Override + public Number getField(Field field) { + return fields.get(field); + } + + @Override + public boolean isSet(Field field) { + return fields.containsKey(field); + } + + @Override + public Duration add(Duration rhs) { + throw new UnsupportedOperationException("Stub implementation"); + } + + @Override + public void addTo(Calendar calendar) { + throw new UnsupportedOperationException("Stub implementation"); + } + + @Override + public Duration multiply(BigDecimal factor) { + throw new UnsupportedOperationException("Stub implementation"); + } + + @Override + public Duration negate() { + throw new UnsupportedOperationException("Stub implementation"); + } + + @Override + public Duration normalizeWith(Calendar startTimeInstant) { + throw new UnsupportedOperationException("Stub implementation"); + } + + @Override + public int compare(Duration duration) { + throw new UnsupportedOperationException("Stub implementation"); + } + + @Override + public int hashCode() { + throw new UnsupportedOperationException("Stub implementation"); + } +} diff --git a/luni/src/test/java/libcore/javax/xml/datatype/DurationTest.java b/luni/src/test/java/libcore/javax/xml/datatype/DurationTest.java new file mode 100644 index 00000000000..ad9dba69c95 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/datatype/DurationTest.java @@ -0,0 +1,187 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.datatype; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import javax.xml.datatype.DatatypeConstants; +import javax.xml.datatype.Duration; +import junit.framework.TestCase; + +/** + * Android has no {@link Duration} implementation, so use stubbed one that throws + * on abstract methods. This unit test is primarily for code coverage, it barely tests + * the actual implementation. + */ +public class DurationTest extends TestCase { + + private static final Duration oneSecond = new DurationImpl(1, 0, 0, 0, 0, 0, 1); + private static final Duration tenMinutes = new DurationImpl(1, 0, 0, 0, 0, 10, 0); + private static final Duration oneDay = new DurationImpl(1, 0, 0, 1, 0 , 0, 0); + private static final Duration fiveDays = new DurationImpl(1, 0, 0, 5, 0, 0, 0); + private static final Duration twoMonths = new DurationImpl(1, 0, 2, 0, 0, 0, 0); + private static final Duration threeYears = new DurationImpl(1, 3, 0, 0, 0, 0, 0); + private static final Duration y6m5d4h3m2s1 = new DurationImpl(1, 6, 5, 4, 3, 2, 1); + + public void testAddTo_Date() { + Date date = null; + try { + oneSecond.addTo(date); + } catch (NullPointerException expected) {} + + date = new Date(1000); + try { + oneSecond.addTo(date); + } catch (UnsupportedOperationException expected) {} + } + + public void testGetDays() { + assertEquals(0, oneSecond.getDays()); + assertEquals(0, tenMinutes.getDays()); + assertEquals(1, oneDay.getDays()); + assertEquals(5, fiveDays.getDays()); + assertEquals(0, twoMonths.getDays()); + assertEquals(4, y6m5d4h3m2s1.getDays()); + } + + public void testGetHours() { + assertEquals(0, oneSecond.getHours()); + assertEquals(0, tenMinutes.getHours()); + assertEquals(0, oneDay.getHours()); + assertEquals(0, fiveDays.getHours()); + assertEquals(0, twoMonths.getHours()); + assertEquals(3, y6m5d4h3m2s1.getHours()); + } + + public void testGetMinutes() { + assertEquals(0, oneSecond.getMinutes()); + assertEquals(10, tenMinutes.getMinutes()); + assertEquals(0, oneDay.getMinutes()); + assertEquals(0, fiveDays.getMinutes()); + assertEquals(0, twoMonths.getMinutes()); + assertEquals(2, y6m5d4h3m2s1.getMinutes()); + } + + public void testGetMonths() { + assertEquals(0, oneSecond.getMonths()); + assertEquals(0, tenMinutes.getMonths()); + assertEquals(0, oneDay.getMonths()); + assertEquals(0, fiveDays.getMonths()); + assertEquals(2, twoMonths.getMonths()); + assertEquals(0, threeYears.getMonths()); + assertEquals(5, y6m5d4h3m2s1.getMonths()); + } + + public void testGetSeconds() { + assertEquals(1, oneSecond.getSeconds()); + assertEquals(0, tenMinutes.getSeconds()); + assertEquals(0, oneDay.getSeconds()); + assertEquals(0, fiveDays.getSeconds()); + assertEquals(0, twoMonths.getSeconds()); + assertEquals(0, threeYears.getSeconds()); + assertEquals(1, y6m5d4h3m2s1.getSeconds()); + } + + public void testGetTimeInMillis_calendar() { + Calendar calendar = GregorianCalendar.getInstance(); + calendar.setTimeInMillis(1000); + try { + oneSecond.getTimeInMillis(calendar); + } catch (UnsupportedOperationException expected) {} + } + + public void testGetTimeInMillis_date() { + Date date = new Date(1000); + try { + oneSecond.getTimeInMillis(date); + } catch (UnsupportedOperationException expected) {} + } + + public void testGetXmlSchemaType() { + Duration allSet = new DurationImpl(1, 1, 1, 1, 1, 1, 1); + assertEquals(DatatypeConstants.DURATION, allSet.getXMLSchemaType()); + + Duration dayHourMinuteSecondSet = new DurationImpl(1, -1, -1, 1, 1, 1, 1); + assertEquals(DatatypeConstants.DURATION_DAYTIME, dayHourMinuteSecondSet.getXMLSchemaType()); + + Duration yearMonthSet = new DurationImpl(1, 1, 1, -1, -1, -1, -1); + assertEquals(DatatypeConstants.DURATION_YEARMONTH, yearMonthSet.getXMLSchemaType()); + + Duration noneSet = new DurationImpl(0, -1, -1, -1, -1, -1, -1); + try { + noneSet.getXMLSchemaType(); + fail("Unexpectedly didn't throw"); + } catch (IllegalStateException expected) {} + } + + public void testGetYears() { + assertEquals(0, oneSecond.getYears()); + assertEquals(0, tenMinutes.getYears()); + assertEquals(0, oneDay.getYears()); + assertEquals(0, fiveDays.getYears()); + assertEquals(0, twoMonths.getYears()); + assertEquals(3, threeYears.getYears()); + assertEquals(6, y6m5d4h3m2s1.getYears()); + } + + public void testIsLongerThan() { + final Duration[] durations = { + oneSecond, tenMinutes, oneDay, fiveDays, twoMonths, threeYears }; + for (int i = 0; i < durations.length - 1; i++) { + for (int j = i + 1; j < durations.length; j++) { + try { + assertTrue(durations[j].isLongerThan(durations[i])); + } catch (UnsupportedOperationException expected) {} + } + } + } + + public void testIsShorterThan() { + final Duration[] durations = { + oneSecond, tenMinutes, oneDay, fiveDays, twoMonths, threeYears }; + for (int i = 0; i < durations.length - 1; i++) { + for (int j = i + 1; j < durations.length; j++) { + try { + assertTrue(durations[i].isShorterThan(durations[j])); + } catch (UnsupportedOperationException expected) {} + } + } + } + + public void testMultiply() { + final Duration[] durations = { + oneSecond, tenMinutes, oneDay, fiveDays, twoMonths, threeYears }; + for (Duration d : durations) { + try { + d.multiply(2); + } catch (UnsupportedOperationException expected) {} + } + } + + public void testSubtract() { + final Duration[] durations = { + oneSecond, tenMinutes, oneDay, fiveDays, twoMonths, threeYears }; + for (Duration i : durations) { + for (Duration j : durations) { + try { + i.subtract(j); + } catch (UnsupportedOperationException expected) {} + } + } + } +} diff --git a/luni/src/test/java/libcore/javax/xml/datatype/XMLGregorianCalendarTest.java b/luni/src/test/java/libcore/javax/xml/datatype/XMLGregorianCalendarTest.java new file mode 100644 index 00000000000..012facc595c --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/datatype/XMLGregorianCalendarTest.java @@ -0,0 +1,251 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.datatype; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.GregorianCalendar; +import java.util.Locale; +import java.util.TimeZone; +import javax.xml.datatype.DatatypeConstants; +import javax.xml.datatype.Duration; +import javax.xml.datatype.XMLGregorianCalendar; +import javax.xml.namespace.QName; +import junit.framework.TestCase; + +public class XMLGregorianCalendarTest extends TestCase { + + private XMLGregorianCalendar calendar; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + calendar = new XMLGregorianCalendarImpl(); + } + + public void testGetMillisecond() { + assertEquals(DatatypeConstants.FIELD_UNDEFINED, calendar.getMillisecond()); + } + + public void testSetTime_iii() { + calendar.setTime(1, 2, 3); + assertEquals(1, calendar.getHour()); + assertEquals(2, calendar.getMinute()); + assertEquals(3, calendar.getSecond()); + } + + public void testSetTime_iiii() { + calendar.setTime(1, 2, 3, 4); + assertEquals(1, calendar.getHour()); + assertEquals(2, calendar.getMinute()); + assertEquals(3, calendar.getSecond()); + assertEquals(DatatypeConstants.FIELD_UNDEFINED, calendar.getMillisecond()); + } + + public void testSetTime_iiiBigDecimal() { + calendar.setTime(1, 2, 3, BigDecimal.valueOf(0.1)); + assertEquals(1, calendar.getHour()); + assertEquals(2, calendar.getMinute()); + assertEquals(3, calendar.getSecond()); + assertEquals(100, calendar.getMillisecond()); + assertEquals(BigDecimal.valueOf(0.1), calendar.getFractionalSecond()); + } + + /** + * Stub implementation intended for test coverage. + */ + private static final class XMLGregorianCalendarImpl extends XMLGregorianCalendar { + + private int year; + private int month; + private int day; + private int hour; + private int minute; + private int second; + private int millisecond; + private BigDecimal fractional; + private int timezoneOffset; + + @Override + public void clear() { + year = month = day = hour = minute = second = millisecond = timezoneOffset = 0; + fractional = BigDecimal.valueOf(0); + } + + @Override + public void reset() { + year = month = day = hour = minute = second = millisecond = timezoneOffset = 0; + fractional = BigDecimal.valueOf(0); + } + + @Override + public void setYear(BigInteger year) { + this.year = year.intValue(); + } + + @Override + public void setYear(int year) { + this.year = year; + } + + @Override + public void setMonth(int month) { + this.month = month; + } + + @Override + public void setDay(int day) { + this.day = day; + } + + @Override + public void setTimezone(int offset) { + this.timezoneOffset = offset; + } + + @Override + public void setHour(int hour) { + this.hour = hour; + } + + @Override + public void setMinute(int minute) { + this.minute = minute; + } + + @Override + public void setSecond(int second) { + this.second = second; + } + + @Override + public void setMillisecond(int millisecond) { + this.millisecond = millisecond; + } + + @Override + public void setFractionalSecond(BigDecimal fractional) { + this.fractional = fractional; + } + + @Override + public BigInteger getEon() { + return null; + } + + @Override + public int getYear() { + return year; + } + + @Override + public BigInteger getEonAndYear() { + return null; + } + + @Override + public int getMonth() { + return month; + } + + @Override + public int getDay() { + return day; + } + + @Override + public int getTimezone() { + return timezoneOffset; + } + + @Override + public int getHour() { + return hour; + } + + @Override + public int getMinute() { + return minute; + } + + @Override + public int getSecond() { + return second; + } + + @Override + public BigDecimal getFractionalSecond() { + return fractional; + } + + @Override + public int compare(XMLGregorianCalendar rhs) { + if (year != rhs.getYear()) return year - rhs.getYear(); + if (month != rhs.getMonth()) return month - rhs.getMonth(); + if (day != rhs.getDay()) return day - rhs.getDay(); + if (hour != rhs.getHour()) return hour - rhs.getHour(); + if (minute != rhs.getMinute()) return minute - rhs.getMinute(); + if (second != rhs.getSecond()) return second - rhs.getSecond(); + if (millisecond != rhs.getMillisecond()) return millisecond - getMillisecond(); + return fractional.subtract(rhs.getFractionalSecond()).intValue(); + } + + @Override + public XMLGregorianCalendar normalize() { + return null; + } + + @Override + public String toXMLFormat() { + return null; + } + + @Override + public QName getXMLSchemaType() { + return null; + } + + @Override + public boolean isValid() { + return false; + } + + @Override + public void add(Duration duration) {} + + @Override + public GregorianCalendar toGregorianCalendar() { + return null; + } + + @Override + public GregorianCalendar toGregorianCalendar(TimeZone timezone, Locale aLocale, + XMLGregorianCalendar defaults) { + return null; + } + + @Override + public TimeZone getTimeZone(int defaultZoneoffset) { + return null; + } + + @Override + public Object clone() { + return null; + } + } +} diff --git a/luni/src/test/java/libcore/javax/xml/namespace/QNameTest.java b/luni/src/test/java/libcore/javax/xml/namespace/QNameTest.java new file mode 100644 index 00000000000..8214c1132ba --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/namespace/QNameTest.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.namespace; + +import javax.xml.namespace.QName; +import junit.framework.TestCase; + +public class QNameTest extends TestCase { + + public void testConstructor() { + QName qName = new QName("hello"); + assertEquals("", qName.getNamespaceURI()); + assertEquals("", qName.getPrefix()); + assertEquals("hello", qName.getLocalPart()); + } + + public void testGetNamespaceURI() { + QName qName = new QName("namespace", "local part", "prefix"); + assertEquals("namespace", qName.getNamespaceURI()); + assertEquals("local part", qName.getLocalPart()); + assertEquals("prefix", qName.getPrefix()); + + qName = new QName(null, "local part", "prefix"); + assertEquals("", qName.getNamespaceURI()); + assertEquals("local part", qName.getLocalPart()); + assertEquals("prefix", qName.getPrefix()); + } + + public void testGetPrefix() { + QName qName = new QName("namespace", "local part", "prefix"); + assertEquals("prefix", qName.getPrefix()); + + try { + new QName("namespace", "local part", null); + fail("Unexpectedly didn't throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) {} + } +} diff --git a/luni/src/test/java/libcore/javax/xml/parsers/DocumentBuilderFactoryTest.java b/luni/src/test/java/libcore/javax/xml/parsers/DocumentBuilderFactoryTest.java new file mode 100644 index 00000000000..ae9a04503f1 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/parsers/DocumentBuilderFactoryTest.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.parsers; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.FactoryConfigurationError; +import javax.xml.validation.Schema; +import javax.xml.validation.Validator; +import javax.xml.validation.ValidatorHandler; +import junit.framework.TestCase; + +public class DocumentBuilderFactoryTest extends TestCase { + + public void testGetSchema() { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + + try { + factory.getSchema(); + fail("Unexpectedly didn't throw UnsupportedOperationException"); + } catch (UnsupportedOperationException expected) {} + } + + public void testSetSchema() { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + + try { + factory.setSchema(new Schema() { + @Override + public Validator newValidator() { return null; } + + @Override + public ValidatorHandler newValidatorHandler() { return null; } + }); + fail("Unexpectedly didn't throw UnsupportedOperationException"); + } catch (UnsupportedOperationException expected) {} + } + + public void testNewInstance_StringClassLoader() { + try { + DocumentBuilderFactory.newInstance(null, null); + } catch (FactoryConfigurationError expected) {} + } +} diff --git a/luni/src/test/java/libcore/javax/xml/parsers/DocumentBuilderTest.java b/luni/src/test/java/libcore/javax/xml/parsers/DocumentBuilderTest.java index 646776a935d..e643bfd25ab 100644 --- a/luni/src/test/java/libcore/javax/xml/parsers/DocumentBuilderTest.java +++ b/luni/src/test/java/libcore/javax/xml/parsers/DocumentBuilderTest.java @@ -16,7 +16,17 @@ package libcore.javax.xml.parsers; -import static tests.support.Support_Xml.*; +import static tests.support.Support_Xml.attrOf; +import static tests.support.Support_Xml.domOf; +import static tests.support.Support_Xml.firstChildTextOf; +import static tests.support.Support_Xml.firstElementOf; + +import javax.xml.parsers.DocumentBuilder; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Document; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; public class DocumentBuilderTest extends junit.framework.TestCase { // http://code.google.com/p/android/issues/detail?id=2607 @@ -61,4 +71,35 @@ public class DocumentBuilderTest extends junit.framework.TestCase { // Expected. } } + + public void testGetSchema() { + DocumentBuilder db = new DocumentBuilder() { + @Override + public Document parse(InputSource is) { return null; } + + @Override + public boolean isNamespaceAware() { return false; } + + @Override + public boolean isValidating() { return false; } + + @Override + public void setEntityResolver(EntityResolver er) {} + + @Override + public void setErrorHandler(ErrorHandler eh) {} + + @Override + public Document newDocument() { return null; } + + @Override + public DOMImplementation getDOMImplementation() { return null; } + }; + + try { + db.getSchema(); + fail("Unexpectedly didn't throw UnsupportedOperationException"); + } catch (UnsupportedOperationException expected) {} + } + } diff --git a/luni/src/test/java/libcore/javax/xml/transform/SourceLocatorImpl.java b/luni/src/test/java/libcore/javax/xml/transform/SourceLocatorImpl.java new file mode 100644 index 00000000000..8219c310080 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/transform/SourceLocatorImpl.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.transform; + +import javax.xml.transform.SourceLocator; + +public class SourceLocatorImpl implements SourceLocator { + @Override + public String getPublicId() { return null; } + + @Override + public String getSystemId() { return null; } + + @Override + public int getLineNumber() { return 0; } + + @Override + public int getColumnNumber() { return 0; } +}
\ No newline at end of file diff --git a/luni/src/test/java/libcore/javax/xml/transform/TransformerConfigurationExceptionTest.java b/luni/src/test/java/libcore/javax/xml/transform/TransformerConfigurationExceptionTest.java new file mode 100644 index 00000000000..c72af960860 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/transform/TransformerConfigurationExceptionTest.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.transform; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import javax.xml.transform.SourceLocator; +import javax.xml.transform.TransformerConfigurationException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class TransformerConfigurationExceptionTest { + + @Test + public void constructor() { + TransformerConfigurationException e = new TransformerConfigurationException(); + assertEquals("Configuration Error", e.getMessage()); + assertNull(e.getCause()); + } + + @Test + public void constructorWithStringAndThrowable() { + Throwable t = new Throwable(); + TransformerConfigurationException e = new TransformerConfigurationException("message", t); + assertEquals("message", e.getMessage()); + assertEquals(t, e.getCause()); + } + + @Test + public void constructorWithStringAndSourceLocator() { + SourceLocator locator = new SourceLocatorImpl(); + TransformerConfigurationException e = new TransformerConfigurationException("message", + locator); + assertEquals("message", e.getMessage()); + assertNull(e.getCause()); + assertEquals(locator, e.getLocator()); + } + + @Test + public void constructorWithStringSourceLocatorAndThrowable() { + SourceLocator locator = new SourceLocatorImpl(); + Throwable t = new Throwable(); + TransformerConfigurationException e = new TransformerConfigurationException("message", + locator, t); + assertEquals("message", e.getMessage()); + assertEquals(t, e.getCause()); + assertEquals(locator, e.getLocator()); + } + + @Test + public void constructorWithThrowable() { + Throwable t = new Throwable(); + TransformerConfigurationException e = new TransformerConfigurationException(t); + assertEquals("java.lang.Throwable", e.getMessage()); + assertEquals(t, e.getCause()); + } +} diff --git a/luni/src/test/java/libcore/javax/xml/transform/TransformerExceptionTest.java b/luni/src/test/java/libcore/javax/xml/transform/TransformerExceptionTest.java new file mode 100644 index 00000000000..83bad0661cb --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/transform/TransformerExceptionTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.transform; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import javax.xml.transform.SourceLocator; +import javax.xml.transform.TransformerException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class TransformerExceptionTest { + + @Test + public void constructorWithStringAndThrowable() { + Throwable t = new Throwable(); + TransformerException e = new TransformerException(t); + assertEquals("java.lang.Throwable", e.getMessage()); + assertEquals(t, e.getCause()); + } + + @Test + public void constructorWithStringAndSourceLocator() { + SourceLocator locator = new SourceLocatorImpl(); + TransformerException e = new TransformerException("message", locator); + assertEquals("message", e.getMessage()); + assertNull(e.getCause()); + assertEquals(locator, e.getLocator()); + } + + @Test + public void constructorWithStringSourceLocatorAndThrowable() { + SourceLocator locator = new SourceLocatorImpl(); + Throwable t = new Throwable(); + TransformerException e = new TransformerException("message", locator, t); + assertEquals("message", e.getMessage()); + assertEquals(t, e.getCause()); + assertEquals(locator, e.getLocator()); + } + + @Test + public void getException() { + TransformerException e = new TransformerException("message"); + Throwable t = e.getException(); + assertEquals("message", e.getMessage()); + assertEquals(t, e.getCause()); + } + + @Test + public void getMessageAndLocation() { + SourceLocator locator = new SourceLocatorImpl(); + TransformerException e = new TransformerException("message", locator); + assertEquals("message", e.getMessageAndLocation()); + } + +}
\ No newline at end of file diff --git a/luni/src/test/java/libcore/javax/xml/transform/TransformerFactoryConfigurationErrorTest.java b/luni/src/test/java/libcore/javax/xml/transform/TransformerFactoryConfigurationErrorTest.java new file mode 100644 index 00000000000..ba8ab5268dc --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/transform/TransformerFactoryConfigurationErrorTest.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.transform; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import javax.xml.transform.TransformerFactoryConfigurationError; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class TransformerFactoryConfigurationErrorTest { + + @Test + public void constructor() { + TransformerFactoryConfigurationError e = new TransformerFactoryConfigurationError(); + assertNull(e.getMessage()); + assertNull(e.getCause()); + } + + @Test + public void constructorWithException() { + Exception ex = new Exception("message"); + TransformerFactoryConfigurationError e = new TransformerFactoryConfigurationError(ex); + assertEquals("java.lang.Exception: message", e.getMessage()); + assertEquals(ex, e.getException()); + assertNull(e.getCause()); + } + + @Test + public void constructorWithExceptionAndString() { + Exception ex = new Exception("message"); + TransformerFactoryConfigurationError e = new TransformerFactoryConfigurationError(ex, + "another message"); + assertEquals("another message", e.getMessage()); + assertEquals(ex, e.getException()); + assertNull(e.getCause()); + } + + @Test + public void constructorWithString() { + TransformerFactoryConfigurationError e = new TransformerFactoryConfigurationError("message"); + assertEquals("message", e.getMessage()); + assertNull(e.getCause()); + } + + @Test + public void getException() { + TransformerFactoryConfigurationError e = new TransformerFactoryConfigurationError("message"); + Throwable t = e.getException(); + assertEquals("message", e.getMessage()); + assertEquals(t, e.getCause()); + } +} diff --git a/luni/src/test/java/libcore/javax/xml/transform/TransformerFactoryTest.java b/luni/src/test/java/libcore/javax/xml/transform/TransformerFactoryTest.java new file mode 100644 index 00000000000..0269e49ef6e --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/transform/TransformerFactoryTest.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.transform; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerFactoryConfigurationError; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class TransformerFactoryTest { + + @Test + public void newInstanceOfStringAndClassLoader() { + assertThrows(TransformerFactoryConfigurationError.class, + () -> TransformerFactory.newInstance(null, null)); + + try { + TransformerFactory.newInstance("nonexistent class", null); + } catch (TransformerFactoryConfigurationError expected) { + assertEquals(ClassNotFoundException.class, expected.getException().getClass()); + } + } +} diff --git a/luni/src/test/java/libcore/javax/xml/transform/TransformerTest.java b/luni/src/test/java/libcore/javax/xml/transform/TransformerTest.java new file mode 100644 index 00000000000..6c44a907d48 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/transform/TransformerTest.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.transform; + +import java.util.Properties; +import javax.xml.transform.ErrorListener; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.URIResolver; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class TransformerTest { + + @Test(expected = UnsupportedOperationException.class) + public void reset() { + Transformer transformer = new TransformerImpl(); + transformer.reset(); + } + + private static final class TransformerImpl extends Transformer { + + @Override + public void reset() { super.reset(); } + + @Override + public void transform(Source xmlSource, Result outputTarget) throws TransformerException {} + + @Override + public void setParameter(String name, Object value) {} + + @Override + public Object getParameter(String name) { return null; } + + @Override + public void clearParameters() {} + + @Override + public void setURIResolver(URIResolver resolver) {} + + @Override + public URIResolver getURIResolver() { return null; } + + @Override + public void setOutputProperties(Properties oformat) {} + + @Override + public Properties getOutputProperties() { return null; } + + @Override + public void setOutputProperty(String name, String value) throws IllegalArgumentException {} + + @Override + public String getOutputProperty(String name) throws IllegalArgumentException { + return null; + } + + @Override + public void setErrorListener(ErrorListener listener) throws IllegalArgumentException {} + + @Override + public ErrorListener getErrorListener() { return null; } + } +} diff --git a/luni/src/test/java/libcore/javax/xml/transform/dom/DOMResultTest.java b/luni/src/test/java/libcore/javax/xml/transform/dom/DOMResultTest.java new file mode 100644 index 00000000000..6f9eacc17cd --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/transform/dom/DOMResultTest.java @@ -0,0 +1,360 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.transform.dom; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.io.IOException; +import java.util.ArrayList; +import javax.xml.transform.dom.DOMResult; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.w3c.dom.Attr; +import org.w3c.dom.CDATASection; +import org.w3c.dom.Comment; +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.DocumentType; +import org.w3c.dom.Element; +import org.w3c.dom.EntityReference; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.ProcessingInstruction; +import org.w3c.dom.Text; +import org.w3c.dom.UserDataHandler; +import org.xml.sax.SAXException; + +@RunWith(JUnit4.class) +public class DOMResultTest { + + private Node node; + private Node documentNode; + + @Before + public void setUp() throws SAXException, IOException { + node = new NodeImpl(); + documentNode = new NodeImpl(); + } + + @Test + public void constructorWithNode() { + DOMResult result = new DOMResult(node); + assertNotNull(result); + assertEquals(node, result.getNode()); + } + + @Test + public void constructorWithNodeAndString() { + DOMResult result = new DOMResult(node, "systemId"); + assertNotNull(result); + assertEquals(node, result.getNode()); + assertEquals("systemId", result.getSystemId()); + } + + @Test + public void constructorWithNodeAndNode() { + documentNode.appendChild(node); + DOMResult result = new DOMResult(documentNode, node); + assertNotNull(result); + assertEquals(documentNode, result.getNode()); + assertEquals(node, result.getNextSibling()); + } + + @Test + public void constructorWithNodeNodeAndString() { + documentNode.appendChild(node); + DOMResult result = new DOMResult(documentNode, node, "systemId"); + assertNotNull(result); + assertEquals(documentNode, result.getNode()); + assertEquals("systemId", result.getSystemId()); + assertEquals(node, result.getNextSibling()); + } + + @Test + public void getSetSystemId() { + DOMResult result = new DOMResult(node); + assertNull(result.getSystemId()); + + result.setSystemId("systemId"); + assertEquals("systemId", result.getSystemId()); + } + + @Test + public void getSetNextSibling() { + DOMResult result = new DOMResult(node); + assertNull(result.getNextSibling()); + + Node anotherNode = new NodeImpl(); + result.setNextSibling(anotherNode); + assertNotEquals(node, result.getNextSibling()); + assertEquals(anotherNode, result.getNextSibling()); + } + + private static final class NodeImpl implements Node, Document { + + private final ArrayList<Node> children; + + public NodeImpl() { + this.children = new ArrayList<>(); + } + + @Override + public String getNodeName() { return null; } + + @Override + public String getNodeValue() throws DOMException { return null; } + + @Override + public void setNodeValue(String nodeValue) throws DOMException {} + + @Override + public short getNodeType() { return 0; } + + @Override + public Node getParentNode() { return null; } + + @Override + public NodeList getChildNodes() { return new NodeListImpl(children); } + + @Override + public Node getFirstChild() { return children.get(0); } + + @Override + public Node getLastChild() { return children.get(children.size() - 1); } + + @Override + public Node getPreviousSibling() { return null; } + + @Override + public Node getNextSibling() { return null; } + + @Override + public NamedNodeMap getAttributes() { return null; } + + @Override + public Document getOwnerDocument() { return null; } + + @Override + public Node insertBefore(Node newChild, Node refChild) throws DOMException { return null; } + + @Override + public Node replaceChild(Node newChild, Node oldChild) throws DOMException { return null; } + + @Override + public Node removeChild(Node oldChild) throws DOMException { return null; } + + @Override + public Node appendChild(Node newChild) throws DOMException { return null; } + + @Override + public boolean hasChildNodes() { return false; } + + @Override + public Node cloneNode(boolean deep) { return null; } + + @Override + public void normalize() {} + + @Override + public boolean isSupported(String feature, String version) { return false; } + + @Override + public String getNamespaceURI() { return null; } + + @Override + public String getPrefix() { return null; } + + @Override + public void setPrefix(String prefix) throws DOMException {} + + @Override + public String getLocalName() { return null; } + + @Override + public boolean hasAttributes() { return false; } + + @Override + public String getBaseURI() { return null; } + + @Override + public short compareDocumentPosition(Node other) throws DOMException { + return DOCUMENT_POSITION_CONTAINED_BY; + } + + @Override + public String getTextContent() throws DOMException { return null; } + + @Override + public void setTextContent(String textContent) throws DOMException {} + + @Override + public boolean isSameNode(Node other) { return false; } + + @Override + public String lookupPrefix(String namespaceURI) { return null; } + + @Override + public boolean isDefaultNamespace(String namespaceURI) { return false; } + + @Override + public String lookupNamespaceURI(String prefix) { return null; } + + @Override + public boolean isEqualNode(Node arg) { return false; } + + @Override + public Object getFeature(String feature, String version) { return null; } + + @Override + public Object setUserData(String key, Object data, UserDataHandler handler) { return null; } + + @Override + public Object getUserData(String key) { return null; } + + @Override + public DocumentType getDoctype() { return null; } + + @Override + public DOMImplementation getImplementation() { return null; } + + @Override + public Element getDocumentElement() { return null; } + + @Override + public Element createElement(String tagName) throws DOMException { return null; } + + @Override + public DocumentFragment createDocumentFragment() { return null; } + + @Override + public Text createTextNode(String data) { return null; } + + @Override + public Comment createComment(String data) { return null; } + + @Override + public CDATASection createCDATASection(String data) throws DOMException { return null; } + + @Override + public ProcessingInstruction createProcessingInstruction(String target, String data) + throws DOMException { return null; } + + @Override + public Attr createAttribute(String name) throws DOMException { return null; } + + @Override + public EntityReference createEntityReference(String name) throws DOMException { + return null; + } + + @Override + public NodeList getElementsByTagName(String tagname) { return null; } + + @Override + public Node importNode(Node importedNode, boolean deep) throws DOMException { + return null; + } + + @Override + public Element createElementNS(String namespaceURI, String qualifiedName) + throws DOMException { return null; } + + @Override + public Attr createAttributeNS(String namespaceURI, String qualifiedName) + throws DOMException { return null; } + + @Override + public NodeList getElementsByTagNameNS(String namespaceURI, String localName) { + return null; + } + + @Override + public Element getElementById(String elementId) { return null; } + + @Override + public String getInputEncoding() { return null; } + + @Override + public String getXmlEncoding() { return null; } + + @Override + public boolean getXmlStandalone() { return false; } + + @Override + public void setXmlStandalone(boolean xmlStandalone) throws DOMException {} + + @Override + public String getXmlVersion() { return null; } + + @Override + public void setXmlVersion(String xmlVersion) throws DOMException {} + + @Override + public boolean getStrictErrorChecking() { return false; } + + @Override + public void setStrictErrorChecking(boolean strictErrorChecking) {} + + @Override + public String getDocumentURI() { return null; } + + @Override + public void setDocumentURI(String documentURI) {} + + @Override + public Node adoptNode(Node source) throws DOMException { return null; } + + @Override + public DOMConfiguration getDomConfig() { return null; } + + @Override + public void normalizeDocument() {} + + @Override + public Node renameNode(Node n, String namespaceURI, String qualifiedName) + throws DOMException { return null; } + } + + private static final class NodeListImpl implements NodeList { + + private final ArrayList<Node> list; + + public NodeListImpl(ArrayList<Node> list) { + this.list = list; + } + + @Override + public Node item(int index) { + return list.get(index); + } + + @Override + public int getLength() { + return list.size(); + } + } + +} diff --git a/luni/src/test/java/libcore/javax/xml/transform/dom/DOMSourceTest.java b/luni/src/test/java/libcore/javax/xml/transform/dom/DOMSourceTest.java new file mode 100644 index 00000000000..2c18046b67f --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/transform/dom/DOMSourceTest.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.transform.dom; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import javax.xml.transform.dom.DOMSource; +import org.apache.harmony.xml.dom.CDATASectionImpl; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.w3c.dom.Node; + +@RunWith(JUnit4.class) +public class DOMSourceTest { + + @Test + public void constructor() { + DOMSource source = new DOMSource(); + assertNotNull(source); + assertNull(source.getNode()); + assertNull(source.getSystemId()); + } + + @Test + public void constructorWithNodeAndString() { + Node node = new CDATASectionImpl(null, ""); + DOMSource source = new DOMSource(node, "systemId"); + assertEquals(node, source.getNode()); + assertEquals("systemId", source.getSystemId()); + } + + @Test + public void setSystemId() { + DOMSource source = new DOMSource(); + assertNull(source.getSystemId()); + + source.setSystemId("systemId"); + assertEquals("systemId", source.getSystemId()); + } +} diff --git a/luni/src/test/java/libcore/javax/xml/transform/sax/SAXResultTest.java b/luni/src/test/java/libcore/javax/xml/transform/sax/SAXResultTest.java new file mode 100644 index 00000000000..becccbbb231 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/transform/sax/SAXResultTest.java @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.transform.sax; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import javax.xml.transform.sax.SAXResult; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.xml.sax.ContentHandler; +import org.xml.sax.ext.DefaultHandler2; +import org.xml.sax.ext.LexicalHandler; +import org.xml.sax.helpers.DefaultHandler; + +@RunWith(JUnit4.class) +public class SAXResultTest { + + private SAXResult result; + + @Before + public void setUp() { + result = new SAXResult(); + } + + @Test + public void constructor() { + result = new SAXResult(); + assertNull(result.getHandler()); + assertNull(result.getLexicalHandler()); + assertNull(result.getSystemId()); + } + + @Test + public void constructor_withContentHandler() { + ContentHandler handler = new DefaultHandler(); + result = new SAXResult(handler); + assertEquals(handler, result.getHandler()); + assertNull(result.getLexicalHandler()); + assertNull(result.getSystemId()); + } + + @Test + public void getSetHandler() { + assertNull(result.getHandler()); + + ContentHandler handler = new DefaultHandler(); + result.setHandler(handler); + assertEquals(handler, result.getHandler()); + + result.setHandler(null); + assertNull(result.getHandler()); + } + + @Test + public void getSetLexicalHandler() { + assertNull(result.getLexicalHandler()); + + LexicalHandler handler = new DefaultHandler2(); + result.setLexicalHandler(handler); + assertEquals(handler, result.getLexicalHandler()); + + result.setLexicalHandler(null); + assertNull(result.getLexicalHandler()); + } + + @Test + public void getSetSystemId() { + assertNull(result.getSystemId()); + + String systemId = "systemId"; + result.setSystemId(systemId); + assertEquals(systemId, result.getSystemId()); + + result.setSystemId(null); + assertNull(result.getSystemId()); + } + +} diff --git a/luni/src/test/java/libcore/javax/xml/transform/sax/SAXSourceTest.java b/luni/src/test/java/libcore/javax/xml/transform/sax/SAXSourceTest.java new file mode 100644 index 00000000000..9a35869781e --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/transform/sax/SAXSourceTest.java @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.transform.sax; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.io.IOException; +import javax.xml.transform.sax.SAXSource; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.xml.sax.ContentHandler; +import org.xml.sax.DTDHandler; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.XMLReader; + +@RunWith(JUnit4.class) +public class SAXSourceTest { + + private SAXSource source; + + @Before + public void setUp() { + source = new SAXSource(); + } + + @Test + public void constructor() { + source = new SAXSource(); + assertNull(source.getInputSource()); + assertNull(source.getXMLReader()); + } + + @Test + public void constructor_withXMLReaderAndInputSource() { + XMLReader reader = new XMLReaderImpl(); + InputSource is = new InputSource(); + source = new SAXSource(reader, is); + assertEquals(reader, source.getXMLReader()); + assertEquals(is, source.getInputSource()); + } + + @Test + public void setInputSource() { + InputSource is = new InputSource(); + source.setInputSource(is); + assertEquals(is, source.getInputSource()); + + source.setInputSource(null); + assertNull(source.getInputSource()); + } + + @Test + public void setXMLReader() { + XMLReader reader = new XMLReaderImpl(); + source.setXMLReader(reader); + assertEquals(reader, source.getXMLReader()); + + source.setXMLReader(null); + assertNull(source.getXMLReader()); + } + + private static final class XMLReaderImpl implements XMLReader { + + @Override + public boolean getFeature(String name) + throws SAXNotRecognizedException, SAXNotSupportedException { return false; } + + @Override + public void setFeature(String name, boolean value) + throws SAXNotRecognizedException, SAXNotSupportedException {} + + @Override + public Object getProperty(String name) + throws SAXNotRecognizedException, SAXNotSupportedException { return null; } + + @Override + public void setProperty(String name, Object value) + throws SAXNotRecognizedException, SAXNotSupportedException {} + + @Override + public void setEntityResolver(EntityResolver resolver) {} + + @Override + public EntityResolver getEntityResolver() { return null; } + + @Override + public void setDTDHandler(DTDHandler handler) {} + + @Override + public DTDHandler getDTDHandler() { return null; } + + @Override + public void setContentHandler(ContentHandler handler) {} + + @Override + public ContentHandler getContentHandler() { return null; } + + @Override + public void setErrorHandler(ErrorHandler handler) {} + + @Override + public ErrorHandler getErrorHandler() { return null; } + + @Override + public void parse(InputSource input) throws IOException, SAXException {} + + @Override + public void parse(String systemId) throws IOException, SAXException {} + } +} diff --git a/luni/src/test/java/libcore/javax/xml/transform/stream/StreamResultTest.java b/luni/src/test/java/libcore/javax/xml/transform/stream/StreamResultTest.java new file mode 100644 index 00000000000..f7e154184ee --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/transform/stream/StreamResultTest.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.transform.stream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.io.File; +import javax.xml.transform.stream.StreamResult; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class StreamResultTest { + + private StreamResult result; + + @Before + public void setUp() { + result = new StreamResult(); + } + + @Test + public void constructor() { + result = new StreamResult(); + assertNotNull(result); + assertNull(result.getSystemId()); + } + + @Test + public void constructor_File() { + result = new StreamResult(new File("path")); + assertEquals("file:///path", result.getSystemId()); + } + + @Test + public void constructor_String() { + result = new StreamResult((String)null); + assertNull(result.getSystemId()); + + result = new StreamResult(""); + assertEquals("", result.getSystemId()); + + result = new StreamResult("some string"); + assertEquals("some string", result.getSystemId()); + } + + @Test + public void getSystemId() { + assertNull(result.getSystemId()); + + result = new StreamResult("string constructor"); + assertEquals("string constructor", result.getSystemId()); + + result = new StreamResult(new File("path")); + assertEquals("file:///path", result.getSystemId()); + + result.setSystemId("hello"); + assertEquals("hello", result.getSystemId()); + } + + @Test + public void setSystemId_File() { + result.setSystemId(new File("path")); + assertEquals("file:///path", result.getSystemId()); + + result.setSystemId(new File(".")); + assertEquals("file:///.", result.getSystemId()); + } + + @Test + public void setSystemId_String() { + result.setSystemId((String)null); + assertNull(result.getSystemId()); + + result.setSystemId("hello"); + assertEquals("hello", result.getSystemId()); + } +} diff --git a/luni/src/test/java/libcore/javax/xml/transform/stream/StreamSourceTest.java b/luni/src/test/java/libcore/javax/xml/transform/stream/StreamSourceTest.java new file mode 100644 index 00000000000..b05ad33c96d --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/transform/stream/StreamSourceTest.java @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.transform.stream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.io.ByteArrayInputStream; +import java.io.CharArrayReader; +import java.io.File; +import java.io.InputStream; +import java.io.Reader; +import javax.xml.transform.stream.StreamSource; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class StreamSourceTest { + + private StreamSource source; + + @Before + public void setUp() { + source = new StreamSource(); + } + + @Test + public void constructor() { + source = new StreamSource(); + assertNull(source.getSystemId()); + assertNull(source.getPublicId()); + assertNull(source.getInputStream()); + assertNull(source.getReader()); + } + + @Test + public void constructor_withInputStreamAndString() { + InputStream is = new ByteArrayInputStream(new byte[]{ 0x00 }); + String systemId = "systemId"; + source = new StreamSource(is, systemId); + assertEquals(is, source.getInputStream()); + assertEquals(systemId, source.getSystemId()); + } + + @Test + public void constructor_withReaderAndString() { + Reader reader = new CharArrayReader(new char[]{ 'a' }); + String systemId = "systemId"; + source = new StreamSource(reader, systemId); + assertEquals(reader, source.getReader()); + assertEquals(systemId, source.getSystemId()); + } + + @Test + public void constructor_withString() { + String systemId = "systemId"; + source = new StreamSource(systemId); + assertEquals(systemId, source.getSystemId()); + } + + @Test + public void setInputStream() { + InputStream is = new ByteArrayInputStream(new byte[]{ 0x00 }); + source.setInputStream(is); + assertEquals(is, source.getInputStream()); + + source.setInputStream(null); + assertNull(source.getInputStream()); + } + + @Test + public void setPublicId() { + String publicId = "publicId"; + source.setPublicId(publicId); + assertEquals(publicId, source.getPublicId()); + + source.setPublicId(null); + assertNull(source.getPublicId()); + } + + @Test + public void setSystemId_withFile() { + source.setSystemId(new File("path")); + assertEquals("file:///path", source.getSystemId()); + + source.setSystemId(new File(".")); + assertEquals("file:///.", source.getSystemId()); + } + + @Test + public void setSystemId_withString() { + String systemId = "systemId"; + source.setSystemId(systemId); + assertEquals(systemId, source.getSystemId()); + + source.setSystemId((String)null); + assertNull(source.getSystemId()); + } +} diff --git a/luni/src/test/java/libcore/javax/xml/validation/ValidatorHandlerTest.java b/luni/src/test/java/libcore/javax/xml/validation/ValidatorHandlerTest.java new file mode 100644 index 00000000000..b03e2886ea7 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/validation/ValidatorHandlerTest.java @@ -0,0 +1,179 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.validation; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; + +import javax.xml.validation.TypeInfoProvider; +import javax.xml.validation.ValidatorHandler; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.w3c.dom.ls.LSResourceResolver; +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.ErrorHandler; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; + +@RunWith(JUnit4.class) +public class ValidatorHandlerTest { + + private ValidatorHandler handler; + + @Before + public void setUp() { + handler = new ValidatorHandlerImpl(); + } + + @Test + public void constructor() { + handler = new ValidatorHandlerImpl(); + assertNotNull(handler); + } + + @Test + public void getFeature() { + assertThrows(NullPointerException.class, () -> handler.getFeature(null)); + assertThrows(SAXNotRecognizedException.class, () -> handler.getFeature("hello")); + assertThrows(SAXNotRecognizedException.class, () -> handler.getFeature("")); + } + + @Test + public void getProperty() { + assertThrows(NullPointerException.class, () -> handler.getFeature(null)); + assertThrows(SAXNotRecognizedException.class, () -> handler.getFeature("hello")); + assertThrows(SAXNotRecognizedException.class, () -> handler.getFeature("")); + } + + @Test + public void setFeature() { + assertThrows(NullPointerException.class, + () -> handler.setFeature(null, false)); + assertThrows(NullPointerException.class, + () -> handler.setFeature(null, true)); + + String[] features = {"", "hello", "feature"}; + boolean[] trueAndFalse = {true, false}; + for (String feature : features) { + for (boolean value : trueAndFalse) { + assertThrows(SAXNotRecognizedException.class, + () -> handler.setFeature(feature, value)); + } + } + } + + @Test + public void setProperty() { + assertThrows(NullPointerException.class, + () -> handler.setProperty(null, false)); + assertThrows(NullPointerException.class, + () -> handler.setProperty(null, true)); + + String[] properties = {"", "hello", "property"}; + boolean[] trueAndFalse = {true, false}; + for (String property : properties) { + for (boolean value : trueAndFalse) { + assertThrows(SAXNotRecognizedException.class, + () -> handler.setProperty(property, value)); + } + } + } + + private static final class ValidatorHandlerImpl extends ValidatorHandler { + + @Override + public ContentHandler getContentHandler() { + return null; + } + + @Override + public void setContentHandler(ContentHandler receiver) { + } + + @Override + public ErrorHandler getErrorHandler() { + return null; + } + + @Override + public void setErrorHandler(ErrorHandler errorHandler) { + } + + @Override + public LSResourceResolver getResourceResolver() { + return null; + } + + @Override + public void setResourceResolver(LSResourceResolver resourceResolver) { + } + + @Override + public TypeInfoProvider getTypeInfoProvider() { + return null; + } + + @Override + public void setDocumentLocator(Locator locator) { + } + + @Override + public void startDocument() throws SAXException { + } + + @Override + public void endDocument() throws SAXException { + } + + @Override + public void startPrefixMapping(String prefix, String uri) throws SAXException { + } + + @Override + public void endPrefixMapping(String prefix) throws SAXException { + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes atts) + throws SAXException { + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + } + + @Override + public void characters(char[] ch, int start, int length) throws SAXException { + } + + @Override + public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { + } + + @Override + public void processingInstruction(String target, String data) throws SAXException { + } + + @Override + public void skippedEntity(String name) throws SAXException { + } + } +} diff --git a/luni/src/test/java/libcore/javax/xml/validation/ValidatorTest.java b/luni/src/test/java/libcore/javax/xml/validation/ValidatorTest.java new file mode 100644 index 00000000000..bc3bd9771f5 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/validation/ValidatorTest.java @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.validation; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; + +import java.io.IOException; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.sax.SAXSource; +import javax.xml.validation.Validator; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.w3c.dom.ls.LSResourceResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; + +@RunWith(JUnit4.class) +public class ValidatorTest { + + private Validator validator; + + @Before + public void setUp() { + validator = new ValidatorImpl(); + } + + @Test + public void constructor() { + validator = new ValidatorImpl(); + assertNotNull(validator); + } + + @Test + public void getFeature() { + assertThrows(NullPointerException.class, () -> validator.getFeature(null)); + assertThrows(SAXNotRecognizedException.class, () -> validator.getFeature("hello")); + assertThrows(SAXNotRecognizedException.class, () -> validator.getFeature("")); + } + + @Test + public void getProperty() { + assertThrows(NullPointerException.class, () -> validator.getFeature(null)); + assertThrows(SAXNotRecognizedException.class, () -> validator.getFeature("hello")); + assertThrows(SAXNotRecognizedException.class, () -> validator.getFeature("")); + } + + @Test + public void setFeature() { + assertThrows(NullPointerException.class, + () -> validator.setFeature(null, false)); + assertThrows(NullPointerException.class, + () -> validator.setFeature(null, true)); + + String[] features = {"", "hello", "feature"}; + boolean[] trueAndFalse = {true, false}; + for (String feature : features) { + for (boolean value : trueAndFalse) { + assertThrows(SAXNotRecognizedException.class, + () -> validator.setFeature(feature, value)); + } + } + } + + @Test + public void setProperty() { + assertThrows(NullPointerException.class, + () -> validator.setProperty(null, false)); + assertThrows(NullPointerException.class, + () -> validator.setProperty(null, true)); + + String[] properties = {"", "hello", "property"}; + boolean[] trueAndFalse = {true, false}; + for (String property : properties) { + for (boolean value : trueAndFalse) { + assertThrows(SAXNotRecognizedException.class, + () -> validator.setProperty(property, value)); + } + } + } + + @Test + public void validate() throws IOException, SAXException { + Source source = new SAXSource(); + validator.validate(source); + } + + private static final class ValidatorImpl extends Validator { + + @Override + public void reset() { + } + + @Override + public void validate(Source source, Result result) throws SAXException, IOException { + } + + @Override + public ErrorHandler getErrorHandler() { + return null; + } + + @Override + public void setErrorHandler(ErrorHandler errorHandler) { + } + + @Override + public LSResourceResolver getResourceResolver() { + return null; + } + + @Override + public void setResourceResolver(LSResourceResolver resourceResolver) { + } + } +} diff --git a/luni/src/test/java/libcore/javax/xml/xpath/XPathExceptionTest.java b/luni/src/test/java/libcore/javax/xml/xpath/XPathExceptionTest.java new file mode 100644 index 00000000000..79e4707b730 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/xpath/XPathExceptionTest.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.xpath; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import javax.xml.xpath.XPathException; + +@RunWith(JUnit4.class) +public class XPathExceptionTest { + @Test + public void constructorWithString() { + XPathException e = new XPathException("message"); + assertEquals("message", e.getMessage()); + assertNull(e.getCause()); + } + + @Test + public void constructorWithThrowable() { + Throwable t = new Throwable(); + XPathException e = new XPathException(t); + assertEquals("java.lang.Throwable", e.getMessage()); + assertEquals(t, e.getCause()); + } +} diff --git a/luni/src/test/java/libcore/javax/xml/xpath/XPathExpressionExceptionTest.java b/luni/src/test/java/libcore/javax/xml/xpath/XPathExpressionExceptionTest.java new file mode 100644 index 00000000000..96568e06583 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/xpath/XPathExpressionExceptionTest.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.xpath; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import javax.xml.xpath.XPathExpressionException; + +@RunWith(JUnit4.class) +public class XPathExpressionExceptionTest { + @Test + public void constructorWithString() { + XPathExpressionException e = new XPathExpressionException("message"); + assertEquals("message", e.getMessage()); + assertNull(e.getCause()); + } + + @Test + public void constructorWithThrowable() { + Throwable t = new Throwable(); + XPathExpressionException e = new XPathExpressionException(t); + assertEquals("java.lang.Throwable", e.getMessage()); + assertEquals(t, e.getCause()); + } +} diff --git a/luni/src/test/java/libcore/javax/xml/xpath/XPathFactoryConfigurationExceptionTest.java b/luni/src/test/java/libcore/javax/xml/xpath/XPathFactoryConfigurationExceptionTest.java new file mode 100644 index 00000000000..d0e5c54ddf7 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/xpath/XPathFactoryConfigurationExceptionTest.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.xpath; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import javax.xml.xpath.XPathFactoryConfigurationException; + +@RunWith(JUnit4.class) +public class XPathFactoryConfigurationExceptionTest { + @Test + public void constructorWithString() { + XPathFactoryConfigurationException e = new XPathFactoryConfigurationException("message"); + assertEquals("message", e.getMessage()); + assertNull(e.getCause()); + } + + @Test + public void constructorWithThrowable() { + Throwable t = new Throwable(); + XPathFactoryConfigurationException e = new XPathFactoryConfigurationException(t); + assertEquals("java.lang.Throwable", e.getMessage()); + assertEquals(t, e.getCause()); + } +} diff --git a/luni/src/test/java/libcore/javax/xml/xpath/XPathFunctionExceptionTest.java b/luni/src/test/java/libcore/javax/xml/xpath/XPathFunctionExceptionTest.java new file mode 100644 index 00000000000..2eaac48ef29 --- /dev/null +++ b/luni/src/test/java/libcore/javax/xml/xpath/XPathFunctionExceptionTest.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2021 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 libcore.javax.xml.xpath; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import javax.xml.xpath.XPathFunctionException; + +@RunWith(JUnit4.class) +public class XPathFunctionExceptionTest { + @Test + public void constructorWithString() { + XPathFunctionException e = new XPathFunctionException("message"); + assertEquals("message", e.getMessage()); + assertNull(e.getCause()); + } + + @Test + public void constructorWithThrowable() { + Throwable t = new Throwable(); + XPathFunctionException e = new XPathFunctionException(t); + assertEquals("java.lang.Throwable", e.getMessage()); + assertEquals(t, e.getCause()); + } +} diff --git a/luni/src/test/java/libcore/libcore/io/MemoryTest.java b/luni/src/test/java/libcore/libcore/io/MemoryTest.java index 80ff4ea56a5..4047e3c5377 100644 --- a/luni/src/test/java/libcore/libcore/io/MemoryTest.java +++ b/luni/src/test/java/libcore/libcore/io/MemoryTest.java @@ -143,4 +143,54 @@ public class MemoryTest extends TestCase { assertEquals(expectedValues[i], Memory.peekShort(ptr + Short.BYTES * i, swap)); } } + + public void testMemmove() { + final int size = 100; + checkPartialMemmove(size, 0, 0, size); + checkPartialMemmove(size, 0, 0, size / 2); + checkPartialMemmove(size, size / 2, size / 2, size - size / 2); + checkPartialMemmove(size, 10, 20, 20); + + checkOverlappingMemmove(size, 0, 0, size); + checkOverlappingMemmove(size, 10, 20, 30); + checkOverlappingMemmove(size, 20, 10, 30); + } + + private void checkPartialMemmove(int size, int offsetDst, int offsetSrc, int count) { + byte[] src = new byte[size]; + for (int i = 0; i < size; ++i) { + src[i] = (byte)i; + } + byte[] dst = new byte[size]; + Arrays.fill(dst, (byte)-1); + + assertTrue(offsetSrc + count <= size); + assertTrue(offsetDst + count <= size); + Memory.memmove(dst, offsetDst, src, offsetSrc, count); + for (int i = 0; i < size; ++i) { + if (i >= offsetDst && i < offsetDst + count) { + assertEquals(src[i + (offsetSrc - offsetDst)], dst[i]); + } else { + assertEquals((byte)-1, dst[i]); + } + } + } + + private void checkOverlappingMemmove(int size, int offsetDst, int offsetSrc, int count) { + byte[] buf = new byte[size]; + for (int i = 0; i < size; ++i) { + buf[i] = (byte)i; + } + + assertTrue(offsetSrc + count <= size); + assertTrue(offsetDst + count <= size); + Memory.memmove(buf, offsetDst, buf, offsetSrc, count); + for (int i = 0; i < size; ++i) { + if (i >= offsetDst && i < offsetDst + count) { + assertEquals(i + (offsetSrc - offsetDst), buf[i]); + } else { + assertEquals((byte)i, buf[i]); + } + } + } } diff --git a/luni/src/test/java/libcore/libcore/io/StreamsTest.java b/luni/src/test/java/libcore/libcore/io/StreamsTest.java new file mode 100644 index 00000000000..2b0e627e666 --- /dev/null +++ b/luni/src/test/java/libcore/libcore/io/StreamsTest.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2015 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 libcore.libcore.io; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.ByteArrayInputStream; +import java.util.Arrays; + +import libcore.io.Streams; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class StreamsTest { + + @Test + public void testReadFully() throws Exception { + final byte[] bytes = "0123456789".getBytes(); + ByteArrayInputStream inStream = new ByteArrayInputStream(bytes); + byte[] dst = new byte[bytes.length - 1]; + + Arrays.fill(dst, (byte)-1); + Streams.readFully(inStream, dst); + for (int i = 0; i < dst.length; ++i) { + assertEquals(bytes[i], dst[i]); + } + } +} diff --git a/luni/src/test/java/libcore/sun/misc/UnsafeTest.java b/luni/src/test/java/libcore/sun/misc/UnsafeTest.java index 483398bd0bd..994f6bb7ffe 100644 --- a/luni/src/test/java/libcore/sun/misc/UnsafeTest.java +++ b/luni/src/test/java/libcore/sun/misc/UnsafeTest.java @@ -16,6 +16,8 @@ package libcore.sun.misc; +import static org.junit.Assert.assertEquals; + import junit.framework.TestCase; import java.lang.reflect.Field; @@ -72,4 +74,230 @@ public class UnsafeTest extends TestCase { assertEquals(null, i.s); assertEquals(i, i.getThis()); } + + public void test_copyMemory() throws Exception { + Unsafe unsafe = getUnsafe(); + + // Source buffer. + byte[] msg = "All your base are belong to us.".getBytes(); + long srcBuf = unsafe.allocateMemory(msg.length); + { + long srcPtr = srcBuf; + for (byte b : msg){ + unsafe.putByte(srcPtr++, b); + } + } + + // Destination buffer. + long dstBuf = getUnsafe().allocateMemory(msg.length); + unsafe.copyMemory(srcBuf, dstBuf, msg.length); + + // Compare buffers. + long srcPtr = srcBuf; + long dstPtr = dstBuf; + for (int i = 0; i < msg.length; ++i) { + byte srcByte = unsafe.getByte(srcPtr++); + byte dstByte = unsafe.getByte(dstPtr++); + assertEquals(String.format("Content mismatch at offset %d: src = '%c', dst = '%c'", + i, srcByte, dstByte), + srcByte, dstByte); + } + + // Clean up. + unsafe.freeMemory(dstBuf); + unsafe.freeMemory(srcBuf); + } + + private class TestFixture { + public boolean booleanVar = true; + public byte byteVar = 42; + public int intVar = 2046; + public long longVar = 123456789; + public float floatVar = 1.618f; + public double doubleVar = 3.141; + public Object objectVar = new Object(); + } + + public void test_getBoolean_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field booleanField = TestFixture.class.getDeclaredField("booleanVar"); + long booleanFieldOffset = unsafe.objectFieldOffset(booleanField); + boolean booleanValue = unsafe.getBoolean(tf, booleanFieldOffset); + assertEquals(tf.booleanVar, booleanValue); + } + + public void test_getByte_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field byteField = TestFixture.class.getDeclaredField("byteVar"); + long byteFieldOffset = unsafe.objectFieldOffset(byteField); + byte byteValue = unsafe.getByte(tf, byteFieldOffset); + assertEquals(tf.byteVar, byteValue); + } + + public void test_getInt_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field intField = TestFixture.class.getDeclaredField("intVar"); + long intFieldOffset = unsafe.objectFieldOffset(intField); + int intValue = unsafe.getInt(tf, intFieldOffset); + assertEquals(tf.intVar, intValue); + } + + public void test_getLong_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field longField = TestFixture.class.getDeclaredField("longVar"); + long longFieldOffset = unsafe.objectFieldOffset(longField); + long longValue = unsafe.getLong(tf, longFieldOffset); + assertEquals(tf.longVar, longValue); + } + + public void test_getFloat_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field floatField = TestFixture.class.getDeclaredField("floatVar"); + long floatFieldOffset = unsafe.objectFieldOffset(floatField); + float floatValue = unsafe.getFloat(tf, floatFieldOffset); + assertEquals(tf.floatVar, floatValue); + } + + public void test_getDouble_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field doubleField = TestFixture.class.getDeclaredField("doubleVar"); + long doubleFieldOffset = unsafe.objectFieldOffset(doubleField); + double doubleValue = unsafe.getDouble(tf, doubleFieldOffset); + assertEquals(tf.doubleVar, doubleValue); + } + + public void test_getObject_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field objectField = TestFixture.class.getDeclaredField("objectVar"); + long objectFieldOffset = unsafe.objectFieldOffset(objectField); + Object objectValue = unsafe.getObject(tf, objectFieldOffset); + assertEquals(tf.objectVar, objectValue); + } + + public void test_putBoolean_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field booleanField = TestFixture.class.getDeclaredField("booleanVar"); + long booleanFieldOffset = unsafe.objectFieldOffset(booleanField); + boolean booleanValue = false; + unsafe.putBoolean(tf, booleanFieldOffset, booleanValue); + assertEquals(booleanValue, tf.booleanVar); + } + + public void test_putByte_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field byteField = TestFixture.class.getDeclaredField("byteVar"); + long byteFieldOffset = unsafe.objectFieldOffset(byteField); + byte byteValue = 83; + unsafe.putByte(tf, byteFieldOffset, byteValue); + assertEquals(byteValue, tf.byteVar); + } + + public void test_putInt_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field intField = TestFixture.class.getDeclaredField("intVar"); + long intFieldOffset = unsafe.objectFieldOffset(intField); + int intValue = 3000; + unsafe.putInt(tf, intFieldOffset, intValue); + assertEquals(intValue, tf.intVar); + } + + public void test_putLong_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field longField = TestFixture.class.getDeclaredField("longVar"); + long longFieldOffset = unsafe.objectFieldOffset(longField); + long longValue = 9000; + unsafe.putLong(tf, longFieldOffset, longValue); + assertEquals(longValue, tf.longVar); + } + + public void test_putFloat_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field floatField = TestFixture.class.getDeclaredField("floatVar"); + long floatFieldOffset = unsafe.objectFieldOffset(floatField); + float floatValue = 0.987f; + unsafe.putFloat(tf, floatFieldOffset, floatValue); + assertEquals(floatValue, tf.floatVar); + } + + public void test_putDouble_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field doubleField = TestFixture.class.getDeclaredField("doubleVar"); + long doubleFieldOffset = unsafe.objectFieldOffset(doubleField); + double doubleValue = 0.123; + unsafe.putDouble(tf, doubleFieldOffset, doubleValue); + assertEquals(doubleValue, tf.doubleVar); + } + + public void test_putObject_Relative() throws Exception { + Unsafe unsafe = getUnsafe(); + TestFixture tf = new TestFixture(); + Field objectField = TestFixture.class.getDeclaredField("objectVar"); + long objectFieldOffset = unsafe.objectFieldOffset(objectField); + Object objectValue = new Object(); + unsafe.putObject(tf, objectFieldOffset, objectValue); + assertEquals(objectValue, tf.objectVar); + } + + public void test_putByte_getByte_Absolute() throws Exception { + Unsafe unsafe = getUnsafe(); + long buffer = unsafe.allocateMemory(Byte.BYTES); + byte byteValue1 = 51; + unsafe.putByte(buffer, byteValue1); + byte byteValue2 = unsafe.getByte(buffer); + assertEquals(byteValue2, byteValue1); + unsafe.freeMemory(buffer); + } + + public void test_putInt_getInt_Absolute() throws Exception { + Unsafe unsafe = getUnsafe(); + long buffer = unsafe.allocateMemory(Integer.BYTES); + int intValue1 = 2047; + unsafe.putInt(buffer, intValue1); + int intValue2 = unsafe.getInt(buffer); + assertEquals(intValue2, intValue1); + unsafe.freeMemory(buffer); + } + + public void test_putLong_getLong_Absolute() throws Exception { + Unsafe unsafe = getUnsafe(); + long buffer = unsafe.allocateMemory(Long.BYTES); + long longValue1 = 987654321; + unsafe.putLong(buffer, longValue1); + long longValue2 = unsafe.getLong(buffer); + assertEquals(longValue2, longValue1); + unsafe.freeMemory(buffer); + } + + public void test_putFloat_getFloat_Absolute() throws Exception { + Unsafe unsafe = getUnsafe(); + long buffer = unsafe.allocateMemory(Float.BYTES); + float floatValue1 = 2.718f; + unsafe.putFloat(buffer, floatValue1); + float floatValue2 = unsafe.getFloat(buffer); + assertEquals(floatValue2, floatValue1); + unsafe.freeMemory(buffer); + } + + public void test_putDouble_getDouble_Absolute() throws Exception { + Unsafe unsafe = getUnsafe(); + long buffer = unsafe.allocateMemory(Double.BYTES); + double doubleValue1 = 6.283; + unsafe.putDouble(buffer, doubleValue1); + double doubleValue2 = unsafe.getDouble(buffer); + assertEquals(doubleValue2, doubleValue1); + unsafe.freeMemory(buffer); + } } diff --git a/luni/src/test/java/libcore/xml/XmlToSax2DriverTest.java b/luni/src/test/java/libcore/xml/XmlToSax2DriverTest.java new file mode 100644 index 00000000000..9e4b47c290f --- /dev/null +++ b/luni/src/test/java/libcore/xml/XmlToSax2DriverTest.java @@ -0,0 +1,474 @@ +/* + * Copyright (C) 2021 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 libcore.xml; + +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import junit.framework.TestCase; +import org.junit.Assert; +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.SAXParseException; +import org.xml.sax.helpers.DefaultHandler; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; +import org.xmlpull.v1.XmlPullParserFactory; +import org.xmlpull.v1.sax2.Driver; + +public class XmlToSax2DriverTest extends TestCase { + + private static final String XML = + "<note type=\"email\" foo=\"bar\">" + + "<to>John</to>" + + "<from>Smith</from>" + + "<heading>Lunch today</heading>" + + "<body>Hi, shall we go to lunch at 12?</body>" + + "</note>"; + + private Driver driver; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + driver = new Driver(); + } + + public void testConstructor() { + Driver driver = null; + try { + driver = new Driver(); + } catch (XmlPullParserException e) { + fail("Unexpected exception: " + e.getMessage()); + } + + assertEquals(0, driver.getLength()); + assertEquals(1, driver.getColumnNumber()); + assertEquals(1, driver.getLineNumber()); + } + + public void testConstructor_parametrized() { + XmlPullParserFactory factory; + XmlPullParser parser = null; + + try { + factory = XmlPullParserFactory.newInstance(null, null); + parser = factory.newPullParser(); + } catch (XmlPullParserException e) { + fail("Couldn't create factory and parser"); + } + Driver driver = null; + + try { + driver = new Driver(parser); + } catch (XmlPullParserException e) { + fail("Unexpected exception: " + e.getMessage()); + } + + assertEquals(0, driver.getLength()); + assertEquals(1, driver.getColumnNumber()); + assertEquals(1, driver.getLineNumber()); + } + + public void testGetColumnNumber() { + assertEquals(driver.getColumnNumber(), 1); + } + + public void testGetSetContentHandler() throws XmlPullParserException { + assertTrue(driver.getContentHandler() instanceof DefaultHandler); + + ContentHandler handler = new DefaultHandler(); + driver.setContentHandler(handler); + assertEquals(driver.getContentHandler(), handler); + + driver.setContentHandler(null); + assertNull(driver.getContentHandler()); + } + + public void testGetSetDTDHandler() { + assertNull(driver.getDTDHandler()); + + driver.setDTDHandler(new DefaultHandler()); + assertNull(driver.getDTDHandler()); + + driver.setDTDHandler(null); + assertNull(driver.getDTDHandler()); + } + + public void testGetSetEntityResolver() { + assertNull(driver.getEntityResolver()); + + driver.setEntityResolver(new DefaultHandler()); + assertNull(driver.getEntityResolver()); + + driver.setEntityResolver((publicId, systemId) -> null); + assertNull(driver.getEntityResolver()); + + driver.setEntityResolver(null); + assertNull(driver.getEntityResolver()); + } + + public void testGetSetErrorHandler() { + assertTrue(driver.getContentHandler() instanceof DefaultHandler); + + ErrorHandler handler = new DefaultHandler(); + driver.setErrorHandler(handler); + assertEquals(handler, driver.getErrorHandler()); + + driver.setErrorHandler(null); + assertNull(driver.getErrorHandler()); + } + + public void testGetSetFeature() throws SAXNotSupportedException, SAXNotRecognizedException { + final String NAMESPACES_FEATURE = "http://xml.org/sax/features/namespaces"; + final String NAMESPACE_PREFIXES_FEATURE = "http://xml.org/sax/features/namespace-prefixes"; + final String VALIDATION_FEATURE = "http://xml.org/sax/features/validation"; + final String PROCESS_DOCDECL_FEATURE = + "http://xmlpull.org/v1/doc/features.html#process-docdecl"; + final String REPORT_NAMESPACE_ATTRIBUTES_FEATURE = + "http://xmlpull.org/v1/doc/features.html#report-namespace-prefixes"; + final String RELAXED_FEATURE = "http://xmlpull.org/v1/doc/features.html#relaxed"; + + final Object[][] expectations = { + {NAMESPACE_PREFIXES_FEATURE, false}, + {VALIDATION_FEATURE, false}, + {PROCESS_DOCDECL_FEATURE, false}, + {REPORT_NAMESPACE_ATTRIBUTES_FEATURE, false}, + {NAMESPACES_FEATURE, true}, + }; + + for (Object[] f : expectations) { + final String feature = (String) f[0]; + final boolean result = (boolean) f[1]; + try { + assertEquals(result, driver.getFeature(feature)); + } catch (SAXNotSupportedException | SAXNotRecognizedException e) { + fail("Unexpected exception: " + e.getMessage()); + } + } + + final String[] settable = { + NAMESPACES_FEATURE, + PROCESS_DOCDECL_FEATURE, + RELAXED_FEATURE, + }; + for (String feature : settable) { + for (boolean value : new boolean[]{ false, true }) { + driver.setFeature(feature, value); + assertEquals(feature, value, driver.getFeature(feature)); + } + } + } + + public void testGetIndex() throws NoSuchFieldException, IllegalAccessException { + assertEquals(-1, driver.getIndex("hello")); + assertEquals(-1, driver.getIndex("encoding")); + assertEquals(-1, driver.getIndex("version")); + } + + public void testGetIndex_namespaced() { + assertEquals(-1, driver.getIndex("", "version")); + } + + public void testGetLength() { + assertEquals(0, driver.getLength()); + } + + public void testGetLineNumber() { + assertEquals(1, driver.getLineNumber()); + } + + public void testGetLocalName() { + try { + driver.getLocalName(0); + } catch (IndexOutOfBoundsException e) { + // expected + } + } + + public void testGetProperty() { + try { + driver.getProperty(""); + } catch (IndexOutOfBoundsException e) { + // expected + } catch (SAXNotSupportedException | SAXNotRecognizedException e) { + fail("Unexpected exception: " + e.getMessage()); + } + } + + public void testGetPublicId() { + assertNull(driver.getPublicId()); + } + + public void testGetQName() { + try { + driver.getQName(0); + } catch (IndexOutOfBoundsException e) { + // expected + } + } + + public void testGetSystemId() { + assertNull(driver.getSystemId()); + } + + public void testGetType() { + assertEquals("CDATA", driver.getType(0)); + assertNull(driver.getType("value")); + assertNull(driver.getType("", "value")); + } + + public void testGetUri() { + try { + driver.getURI(0); + } catch (IndexOutOfBoundsException e) { + // expected + } + } + + public void testGetValue() { + try { + driver.getValue(0); + } catch (IndexOutOfBoundsException e) { + // expected + } + assertNull("CDATA", driver.getValue("value")); + assertNull("CDATA", driver.getValue("", "value")); + } + + public void testParse_String() { + try { + driver.parse("systemId"); + } catch (SAXException | IOException e) { + // expected + } + + String systemId = null; + try { + driver.parse(systemId); + } catch (SAXException | IOException e) { + // expected + } + } + + public void testParse_InputSource() throws IOException, SAXException { + InputSource source = new InputSource(); + source.setCharacterStream(new StringReader(XML)); + source.setSystemId("systemId"); + + final int[] errors = {0, 0, 0}; + driver.setErrorHandler(new ErrorHandler() { + @Override + public void warning(SAXParseException exception) throws SAXException { + errors[0]++; + } + + @Override + public void error(SAXParseException exception) throws SAXException { + errors[1]++; + } + + @Override + public void fatalError(SAXParseException exception) throws SAXException { + errors[2]++; + } + }); + + // Four events counter: { "startDocument", "endDocument", "startElement", "endElement" } + final int[] events = {0, 0, 0, 0}; + final ArrayList<String> tagsEncountered = new ArrayList<>(); + final ArrayList<String> textsEncountered = new ArrayList<>(); + driver.setContentHandler(new DefaultHandler() { + @Override + public void startDocument() throws SAXException { + super.startDocument(); + events[0]++; + } + + @Override + public void endDocument() throws SAXException { + super.endDocument(); + events[1]++; + } + + @Override + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + super.startElement(uri, localName, qName, attributes); + events[2]++; + tagsEncountered.add(localName); + if ("note".equals(localName)) { + assertEquals(2, attributes.getLength()); + assertEquals("type", attributes.getLocalName(0)); + assertEquals("email", attributes.getValue(0)); + assertEquals("foo", attributes.getLocalName(1)); + assertEquals("bar", attributes.getValue(1)); + } + } + + @Override + public void characters(char[] ch, int start, int length) throws SAXException { + super.characters(ch, start, length); + textsEncountered.add(StringFactory.newStringFromChars(ch, start, length)); + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + super.endElement(uri, localName, qName); + events[3]++; + } + }); + driver.parse(source); + + assertEquals("systemId", driver.getSystemId()); + Assert.assertArrayEquals(new int[]{0, 0, 0}, errors); + Assert.assertArrayEquals(new int[]{1, 1, 5, 5}, events); + Assert.assertArrayEquals(new String[]{"note", "to", "from", "heading", "body"}, + tagsEncountered.toArray()); + Assert.assertArrayEquals(new String[]{ + "John", "Smith", "Lunch today", "Hi, shall we go to lunch at 12?" + }, textsEncountered.toArray()); + } + + public void testParseSubtree() throws XmlPullParserException, IOException { + XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); + parser.setInput(new StringReader(XML)); + + final int[] errors = {0, 0, 0}; + driver.setErrorHandler(new ErrorHandler() { + @Override + public void warning(SAXParseException exception) throws SAXException { + errors[0]++; + } + + @Override + public void error(SAXParseException exception) throws SAXException { + errors[1]++; + } + + @Override + public void fatalError(SAXParseException exception) throws SAXException { + errors[2]++; + } + }); + + // Four events counter: { "startDocument", "endDocument", "startElement", "endElement" } + final int[] events = {0, 0, 0, 0}; + final ArrayList<String> tagsEncountered = new ArrayList<>(); + final ArrayList<String> textsEncountered = new ArrayList<>(); + driver.setContentHandler(new DefaultHandler() { + @Override + public void startDocument() throws SAXException { + super.startDocument(); + events[0]++; + } + + @Override + public void endDocument() throws SAXException { + super.endDocument(); + events[1]++; + } + + @Override + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + super.startElement(uri, localName, qName, attributes); + events[2]++; + tagsEncountered.add(localName); + if ("note".equals(localName)) { + assertEquals(2, attributes.getLength()); + assertEquals("type", attributes.getLocalName(0)); + assertEquals("email", attributes.getValue(0)); + assertEquals("foo", attributes.getLocalName(1)); + assertEquals("bar", attributes.getValue(1)); + } + } + + @Override + public void characters(char[] ch, int start, int length) throws SAXException { + super.characters(ch, start, length); + textsEncountered.add(StringFactory.newStringFromChars(ch, start, length)); + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + super.endElement(uri, localName, qName); + events[3]++; + } + }); + + try { + driver.parseSubTree(parser); + } catch (SAXException e) { + // expected, as START_TAG should have been read already + } catch (IOException e) { + fail("Unexpected exception: " + e.getMessage()); + } + assertEquals(XmlPullParser.START_TAG, parser.next()); + try { + driver.parseSubTree(parser); + } catch (SAXException | IOException e) { + fail("Unexpected exception: " + e.getMessage()); + } + + Assert.assertArrayEquals(new int[]{0, 0, 0}, errors); + Assert.assertArrayEquals(new int[]{0, 0, 5, 5}, events); + Assert.assertArrayEquals(new String[]{"note", "to", "from", "heading", "body"}, + tagsEncountered.toArray()); + Assert.assertArrayEquals(new String[]{ + "John", "Smith", "Lunch today", "Hi, shall we go to lunch at 12?" + }, textsEncountered.toArray()); + } + + public void testStartElement() throws XmlPullParserException, IOException, SAXException { + boolean[] called = {false}; + ContentHandler handler = new DefaultHandler() { + @Override + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + super.startElement(uri, localName, qName, attributes); + called[0] = true; + } + }; + ExtendsDriver d = new ExtendsDriver(handler); + d.setContentHandler(handler); + d.parse(new InputSource(new StringReader(XML))); + + assertTrue(called[0]); + } + + private static class ExtendsDriver extends Driver { + + private final ContentHandler handler; + + public ExtendsDriver(ContentHandler handler) throws XmlPullParserException { + this.handler = handler; + } + + @Override + protected void startElement(String namespace, String localName, String qName) + throws SAXException { + super.startElement(namespace, localName, qName); + handler.startElement(namespace, localName, qName, this); + } + } +} diff --git a/luni/src/test/java/tests/java/lang/ClassLoaderTest.java b/luni/src/test/java/tests/java/lang/ClassLoaderTest.java new file mode 100644 index 00000000000..717a562ae9d --- /dev/null +++ b/luni/src/test/java/tests/java/lang/ClassLoaderTest.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2021 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 tests.java.lang; +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.Before; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ClassLoaderTest { + + class TestClassLoader extends ClassLoader { + public Class<?> findSystemClassWrapper(String s) throws ClassNotFoundException { + return findSystemClass(s); + } + + public Class<?> defineClassWrapper(String name, byte[] b, int off, int len) + throws ClassFormatError { + return defineClass(name, b, off, len); + } + + public void setSignersWrapper(Class<?> c, Object[] signers) { + setSigners(c, signers); + } + } + + private TestClassLoader loader; + + @Before + public void setUp() throws Exception { + loader = new TestClassLoader(); + } + + @Test + public void test_findSystemClass() { + try { + loader.findSystemClassWrapper("java.lang.String"); + } catch (ClassNotFoundException cnfe) { + throw new RuntimeException(cnfe); + } + try { + loader.findSystemClassWrapper("nonexistentclass"); + fail("Expected ClassNotFoundException"); + } catch (ClassNotFoundException cnfe) { + } + } + + @Test + public void test_defineClass() { + try { + byte b[] = new byte[1]; + loader.defineClassWrapper("java.lang.String", b, 0, 0); + fail(); + } catch (UnsupportedOperationException e) { + assertEquals(e.getMessage(), "can't load this type of class file"); + } + } + + // setSigners does nothing + @Test + public void test_setSigners() { + Object[] signers = new Object[] { null }; + loader.setSignersWrapper(Object.class, signers); + } +}
\ No newline at end of file diff --git a/ojluni/src/test/java/lang/ClassLoader/AssertTest.java b/ojluni/src/test/java/lang/ClassLoader/AssertTest.java new file mode 100644 index 00000000000..83483fed5fa --- /dev/null +++ b/ojluni/src/test/java/lang/ClassLoader/AssertTest.java @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 4290640 4785473 + * @build package1.Class1 package2.Class2 package1.package3.Class3 Assert + * @run main/othervm Assert + * @summary Test the assertion facility + * @author Mike McCloskey + * @key randomness + */ + +// Android-changed: Adapt structure and expectations to Android. +// Android does not use AssertionStatuses, so this test changes expectations to reflect that. +// Furthermore, the test structure is simplified to avoid relying on args[] and use the +// org.testng.annotations.Test package instead of a main() method. +package test.java.lang.ClassLoader; + +import test.java.lang.ClassLoader.package1.*; +import test.java.lang.ClassLoader.package2.*; +import test.java.lang.ClassLoader.package1.package3.*; +import java.util.Random; + +import org.testng.annotations.Test; + +public class AssertTest { + + private static Class1 testClass1; + private static Class2 testClass2; + private static Class3 testClass3; + private static Random generator = new Random(); + + /** + * AssertionStatuses don't actually do anything on Android, this test proves as much. + */ + @Test + public void testAssert() { + // Switch values: 0=don't touch, 1=off, 2 = on + int[] switches = new int[7]; + for(int x=0; x<10; x++) { + int temp = generator.nextInt(2187); + for (int i = 0; i < 7; i++) { + switches[i] = temp % 3; + temp = temp / 3; + } + SetAssertionSwitches(switches); + ConstructClassTree(); + TestClassTree(); + } + + + // Android-added: Add testing of clearAssertionStatus(). + for(int x=0; x<7; x++) { + switches[x]=2; + } + ClassLoader loader = SetAssertionSwitches(switches); + loader.clearAssertionStatus(); // Clearing also does nothing + ConstructClassTree(); + TestClassTree(); + } + + /* + * Activate/Deactivate the assertions in the tree according to the + * specified switches. + */ + private static ClassLoader SetAssertionSwitches(int[] switches) { + ClassLoader loader = ClassLoader.getSystemClassLoader(); + + if (switches[0] != 0) + loader.setDefaultAssertionStatus(switches[0]==2); + if (switches[1] != 0) + loader.setPackageAssertionStatus("package1", switches[1]==2); + if (switches[2] != 0) + loader.setPackageAssertionStatus("package2", switches[2]==2); + if (switches[3] != 0) + loader.setPackageAssertionStatus("package1.package3", switches[3]==2); + if (switches[4] != 0) + loader.setClassAssertionStatus("package1.Class1", switches[4]==2); + if (switches[5] != 0) + loader.setClassAssertionStatus("package2.Class2", switches[5]==2); + if (switches[6] != 0) + loader.setClassAssertionStatus("package1.package3.Class3", switches[6]==2); + return loader; + } + + /* + * Verify that the assertions are activated or deactivated as specified + * by the switches. + */ + private static void TestClassTree() { + testClass1.testAssert(false); + Class1.Class11.testAssert(false); + testClass2.testAssert(false); + testClass3.testAssert(false); + Class3.Class31.testAssert(false); + + } + + /* + * Create the class tree to be tested. Each test run must reload the classes + * of the tree since assertion status is determined at class load time. + */ + private static void ConstructClassTree() { + testClass1 = new Class1(); + testClass2 = new Class2(); + testClass3 = new Class3(); + } + +}
\ No newline at end of file diff --git a/ojluni/src/test/java/lang/ClassLoader/package1/Class1.java b/ojluni/src/test/java/lang/ClassLoader/package1/Class1.java new file mode 100644 index 00000000000..0c5e7cc818f --- /dev/null +++ b/ojluni/src/test/java/lang/ClassLoader/package1/Class1.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package test.java.lang.ClassLoader.package1; + +public class Class1 { + public void testAssert(boolean assertsShouldBeOn) { + boolean assertsEnabled = false; + assert(assertsEnabled = true); + if (assertsEnabled != assertsShouldBeOn) + throw new RuntimeException(assertsEnabled ? "TRUE" : "FALSE"); + + Class1 anonTest = new Class1() { + public void testAssert(boolean assertsShouldBeOn) { + boolean assertsEnabled2 = false; + assert(assertsEnabled2 = true); + if (assertsEnabled2 != assertsShouldBeOn) + throw new RuntimeException("Failure of Asserts Facility."); + } + }; + anonTest.testAssert(assertsShouldBeOn); + } + + // Named inner class + public static class Class11 { + public static void testAssert(boolean assertsShouldBeOn) { + boolean assertsEnabled3 = false; + assert(assertsEnabled3 = true); + if (assertsEnabled3 != assertsShouldBeOn) + throw new RuntimeException("Failure of Asserts Facility."); + } + } + +}
\ No newline at end of file diff --git a/ojluni/src/test/java/lang/ClassLoader/package1/package3/Class3.java b/ojluni/src/test/java/lang/ClassLoader/package1/package3/Class3.java new file mode 100644 index 00000000000..b133e2a375b --- /dev/null +++ b/ojluni/src/test/java/lang/ClassLoader/package1/package3/Class3.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package test.java.lang.ClassLoader.package1.package3; + +import org.testng.Assert; + +public class Class3 { + public void testAssert(boolean assertsShouldBeOn) { + boolean assertsEnabled = false; + assert(assertsEnabled = true); + if (assertsEnabled != assertsShouldBeOn) + Assert.fail("Failure of Asserts Facility."); + + Class3 anonTest = new Class3() { + public void testAssert(boolean assertsShouldBeOn) { + boolean assertsEnabled = false; + assert(assertsEnabled = true); + if (assertsEnabled != assertsShouldBeOn) + Assert.fail("Failure of Asserts Facility."); + } + }; + anonTest.testAssert(assertsShouldBeOn); + } + + // Named inner class + public static class Class31 { + public static void testAssert(boolean assertsShouldBeOn) { + boolean assertsEnabled = false; + assert(assertsEnabled = true); + if (assertsEnabled != assertsShouldBeOn) + Assert.fail("Failure of Asserts Facility."); + } + } +}
\ No newline at end of file diff --git a/ojluni/src/test/java/lang/ClassLoader/package2/Class2.java b/ojluni/src/test/java/lang/ClassLoader/package2/Class2.java new file mode 100644 index 00000000000..d7d92bc2e75 --- /dev/null +++ b/ojluni/src/test/java/lang/ClassLoader/package2/Class2.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package test.java.lang.ClassLoader.package2; + + +public class Class2 { + public void testAssert(boolean assertsShouldBeOn) { + boolean assertsEnabled = false; + assert(assertsEnabled = true); + if (assertsEnabled != assertsShouldBeOn) + throw new RuntimeException("Failure of Asserts Facility."); + } +}
\ No newline at end of file |