Snap for 8078460 from 408942f8bd1a6fe9f8acc78c65d6ddf3145de52c to tm-release
Change-Id: I03a18f520fdbf3cf76ffba2d673392ca35af47b3
diff --git a/ojluni/src/main/java/java/security/AccessControlException.java b/ojluni/src/main/java/java/security/AccessControlException.java
index a4f2a78..3d654ee 100644
--- a/ojluni/src/main/java/java/security/AccessControlException.java
+++ b/ojluni/src/main/java/java/security/AccessControlException.java
@@ -38,6 +38,7 @@
*
* @author Li Gong
* @author Roland Schemers
+ * @since 1.2
*/
public class AccessControlException extends SecurityException {
diff --git a/ojluni/src/main/java/java/security/Certificate.java b/ojluni/src/main/java/java/security/Certificate.java
index 489c6d6..28690e2 100644
--- a/ojluni/src/main/java/java/security/Certificate.java
+++ b/ojluni/src/main/java/java/security/Certificate.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2017, 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
@@ -56,13 +56,13 @@
* the certificate and satisfy itself of its validity.
*
* @author Benjamin Renaud
- * @deprecated A new certificate handling package is created in the Java platform.
- * This Certificate interface is entirely deprecated and
- * is here to allow for a smooth transition to the new
- * package.
+ * @since 1.1
+ * @deprecated This class is deprecated and subject to removal in a future
+ * version of Java SE. It has been replaced by
+ * {@code java.security.cert.Certificate} and related classes.
* @see java.security.cert.Certificate
*/
-@Deprecated
+@Deprecated(since="1.2", forRemoval=true)
public interface Certificate {
/**
diff --git a/ojluni/src/main/java/java/security/DigestInputStream.java b/ojluni/src/main/java/java/security/DigestInputStream.java
index a1bf55a..a76ebda 100644
--- a/ojluni/src/main/java/java/security/DigestInputStream.java
+++ b/ojluni/src/main/java/java/security/DigestInputStream.java
@@ -52,13 +52,14 @@
* {@link MessageDigest}),
* so that in order to compute intermediate digests, a caller should
* retain a handle onto the digest object, and clone it for each
- * digest to be computed, leaving the orginal digest untouched.
+ * digest to be computed, leaving the original digest untouched.
*
* @see MessageDigest
*
* @see DigestOutputStream
*
* @author Benjamin Renaud
+ * @since 1.2
*/
public class DigestInputStream extends FilterInputStream {
diff --git a/ojluni/src/main/java/java/security/DigestOutputStream.java b/ojluni/src/main/java/java/security/DigestOutputStream.java
index 619faec..e941673 100644
--- a/ojluni/src/main/java/java/security/DigestOutputStream.java
+++ b/ojluni/src/main/java/java/security/DigestOutputStream.java
@@ -51,6 +51,7 @@
* @see DigestInputStream
*
* @author Benjamin Renaud
+ * @since 1.2
*/
public class DigestOutputStream extends FilterOutputStream {
diff --git a/ojluni/src/main/java/java/security/DomainCombiner.java b/ojluni/src/main/java/java/security/DomainCombiner.java
index e9c010f..d844939 100644
--- a/ojluni/src/main/java/java/security/DomainCombiner.java
+++ b/ojluni/src/main/java/java/security/DomainCombiner.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2015, 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
diff --git a/ojluni/src/main/java/java/security/GeneralSecurityException.java b/ojluni/src/main/java/java/security/GeneralSecurityException.java
index dc9ea06..69ee8ea 100644
--- a/ojluni/src/main/java/java/security/GeneralSecurityException.java
+++ b/ojluni/src/main/java/java/security/GeneralSecurityException.java
@@ -31,6 +31,7 @@
* security-related exception classes that extend from it.
*
* @author Jan Luehe
+ * @since 1.2
*/
public class GeneralSecurityException extends Exception {
diff --git a/ojluni/src/main/java/java/security/Guard.java b/ojluni/src/main/java/java/security/Guard.java
index abafb58..f64a0d9 100644
--- a/ojluni/src/main/java/java/security/Guard.java
+++ b/ojluni/src/main/java/java/security/Guard.java
@@ -38,6 +38,7 @@
*
* @author Roland Schemers
* @author Li Gong
+ * @since 1.2
*/
public interface Guard {
diff --git a/ojluni/src/main/java/java/security/GuardedObject.java b/ojluni/src/main/java/java/security/GuardedObject.java
index a275ddf..a2bf3f7 100644
--- a/ojluni/src/main/java/java/security/GuardedObject.java
+++ b/ojluni/src/main/java/java/security/GuardedObject.java
@@ -44,6 +44,7 @@
*
* @author Roland Schemers
* @author Li Gong
+ * @since 1.2
*/
public class GuardedObject implements java.io.Serializable {
diff --git a/ojluni/src/main/java/java/security/Identity.java b/ojluni/src/main/java/java/security/Identity.java
index e63131e..e6b2e80 100644
--- a/ojluni/src/main/java/java/security/Identity.java
+++ b/ojluni/src/main/java/java/security/Identity.java
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2014 The Android Open Source Project
- * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2017, 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
@@ -52,12 +52,14 @@
* @see Principal
*
* @author Benjamin Renaud
- * @deprecated This class is no longer used. Its functionality has been
- * replaced by {@code java.security.KeyStore}, the
- * {@code java.security.cert} package, and
- * {@code java.security.Principal}.
+ * @since 1.1
+ * @deprecated This class is deprecated and subject to removal in a future
+ * version of Java SE. It has been replaced by
+ * {@code java.security.KeyStore}, the {@code java.security.cert} package,
+ * and {@code java.security.Principal}.
*/
-@Deprecated
+@Deprecated(since="1.2", forRemoval=true)
+@SuppressWarnings("removal")
public abstract class Identity implements Principal, Serializable {
/** use serialVersionUID from JDK 1.1.x for interoperability */
@@ -186,7 +188,7 @@
check("setIdentityPublicKey");
this.publicKey = key;
- certificates = new Vector<Certificate>();
+ certificates = new Vector<>();
}
/**
@@ -249,7 +251,7 @@
check("addIdentityCertificate");
if (certificates == null) {
- certificates = new Vector<Certificate>();
+ certificates = new Vector<>();
}
if (publicKey != null) {
if (!keyEquals(publicKey, certificate.getPublicKey())) {
diff --git a/ojluni/src/main/java/java/security/InvalidAlgorithmParameterException.java b/ojluni/src/main/java/java/security/InvalidAlgorithmParameterException.java
index 559a8be..9636fcb 100644
--- a/ojluni/src/main/java/java/security/InvalidAlgorithmParameterException.java
+++ b/ojluni/src/main/java/java/security/InvalidAlgorithmParameterException.java
@@ -65,7 +65,7 @@
}
/**
- * Creates a {@code InvalidAlgorithmParameterException} with the
+ * Creates an {@code InvalidAlgorithmParameterException} with the
* specified detail message and cause.
*
* @param message the detail message (which is saved for later retrieval
@@ -80,7 +80,7 @@
}
/**
- * Creates a {@code InvalidAlgorithmParameterException} with the
+ * Creates an {@code InvalidAlgorithmParameterException} with the
* specified cause and a detail message of
* {@code (cause==null ? null : cause.toString())}
* (which typically contains the class and detail message of
diff --git a/ojluni/src/main/java/java/security/InvalidKeyException.java b/ojluni/src/main/java/java/security/InvalidKeyException.java
index 35fc64c..5349796 100644
--- a/ojluni/src/main/java/java/security/InvalidKeyException.java
+++ b/ojluni/src/main/java/java/security/InvalidKeyException.java
@@ -31,6 +31,7 @@
* length, uninitialized, etc).
*
* @author Benjamin Renaud
+ * @since 1.1
*/
public class InvalidKeyException extends KeyException {
@@ -58,7 +59,7 @@
}
/**
- * Creates a {@code InvalidKeyException} with the specified
+ * Creates an {@code InvalidKeyException} with the specified
* detail message and cause.
*
* @param message the detail message (which is saved for later retrieval
@@ -73,7 +74,7 @@
}
/**
- * Creates a {@code InvalidKeyException} with the specified cause
+ * Creates an {@code InvalidKeyException} with the specified cause
* and a detail message of {@code (cause==null ? null : cause.toString())}
* (which typically contains the class and detail message of
* {@code cause}).
diff --git a/ojluni/src/main/java/java/security/InvalidParameterException.java b/ojluni/src/main/java/java/security/InvalidParameterException.java
index a095f90..18d413e 100644
--- a/ojluni/src/main/java/java/security/InvalidParameterException.java
+++ b/ojluni/src/main/java/java/security/InvalidParameterException.java
@@ -31,6 +31,7 @@
* to a method.
*
* @author Benjamin Renaud
+ * @since 1.1
*/
public class InvalidParameterException extends IllegalArgumentException {
diff --git a/ojluni/src/main/java/java/security/Key.java b/ojluni/src/main/java/java/security/Key.java
index c0c63d7..ff5611e 100644
--- a/ojluni/src/main/java/java/security/Key.java
+++ b/ojluni/src/main/java/java/security/Key.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2017, 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
@@ -63,7 +63,7 @@
* </pre>
*
* For more information, see
- * <a href="http://www.ietf.org/rfc/rfc3280.txt">RFC 3280:
+ * <a href="http://tools.ietf.org/html/rfc5280">RFC 5280:
* Internet X.509 Public Key Infrastructure Certificate and CRL Profile</a>.
*
* <LI>A Format
@@ -82,7 +82,7 @@
* <p> A Key should use KeyRep as its serialized representation.
* Note that a serialized Key may contain sensitive information
* which should not be exposed in untrusted environments. See the
- * <a href="../../../platform/serialization/spec/security.html">
+ * <a href="{@docRoot}/../specs/serialization/security.html">
* Security Appendix</a>
* of the Serialization Specification for more information.
*
@@ -97,6 +97,7 @@
* @see Signer
*
* @author Benjamin Renaud
+ * @since 1.1
*/
public interface Key extends java.io.Serializable {
@@ -113,10 +114,10 @@
/**
* Returns the standard algorithm name for this key. For
* example, "DSA" would indicate that this key is a DSA key.
- * See Appendix A in the <a href=
- * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
- * Java Cryptography Architecture API Specification & Reference </a>
- * for information about standard algorithm names.
+ * See the <a href=
+ * "{@docRoot}/../specs/security/standard-names.html">
+ * Java Security Standard Algorithm Names</a> document
+ * for more information.
*
* @return the name of the algorithm associated with this key.
*/
diff --git a/ojluni/src/main/java/java/security/KeyException.java b/ojluni/src/main/java/java/security/KeyException.java
index 59cdd6f..b8b87d9 100644
--- a/ojluni/src/main/java/java/security/KeyException.java
+++ b/ojluni/src/main/java/java/security/KeyException.java
@@ -33,6 +33,7 @@
* @see KeyManagementException
*
* @author Benjamin Renaud
+ * @since 1.1
*/
public class KeyException extends GeneralSecurityException {
diff --git a/ojluni/src/main/java/java/security/KeyManagementException.java b/ojluni/src/main/java/java/security/KeyManagementException.java
index be212b9..fe1ab3e 100644
--- a/ojluni/src/main/java/java/security/KeyManagementException.java
+++ b/ojluni/src/main/java/java/security/KeyManagementException.java
@@ -38,6 +38,7 @@
* </ul>
*
* @author Benjamin Renaud
+ * @since 1.1
*
* @see Key
* @see KeyException
diff --git a/ojluni/src/main/java/java/security/KeyPair.java b/ojluni/src/main/java/java/security/KeyPair.java
index 6147a16..1d9e164 100644
--- a/ojluni/src/main/java/java/security/KeyPair.java
+++ b/ojluni/src/main/java/java/security/KeyPair.java
@@ -36,6 +36,7 @@
* @see PrivateKey
*
* @author Benjamin Renaud
+ * @since 1.1
*/
public final class KeyPair implements java.io.Serializable {
diff --git a/ojluni/src/main/java/java/security/KeyPairGenerator.java b/ojluni/src/main/java/java/security/KeyPairGenerator.java
index 7c6cdb5..842bee0 100644
--- a/ojluni/src/main/java/java/security/KeyPairGenerator.java
+++ b/ojluni/src/main/java/java/security/KeyPairGenerator.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, 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
@@ -70,12 +70,12 @@
* associated with each of the keys.
*
* <p>If the algorithm is the <i>DSA</i> algorithm, and the keysize (modulus
- * size) is 512, 768, or 1024, then the <i>Sun</i> provider uses a set of
+ * size) is 512, 768, 1024, or 2048, then the <i>Sun</i> provider uses a set of
* precomputed values for the {@code p}, {@code q}, and
* {@code g} parameters. If the modulus size is not one of the above
* values, the <i>Sun</i> provider creates a new set of parameters. Other
* providers might have precomputed parameter sets for more than just the
- * three modulus sizes mentioned above. Still others might not have a list of
+ * modulus sizes mentioned above. Still others might not have a list of
* precomputed parameters at all and instead always create new parameter sets.
*
* <li><b>Algorithm-Specific Initialization</b>
@@ -83,7 +83,7 @@
* exists (e.g., so-called <i>community parameters</i> in DSA), there are two
* {@link #initialize(java.security.spec.AlgorithmParameterSpec)
* initialize} methods that have an {@code AlgorithmParameterSpec}
- * argument. One also has a {@code SecureRandom} argument, while the
+ * argument. One also has a {@code SecureRandom} argument, while
* the other uses the {@code SecureRandom}
* implementation of the highest-priority installed provider as the source
* of randomness. (If none of the installed providers supply an implementation
@@ -133,11 +133,12 @@
* </table>
*
* These algorithms are described in the <a href=
- * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyPairGenerator">
+ * "{@docRoot}/../specs/security/standard-names.html#keypairgenerator-algorithms">
* KeyPairGenerator section</a> of the
* Java Cryptography Architecture Standard Algorithm Name Documentation.
*
* @author Benjamin Renaud
+ * @since 1.1
*
* @see java.security.spec.AlgorithmParameterSpec
*/
@@ -162,8 +163,8 @@
*
* @param algorithm the standard string name of the algorithm.
* See the KeyPairGenerator section in the <a href=
- * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyPairGenerator">
- * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
+ * "{@docRoot}/../specs/security/standard-names.html#keypairgenerator-algorithms">
+ * Java Security Standard Algorithm Names Specification</a>
* for information about standard algorithm names.
*/
protected KeyPairGenerator(String algorithm) {
@@ -173,8 +174,8 @@
/**
* Returns the standard name of the algorithm for this key pair generator.
* See the KeyPairGenerator section in the <a href=
- * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyPairGenerator">
- * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
+ * "{@docRoot}/../specs/security/standard-names.html#keypairgenerator-algorithms">
+ * Java Security Standard Algorithm Names Specification</a>
* for information about standard algorithm names.
*
* @return the standard string name of the algorithm.
@@ -218,22 +219,33 @@
* <p> Note that the list of registered providers may be retrieved via
* the {@link Security#getProviders() Security.getProviders()} method.
*
+ * @implNote
+ * The JDK Reference Implementation additionally uses the
+ * {@code jdk.security.provider.preferred}
+ * {@link Security#getProperty(String) Security} property to determine
+ * the preferred provider order for the specified algorithm. This
+ * may be different than the order of providers returned by
+ * {@link Security#getProviders() Security.getProviders()}.
+ *
* @param algorithm the standard string name of the algorithm.
* See the KeyPairGenerator section in the <a href=
- * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyPairGenerator">
- * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
+ * "{@docRoot}/../specs/security/standard-names.html#keypairgenerator-algorithms">
+ * Java Security Standard Algorithm Names Specification</a>
* for information about standard algorithm names.
*
- * @return the new KeyPairGenerator object.
+ * @return the new {@code KeyPairGenerator} object
*
- * @exception NoSuchAlgorithmException if no Provider supports a
- * KeyPairGeneratorSpi implementation for the
- * specified algorithm.
+ * @throws NoSuchAlgorithmException if no {@code Provider} supports a
+ * {@code KeyPairGeneratorSpi} implementation for the
+ * specified algorithm
+ *
+ * @throws NullPointerException if {@code algorithm} is {@code null}
*
* @see Provider
*/
public static KeyPairGenerator getInstance(String algorithm)
throws NoSuchAlgorithmException {
+ Objects.requireNonNull(algorithm, "null algorithm name");
List<Service> list =
GetInstance.getServices("KeyPairGenerator", algorithm);
Iterator<Service> t = list.iterator();
@@ -276,29 +288,32 @@
*
* @param algorithm the standard string name of the algorithm.
* See the KeyPairGenerator section in the <a href=
- * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyPairGenerator">
- * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
+ * "{@docRoot}/../specs/security/standard-names.html#keypairgenerator-algorithms">
+ * Java Security Standard Algorithm Names Specification</a>
* for information about standard algorithm names.
*
* @param provider the string name of the provider.
*
- * @return the new KeyPairGenerator object.
+ * @return the new {@code KeyPairGenerator} object
*
- * @exception NoSuchAlgorithmException if a KeyPairGeneratorSpi
- * implementation for the specified algorithm is not
- * available from the specified provider.
+ * @throws IllegalArgumentException if the provider name is {@code null}
+ * or empty
*
- * @exception NoSuchProviderException if the specified provider is not
- * registered in the security provider list.
+ * @throws NoSuchAlgorithmException if a {@code KeyPairGeneratorSpi}
+ * implementation for the specified algorithm is not
+ * available from the specified provider
*
- * @exception IllegalArgumentException if the provider name is null
- * or empty.
+ * @throws NoSuchProviderException if the specified provider is not
+ * registered in the security provider list
+ *
+ * @throws NullPointerException if {@code algorithm} is {@code null}
*
* @see Provider
*/
public static KeyPairGenerator getInstance(String algorithm,
String provider)
throws NoSuchAlgorithmException, NoSuchProviderException {
+ Objects.requireNonNull(algorithm, "null algorithm name");
// Android-added: Check for Bouncy Castle deprecation
Providers.checkBouncyCastleDeprecation(provider, "KeyPairGenerator", algorithm);
Instance instance = GetInstance.getInstance("KeyPairGenerator",
@@ -317,19 +332,22 @@
*
* @param algorithm the standard string name of the algorithm.
* See the KeyPairGenerator section in the <a href=
- * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyPairGenerator">
- * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
+ * "{@docRoot}/../specs/security/standard-names.html#keypairgenerator-algorithms">
+ * Java Security Standard Algorithm Names Specification</a>
* for information about standard algorithm names.
*
* @param provider the provider.
*
- * @return the new KeyPairGenerator object.
+ * @return the new {@code KeyPairGenerator} object
*
- * @exception NoSuchAlgorithmException if a KeyPairGeneratorSpi
- * implementation for the specified algorithm is not available
- * from the specified Provider object.
+ * @throws IllegalArgumentException if the specified provider is
+ * {@code null}
*
- * @exception IllegalArgumentException if the specified provider is null.
+ * @throws NoSuchAlgorithmException if a {@code KeyPairGeneratorSpi}
+ * implementation for the specified algorithm is not available
+ * from the specified {@code Provider} object
+ *
+ * @throws NullPointerException if {@code algorithm} is {@code null}
*
* @see Provider
*
@@ -337,6 +355,7 @@
*/
public static KeyPairGenerator getInstance(String algorithm,
Provider provider) throws NoSuchAlgorithmException {
+ Objects.requireNonNull(algorithm, "null algorithm name");
// Android-added: Check for Bouncy Castle deprecation
Providers.checkBouncyCastleDeprecation(provider, "KeyPairGenerator", algorithm);
Instance instance = GetInstance.getInstance("KeyPairGenerator",
@@ -412,7 +431,7 @@
* of randomness.
* (If none of the installed providers supply an implementation of
* {@code SecureRandom}, a system-provided source of randomness is
- * used.).
+ * used.)
*
* <p>This concrete method has been added to this previously-defined
* abstract class.
@@ -574,9 +593,9 @@
private Iterator<Service> serviceIterator;
- private final static int I_NONE = 1;
- private final static int I_SIZE = 2;
- private final static int I_PARAMS = 3;
+ private static final int I_NONE = 1;
+ private static final int I_SIZE = 2;
+ private static final int I_PARAMS = 3;
private int initType;
private int initKeySize;
diff --git a/ojluni/src/main/java/java/security/KeyPairGeneratorSpi.java b/ojluni/src/main/java/java/security/KeyPairGeneratorSpi.java
index dfe8c04..a5dba15 100644
--- a/ojluni/src/main/java/java/security/KeyPairGeneratorSpi.java
+++ b/ojluni/src/main/java/java/security/KeyPairGeneratorSpi.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, 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
@@ -43,6 +43,7 @@
* of 1024 bits.
*
* @author Benjamin Renaud
+ * @since 1.2
*
*
* @see KeyPairGenerator
diff --git a/ojluni/src/main/java/java/security/KeyRep.java b/ojluni/src/main/java/java/security/KeyRep.java
index 0b1412c..9d53635 100644
--- a/ojluni/src/main/java/java/security/KeyRep.java
+++ b/ojluni/src/main/java/java/security/KeyRep.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2017, 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
@@ -42,7 +42,7 @@
*
* Note that a serialized Key may contain sensitive information
* which should not be exposed in untrusted environments. See the
- * <a href="../../../platform/serialization/spec/security.html">
+ * <a href="{@docRoot}/../specs/serialization/security.html">
* Security Appendix</a>
* of the Serialization Specification for more information.
*
@@ -112,8 +112,6 @@
/**
* Construct the alternate Key class.
*
- * <p>
- *
* @param type either one of Type.SECRET, Type.PUBLIC, or Type.PRIVATE
* @param algorithm the algorithm returned from
* {@code Key.getAlgorithm()}
@@ -157,8 +155,6 @@
* encoded key bytes, and generates a private key from the spec
* </ul>
*
- * <p>
- *
* @return the resolved Key object
*
* @exception ObjectStreamException if the Type/format
diff --git a/ojluni/src/main/java/java/security/NoSuchAlgorithmException.java b/ojluni/src/main/java/java/security/NoSuchAlgorithmException.java
index 951e44e..24455d3 100644
--- a/ojluni/src/main/java/java/security/NoSuchAlgorithmException.java
+++ b/ojluni/src/main/java/java/security/NoSuchAlgorithmException.java
@@ -30,6 +30,7 @@
* requested but is not available in the environment.
*
* @author Benjamin Renaud
+ * @since 1.1
*/
public class NoSuchAlgorithmException extends GeneralSecurityException {
diff --git a/ojluni/src/main/java/java/security/PKCS12Attribute.java b/ojluni/src/main/java/java/security/PKCS12Attribute.java
index e389862..4a8ebfb 100644
--- a/ojluni/src/main/java/java/security/PKCS12Attribute.java
+++ b/ojluni/src/main/java/java/security/PKCS12Attribute.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2020, 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
@@ -44,7 +44,7 @@
Pattern.compile("^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2})+$");
private String name;
private String value;
- private byte[] encoded;
+ private final byte[] encoded;
private int hashValue = -1;
/**
@@ -85,7 +85,8 @@
// Validate value
int length = value.length();
String[] values;
- if (value.charAt(0) == '[' && value.charAt(length - 1) == ']') {
+ if (length > 1 &&
+ value.charAt(0) == '[' && value.charAt(length - 1) == ']') {
values = value.substring(1, length - 1).split(", ");
} else {
values = new String[]{ value };
@@ -198,7 +199,7 @@
if (!(obj instanceof PKCS12Attribute)) {
return false;
}
- return Arrays.equals(encoded, ((PKCS12Attribute) obj).getEncoded());
+ return Arrays.equals(encoded, ((PKCS12Attribute) obj).encoded);
}
/**
@@ -209,10 +210,11 @@
*/
@Override
public int hashCode() {
- if (hashValue == -1) {
- Arrays.hashCode(encoded);
+ int h = hashValue;
+ if (h == -1) {
+ hashValue = h = Arrays.hashCode(encoded);
}
- return hashValue;
+ return h;
}
/**
@@ -252,6 +254,9 @@
private void parse(byte[] encoded) throws IOException {
DerInputStream attributeValue = new DerInputStream(encoded);
DerValue[] attrSeq = attributeValue.getSequence(2);
+ if (attrSeq.length != 2) {
+ throw new IOException("Invalid length for PKCS12Attribute");
+ }
ObjectIdentifier type = attrSeq[0].getOID();
DerInputStream attrContent =
new DerInputStream(attrSeq[1].toByteArray());
diff --git a/ojluni/src/main/java/java/security/Principal.java b/ojluni/src/main/java/java/security/Principal.java
index a538e70..40ee260 100644
--- a/ojluni/src/main/java/java/security/Principal.java
+++ b/ojluni/src/main/java/java/security/Principal.java
@@ -35,6 +35,7 @@
* @see java.security.cert.X509Certificate
*
* @author Li Gong
+ * @since 1.1
*/
public interface Principal {
@@ -74,7 +75,8 @@
/**
* Returns true if the specified subject is implied by this principal.
*
- * <p>The default implementation of this method returns true if
+ * @implSpec
+ * The default implementation of this method returns true if
* {@code subject} is non-null and contains at least one principal that
* is equal to this principal.
*
diff --git a/ojluni/src/main/java/java/security/PrivateKey.java b/ojluni/src/main/java/java/security/PrivateKey.java
index 7d8a7ea..0bc933b 100644
--- a/ojluni/src/main/java/java/security/PrivateKey.java
+++ b/ojluni/src/main/java/java/security/PrivateKey.java
@@ -54,6 +54,7 @@
*
* @author Benjamin Renaud
* @author Josh Bloch
+ * @since 1.1
*/
public interface PrivateKey extends Key, javax.security.auth.Destroyable {
diff --git a/ojluni/src/main/java/java/security/PrivilegedActionException.java b/ojluni/src/main/java/java/security/PrivilegedActionException.java
index b1eb28f..ddfb89d 100644
--- a/ojluni/src/main/java/java/security/PrivilegedActionException.java
+++ b/ojluni/src/main/java/java/security/PrivilegedActionException.java
@@ -47,6 +47,7 @@
* <i>cause</i>, and may be accessed via the {@link Throwable#getCause()}
* method, as well as the aforementioned "legacy method."
*
+ * @since 1.2
* @see PrivilegedExceptionAction
* @see AccessController#doPrivileged(PrivilegedExceptionAction)
* @see AccessController#doPrivileged(PrivilegedExceptionAction,AccessControlContext)
diff --git a/ojluni/src/main/java/java/security/ProtectionDomain.java b/ojluni/src/main/java/java/security/ProtectionDomain.java
index 9e117a6..47e9fb1 100644
--- a/ojluni/src/main/java/java/security/ProtectionDomain.java
+++ b/ojluni/src/main/java/java/security/ProtectionDomain.java
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2014 The Android Open Source Project
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2018, 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
diff --git a/ojluni/src/main/java/java/security/ProviderException.java b/ojluni/src/main/java/java/security/ProviderException.java
index b372ee7..48c3638 100644
--- a/ojluni/src/main/java/java/security/ProviderException.java
+++ b/ojluni/src/main/java/java/security/ProviderException.java
@@ -32,6 +32,7 @@
* throw specialized, provider-specific runtime errors.
*
* @author Benjamin Renaud
+ * @since 1.1
*/
public class ProviderException extends RuntimeException {
diff --git a/ojluni/src/main/java/java/security/PublicKey.java b/ojluni/src/main/java/java/security/PublicKey.java
index df49807..986f9d0 100644
--- a/ojluni/src/main/java/java/security/PublicKey.java
+++ b/ojluni/src/main/java/java/security/PublicKey.java
@@ -34,6 +34,7 @@
* See, for example, the DSAPublicKey interface in
* {@code java.security.interfaces}.
*
+ * @since 1.1
* @see Key
* @see PrivateKey
* @see Certificate
diff --git a/ojluni/src/main/java/java/security/SecurityPermission.java b/ojluni/src/main/java/java/security/SecurityPermission.java
index f02755c..a4eefcc 100644
--- a/ojluni/src/main/java/java/security/SecurityPermission.java
+++ b/ojluni/src/main/java/java/security/SecurityPermission.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, 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
diff --git a/ojluni/src/main/java/java/security/SignatureException.java b/ojluni/src/main/java/java/security/SignatureException.java
index 2e1fa59..7788e12 100644
--- a/ojluni/src/main/java/java/security/SignatureException.java
+++ b/ojluni/src/main/java/java/security/SignatureException.java
@@ -29,6 +29,7 @@
* This is the generic Signature exception.
*
* @author Benjamin Renaud
+ * @since 1.1
*/
public class SignatureException extends GeneralSecurityException {
diff --git a/ojluni/src/main/java/java/security/SignedObject.java b/ojluni/src/main/java/java/security/SignedObject.java
index 9ac864e..e14c0fa 100644
--- a/ojluni/src/main/java/java/security/SignedObject.java
+++ b/ojluni/src/main/java/java/security/SignedObject.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, 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
@@ -81,13 +81,12 @@
* verification in an attempt to bypass a security check.
*
* <p> The signature algorithm can be, among others, the NIST standard
- * DSA, using DSA and SHA-1. The algorithm is specified using the
+ * DSA, using DSA and SHA-256. The algorithm is specified using the
* same convention as that for signatures. The DSA algorithm using the
- * SHA-1 message digest algorithm can be specified, for example, as
- * "SHA/DSA" or "SHA-1/DSA" (they are equivalent). In the case of
- * RSA, there are multiple choices for the message digest algorithm,
- * so the signing algorithm could be specified as, for example,
- * "MD2/RSA", "MD5/RSA" or "SHA-1/RSA". The algorithm name must be
+ * SHA-256 message digest algorithm can be specified, for example, as
+ * "SHA256withDSA". In the case of
+ * RSA the signing algorithm could be specified as, for example,
+ * "SHA256withRSA". The algorithm name must be
* specified, as there is no default.
*
* <p> The name of the Cryptography Package Provider is designated
@@ -114,6 +113,7 @@
* @see Signature
*
* @author Li Gong
+ * @since 1.2
*/
public final class SignedObject implements Serializable {
diff --git a/ojluni/src/main/java/java/security/Signer.java b/ojluni/src/main/java/java/security/Signer.java
index 077538d..56d3780 100644
--- a/ojluni/src/main/java/java/security/Signer.java
+++ b/ojluni/src/main/java/java/security/Signer.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2017, 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
@@ -38,13 +38,15 @@
* @see Identity
*
* @author Benjamin Renaud
+ * @since 1.1
*
- * @deprecated This class is no longer used. Its functionality has been
- * replaced by {@code java.security.KeyStore}, the
- * {@code java.security.cert} package, and
- * {@code java.security.Principal}.
+ * @deprecated This class is deprecated and subject to removal in a future
+ * version of Java SE. It has been replaced by
+ * {@code java.security.KeyStore}, the {@code java.security.cert} package,
+ * and {@code java.security.Principal}.
*/
-@Deprecated
+@Deprecated(since="1.2", forRemoval=true)
+@SuppressWarnings("removal")
public abstract class Signer extends Identity {
private static final long serialVersionUID = -1763464102261361480L;
diff --git a/ojluni/src/main/java/java/security/Timestamp.java b/ojluni/src/main/java/java/security/Timestamp.java
index f66d288..668a611 100644
--- a/ojluni/src/main/java/java/security/Timestamp.java
+++ b/ojluni/src/main/java/java/security/Timestamp.java
@@ -141,7 +141,7 @@
* its signer's certificate.
*/
public String toString() {
- StringBuffer sb = new StringBuffer();
+ StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append("timestamp: " + timestamp);
List<? extends Certificate> certs = signerCertPath.getCertificates();
diff --git a/ojluni/src/main/java/java/security/UnresolvedPermission.java b/ojluni/src/main/java/java/security/UnresolvedPermission.java
index 6f3bf4a..7e535da 100644
--- a/ojluni/src/main/java/java/security/UnresolvedPermission.java
+++ b/ojluni/src/main/java/java/security/UnresolvedPermission.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, 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
diff --git a/ojluni/src/main/java/java/security/interfaces/DSAKey.java b/ojluni/src/main/java/java/security/interfaces/DSAKey.java
index d78b3e1..64ac8c4 100644
--- a/ojluni/src/main/java/java/security/interfaces/DSAKey.java
+++ b/ojluni/src/main/java/java/security/interfaces/DSAKey.java
@@ -35,6 +35,7 @@
*
* @author Benjamin Renaud
* @author Josh Bloch
+ * @since 1.1
*/
public interface DSAKey {
diff --git a/ojluni/src/main/java/java/security/interfaces/DSAKeyPairGenerator.java b/ojluni/src/main/java/java/security/interfaces/DSAKeyPairGenerator.java
index e50cfd2..96e55eb 100644
--- a/ojluni/src/main/java/java/security/interfaces/DSAKeyPairGenerator.java
+++ b/ojluni/src/main/java/java/security/interfaces/DSAKeyPairGenerator.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, 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
@@ -32,9 +32,12 @@
*
* <p>The {@code initialize} methods may each be called any number
* of times. If no {@code initialize} method is called on a
- * DSAKeyPairGenerator, the default is to generate 1024-bit keys, using
- * precomputed p, q and g parameters and an instance of SecureRandom as
- * the random bit source.
+ * DSAKeyPairGenerator, each provider that implements this interface
+ * should supply (and document) a default initialization. Note that
+ * defaults may vary across different providers. Additionally, the default
+ * value for a provider may change in a future version. Therefore, it is
+ * recommended to explicitly initialize the DSAKeyPairGenerator instead
+ * of relying on provider-specific defaults.
*
* <p>Users wishing to indicate DSA-specific parameters, and to generate a key
* pair suitable for use with the DSA algorithm typically
@@ -45,16 +48,17 @@
* KeyPairGenerator {@code getInstance} method with "DSA"
* as its argument.
*
- * <li>Initialize the generator by casting the result to a DSAKeyPairGenerator
- * and calling one of the
- * {@code initialize} methods from this DSAKeyPairGenerator interface.
+ * <li>Check if the returned key pair generator is an instance of
+ * DSAKeyPairGenerator before casting the result to a DSAKeyPairGenerator
+ * and calling one of the {@code initialize} methods from this
+ * DSAKeyPairGenerator interface.
*
* <li>Generate a key pair by calling the {@code generateKeyPair}
- * method from the KeyPairGenerator class.
+ * method of the KeyPairGenerator class.
*
* </ol>
*
- * <p>Note: it is not always necessary to do do algorithm-specific
+ * <p>Note: it is not always necessary to do algorithm-specific
* initialization for a DSA key pair generator. That is, it is not always
* necessary to call an {@code initialize} method in this interface.
* Algorithm-independent initialization using the {@code initialize} method
@@ -63,8 +67,9 @@
* parameters.
*
* <p>Note: Some earlier implementations of this interface may not support
- * larger sizes of DSA parameters such as 2048 and 3072-bit.
+ * larger values of DSA parameters such as 3072-bit.
*
+ * @since 1.1
* @see java.security.KeyPairGenerator
*/
public interface DSAKeyPairGenerator {
@@ -96,8 +101,7 @@
* p, q and g parameters. If it is false, the method uses precomputed
* parameters for the modulus length requested. If there are no
* precomputed parameters for that modulus length, an exception will be
- * thrown. It is guaranteed that there will always be
- * default parameters for modulus lengths of 512 and 1024 bits.
+ * thrown.
*
* @param modlen the modulus length in bits. Valid values are any
* multiple of 64 between 512 and 1024, inclusive, 2048, and 3072.
diff --git a/ojluni/src/main/java/java/security/interfaces/DSAParams.java b/ojluni/src/main/java/java/security/interfaces/DSAParams.java
index 8c46ed5..2eafe87 100644
--- a/ojluni/src/main/java/java/security/interfaces/DSAParams.java
+++ b/ojluni/src/main/java/java/security/interfaces/DSAParams.java
@@ -38,6 +38,7 @@
*
* @author Benjamin Renaud
* @author Josh Bloch
+ * @since 1.1
*/
public interface DSAParams {
diff --git a/ojluni/src/main/java/java/security/interfaces/DSAPrivateKey.java b/ojluni/src/main/java/java/security/interfaces/DSAPrivateKey.java
index 81ab358..b23a5c1 100644
--- a/ojluni/src/main/java/java/security/interfaces/DSAPrivateKey.java
+++ b/ojluni/src/main/java/java/security/interfaces/DSAPrivateKey.java
@@ -37,6 +37,7 @@
* @see DSAPublicKey
*
* @author Benjamin Renaud
+ * @since 1.1
*/
public interface DSAPrivateKey extends DSAKey, java.security.PrivateKey {
diff --git a/ojluni/src/main/java/java/security/interfaces/DSAPublicKey.java b/ojluni/src/main/java/java/security/interfaces/DSAPublicKey.java
index e56b795..fb4a2f3 100644
--- a/ojluni/src/main/java/java/security/interfaces/DSAPublicKey.java
+++ b/ojluni/src/main/java/java/security/interfaces/DSAPublicKey.java
@@ -37,6 +37,7 @@
* @see DSAPrivateKey
*
* @author Benjamin Renaud
+ * @since 1.1
*/
public interface DSAPublicKey extends DSAKey, java.security.PublicKey {
diff --git a/ojluni/src/main/java/java/security/interfaces/RSAMultiPrimePrivateCrtKey.java b/ojluni/src/main/java/java/security/interfaces/RSAMultiPrimePrivateCrtKey.java
index f85d96a..fd42c5c 100644
--- a/ojluni/src/main/java/java/security/interfaces/RSAMultiPrimePrivateCrtKey.java
+++ b/ojluni/src/main/java/java/security/interfaces/RSAMultiPrimePrivateCrtKey.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2018, 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
@@ -30,8 +30,8 @@
/**
* The interface to an RSA multi-prime private key, as defined in the
- * PKCS#1 v2.1, using the <i>Chinese Remainder Theorem</i>
- * (CRT) information values.
+ * <a href="https://tools.ietf.org/rfc/rfc8017.txt">PKCS#1 v2.2</a> standard,
+ * using the <i>Chinese Remainder Theorem</i> (CRT) information values.
*
* @author Valerie Peng
*
diff --git a/ojluni/src/main/java/java/security/interfaces/RSAPrivateCrtKey.java b/ojluni/src/main/java/java/security/interfaces/RSAPrivateCrtKey.java
index 0408fea..e6acef1 100644
--- a/ojluni/src/main/java/java/security/interfaces/RSAPrivateCrtKey.java
+++ b/ojluni/src/main/java/java/security/interfaces/RSAPrivateCrtKey.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2018, 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
@@ -28,10 +28,12 @@
import java.math.BigInteger;
/**
- * The interface to an RSA private key, as defined in the PKCS#1 standard,
+ * The interface to an RSA private key, as defined in the
+ * <a href="https://tools.ietf.org/rfc/rfc8017.txt">PKCS#1 v2.2</a> standard,
* using the <i>Chinese Remainder Theorem</i> (CRT) information values.
*
* @author Jan Luehe
+ * @since 1.2
*
*
* @see RSAPrivateKey
diff --git a/ojluni/src/main/java/java/security/interfaces/RSAPrivateKey.java b/ojluni/src/main/java/java/security/interfaces/RSAPrivateKey.java
index 5d69ad6..390da4e 100644
--- a/ojluni/src/main/java/java/security/interfaces/RSAPrivateKey.java
+++ b/ojluni/src/main/java/java/security/interfaces/RSAPrivateKey.java
@@ -31,6 +31,7 @@
* The interface to an RSA private key.
*
* @author Jan Luehe
+ * @since 1.2
*
*
* @see RSAPrivateCrtKey
diff --git a/ojluni/src/main/java/java/security/interfaces/RSAPublicKey.java b/ojluni/src/main/java/java/security/interfaces/RSAPublicKey.java
index a698c05..f195306 100644
--- a/ojluni/src/main/java/java/security/interfaces/RSAPublicKey.java
+++ b/ojluni/src/main/java/java/security/interfaces/RSAPublicKey.java
@@ -31,6 +31,7 @@
* The interface to an RSA public key.
*
* @author Jan Luehe
+ * @since 1.2
*
*/
diff --git a/ojluni/src/test/java/util/Collection/MOAT.java b/ojluni/src/test/java/util/Collection/MOAT.java
new file mode 100644
index 0000000..e0e7b0d
--- /dev/null
+++ b/ojluni/src/test/java/util/Collection/MOAT.java
@@ -0,0 +1,1795 @@
+/*
+ * Copyright (c) 2005, 2017, 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 6207984 6272521 6192552 6269713 6197726 6260652 5073546 4137464
+ * 4155650 4216399 4294891 6282555 6318622 6355327 6383475 6420753
+ * 6431845 4802633 6570566 6570575 6570631 6570924 6691185 6691215
+ * 4802647 7123424 8024709 8193128
+ * @summary Run many tests on many Collection and Map implementations
+ * @author Martin Buchholz
+ * @modules java.base/java.util:open
+ * @run main MOAT
+ * @key randomness
+ */
+
+/* Mother Of All (Collection) Tests
+ *
+ * Testing of collection classes is often spotty, because many tests
+ * need to be performed on many implementations, but the onus on
+ * writing the tests falls on the engineer introducing the new
+ * implementation.
+ *
+ * The idea of this mega-test is that:
+ *
+ * An engineer adding a new collection implementation could simply add
+ * their new implementation to a list of implementations in this
+ * test's main method. Any general purpose Collection<Integer> or
+ * Map<Integer,Integer> class is appropriate.
+ *
+ * An engineer fixing a regression could add their regression test here and
+ * simultaneously test all other implementations.
+ */
+package test.java.util.Collection;
+
+import java.io.*;
+import java.util.*;
+import java.util.concurrent.*;
+import static java.util.Collections.*;
+import java.lang.reflect.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+// Android-added: run as TestNG test case.
+import org.testng.annotations.Test;
+
+public class MOAT {
+ // Collections under test must not be initialized to contain this value,
+ // and maps under test must not contain this value as a key.
+ // It's used as a sentinel for absent-element testing.
+ static final int ABSENT_VALUE = 778347983;
+
+ static final Integer[] integerArray;
+ static {
+ Integer[] ia = new Integer[20];
+ // fill with 1..20 inclusive
+ for (int i = 0; i < ia.length; i++) {
+ ia[i] = i + 1;
+ }
+ integerArray = ia;
+ }
+
+ // Android-changed: run as TestNG test case and do not accept arguments.
+ // public static void realMain(String[] args) {
+ @Test
+ public void realMain() {
+
+ testCollection(new NewAbstractCollection<Integer>());
+ testCollection(new NewAbstractSet<Integer>());
+ testCollection(new LinkedHashSet<Integer>());
+ testCollection(new HashSet<Integer>());
+ testCollection(new Vector<Integer>());
+ testCollection(new Vector<Integer>().subList(0,0));
+ testCollection(new ArrayDeque<Integer>());
+ testCollection(new ArrayList<Integer>());
+ testCollection(new ArrayList<Integer>().subList(0,0));
+ testCollection(new LinkedList<Integer>());
+ testCollection(new LinkedList<Integer>().subList(0,0));
+ testCollection(new TreeSet<Integer>());
+ testCollection(Collections.checkedList(new ArrayList<Integer>(), Integer.class));
+ testCollection(Collections.synchronizedList(new ArrayList<Integer>()));
+ testCollection(Collections.checkedSet(new HashSet<Integer>(), Integer.class));
+ testCollection(Collections.checkedSortedSet(new TreeSet<Integer>(), Integer.class));
+ testCollection(Collections.checkedNavigableSet(new TreeSet<Integer>(), Integer.class));
+ testCollection(Collections.synchronizedSet(new HashSet<Integer>()));
+ testCollection(Collections.synchronizedSortedSet(new TreeSet<Integer>()));
+ testCollection(Collections.synchronizedNavigableSet(new TreeSet<Integer>()));
+
+ testCollection(new CopyOnWriteArrayList<Integer>());
+ testCollection(new CopyOnWriteArrayList<Integer>().subList(0,0));
+ testCollection(new CopyOnWriteArraySet<Integer>());
+ testCollection(new PriorityQueue<Integer>());
+ testCollection(new PriorityBlockingQueue<Integer>());
+ testCollection(new ArrayBlockingQueue<Integer>(20));
+ testCollection(new LinkedBlockingQueue<Integer>(20));
+ testCollection(new LinkedBlockingDeque<Integer>(20));
+ testCollection(new ConcurrentLinkedDeque<Integer>());
+ testCollection(new ConcurrentLinkedQueue<Integer>());
+ testCollection(new LinkedTransferQueue<Integer>());
+ testCollection(new ConcurrentSkipListSet<Integer>());
+ testCollection(Arrays.asList(new Integer(42)));
+ testCollection(Arrays.asList(1,2,3));
+ testCollection(nCopies(25,1));
+ testImmutableList(nCopies(25,1));
+
+ testMap(new HashMap<Integer,Integer>());
+ testMap(new LinkedHashMap<Integer,Integer>());
+
+ // TODO: Add reliable support for WeakHashMap.
+ // This test is subject to very rare failures because the GC
+ // may remove unreferenced-keys from the map at any time.
+ // testMap(new WeakHashMap<Integer,Integer>());
+
+ testMap(new IdentityHashMap<Integer,Integer>());
+ testMap(new TreeMap<Integer,Integer>());
+ testMap(new Hashtable<Integer,Integer>());
+ testMap(new ConcurrentHashMap<Integer,Integer>(10, 0.5f));
+ testMap(new ConcurrentSkipListMap<Integer,Integer>());
+ testMap(Collections.checkedMap(new HashMap<Integer,Integer>(), Integer.class, Integer.class));
+ testMap(Collections.checkedSortedMap(new TreeMap<Integer,Integer>(), Integer.class, Integer.class));
+ testMap(Collections.checkedNavigableMap(new TreeMap<Integer,Integer>(), Integer.class, Integer.class));
+ testMap(Collections.synchronizedMap(new HashMap<Integer,Integer>()));
+ testMap(Collections.synchronizedSortedMap(new TreeMap<Integer,Integer>()));
+ testMap(Collections.synchronizedNavigableMap(new TreeMap<Integer,Integer>()));
+
+ // Unmodifiable wrappers
+ testImmutableSet(unmodifiableSet(new HashSet<>(Arrays.asList(1,2,3))));
+ testImmutableList(unmodifiableList(Arrays.asList(1,2,3)));
+ testImmutableMap(unmodifiableMap(Collections.singletonMap(1,2)));
+ testCollMutatorsAlwaysThrow(unmodifiableSet(new HashSet<>(Arrays.asList(1,2,3))));
+ testCollMutatorsAlwaysThrow(unmodifiableSet(Collections.emptySet()));
+ testEmptyCollMutatorsAlwaysThrow(unmodifiableSet(Collections.emptySet()));
+ testListMutatorsAlwaysThrow(unmodifiableList(Arrays.asList(1,2,3)));
+ testListMutatorsAlwaysThrow(unmodifiableList(Collections.emptyList()));
+ testEmptyListMutatorsAlwaysThrow(unmodifiableList(Collections.emptyList()));
+ testMapMutatorsAlwaysThrow(unmodifiableMap(Collections.singletonMap(1,2)));
+ testMapMutatorsAlwaysThrow(unmodifiableMap(Collections.emptyMap()));
+ testEmptyMapMutatorsAlwaysThrow(unmodifiableMap(Collections.emptyMap()));
+
+ // Empty collections
+ final List<Integer> emptyArray = Arrays.asList(new Integer[]{});
+ testCollection(emptyArray);
+ testEmptyList(emptyArray);
+ THROWS(IndexOutOfBoundsException.class, () -> emptyArray.set(0,1));
+ THROWS(UnsupportedOperationException.class, () -> emptyArray.add(0,1));
+
+ List<Integer> noOne = nCopies(0,1);
+ testCollection(noOne);
+ testEmptyList(noOne);
+ testImmutableList(noOne);
+
+ Set<Integer> emptySet = emptySet();
+ testCollection(emptySet);
+ testEmptySet(emptySet);
+ testEmptySet(EMPTY_SET);
+ testEmptySet(Collections.emptySet());
+ testEmptySet(Collections.emptySortedSet());
+ testEmptySet(Collections.emptyNavigableSet());
+ testImmutableSet(emptySet);
+
+ List<Integer> emptyList = emptyList();
+ testCollection(emptyList);
+ testEmptyList(emptyList);
+ testEmptyList(EMPTY_LIST);
+ testEmptyList(Collections.emptyList());
+ testImmutableList(emptyList);
+
+ Map<Integer,Integer> emptyMap = emptyMap();
+ testMap(emptyMap);
+ testEmptyMap(emptyMap);
+ testEmptyMap(EMPTY_MAP);
+ testEmptyMap(Collections.emptyMap());
+ testEmptyMap(Collections.emptySortedMap());
+ testEmptyMap(Collections.emptyNavigableMap());
+ testImmutableMap(emptyMap);
+ testImmutableMap(Collections.emptyMap());
+ testImmutableMap(Collections.emptySortedMap());
+ testImmutableMap(Collections.emptyNavigableMap());
+
+ // Singleton collections
+ Set<Integer> singletonSet = singleton(1);
+ equal(singletonSet.size(), 1);
+ testCollection(singletonSet);
+ testImmutableSet(singletonSet);
+
+ List<Integer> singletonList = singletonList(1);
+ equal(singletonList.size(), 1);
+ testCollection(singletonList);
+ testImmutableList(singletonList);
+ testImmutableList(singletonList.subList(0,1));
+ testImmutableList(singletonList.subList(0,1).subList(0,1));
+ testEmptyList(singletonList.subList(0,0));
+ testEmptyList(singletonList.subList(0,0).subList(0,0));
+
+ Map<Integer,Integer> singletonMap = singletonMap(1,2);
+ equal(singletonMap.size(), 1);
+ testMap(singletonMap);
+ testImmutableMap(singletonMap);
+
+ // Immutable List
+ testEmptyList(List.of());
+ testEmptyList(List.of().subList(0,0));
+ testListMutatorsAlwaysThrow(List.of());
+ testListMutatorsAlwaysThrow(List.<Integer>of().subList(0,0));
+ testEmptyListMutatorsAlwaysThrow(List.of());
+ testEmptyListMutatorsAlwaysThrow(List.<Integer>of().subList(0,0));
+ for (List<Integer> list : Arrays.asList(
+ List.<Integer>of(),
+ List.of(1),
+ List.of(1, 2),
+ List.of(1, 2, 3),
+ List.of(1, 2, 3, 4),
+ List.of(1, 2, 3, 4, 5),
+ List.of(1, 2, 3, 4, 5, 6),
+ List.of(1, 2, 3, 4, 5, 6, 7),
+ List.of(1, 2, 3, 4, 5, 6, 7, 8),
+ List.of(1, 2, 3, 4, 5, 6, 7, 8, 9),
+ List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
+ List.of(integerArray))) {
+ testCollection(list);
+ testImmutableList(list);
+ testListMutatorsAlwaysThrow(list);
+ if (list.size() >= 1) {
+ // test subLists
+ List<Integer> headList = list.subList(0, list.size() - 1);
+ List<Integer> tailList = list.subList(1, list.size());
+ testCollection(headList);
+ testCollection(tailList);
+ testImmutableList(headList);
+ testImmutableList(tailList);
+ testListMutatorsAlwaysThrow(headList);
+ testListMutatorsAlwaysThrow(tailList);
+ }
+ }
+
+ List<Integer> listCopy = List.copyOf(Arrays.asList(1, 2, 3));
+ testCollection(listCopy);
+ testImmutableList(listCopy);
+ testListMutatorsAlwaysThrow(listCopy);
+
+ List<Integer> listCollected = Stream.of(1, 2, 3).collect(Collectors.toUnmodifiableList());
+ equal(listCollected, List.of(1, 2, 3));
+ testCollection(listCollected);
+ testImmutableList(listCollected);
+ testListMutatorsAlwaysThrow(listCollected);
+
+ // List indexOf / lastIndexOf
+
+ // 0 element
+ System.out.println("testListIndexOf size 0");
+ testListIndexOf(-1, -1);
+
+ System.out.println("testListIndexOf size 1");
+ testListIndexOf(-1, -1, 0);
+ testListIndexOf(0, 0, 1);
+
+ System.out.println("testListIndexOf size 2");
+ testListIndexOf(-1, -1, 0, 0);
+ testListIndexOf(0, 0, 1, 0);
+ testListIndexOf(0, 1, 1, 1);
+ testListIndexOf(1, 1, 0, 1);
+
+
+ System.out.println("testListIndexOf size 3");
+ testListIndexOf(-1, -1, 0, 0, 0);
+ testListIndexOf(0, 0, 1, 0, 0);
+ testListIndexOf(0, 1, 1, 1, 0);
+ testListIndexOf(1, 2, 0, 1, 1);
+ testListIndexOf(2, 2, 0, 0, 1);
+
+ System.out.println("testListIndexOf size N");
+ testListIndexOf(-1, -1, 0, 0, 0, 0, 0, 0, 0);
+ testListIndexOf(2, 6, 0, 0, 1, 0, 1, 0, 1);
+ testListIndexOf(4, 4, 0, 0, 0, 0, 1, 0, 0);
+ testListIndexOf(0, 6, 1, 1, 1, 1, 1, 1, 1);
+ testListIndexOf(0, 7, 1, 1, 1, 1, 1, 1, 1, 1);
+ testListIndexOf(0, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1);
+ testListIndexOf(0, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
+ testListIndexOf(0, 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
+ testListIndexOf(0, 11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
+ testListIndexOf(0, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
+ testListIndexOf(12, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
+ testListIndexOf(-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
+
+ // Immutable Set
+ testEmptySet(Set.of());
+ testCollMutatorsAlwaysThrow(Set.of());
+ testEmptyCollMutatorsAlwaysThrow(Set.of());
+ for (Set<Integer> set : Arrays.asList(
+ Set.<Integer>of(),
+ Set.of(1),
+ Set.of(1, 2),
+ Set.of(1, 2, 3),
+ Set.of(1, 2, 3, 4),
+ Set.of(1, 2, 3, 4, 5),
+ Set.of(1, 2, 3, 4, 5, 6),
+ Set.of(1, 2, 3, 4, 5, 6, 7),
+ Set.of(1, 2, 3, 4, 5, 6, 7, 8),
+ Set.of(1, 2, 3, 4, 5, 6, 7, 8, 9),
+ Set.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
+ Set.of(integerArray))) {
+ testCollection(set);
+ testImmutableSet(set);
+ testCollMutatorsAlwaysThrow(set);
+ }
+
+ Set<Integer> setCopy = Set.copyOf(Arrays.asList(1, 2, 3));
+ testCollection(setCopy);
+ testImmutableSet(setCopy);
+ testCollMutatorsAlwaysThrow(setCopy);
+
+ Set<Integer> setCollected = Stream.of(1, 1, 2, 3, 2, 3)
+ .collect(Collectors.toUnmodifiableSet());
+ equal(setCollected, Set.of(1, 2, 3));
+ testCollection(setCollected);
+ testImmutableSet(setCollected);
+ testCollMutatorsAlwaysThrow(setCollected);
+
+ // Immutable Map
+
+ @SuppressWarnings("unchecked")
+ Map.Entry<Integer,Integer>[] ea = (Map.Entry<Integer,Integer>[])new Map.Entry<?,?>[20];
+ for (int i = 0; i < ea.length; i++) {
+ ea[i] = Map.entry(i+1, i+101);
+ }
+
+ testEmptyMap(Map.of());
+ testMapMutatorsAlwaysThrow(Map.of());
+ testEmptyMapMutatorsAlwaysThrow(Map.of());
+ for (Map<Integer,Integer> map : Arrays.asList(
+ Map.<Integer,Integer>of(),
+ Map.of(1, 101),
+ Map.of(1, 101, 2, 202),
+ Map.of(1, 101, 2, 202, 3, 303),
+ Map.of(1, 101, 2, 202, 3, 303, 4, 404),
+ Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505),
+ Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606),
+ Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707),
+ Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707, 8, 808),
+ Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707, 8, 808, 9, 909),
+ Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707, 8, 808, 9, 909, 10, 1010),
+ Map.ofEntries(ea))) {
+ testMap(map);
+ testImmutableMap(map);
+ testMapMutatorsAlwaysThrow(map);
+ }
+
+ Map<Integer,Integer> mapCopy = Map.copyOf(new HashMap<>(Map.of(1, 101, 2, 202, 3, 303)));
+ testMap(mapCopy);
+ testImmutableMap(mapCopy);
+ testMapMutatorsAlwaysThrow(mapCopy);
+
+ Map<Integer,Integer> mapCollected1 =
+ Stream.of(1, 2, 3)
+ .collect(Collectors.toUnmodifiableMap(i -> i, i -> 101 * i));
+ equal(mapCollected1, Map.of(1, 101, 2, 202, 3, 303));
+ testMap(mapCollected1);
+ testImmutableMap(mapCollected1);
+ testMapMutatorsAlwaysThrow(mapCollected1);
+
+ try {
+ Stream.of(1, 1, 2, 3, 2, 3)
+ .collect(Collectors.toUnmodifiableMap(i -> i, i -> 101 * i));
+ fail("duplicates should have thrown an exception");
+ } catch (IllegalStateException ise) {
+ pass();
+ }
+
+ Map<Integer,Integer> mapCollected2 =
+ Stream.of(1, 1, 2, 3, 2, 3)
+ .collect(Collectors.toUnmodifiableMap(i -> i, i -> 101 * i, Integer::sum));
+ equal(mapCollected2, Map.of(1, 202, 2, 404, 3, 606));
+ testMap(mapCollected2);
+ testImmutableMap(mapCollected2);
+ testMapMutatorsAlwaysThrow(mapCollected2);
+ }
+
+ private static void checkContainsSelf(Collection<Integer> c) {
+ check(c.containsAll(c));
+ check(c.containsAll(Arrays.asList(c.toArray())));
+ check(c.containsAll(Arrays.asList(c.toArray(new Integer[0]))));
+ }
+
+ private static void checkContainsEmpty(Collection<Integer> c) {
+ check(c.containsAll(new ArrayList<Integer>()));
+ }
+
+ private static void checkUnique(Set<Integer> s) {
+ for (Integer i : s) {
+ int count = 0;
+ for (Integer j : s) {
+ if (Objects.equals(i,j))
+ ++count;
+ }
+ check(count == 1);
+ }
+ }
+
+ private static <T> void testEmptyCollection(Collection<T> c) {
+ check(c.isEmpty());
+ equal(c.size(), 0);
+ equal(c.toString(),"[]");
+ equal(c.toArray().length, 0);
+ equal(c.toArray(new Object[0]).length, 0);
+ equal(c.toArray(Object[]::new).length, 0);
+ check(c.toArray(new Object[]{42})[0] == null);
+
+ Object[] a = new Object[1]; a[0] = Boolean.TRUE;
+ equal(c.toArray(a), a);
+ equal(a[0], null);
+ testEmptyIterator(c.iterator());
+ }
+
+ static <T> void testEmptyIterator(final Iterator<T> it) {
+ if (rnd.nextBoolean())
+ check(! it.hasNext());
+
+ THROWS(NoSuchElementException.class, () -> it.next());
+
+ try { it.remove(); }
+ catch (IllegalStateException ignored) { pass(); }
+ catch (UnsupportedOperationException ignored) { pass(); }
+ catch (Throwable t) { unexpected(t); }
+
+ if (rnd.nextBoolean())
+ check(! it.hasNext());
+ }
+
+ private static void testEmptyList(List<?> c) {
+ testEmptyCollection(c);
+ equal(c.hashCode(), 1);
+ equal2(c, Collections.<Integer>emptyList());
+ }
+
+ private static <T> void testEmptySet(Set<T> c) {
+ testEmptyCollection(c);
+ equal(c.hashCode(), 0);
+ equal2(c, Collections.<Integer>emptySet());
+ if (c instanceof NavigableSet<?>)
+ testEmptyIterator(((NavigableSet<T>)c).descendingIterator());
+ }
+
+ private static void testImmutableCollection(final Collection<Integer> c) {
+ THROWS(UnsupportedOperationException.class,
+ () -> c.add(99),
+ () -> c.addAll(singleton(99)));
+ if (! c.isEmpty()) {
+ final Integer first = c.iterator().next();
+ THROWS(UnsupportedOperationException.class,
+ () -> c.clear(),
+ () -> c.remove(first),
+ () -> c.removeAll(singleton(first)),
+ () -> c.retainAll(emptyList()));
+ }
+ }
+
+ private static void testImmutableSet(final Set<Integer> c) {
+ testImmutableCollection(c);
+ }
+
+ private static void testImmutableList(final List<Integer> c) {
+ testList(c);
+ testImmutableCollection(c);
+ THROWS(UnsupportedOperationException.class,
+ () -> c.set(0,42),
+ () -> c.add(0,42),
+ () -> c.addAll(0,singleton(86)));
+ if (! c.isEmpty())
+ THROWS(UnsupportedOperationException.class,
+ () -> { Iterator<Integer> it = c.iterator();
+ it.next();
+ it.remove(); },
+ () -> { ListIterator<Integer> it = c.listIterator();
+ it.next();
+ it.remove(); });
+ }
+
+ /**
+ * Test that calling a mutator always throws UOE, even if the mutator
+ * wouldn't actually do anything, given its arguments.
+ *
+ * @param c the collection instance to test
+ */
+ private static void testCollMutatorsAlwaysThrow(Collection<Integer> c) {
+ THROWS(UnsupportedOperationException.class,
+ () -> c.addAll(Collections.emptyList()),
+ () -> c.remove(ABSENT_VALUE),
+ () -> c.removeAll(Collections.emptyList()),
+ () -> c.removeIf(x -> false),
+ () -> c.retainAll(c));
+ }
+
+ /**
+ * Test that calling a mutator always throws UOE, even if the mutator
+ * wouldn't actually do anything on an empty collection.
+ *
+ * @param c the collection instance to test, must be empty
+ */
+ private static void testEmptyCollMutatorsAlwaysThrow(Collection<Integer> c) {
+ if (! c.isEmpty()) {
+ fail("collection is not empty");
+ }
+ THROWS(UnsupportedOperationException.class,
+ () -> c.clear());
+ }
+
+ /**
+ * As above, for a list.
+ *
+ * @param c the list instance to test
+ */
+ private static void testListMutatorsAlwaysThrow(List<Integer> c) {
+ testCollMutatorsAlwaysThrow(c);
+ THROWS(UnsupportedOperationException.class,
+ () -> c.addAll(0, Collections.emptyList()));
+ }
+
+ /**
+ * As above, for an empty list.
+ *
+ * @param c the list instance to test, must be empty
+ */
+ private static void testEmptyListMutatorsAlwaysThrow(List<Integer> c) {
+ if (! c.isEmpty()) {
+ fail("list is not empty");
+ }
+ testEmptyCollMutatorsAlwaysThrow(c);
+ THROWS(UnsupportedOperationException.class,
+ () -> c.replaceAll(x -> x),
+ () -> c.sort(null));
+ }
+
+ /**
+ * As above, for a map.
+ *
+ * @param m the map instance to test
+ */
+ private static void testMapMutatorsAlwaysThrow(Map<Integer,Integer> m) {
+ THROWS(UnsupportedOperationException.class,
+ () -> m.compute(ABSENT_VALUE, (k, v) -> null),
+ () -> m.computeIfAbsent(ABSENT_VALUE, k -> null),
+ () -> m.computeIfPresent(ABSENT_VALUE, (k, v) -> null),
+ () -> m.merge(ABSENT_VALUE, 0, (k, v) -> null),
+ () -> m.putAll(Collections.emptyMap()),
+ () -> m.remove(ABSENT_VALUE),
+ () -> m.remove(ABSENT_VALUE, 0),
+ () -> m.replace(ABSENT_VALUE, 0),
+ () -> m.replace(ABSENT_VALUE, 0, 1));
+ }
+
+ /**
+ * As above, for an empty map.
+ *
+ * @param map the map instance to test, must be empty
+ */
+ private static void testEmptyMapMutatorsAlwaysThrow(Map<Integer,Integer> m) {
+ if (! m.isEmpty()) {
+ fail("map is not empty");
+ }
+ THROWS(UnsupportedOperationException.class,
+ () -> m.clear(),
+ () -> m.replaceAll((k, v) -> v));
+ }
+
+ private static void clear(Collection<Integer> c) {
+ try { c.clear(); }
+ catch (Throwable t) { unexpected(t); }
+ testEmptyCollection(c);
+ }
+
+ private static <K,V> void testEmptyMap(final Map<K,V> m) {
+ check(m.isEmpty());
+ equal(m.size(), 0);
+ equal(m.toString(),"{}");
+ testEmptySet(m.keySet());
+ testEmptySet(m.entrySet());
+ testEmptyCollection(m.values());
+
+ try { check(! m.containsValue(null)); }
+ catch (NullPointerException ignored) { /* OK */ }
+ try { check(! m.containsKey(null)); }
+ catch (NullPointerException ignored) { /* OK */ }
+ check(! m.containsValue(1));
+ check(! m.containsKey(1));
+ }
+
+ private static void testImmutableMap(final Map<Integer,Integer> m) {
+ THROWS(UnsupportedOperationException.class,
+ () -> m.put(1,1),
+ () -> m.putAll(singletonMap(1,1)));
+ if (! m.isEmpty()) {
+ final Integer first = m.keySet().iterator().next();
+ THROWS(UnsupportedOperationException.class,
+ () -> m.remove(first),
+ () -> m.clear());
+ final Map.Entry<Integer,Integer> me
+ = m.entrySet().iterator().next();
+ Integer key = me.getKey();
+ Integer val = me.getValue();
+ THROWS(UnsupportedOperationException.class,
+ () -> me.setValue(3));
+ equal(key, me.getKey());
+ equal(val, me.getValue());
+ }
+ testImmutableSet(m.keySet());
+ testImmutableCollection(m.values());
+ //testImmutableSet(m.entrySet());
+ }
+
+ private static void clear(Map<?,?> m) {
+ try { m.clear(); }
+ catch (Throwable t) { unexpected(t); }
+ testEmptyMap(m);
+ }
+
+ private static void oneElement(Collection<Integer> c) {
+ clear(c);
+ try {
+ check(c.add(-42));
+ equal(c.toString(), "[-42]");
+ if (c instanceof Set) check(! c.add(-42));
+ } catch (Throwable t) { unexpected(t); }
+ check(! c.isEmpty()); check(c.size() == 1);
+ }
+
+ private static boolean supportsAdd(Collection<Integer> c) {
+ try { check(c.add(ABSENT_VALUE)); }
+ catch (UnsupportedOperationException t) { return false; }
+ catch (Throwable t) { unexpected(t); }
+
+ try {
+ check(c.contains(ABSENT_VALUE));
+ check(c.remove(ABSENT_VALUE));
+ } catch (Throwable t) { unexpected(t); }
+ return true;
+ }
+
+ private static boolean supportsRemove(Collection<Integer> c) {
+ try { check(! c.remove(ABSENT_VALUE)); }
+ catch (UnsupportedOperationException t) { return false; }
+ catch (Throwable t) { unexpected(t); }
+ return true;
+ }
+
+ // 6260652: (coll) Arrays.asList(x).toArray().getClass()
+ // should be Object[].class
+ // Fixed in jdk9, but not jdk8 ...
+ static final boolean needToWorkAround6260652 =
+ Arrays.asList("").toArray().getClass() != Object[].class;
+
+ private static void checkFunctionalInvariants(Collection<Integer> c) {
+ try {
+ checkContainsSelf(c);
+ checkContainsEmpty(c);
+ check(c.size() != 0 ^ c.isEmpty());
+ check(! c.contains(ABSENT_VALUE));
+
+ {
+ int size = 0;
+ for (Integer i : c) size++;
+ check(c.size() == size);
+ }
+
+ if (c instanceof Set) {
+ checkUnique((Set<Integer>)c);
+ }
+
+ check(c.toArray().length == c.size());
+ check(c.toArray().getClass() == Object[].class
+ ||
+ (needToWorkAround6260652 &&
+ c.getClass().getName().equals("java.util.Arrays$ArrayList")));
+ for (int size : new int[]{0,1,c.size(), c.size()+1}) {
+ Integer[] a = c.toArray(new Integer[size]);
+ check((size > c.size()) || a.length == c.size());
+ int i = 0; for (Integer j : c) check(a[i++] == j);
+ check((size <= c.size()) || (a[c.size()] == null));
+ check(a.getClass() == Integer[].class);
+ }
+
+ {
+ Integer[] a = c.toArray(Integer[]::new);
+ equal(c.size(), a.length);
+ check(a.getClass() == Integer[].class);
+ check(Arrays.equals(c.toArray(new Integer[0]), a));
+ }
+
+ check(c.equals(c));
+ if (c instanceof Serializable) {
+ //System.out.printf("Serializing %s%n", c.getClass().getName());
+ try {
+ Object clone = serialClone(c);
+ equal(c instanceof Serializable,
+ clone instanceof Serializable);
+ equal(c instanceof RandomAccess,
+ clone instanceof RandomAccess);
+ if ((c instanceof List) || (c instanceof Set))
+ equal(c, clone);
+ else
+ equal(new HashSet<Integer>(c),
+ new HashSet<Integer>(serialClone(c)));
+ } catch (Error xxx) {
+ if (! (xxx.getCause() instanceof NotSerializableException))
+ throw xxx;
+ }
+ }
+ }
+ catch (Throwable t) { unexpected(t); }
+ }
+
+ //----------------------------------------------------------------
+ // If add(null) succeeds, contains(null) & remove(null) should succeed
+ //----------------------------------------------------------------
+ private static void testNullElement(Collection<Integer> c) {
+
+ try {
+ check(c.add(null));
+ if (c.size() == 1)
+ equal(c.toString(), "[null]");
+ try {
+ checkFunctionalInvariants(c);
+ check(c.contains(null));
+ check(c.remove(null));
+ }
+ catch (Throwable t) { unexpected(t); }
+ }
+ catch (NullPointerException e) { /* OK */ }
+ catch (Throwable t) { unexpected(t); }
+ }
+
+ //----------------------------------------------------------------
+ // If add("x") succeeds, contains("x") & remove("x") should succeed
+ //----------------------------------------------------------------
+ @SuppressWarnings("unchecked")
+ private static void testStringElement(Collection<Integer> c) {
+ Collection x = (Collection)c; // Make type-unsafe
+ try {
+ check(x.add("x"));
+ try {
+ check(x.contains("x"));
+ check(x.remove("x"));
+ } catch (Throwable t) { unexpected(t); }
+ }
+ catch (ClassCastException e) { /* OK */ }
+ catch (Throwable t) { unexpected(t); }
+ }
+
+ private static void testConcurrentCollection(Collection<Integer> c) {
+ try {
+ c.add(1);
+ Iterator<Integer> it = c.iterator();
+ check(it.hasNext());
+ clear(c);
+ check(it.next() instanceof Integer); // No CME
+ check(c.isEmpty());
+ }
+ catch (Throwable t) { unexpected(t); }
+ }
+
+ private static void testQueue(Queue<Integer> q) {
+ q.clear();
+ for (int i = 0; i < 5; i++) {
+ testQueueAddRemove(q, null);
+ testQueueAddRemove(q, 537);
+ q.add(i);
+ }
+ equal(q.size(), 5);
+ checkFunctionalInvariants(q);
+ q.poll();
+ equal(q.size(), 4);
+ checkFunctionalInvariants(q);
+ if ((q instanceof LinkedBlockingQueue) ||
+ (q instanceof LinkedBlockingDeque) ||
+ (q instanceof ConcurrentLinkedDeque) ||
+ (q instanceof ConcurrentLinkedQueue)) {
+ testQueueIteratorRemove(q);
+ }
+ }
+
+ private static void testQueueAddRemove(final Queue<Integer> q,
+ final Integer e) {
+ final List<Integer> originalContents = new ArrayList<>(q);
+ final boolean isEmpty = q.isEmpty();
+ final boolean isList = (q instanceof List);
+ final List asList = isList ? (List) q : null;
+ check(!q.contains(e));
+ try {
+ q.add(e);
+ } catch (NullPointerException npe) {
+ check(e == null);
+ return; // Null elements not supported
+ }
+ check(q.contains(e));
+ check(q.remove(e));
+ check(!q.contains(e));
+ equal(new ArrayList<Integer>(q), originalContents);
+
+ if (q instanceof Deque<?>) {
+ final Deque<Integer> deq = (Deque<Integer>) q;
+ final List<Integer> singleton = Collections.singletonList(e);
+
+ // insert, query, remove element at head
+ if (isEmpty) {
+ THROWS(NoSuchElementException.class,
+ () -> deq.getFirst(),
+ () -> deq.element(),
+ () -> deq.iterator().next());
+ check(deq.peekFirst() == null);
+ check(deq.peek() == null);
+ } else {
+ check(deq.getFirst() != e);
+ check(deq.element() != e);
+ check(deq.iterator().next() != e);
+ check(deq.peekFirst() != e);
+ check(deq.peek() != e);
+ }
+ check(!deq.contains(e));
+ check(!deq.removeFirstOccurrence(e));
+ check(!deq.removeLastOccurrence(e));
+ if (isList) {
+ check(asList.indexOf(e) == -1);
+ check(asList.lastIndexOf(e) == -1);
+ }
+ switch (rnd.nextInt(isList ? 4 : 3)) {
+ case 0: deq.addFirst(e); break;
+ case 1: check(deq.offerFirst(e)); break;
+ case 2: deq.push(e); break;
+ case 3: asList.add(0, e); break;
+ default: throw new AssertionError();
+ }
+ check(deq.peekFirst() == e);
+ check(deq.getFirst() == e);
+ check(deq.element() == e);
+ check(deq.peek() == e);
+ check(deq.iterator().next() == e);
+ check(deq.contains(e));
+ if (isList) {
+ check(asList.get(0) == e);
+ check(asList.indexOf(e) == 0);
+ check(asList.lastIndexOf(e) == 0);
+ check(asList.subList(0, 1).equals(singleton));
+ }
+ switch (rnd.nextInt(isList ? 11 : 9)) {
+ case 0: check(deq.pollFirst() == e); break;
+ case 1: check(deq.removeFirst() == e); break;
+ case 2: check(deq.remove() == e); break;
+ case 3: check(deq.pop() == e); break;
+ case 4: check(deq.removeFirstOccurrence(e)); break;
+ case 5: check(deq.removeLastOccurrence(e)); break;
+ case 6: check(deq.remove(e)); break;
+ case 7: check(deq.removeAll(singleton)); break;
+ case 8: Iterator it = deq.iterator(); it.next(); it.remove(); break;
+ case 9: asList.remove(0); break;
+ case 10: asList.subList(0, 1).clear(); break;
+ default: throw new AssertionError();
+ }
+ if (isEmpty) {
+ THROWS(NoSuchElementException.class,
+ () -> deq.getFirst(),
+ () -> deq.element(),
+ () -> deq.iterator().next());
+ check(deq.peekFirst() == null);
+ check(deq.peek() == null);
+ } else {
+ check(deq.getFirst() != e);
+ check(deq.element() != e);
+ check(deq.iterator().next() != e);
+ check(deq.peekFirst() != e);
+ check(deq.peek() != e);
+ }
+ check(!deq.contains(e));
+ check(!deq.removeFirstOccurrence(e));
+ check(!deq.removeLastOccurrence(e));
+ if (isList) {
+ check(isEmpty || asList.get(0) != e);
+ check(asList.indexOf(e) == -1);
+ check(asList.lastIndexOf(e) == -1);
+ }
+ equal(new ArrayList<Integer>(deq), originalContents);
+
+ // insert, query, remove element at tail
+ if (isEmpty) {
+ check(deq.peekLast() == null);
+ THROWS(NoSuchElementException.class, () -> deq.getLast());
+ } else {
+ check(deq.peekLast() != e);
+ check(deq.getLast() != e);
+ }
+ switch (rnd.nextInt(isList ? 6 : 4)) {
+ case 0: deq.addLast(e); break;
+ case 1: check(deq.offerLast(e)); break;
+ case 2: check(deq.add(e)); break;
+ case 3: deq.addAll(singleton); break;
+ case 4: asList.addAll(deq.size(), singleton); break;
+ case 5: asList.add(deq.size(), e); break;
+ default: throw new AssertionError();
+ }
+ check(deq.peekLast() == e);
+ check(deq.getLast() == e);
+ check(deq.contains(e));
+ if (isList) {
+ ListIterator it = asList.listIterator(asList.size());
+ check(it.previous() == e);
+ check(asList.get(asList.size() - 1) == e);
+ check(asList.indexOf(e) == asList.size() - 1);
+ check(asList.lastIndexOf(e) == asList.size() - 1);
+ int size = asList.size();
+ check(asList.subList(size - 1, size).equals(singleton));
+ }
+ switch (rnd.nextInt(isList ? 8 : 6)) {
+ case 0: check(deq.pollLast() == e); break;
+ case 1: check(deq.removeLast() == e); break;
+ case 2: check(deq.removeFirstOccurrence(e)); break;
+ case 3: check(deq.removeLastOccurrence(e)); break;
+ case 4: check(deq.remove(e)); break;
+ case 5: check(deq.removeAll(singleton)); break;
+ case 6: asList.remove(asList.size() - 1); break;
+ case 7:
+ ListIterator it = asList.listIterator(asList.size());
+ it.previous();
+ it.remove();
+ break;
+ default: throw new AssertionError();
+ }
+ if (isEmpty) {
+ check(deq.peekLast() == null);
+ THROWS(NoSuchElementException.class, () -> deq.getLast());
+ } else {
+ check(deq.peekLast() != e);
+ check(deq.getLast() != e);
+ }
+ check(!deq.contains(e));
+ equal(new ArrayList<Integer>(deq), originalContents);
+
+ // Test operations on empty deque
+ switch (rnd.nextInt(isList ? 4 : 2)) {
+ case 0: deq.clear(); break;
+ case 1:
+ Iterator it = deq.iterator();
+ while (it.hasNext()) {
+ it.next();
+ it.remove();
+ }
+ break;
+ case 2: asList.subList(0, asList.size()).clear(); break;
+ case 3:
+ ListIterator lit = asList.listIterator(asList.size());
+ while (lit.hasPrevious()) {
+ lit.previous();
+ lit.remove();
+ }
+ break;
+ default: throw new AssertionError();
+ }
+ testEmptyCollection(deq);
+ check(!deq.iterator().hasNext());
+ if (isList) {
+ check(!asList.listIterator().hasPrevious());
+ THROWS(NoSuchElementException.class,
+ () -> asList.listIterator().previous());
+ }
+ THROWS(NoSuchElementException.class,
+ () -> deq.iterator().next(),
+ () -> deq.element(),
+ () -> deq.getFirst(),
+ () -> deq.getLast(),
+ () -> deq.pop(),
+ () -> deq.remove(),
+ () -> deq.removeFirst(),
+ () -> deq.removeLast());
+
+ check(deq.poll() == null);
+ check(deq.pollFirst() == null);
+ check(deq.pollLast() == null);
+ check(deq.peek() == null);
+ check(deq.peekFirst() == null);
+ check(deq.peekLast() == null);
+ check(!deq.removeFirstOccurrence(e));
+ check(!deq.removeLastOccurrence(e));
+
+ check(deq.addAll(originalContents) == !isEmpty);
+ equal(new ArrayList<Integer>(deq), originalContents);
+ check(!deq.addAll(Collections.<Integer>emptyList()));
+ equal(new ArrayList<Integer>(deq), originalContents);
+ }
+ }
+
+ private static void testQueueIteratorRemove(Queue<Integer> q) {
+ System.err.printf("testQueueIteratorRemove %s%n",
+ q.getClass().getSimpleName());
+ q.clear();
+ for (int i = 0; i < 5; i++)
+ q.add(i);
+ Iterator<Integer> it = q.iterator();
+ check(it.hasNext());
+ for (int i = 3; i >= 0; i--)
+ q.remove(i);
+ equal(it.next(), 0);
+ equal(it.next(), 4);
+
+ q.clear();
+ for (int i = 0; i < 5; i++)
+ q.add(i);
+ it = q.iterator();
+ equal(it.next(), 0);
+ check(it.hasNext());
+ for (int i = 1; i < 4; i++)
+ q.remove(i);
+ equal(it.next(), 1);
+ equal(it.next(), 4);
+ }
+
+ // for any array of integer values, check that the result of lastIndexOf(1)
+ // and indexOf(1) match assumptions for all types of List<Integer> we can
+ // construct
+ private static void testListIndexOf(final int index,
+ final int lastIndex,
+ final Integer ... values) {
+ if (values.length == 0) {
+ checkListIndexOf(emptyList(), index, lastIndex);
+ } else if (values.length == 1) {
+ checkListIndexOf(singletonList(values[0]), index, lastIndex);
+ checkListIndexOf(nCopies(25, values[0]), index, lastIndex == 0 ? 24 : -1);
+ }
+ List<Integer> l = List.of(values);
+ checkListIndexOf(l, index, lastIndex);
+ checkListIndexOf(Arrays.asList(values), index, lastIndex);
+ checkListIndexOf(new ArrayList(l), index, lastIndex);
+ checkListIndexOf(new LinkedList(l), index, lastIndex);
+ checkListIndexOf(new Vector(l), index, lastIndex);
+ checkListIndexOf(new CopyOnWriteArrayList(l), index, lastIndex);
+ }
+
+ private static void checkListIndexOf(final List<Integer> list,
+ final int index,
+ final int lastIndex) {
+ String msg = list.getClass().toString();
+ equal(list.indexOf(1), index, msg);
+ equal(list.lastIndexOf(1), lastIndex, msg);
+ equal(list.subList(0, list.size()).indexOf(1), index, msg);
+ equal(list.subList(0, list.size()).lastIndexOf(1), lastIndex, msg);
+ }
+
+ private static void testList(final List<Integer> l) {
+ //----------------------------------------------------------------
+ // 4802633: (coll) AbstractList.addAll(-1,emptyCollection)
+ // doesn't throw IndexOutOfBoundsException
+ //----------------------------------------------------------------
+ try {
+ l.addAll(-1, Collections.<Integer>emptyList());
+ fail("Expected IndexOutOfBoundsException not thrown");
+ }
+ catch (UnsupportedOperationException ignored) {/* OK */}
+ catch (IndexOutOfBoundsException ignored) {/* OK */}
+ catch (Throwable t) { unexpected(t); }
+
+// equal(l instanceof Serializable,
+// l.subList(0,0) instanceof Serializable);
+ if (l.subList(0,0) instanceof Serializable)
+ check(l instanceof Serializable);
+
+ equal(l instanceof RandomAccess,
+ l.subList(0,0) instanceof RandomAccess);
+
+ l.iterator();
+ l.listIterator();
+ l.listIterator(0);
+ l.listIterator(l.size());
+ THROWS(IndexOutOfBoundsException.class,
+ () -> l.listIterator(-1),
+ () -> l.listIterator(l.size() + 1));
+
+ if (l instanceof AbstractList) {
+ try {
+ int size = l.size();
+ AbstractList<Integer> abList = (AbstractList<Integer>) l;
+ Method m = AbstractList.class.getDeclaredMethod("removeRange", new Class[] { int.class, int.class });
+ m.setAccessible(true);
+ m.invoke(abList, new Object[] { 0, 0 });
+ m.invoke(abList, new Object[] { size, size });
+ equal(size, l.size());
+ }
+ catch (UnsupportedOperationException ignored) {/* OK */}
+ catch (Throwable t) { unexpected(t); }
+ }
+ }
+
+ private static void testCollection(Collection<Integer> c) {
+ try { testCollection1(c); }
+ catch (Throwable t) { unexpected(t); }
+ }
+
+ private static void testCollection1(Collection<Integer> c) {
+
+ System.out.println("\n==> " + c.getClass().getName());
+
+ checkFunctionalInvariants(c);
+
+ if (! supportsAdd(c)) return;
+ //System.out.println("add() supported");
+
+ if (c instanceof NavigableSet) {
+ System.out.println("NavigableSet tests...");
+
+ NavigableSet<Integer> ns = (NavigableSet<Integer>)c;
+ testNavigableSet(ns);
+ testNavigableSet(ns.headSet(6, false));
+ testNavigableSet(ns.headSet(5, true));
+ testNavigableSet(ns.tailSet(0, false));
+ testNavigableSet(ns.tailSet(1, true));
+ testNavigableSet(ns.subSet(0, false, 5, true));
+ testNavigableSet(ns.subSet(1, true, 6, false));
+ }
+
+ if (c instanceof Queue)
+ testQueue((Queue<Integer>)c);
+
+ if (c instanceof List)
+ testList((List<Integer>)c);
+
+ check(supportsRemove(c));
+
+ try {
+ oneElement(c);
+ checkFunctionalInvariants(c);
+ }
+ catch (Throwable t) { unexpected(t); }
+
+ clear(c); testNullElement(c);
+ oneElement(c); testNullElement(c);
+
+ clear(c); testStringElement(c);
+ oneElement(c); testStringElement(c);
+
+ if (c.getClass().getName().matches(".*concurrent.*"))
+ testConcurrentCollection(c);
+
+ //----------------------------------------------------------------
+ // The "all" operations should throw NPE when passed null
+ //----------------------------------------------------------------
+ {
+ clear(c);
+ try {
+ c.removeAll(null);
+ fail("Expected NullPointerException");
+ }
+ catch (NullPointerException e) { pass(); }
+ catch (Throwable t) { unexpected(t); }
+
+ oneElement(c);
+ try {
+ c.removeAll(null);
+ fail("Expected NullPointerException");
+ }
+ catch (NullPointerException e) { pass(); }
+ catch (Throwable t) { unexpected(t); }
+
+ clear(c);
+ try {
+ c.retainAll(null);
+ fail("Expected NullPointerException");
+ }
+ catch (NullPointerException e) { pass(); }
+ catch (Throwable t) { unexpected(t); }
+
+ oneElement(c);
+ try {
+ c.retainAll(null);
+ fail("Expected NullPointerException");
+ }
+ catch (NullPointerException e) { pass(); }
+ catch (Throwable t) { unexpected(t); }
+
+ oneElement(c);
+ try {
+ c.addAll(null);
+ fail("Expected NullPointerException");
+ }
+ catch (NullPointerException e) { pass(); }
+ catch (Throwable t) { unexpected(t); }
+
+ oneElement(c);
+ try {
+ c.containsAll(null);
+ fail("Expected NullPointerException");
+ }
+ catch (NullPointerException e) { pass(); }
+ catch (Throwable t) { unexpected(t); }
+ }
+ }
+
+ //----------------------------------------------------------------
+ // Map
+ //----------------------------------------------------------------
+ private static void checkFunctionalInvariants(Map<Integer,Integer> m) {
+ check(m.keySet().size() == m.entrySet().size());
+ check(m.keySet().size() == m.size());
+ checkFunctionalInvariants(m.keySet());
+ checkFunctionalInvariants(m.values());
+ check(m.size() != 0 ^ m.isEmpty());
+ check(! m.containsKey(ABSENT_VALUE));
+
+ if (m instanceof Serializable) {
+ //System.out.printf("Serializing %s%n", m.getClass().getName());
+ try {
+ Object clone = serialClone(m);
+ equal(m instanceof Serializable,
+ clone instanceof Serializable);
+ equal(m, clone);
+ } catch (Error xxx) {
+ if (! (xxx.getCause() instanceof NotSerializableException))
+ throw xxx;
+ }
+ }
+ }
+
+ private static void testMap(Map<Integer,Integer> m) {
+ System.out.println("\n==> " + m.getClass().getName());
+
+ if (m instanceof ConcurrentMap)
+ testConcurrentMap((ConcurrentMap<Integer,Integer>) m);
+
+ if (m instanceof NavigableMap) {
+ System.out.println("NavigableMap tests...");
+
+ NavigableMap<Integer,Integer> nm =
+ (NavigableMap<Integer,Integer>) m;
+ testNavigableMapRemovers(nm);
+ testNavigableMap(nm);
+ testNavigableMap(nm.headMap(6, false));
+ testNavigableMap(nm.headMap(5, true));
+ testNavigableMap(nm.tailMap(0, false));
+ testNavigableMap(nm.tailMap(1, true));
+ testNavigableMap(nm.subMap(1, true, 6, false));
+ testNavigableMap(nm.subMap(0, false, 5, true));
+ }
+
+ checkFunctionalInvariants(m);
+
+ if (supportsClear(m)) {
+ try { clear(m); }
+ catch (Throwable t) { unexpected(t); }
+ }
+
+ if (supportsPut(m)) {
+ try {
+ check(m.put(3333, 77777) == null);
+ check(m.put(9134, 74982) == null);
+ check(m.get(9134) == 74982);
+ check(m.put(9134, 1382) == 74982);
+ check(m.get(9134) == 1382);
+ check(m.size() == 2);
+ checkFunctionalInvariants(m);
+ checkNPEConsistency(m);
+ }
+ catch (Throwable t) { unexpected(t); }
+ }
+ }
+
+ private static boolean supportsPut(Map<Integer,Integer> m) {
+ // We're asking for .equals(...) semantics
+ if (m instanceof IdentityHashMap) return false;
+
+ try { check(m.put(ABSENT_VALUE,12735) == null); }
+ catch (UnsupportedOperationException t) { return false; }
+ catch (Throwable t) { unexpected(t); }
+
+ try {
+ check(m.containsKey(ABSENT_VALUE));
+ check(m.remove(ABSENT_VALUE) != null);
+ } catch (Throwable t) { unexpected(t); }
+ return true;
+ }
+
+ private static boolean supportsClear(Map<?,?> m) {
+ try { m.clear(); }
+ catch (UnsupportedOperationException t) { return false; }
+ catch (Throwable t) { unexpected(t); }
+ return true;
+ }
+
+ //----------------------------------------------------------------
+ // ConcurrentMap
+ //----------------------------------------------------------------
+ private static void testConcurrentMap(ConcurrentMap<Integer,Integer> m) {
+ System.out.println("ConcurrentMap tests...");
+
+ try {
+ clear(m);
+
+ check(m.putIfAbsent(18357,7346) == null);
+ check(m.containsKey(18357));
+ check(m.putIfAbsent(18357,8263) == 7346);
+ try { m.putIfAbsent(18357,null); fail("NPE"); }
+ catch (NullPointerException t) { }
+ check(m.containsKey(18357));
+
+ check(! m.replace(18357,8888,7777));
+ check(m.containsKey(18357));
+ try { m.replace(18357,null,7777); fail("NPE"); }
+ catch (NullPointerException t) { }
+ check(m.containsKey(18357));
+ check(m.get(18357) == 7346);
+ check(m.replace(18357,7346,5555));
+ check(m.replace(18357,5555,7346));
+ check(m.get(18357) == 7346);
+
+ check(m.replace(92347,7834) == null);
+ try { m.replace(18357,null); fail("NPE"); }
+ catch (NullPointerException t) { }
+ check(m.replace(18357,7346) == 7346);
+ check(m.replace(18357,5555) == 7346);
+ check(m.get(18357) == 5555);
+ check(m.replace(18357,7346) == 5555);
+ check(m.get(18357) == 7346);
+
+ check(! m.remove(18357,9999));
+ check(m.get(18357) == 7346);
+ check(m.containsKey(18357));
+ check(! m.remove(18357,null)); // 6272521
+ check(m.get(18357) == 7346);
+ check(m.remove(18357,7346));
+ check(m.get(18357) == null);
+ check(! m.containsKey(18357));
+ check(m.isEmpty());
+
+ m.putIfAbsent(1,2);
+ check(m.size() == 1);
+ check(! m.remove(1,null));
+ check(! m.remove(1,null));
+ check(! m.remove(1,1));
+ check(m.remove(1,2));
+ check(m.isEmpty());
+
+ testEmptyMap(m);
+ }
+ catch (Throwable t) { unexpected(t); }
+ }
+
+ private static void throwsConsistently(Class<? extends Throwable> k,
+ Iterable<Fun> fs) {
+ List<Class<? extends Throwable>> threw = new ArrayList<>();
+ for (Fun f : fs)
+ try { f.f(); threw.add(null); }
+ catch (Throwable t) {
+ check(k.isAssignableFrom(t.getClass()));
+ threw.add(t.getClass());
+ }
+ if (new HashSet<Object>(threw).size() != 1)
+ fail(threw.toString());
+ }
+
+ private static <T> void checkNPEConsistency(final Map<T,Integer> m) {
+ m.clear();
+ final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap)
+ ? (ConcurrentMap<T,Integer>) m
+ : null;
+ List<Fun> fs = new ArrayList<>();
+ fs.add(() -> check(! m.containsKey(null)));
+ fs.add(() -> equal(m.remove(null), null));
+ fs.add(() -> equal(m.get(null), null));
+ if (cm != null)
+ fs.add(() -> check(! cm.remove(null,null)));
+ throwsConsistently(NullPointerException.class, fs);
+
+ fs.clear();
+ final Map<T,Integer> sm = singletonMap(null,1);
+ fs.add(() -> { equal(m.put(null,1), null); m.clear();});
+ fs.add(() -> { m.putAll(sm); m.clear();});
+ if (cm != null) {
+ fs.add(() -> check(! cm.remove(null,null)));
+ fs.add(() -> equal(cm.putIfAbsent(null,1), 1));
+ fs.add(() -> equal(cm.replace(null,1), null));
+ fs.add(() -> equal(cm.replace(null,1, 1), 1));
+ }
+ throwsConsistently(NullPointerException.class, fs);
+ }
+
+ //----------------------------------------------------------------
+ // NavigableMap
+ //----------------------------------------------------------------
+ private static void
+ checkNavigableMapKeys(NavigableMap<Integer,Integer> m,
+ Integer i,
+ Integer lower,
+ Integer floor,
+ Integer ceiling,
+ Integer higher) {
+ equal(m.lowerKey(i), lower);
+ equal(m.floorKey(i), floor);
+ equal(m.ceilingKey(i), ceiling);
+ equal(m.higherKey(i), higher);
+ }
+
+ private static void
+ checkNavigableSetKeys(NavigableSet<Integer> m,
+ Integer i,
+ Integer lower,
+ Integer floor,
+ Integer ceiling,
+ Integer higher) {
+ equal(m.lower(i), lower);
+ equal(m.floor(i), floor);
+ equal(m.ceiling(i), ceiling);
+ equal(m.higher(i), higher);
+ }
+
+ static final Random rnd = new Random();
+ static void equalNext(final Iterator<?> it, Object expected) {
+ if (rnd.nextBoolean())
+ check(it.hasNext());
+ equal(it.next(), expected);
+ }
+
+ static void equalMaps(Map m1, Map m2) {
+ equal(m1, m2);
+ equal(m2, m1);
+ equal(m1.size(), m2.size());
+ equal(m1.isEmpty(), m2.isEmpty());
+ equal(m1.toString(), m2.toString());
+ check(Arrays.equals(m1.entrySet().toArray(), m2.entrySet().toArray()));
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ static void testNavigableMapRemovers(NavigableMap m)
+ {
+ final Map emptyMap = new HashMap();
+
+ final Map singletonMap = new HashMap();
+ singletonMap.put(1, 2);
+
+ abstract class NavigableMapView {
+ abstract NavigableMap view(NavigableMap m);
+ }
+
+ NavigableMapView[] views = {
+ new NavigableMapView() { NavigableMap view(NavigableMap m) {
+ return m; }},
+ new NavigableMapView() { NavigableMap view(NavigableMap m) {
+ return m.headMap(99, true); }},
+ new NavigableMapView() { NavigableMap view(NavigableMap m) {
+ return m.tailMap(-99, false); }},
+ new NavigableMapView() { NavigableMap view(NavigableMap m) {
+ return m.subMap(-99, true, 99, false); }},
+ };
+
+ abstract class Remover {
+ abstract void remove(NavigableMap m, Object k, Object v);
+ }
+
+ Remover[] removers = {
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ equal(m.remove(k), v); }},
+
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ equal(m.descendingMap().remove(k), v); }},
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ equal(m.descendingMap().headMap(-86, false).remove(k), v); }},
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ equal(m.descendingMap().tailMap(86, true).remove(k), v); }},
+
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ equal(m.headMap(86, true).remove(k), v); }},
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ equal(m.tailMap(-86, true).remove(k), v); }},
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ equal(m.subMap(-86, false, 86, true).remove(k), v); }},
+
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ check(m.keySet().remove(k)); }},
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ check(m.navigableKeySet().remove(k)); }},
+
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ check(m.navigableKeySet().headSet(86, true).remove(k)); }},
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ check(m.navigableKeySet().tailSet(-86, false).remove(k)); }},
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ check(m.navigableKeySet().subSet(-86, true, 86, false)
+ .remove(k)); }},
+
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ check(m.descendingKeySet().headSet(-86, false).remove(k)); }},
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ check(m.descendingKeySet().tailSet(86, true).remove(k)); }},
+ new Remover() { void remove(NavigableMap m, Object k, Object v) {
+ check(m.descendingKeySet().subSet(86, true, -86, false)
+ .remove(k)); }},
+ };
+
+ for (NavigableMapView view : views) {
+ for (Remover remover : removers) {
+ try {
+ m.clear();
+ equalMaps(m, emptyMap);
+ equal(m.put(1, 2), null);
+ equalMaps(m, singletonMap);
+ NavigableMap v = view.view(m);
+ remover.remove(v, 1, 2);
+ equalMaps(m, emptyMap);
+ } catch (Throwable t) { unexpected(t); }
+ }
+ }
+ }
+
+ private static void testNavigableMap(NavigableMap<Integer,Integer> m)
+ {
+ clear(m);
+ checkNavigableMapKeys(m, 1, null, null, null, null);
+
+ equal(m.put(1, 2), null);
+ equal(m.put(3, 4), null);
+ equal(m.put(5, 9), null);
+
+ equal(m.put(1, 2), 2);
+ equal(m.put(3, 4), 4);
+ equal(m.put(5, 6), 9);
+
+ checkNavigableMapKeys(m, 0, null, null, 1, 1);
+ checkNavigableMapKeys(m, 1, null, 1, 1, 3);
+ checkNavigableMapKeys(m, 2, 1, 1, 3, 3);
+ checkNavigableMapKeys(m, 3, 1, 3, 3, 5);
+ checkNavigableMapKeys(m, 5, 3, 5, 5, null);
+ checkNavigableMapKeys(m, 6, 5, 5, null, null);
+
+ for (final Iterator<Integer> it :
+ (Iterator<Integer>[])
+ new Iterator<?>[] {
+ m.descendingKeySet().iterator(),
+ m.navigableKeySet().descendingIterator()}) {
+ equalNext(it, 5);
+ equalNext(it, 3);
+ equalNext(it, 1);
+ check(! it.hasNext());
+ THROWS(NoSuchElementException.class, () -> it.next());
+ }
+
+ {
+ final Iterator<Map.Entry<Integer,Integer>> it
+ = m.descendingMap().entrySet().iterator();
+ check(it.hasNext()); equal(it.next().getKey(), 5);
+ check(it.hasNext()); equal(it.next().getKey(), 3);
+ check(it.hasNext()); equal(it.next().getKey(), 1);
+ check(! it.hasNext());
+ THROWS(NoSuchElementException.class, () -> it.next());
+ }
+
+ prepMapForDescItrTests(m);
+ checkDescItrRmFirst(m.keySet(), m.navigableKeySet().descendingIterator());
+ prepMapForDescItrTests(m);
+ checkDescItrRmMid(m.keySet(), m.navigableKeySet().descendingIterator());
+ prepMapForDescItrTests(m);
+ checkDescItrRmLast(m.keySet(), m.navigableKeySet().descendingIterator());
+
+ prepMapForDescItrTests(m);
+ checkDescItrRmFirst(m.keySet(), m.descendingMap().keySet().iterator());
+ prepMapForDescItrTests(m);
+ checkDescItrRmMid(m.keySet(), m.descendingMap().keySet().iterator());
+ prepMapForDescItrTests(m);
+ checkDescItrRmLast(m.keySet(), m.descendingMap().keySet().iterator());
+
+ prepMapForDescItrTests(m);
+ checkDescItrRmFirst(m.keySet(), m.descendingKeySet().iterator());
+ prepMapForDescItrTests(m);
+ checkDescItrRmMid(m.keySet(), m.descendingKeySet().iterator());
+ prepMapForDescItrTests(m);
+ checkDescItrRmLast(m.keySet(), m.descendingKeySet().iterator());
+
+ prepMapForDescItrTests(m);
+ checkDescItrRmFirst(m.values(), m.descendingMap().values().iterator());
+ prepMapForDescItrTests(m);
+ checkDescItrRmMid(m.values(), m.descendingMap().values().iterator());
+ prepMapForDescItrTests(m);
+ checkDescItrRmLast(m.values(), m.descendingMap().values().iterator());
+
+ prepMapForDescItrTests(m);
+ checkDescItrRmFirst((Collection)m.entrySet(),
+ m.descendingMap().entrySet().iterator());
+ prepMapForDescItrTests(m);
+ checkDescItrRmMid((Collection)m.entrySet(),
+ m.descendingMap().entrySet().iterator());
+ prepMapForDescItrTests(m);
+ checkDescItrRmLast((Collection)m.entrySet(),
+ m.descendingMap().entrySet().iterator());
+ }
+
+ private static void testNavigableSet(NavigableSet<Integer> s) {
+ clear(s);
+ checkNavigableSetKeys(s, 1, null, null, null, null);
+
+ check(s.add(1));
+ check(s.add(3));
+ check(s.add(5));
+
+ check(! s.add(1));
+ check(! s.add(3));
+ check(! s.add(5));
+
+ checkNavigableSetKeys(s, 0, null, null, 1, 1);
+ checkNavigableSetKeys(s, 1, null, 1, 1, 3);
+ checkNavigableSetKeys(s, 2, 1, 1, 3, 3);
+ checkNavigableSetKeys(s, 3, 1, 3, 3, 5);
+ checkNavigableSetKeys(s, 5, 3, 5, 5, null);
+ checkNavigableSetKeys(s, 6, 5, 5, null, null);
+
+ for (final Iterator<Integer> it :
+ (Iterator<Integer>[])
+ new Iterator<?>[] {
+ s.descendingIterator(),
+ s.descendingSet().iterator()}) {
+ equalNext(it, 5);
+ equalNext(it, 3);
+ equalNext(it, 1);
+ check(! it.hasNext());
+ THROWS(NoSuchElementException.class, () -> it.next());
+ }
+
+ prepSetForDescItrTests(s);
+ checkDescItrRmFirst(s, s.descendingIterator());
+ prepSetForDescItrTests(s);
+ checkDescItrRmMid(s, s.descendingIterator());
+ prepSetForDescItrTests(s);
+ checkDescItrRmLast(s, s.descendingIterator());
+
+ prepSetForDescItrTests(s);
+ checkDescItrRmFirst(s, s.descendingSet().iterator());
+ prepSetForDescItrTests(s);
+ checkDescItrRmMid(s, s.descendingSet().iterator());
+ prepSetForDescItrTests(s);
+ checkDescItrRmLast(s, s.descendingSet().iterator());
+ }
+
+ private static void prepSetForDescItrTests(Set s) {
+ clear(s);
+ check(s.add(1));
+ check(s.add(3));
+ check(s.add(5));
+ }
+
+ private static void prepMapForDescItrTests(Map m) {
+ clear(m);
+ equal(m.put(1, 2), null);
+ equal(m.put(3, 4), null);
+ equal(m.put(5, 9), null);
+ }
+
+ //--------------------------------------------------------------------
+ // Check behavior of descending iterator when first element is removed
+ //--------------------------------------------------------------------
+ private static <T> void checkDescItrRmFirst(Collection<T> ascColl,
+ Iterator<T> descItr) {
+ T[] expected = (T[]) ascColl.toArray();
+ int idx = expected.length -1;
+
+ equalNext(descItr, expected[idx--]);
+ descItr.remove();
+ while (idx >= 0 && descItr.hasNext()) {
+ equalNext(descItr, expected[idx--]);
+ }
+ equal(descItr.hasNext(), false);
+ equal(idx, -1);
+ }
+
+ //-----------------------------------------------------------------------
+ // Check behavior of descending iterator when a middle element is removed
+ //-----------------------------------------------------------------------
+ private static <T> void checkDescItrRmMid(Collection<T> ascColl,
+ Iterator<T> descItr) {
+ T[] expected = (T[]) ascColl.toArray();
+ int idx = expected.length -1;
+
+ while (idx >= expected.length / 2) {
+ equalNext(descItr, expected[idx--]);
+ }
+ descItr.remove();
+ while (idx >= 0 && descItr.hasNext()) {
+ equalNext(descItr, expected[idx--]);
+ }
+ equal(descItr.hasNext(), false);
+ equal(idx, -1);
+ }
+
+ //-----------------------------------------------------------------------
+ // Check behavior of descending iterator when the last element is removed
+ //-----------------------------------------------------------------------
+ private static <T> void checkDescItrRmLast(Collection<T> ascColl,
+ Iterator<T> descItr) {
+ T[] expected = (T[]) ascColl.toArray();
+ int idx = expected.length -1;
+
+ while (idx >= 0 && descItr.hasNext()) {
+ equalNext(descItr, expected[idx--]);
+ }
+ equal(idx, -1);
+ equal(descItr.hasNext(), false);
+ descItr.remove();
+ equal(ascColl.contains(expected[0]), false);
+ }
+
+ //--------------------- Infrastructure ---------------------------
+ static volatile int passed = 0, failed = 0;
+ static void pass() { passed++; }
+ static void fail() { failed++; Thread.dumpStack(); }
+ static void fail(String msg) { System.out.println(msg); fail(); }
+ static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
+ static void check(boolean cond) { if (cond) pass(); else fail(); }
+ static void equal(Object x, Object y) {
+ if (x == null ? y == null : x.equals(y)) pass();
+ else {System.out.println(x + " not equal to " + y); fail();}}
+ static void equal(Object x, Object y, String msg) {
+ if (x == null ? y == null : x.equals(y)) pass();
+ else {System.out.println(x + " not equal to " + y + " : " + msg); fail();}}
+ static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
+ // BEGIN Android-removed: we do not run tests via main.
+ /*
+ public static void main(String[] args) throws Throwable {
+ try { realMain(args); } catch (Throwable t) { unexpected(t); }
+
+ System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
+ if (failed > 0) throw new Exception("Some tests failed");
+ }
+ */
+ // END Android-removed: we do not run tests via main.
+ interface Fun {void f() throws Throwable;}
+ private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
+ for (Fun f : fs)
+ try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
+ catch (Throwable t) {
+ if (k.isAssignableFrom(t.getClass())) pass();
+ else unexpected(t);}}
+ static byte[] serializedForm(Object obj) {
+ try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ new ObjectOutputStream(baos).writeObject(obj);
+ return baos.toByteArray();
+ } catch (IOException e) { throw new Error(e); }}
+ static Object readObject(byte[] bytes)
+ throws IOException, ClassNotFoundException {
+ InputStream is = new ByteArrayInputStream(bytes);
+ return new ObjectInputStream(is).readObject();}
+ @SuppressWarnings("unchecked")
+ static <T> T serialClone(T obj) {
+ try { return (T) readObject(serializedForm(obj)); }
+ catch (Exception e) { throw new Error(e); }}
+ private static class NewAbstractCollection<E> extends AbstractCollection<E> {
+ ArrayList<E> list = new ArrayList<>();
+ public boolean remove(Object obj) {
+ return list.remove(obj);
+ }
+ public boolean add(E e) {
+ return list.add(e);
+ }
+ public Iterator<E> iterator() {
+ return list.iterator();
+ }
+ public int size() {
+ return list.size();
+ }
+ }
+ private static class NewAbstractSet<E> extends AbstractSet<E> {
+ HashSet<E> set = new HashSet<>();
+ public boolean remove(Object obj) {
+ return set.remove(obj);
+ }
+ public boolean add(E e) {
+ return set.add(e);
+ }
+ public Iterator<E> iterator() {
+ return set.iterator();
+ }
+ public int size() {
+ return set.size();
+ }
+ }
+
+}