test/jdk/java/lang/invoke/SpreadCollectTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 36221 jdk/test/java/lang/invoke/SpreadCollectTest.java@0543ccb78f27
child 47856 76519338df34
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36221
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
     1
/*
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
     4
 *
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    10
 *
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    15
 * accompanied this code).
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    16
 *
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    20
 *
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    23
 * questions.
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    24
 */
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    25
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    26
/* @test
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    27
 * @bug 8139885
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    28
 * @bug 8143798
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    29
 * @run testng/othervm -ea -esa test.java.lang.invoke.SpreadCollectTest
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    30
 */
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    31
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    32
package test.java.lang.invoke;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    33
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    34
import java.io.StringWriter;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    35
import java.lang.invoke.MethodHandle;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    36
import java.lang.invoke.MethodHandles;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    37
import java.lang.invoke.MethodHandles.Lookup;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    38
import java.lang.invoke.MethodType;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    39
import java.lang.invoke.WrongMethodTypeException;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    40
import java.util.*;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    41
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    42
import static java.lang.invoke.MethodType.methodType;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    43
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    44
import static org.testng.AssertJUnit.*;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    45
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    46
import org.testng.annotations.*;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    47
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    48
/**
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    49
 * Tests for the new asSpreader/asCollector API added in JEP 274.
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    50
 */
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    51
public class SpreadCollectTest {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    52
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    53
    static final Lookup LOOKUP = MethodHandles.lookup();
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    54
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    55
    @Test
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    56
    public static void testAsSpreader() throws Throwable {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    57
        MethodHandle spreader = SpreadCollect.MH_forSpreading.asSpreader(1, int[].class, 3);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    58
        assertEquals(SpreadCollect.MT_spreader, spreader.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    59
        assertEquals("A456B", (String) spreader.invoke("A", new int[]{4, 5, 6}, "B"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    60
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    61
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    62
    @Test
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    63
    public static void testAsSpreaderExample() throws Throwable {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    64
        // test the JavaDoc asSpreader-with-pos example
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    65
        MethodHandle compare = LOOKUP.findStatic(Objects.class, "compare", methodType(int.class, Object.class, Object.class, Comparator.class));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    66
        MethodHandle compare2FromArray = compare.asSpreader(0, Object[].class, 2);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    67
        Object[] ints = new Object[]{3, 9, 7, 7};
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    68
        Comparator<Integer> cmp = (a, b) -> a - b;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    69
        assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 0, 2), cmp) < 0);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    70
        assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 1, 3), cmp) > 0);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    71
        assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 2, 4), cmp) == 0);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    72
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    73
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    74
    @DataProvider
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    75
    static Object[][] asSpreaderIllegalPositions() {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    76
        return new Object[][]{{-7}, {3}, {19}};
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    77
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    78
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    79
    @Test(dataProvider = "asSpreaderIllegalPositions")
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    80
    public static void testAsSpreaderIllegalPos(int p) throws Throwable {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    81
        boolean caught = false;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    82
        try {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    83
            SpreadCollect.MH_forSpreading.asSpreader(p, Object[].class, 3);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    84
        } catch (IllegalArgumentException iae) {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    85
            assertEquals("bad spread position", iae.getMessage());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    86
            caught = true;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    87
        }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    88
        assertTrue(caught);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    89
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    90
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    91
    @Test(expectedExceptions = {WrongMethodTypeException.class})
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    92
    public static void testAsSpreaderIllegalMethodType() {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    93
        MethodHandle h = MethodHandles.dropArguments(MethodHandles.constant(String.class, ""), 0, int.class, int.class);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    94
        MethodHandle s = h.asSpreader(String[].class, 1);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    95
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    96
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    97
    @Test
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    98
    public static void testAsCollector() throws Throwable {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
    99
        MethodHandle collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 1);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   100
        assertEquals(SpreadCollect.MT_collector1, collector.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   101
        assertEquals("A4B", (String) collector.invoke("A", 4, "B"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   102
        collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 2);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   103
        assertEquals(SpreadCollect.MT_collector2, collector.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   104
        assertEquals("A45B", (String) collector.invoke("A", 4, 5, "B"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   105
        collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 3);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   106
        assertEquals(SpreadCollect.MT_collector3, collector.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   107
        assertEquals("A456B", (String) collector.invoke("A", 4, 5, 6, "B"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   108
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   109
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   110
    @Test
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   111
    public static void testAsCollectorInvokeWithArguments() throws Throwable {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   112
        MethodHandle collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 1);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   113
        assertEquals(SpreadCollect.MT_collector1, collector.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   114
        assertEquals("A4B", (String) collector.invokeWithArguments("A", 4, "B"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   115
        collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 2);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   116
        assertEquals(SpreadCollect.MT_collector2, collector.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   117
        assertEquals("A45B", (String) collector.invokeWithArguments("A", 4, 5, "B"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   118
        collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 3);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   119
        assertEquals(SpreadCollect.MT_collector3, collector.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   120
        assertEquals("A456B", (String) collector.invokeWithArguments("A", 4, 5, 6, "B"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   121
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   122
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   123
    @Test
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   124
    public static void testAsCollectorLeading() throws Throwable {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   125
        MethodHandle collector = SpreadCollect.MH_forCollectingLeading.asCollector(0, int[].class, 1);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   126
        assertEquals(SpreadCollect.MT_collectorLeading1, collector.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   127
        assertEquals("7Q", (String) collector.invoke(7, "Q"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   128
        collector = SpreadCollect.MH_forCollectingLeading.asCollector(0, int[].class, 2);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   129
        assertEquals(SpreadCollect.MT_collectorLeading2, collector.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   130
        assertEquals("78Q", (String) collector.invoke(7, 8, "Q"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   131
        collector = SpreadCollect.MH_forCollectingLeading.asCollector(0, int[].class, 3);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   132
        assertEquals(SpreadCollect.MT_collectorLeading3, collector.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   133
        assertEquals("789Q", (String) collector.invoke(7, 8, 9, "Q"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   134
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   135
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   136
    @Test
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   137
    public static void testAsCollectorLeadingInvokeWithArguments() throws Throwable {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   138
        MethodHandle collector = SpreadCollect.MH_forCollectingLeading.asCollector(0, int[].class, 1);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   139
        assertEquals(SpreadCollect.MT_collectorLeading1, collector.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   140
        assertEquals("7Q", (String) collector.invokeWithArguments(7, "Q"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   141
        collector = SpreadCollect.MH_forCollectingLeading.asCollector(0, int[].class, 2);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   142
        assertEquals(SpreadCollect.MT_collectorLeading2, collector.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   143
        assertEquals("78Q", (String) collector.invokeWithArguments(7, 8, "Q"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   144
        collector = SpreadCollect.MH_forCollectingLeading.asCollector(0, int[].class, 3);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   145
        assertEquals(SpreadCollect.MT_collectorLeading3, collector.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   146
        assertEquals("789Q", (String) collector.invokeWithArguments(7, 8, 9, "Q"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   147
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   148
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   149
    @Test
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   150
    public static void testAsCollectorNone() throws Throwable {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   151
        MethodHandle collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 0);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   152
        assertEquals(SpreadCollect.MT_collector0, collector.type());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   153
        assertEquals("AB", (String) collector.invoke("A", "B"));
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   154
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   155
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   156
    @DataProvider
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   157
    static Object[][] asCollectorIllegalPositions() {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   158
        return new Object[][]{{-1}, {17}};
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   159
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   160
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   161
    @Test(dataProvider = "asCollectorIllegalPositions")
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   162
    public static void testAsCollectorIllegalPos(int p) {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   163
        boolean caught = false;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   164
        try {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   165
            SpreadCollect.MH_forCollecting.asCollector(p, int[].class, 0);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   166
        } catch (IllegalArgumentException iae) {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   167
            assertEquals("bad collect position", iae.getMessage());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   168
            caught = true;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   169
        }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   170
        assertTrue(caught);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   171
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   172
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   173
    @Test
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   174
    public static void testAsCollectorExample() throws Throwable {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   175
        // test the JavaDoc asCollector-with-pos example
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   176
        StringWriter swr = new StringWriter();
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   177
        MethodHandle swWrite = LOOKUP.
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   178
                findVirtual(StringWriter.class, "write", methodType(void.class, char[].class, int.class, int.class)).
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   179
                bindTo(swr);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   180
        MethodHandle swWrite4 = swWrite.asCollector(0, char[].class, 4);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   181
        swWrite4.invoke('A', 'B', 'C', 'D', 1, 2);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   182
        assertEquals("BC", swr.toString());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   183
        swWrite4.invoke('P', 'Q', 'R', 'S', 0, 4);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   184
        assertEquals("BCPQRS", swr.toString());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   185
        swWrite4.invoke('W', 'X', 'Y', 'Z', 3, 1);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   186
        assertEquals("BCPQRSZ", swr.toString());
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   187
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   188
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   189
    static class SpreadCollect {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   190
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   191
        static String forSpreading(String s1, int i1, int i2, int i3, String s2) {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   192
            return s1 + i1 + i2 + i3 + s2;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   193
        }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   194
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   195
        static String forCollecting(String s1, int[] is, String s2) {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   196
            StringBuilder sb = new StringBuilder(s1);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   197
            for (int i : is) {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   198
                sb.append(i);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   199
            }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   200
            return sb.append(s2).toString();
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   201
        }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   202
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   203
        static String forCollectingLeading(int[] is, String s) {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   204
            return forCollecting("", is, s);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   205
        }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   206
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   207
        static final Class<SpreadCollect> SPREAD_COLLECT = SpreadCollect.class;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   208
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   209
        static final MethodType MT_forSpreading = methodType(String.class, String.class, int.class, int.class, int.class, String.class);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   210
        static final MethodType MT_forCollecting = methodType(String.class, String.class, int[].class, String.class);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   211
        static final MethodType MT_forCollectingLeading = methodType(String.class, int[].class, String.class);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   212
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   213
        static final MethodHandle MH_forSpreading;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   214
        static final MethodHandle MH_forCollecting;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   215
        static final MethodHandle MH_forCollectingLeading;
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   216
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   217
        static final MethodType MT_spreader = methodType(String.class, String.class, int[].class, String.class);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   218
        static final MethodType MT_collector0 = methodType(String.class, String.class, String.class);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   219
        static final MethodType MT_collector1 = methodType(String.class, String.class, int.class, String.class);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   220
        static final MethodType MT_collector2 = methodType(String.class, String.class, int.class, int.class, String.class);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   221
        static final MethodType MT_collector3 = methodType(String.class, String.class, int.class, int.class, int.class, String.class);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   222
        static final MethodType MT_collectorLeading1 = methodType(String.class, int.class, String.class);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   223
        static final MethodType MT_collectorLeading2 = methodType(String.class, int.class, int.class, String.class);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   224
        static final MethodType MT_collectorLeading3 = methodType(String.class, int.class, int.class, int.class, String.class);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   225
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   226
        static final String NONE_ERROR = "zero array length in MethodHandle.asCollector";
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   227
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   228
        static {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   229
            try {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   230
                MH_forSpreading = LOOKUP.findStatic(SPREAD_COLLECT, "forSpreading", MT_forSpreading);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   231
                MH_forCollecting = LOOKUP.findStatic(SPREAD_COLLECT, "forCollecting", MT_forCollecting);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   232
                MH_forCollectingLeading = LOOKUP.findStatic(SPREAD_COLLECT, "forCollectingLeading", MT_forCollectingLeading);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   233
            } catch (Exception e) {
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   234
                throw new ExceptionInInitializerError(e);
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   235
            }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   236
        }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   237
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   238
    }
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   239
0543ccb78f27 8150832: split T8139885 into several tests by functionality
mhaupt
parents:
diff changeset
   240
}