jdk/test/java/util/Collections/CheckedQueue.java
author mduigou
Fri, 27 Jun 2014 10:26:39 -0700
changeset 25177 487a5e71f6dd
parent 10790 331fdf9a8824
permissions -rw-r--r--
8048207: Collections.checkedQueue.offer() calls add on wrapped queue Reviewed-by: psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
     1
/*
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
     2
 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
     4
 *
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
     8
 *
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    13
 * accompanied this code).
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    14
 *
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    18
 *
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    21
 * questions.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    22
 */
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    23
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    24
/*
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    25
 * @test
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    26
 * @bug 5020931 8048207
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    27
 * @summary Unit test for Collections.checkedQueue
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    28
 * @run testng CheckedQueue
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    29
 */
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    30
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    31
import java.util.Collections;
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    32
import java.util.Queue;
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    33
import java.util.concurrent.ArrayBlockingQueue;
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    34
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    35
import org.testng.annotations.Test;
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    36
import static org.testng.Assert.fail;
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    37
import static org.testng.Assert.assertEquals;
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    38
import static org.testng.Assert.assertTrue;
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    39
import static org.testng.Assert.assertFalse;
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    40
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    41
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    42
public class CheckedQueue {
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    43
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    44
    /**
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    45
     * This test adds items to a queue.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    46
     */
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    47
    @Test
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    48
    public void testAdd() {
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    49
        int arrayLength = 10;
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    50
        Queue<String> abq = Collections.checkedQueue(new ArrayBlockingQueue<>(arrayLength), String.class);
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    51
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    52
        for (int i = 0; i < arrayLength; i++) {
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    53
            abq.add(Integer.toString(i));
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    54
        }
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    55
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    56
        try {
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    57
            abq.add("full");
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    58
        } catch (IllegalStateException full) {
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    59
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    60
        }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    61
    }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    62
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    63
    /**
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    64
     * This test tests the CheckedQueue.add method.  It creates a queue of
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    65
     * {@code String}s gets the checked queue, and attempt to add an Integer to
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    66
     * the checked queue.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    67
     */
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    68
    @Test(expectedExceptions = ClassCastException.class)
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    69
    public void testAddFail1() {
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    70
        int arrayLength = 10;
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    71
        ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(arrayLength + 1);
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    72
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    73
        for (int i = 0; i < arrayLength; i++) {
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    74
            abq.add(Integer.toString(i));
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    75
        }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    76
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    77
        Queue q = Collections.checkedQueue(abq, String.class);
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    78
        q.add(0);
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    79
    }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    80
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    81
    /**
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    82
     * This test tests the CheckedQueue.add method.  It creates a queue of one
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    83
     * {@code String}, gets the checked queue, and attempt to add an Integer to
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    84
     * the checked queue.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    85
     */
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    86
    @Test(expectedExceptions = ClassCastException.class)
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    87
    public void testAddFail2() {
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    88
        ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    89
        Queue q = Collections.checkedQueue(abq, String.class);
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    90
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    91
        q.add(0);
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    92
    }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    93
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    94
    /**
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    95
     * This test tests the Collections.checkedQueue method call for nulls in
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    96
     * each and both of the parameters.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
    97
     */
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    98
    @Test
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
    99
    public void testArgs() {
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   100
        ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   101
        Queue q;
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   102
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   103
        try {
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   104
            q = Collections.checkedQueue(null, String.class);
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
   105
            fail( "should throw NullPointerException.");
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   106
        } catch(NullPointerException npe) {
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   107
            // Do nothing
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   108
        }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   109
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   110
        try {
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   111
            q = Collections.checkedQueue(abq, null);
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
   112
            fail( "should throw NullPointerException.");
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   113
        } catch(Exception e) {
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   114
            // Do nothing
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   115
        }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   116
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   117
        try {
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   118
            q = Collections.checkedQueue(null, null);
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
   119
            fail( "should throw NullPointerException.");
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   120
        } catch(Exception e) {
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   121
            // Do nothing
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   122
        }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   123
    }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   124
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   125
    /**
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   126
     * This test tests the CheckedQueue.offer method.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   127
     */
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
   128
    @Test
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
   129
    public void testOffer() {
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   130
        ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(1);
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   131
        Queue q = Collections.checkedQueue(abq, String.class);
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   132
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   133
        try {
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   134
            q.offer(null);
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
   135
            fail("should throw NullPointerException.");
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   136
        } catch (NullPointerException npe) {
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   137
            // Do nothing
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   138
        }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   139
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   140
        try {
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
   141
            q.offer(0);
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
   142
            fail("should throw ClassCastException.");
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   143
        } catch (ClassCastException cce) {
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   144
            // Do nothing
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   145
        }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   146
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
   147
        assertTrue(q.offer("0"), "queue should have room");
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   148
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
   149
        // no room at the inn!
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 10790
diff changeset
   150
        assertFalse(q.offer("1"), "queue should be full");
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   151
    }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents:
diff changeset
   152
}