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