test/jdk/java/net/httpclient/FlowAdaptersCompileOnly.java
changeset 49765 ee6f7a61f3a5
parent 48408 4f830b447edf
child 56451 9585061fdb04
--- a/test/jdk/java/net/httpclient/FlowAdaptersCompileOnly.java	Tue Apr 17 15:39:20 2018 +0200
+++ b/test/jdk/java/net/httpclient/FlowAdaptersCompileOnly.java	Tue Apr 17 08:54:17 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 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
@@ -21,15 +21,20 @@
  * questions.
  */
 
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
 import java.nio.ByteBuffer;
 import java.nio.MappedByteBuffer;
 import java.util.Collection;
 import java.util.List;
 import java.util.concurrent.Flow;
 import java.util.function.Function;
-import jdk.incubator.http.HttpRequest.BodyPublisher;
-import jdk.incubator.http.HttpResponse.BodyHandler;
-import jdk.incubator.http.HttpResponse.BodySubscriber;
+import java.net.http.HttpRequest.BodyPublishers;
+import java.net.http.HttpResponse.BodyHandler;
+import java.net.http.HttpResponse.BodyHandlers;
+import java.net.http.HttpResponse.BodySubscriber;
+import java.net.http.HttpResponse.BodySubscribers;
 
 /*
  * @test
@@ -40,31 +45,31 @@
 public class FlowAdaptersCompileOnly {
 
     static void makesSureDifferentGenericSignaturesCompile() {
-        BodyPublisher.fromPublisher(new BBPublisher());
-        BodyPublisher.fromPublisher(new MBBPublisher());
+        BodyPublishers.fromPublisher(new BBPublisher());
+        BodyPublishers.fromPublisher(new MBBPublisher());
 
-        BodyHandler.fromSubscriber(new ListSubscriber());
-        BodyHandler.fromSubscriber(new CollectionSubscriber());
-        BodyHandler.fromSubscriber(new IterableSubscriber());
-        BodyHandler.fromSubscriber(new ObjectSubscriber());
+        BodyHandlers.fromSubscriber(new ListSubscriber());
+        BodyHandlers.fromSubscriber(new CollectionSubscriber());
+        BodyHandlers.fromSubscriber(new IterableSubscriber());
+        BodyHandlers.fromSubscriber(new ObjectSubscriber());
 
-        BodySubscriber.fromSubscriber(new ListSubscriber());
-        BodySubscriber.fromSubscriber(new CollectionSubscriber());
-        BodySubscriber.fromSubscriber(new IterableSubscriber());
-        BodySubscriber.fromSubscriber(new ObjectSubscriber());
+        BodySubscribers.fromSubscriber(new ListSubscriber());
+        BodySubscribers.fromSubscriber(new CollectionSubscriber());
+        BodySubscribers.fromSubscriber(new IterableSubscriber());
+        BodySubscribers.fromSubscriber(new ObjectSubscriber());
 
-        BodyPublisher.fromPublisher(new BBPublisher(), 1);
-        BodyPublisher.fromPublisher(new MBBPublisher(), 1);
+        BodyPublishers.fromPublisher(new BBPublisher(), 1);
+        BodyPublishers.fromPublisher(new MBBPublisher(), 1);
 
-        BodyHandler.fromSubscriber(new ListSubscriber(), Function.identity());
-        BodyHandler.fromSubscriber(new CollectionSubscriber(), Function.identity());
-        BodyHandler.fromSubscriber(new IterableSubscriber(), Function.identity());
-        BodyHandler.fromSubscriber(new ObjectSubscriber(), Function.identity());
+        BodyHandlers.fromSubscriber(new ListSubscriber(), Function.identity());
+        BodyHandlers.fromSubscriber(new CollectionSubscriber(), Function.identity());
+        BodyHandlers.fromSubscriber(new IterableSubscriber(), Function.identity());
+        BodyHandlers.fromSubscriber(new ObjectSubscriber(), Function.identity());
 
-        BodySubscriber.fromSubscriber(new ListSubscriber(), Function.identity());
-        BodySubscriber.fromSubscriber(new CollectionSubscriber(), Function.identity());
-        BodySubscriber.fromSubscriber(new IterableSubscriber(), Function.identity());
-        BodySubscriber.fromSubscriber(new ObjectSubscriber(), Function.identity());
+        BodySubscribers.fromSubscriber(new ListSubscriber(), Function.identity());
+        BodySubscribers.fromSubscriber(new CollectionSubscriber(), Function.identity());
+        BodySubscribers.fromSubscriber(new IterableSubscriber(), Function.identity());
+        BodySubscribers.fromSubscriber(new ObjectSubscriber(), Function.identity());
     }
 
     static class BBPublisher implements Flow.Publisher<ByteBuffer> {
@@ -104,4 +109,97 @@
         @Override public void onError(Throwable throwable) { }
         @Override public void onComplete() { }
     }
+
+    // ---
+
+    static final Function<ListSubscriber,Integer> f1 = subscriber -> 1;
+    static final Function<ListSubscriber,Number> f2 = subscriber -> 2;
+    static final Function<ListSubscriberX,Integer> f3 = subscriber -> 3;
+    static final Function<ListSubscriberX,Number> f4 = subscriber -> 4;
+
+    static class ListSubscriberX extends ListSubscriber {
+        int getIntegerX() { return 5; }
+    }
+
+    static void makesSureDifferentGenericFunctionSignaturesCompile() {
+        BodyHandler<Integer> bh01 = BodyHandlers.fromSubscriber(new ListSubscriber(), s -> 6);
+        BodyHandler<Number>  bh02 = BodyHandlers.fromSubscriber(new ListSubscriber(), s -> 7);
+        BodyHandler<Integer> bh03 = BodyHandlers.fromSubscriber(new ListSubscriber(), f1);
+        BodyHandler<Number>  bh04 = BodyHandlers.fromSubscriber(new ListSubscriber(), f1);
+        BodyHandler<Number>  bh05 = BodyHandlers.fromSubscriber(new ListSubscriber(), f2);
+        BodyHandler<Integer> bh06 = BodyHandlers.fromSubscriber(new ListSubscriberX(), f1);
+        BodyHandler<Number>  bh07 = BodyHandlers.fromSubscriber(new ListSubscriberX(), f1);
+        BodyHandler<Number>  bh08 = BodyHandlers.fromSubscriber(new ListSubscriberX(), f2);
+        BodyHandler<Integer> bh09 = BodyHandlers.fromSubscriber(new ListSubscriberX(), ListSubscriberX::getIntegerX);
+        BodyHandler<Number>  bh10 = BodyHandlers.fromSubscriber(new ListSubscriberX(), ListSubscriberX::getIntegerX);
+        BodyHandler<Integer> bh11 = BodyHandlers.fromSubscriber(new ListSubscriberX(), f3);
+        BodyHandler<Number>  bh12 = BodyHandlers.fromSubscriber(new ListSubscriberX(), f3);
+        BodyHandler<Number>  bh13 = BodyHandlers.fromSubscriber(new ListSubscriberX(), f4);
+
+        BodySubscriber<Integer> bs01 = BodySubscribers.fromSubscriber(new ListSubscriber(), s -> 6);
+        BodySubscriber<Number>  bs02 = BodySubscribers.fromSubscriber(new ListSubscriber(), s -> 7);
+        BodySubscriber<Integer> bs03 = BodySubscribers.fromSubscriber(new ListSubscriber(), f1);
+        BodySubscriber<Number>  bs04 = BodySubscribers.fromSubscriber(new ListSubscriber(), f1);
+        BodySubscriber<Number>  bs05 = BodySubscribers.fromSubscriber(new ListSubscriber(), f2);
+        BodySubscriber<Integer> bs06 = BodySubscribers.fromSubscriber(new ListSubscriberX(), f1);
+        BodySubscriber<Number>  bs07 = BodySubscribers.fromSubscriber(new ListSubscriberX(), f1);
+        BodySubscriber<Number>  bs08 = BodySubscribers.fromSubscriber(new ListSubscriberX(), f2);
+        BodySubscriber<Integer> bs09 = BodySubscribers.fromSubscriber(new ListSubscriberX(), ListSubscriberX::getIntegerX);
+        BodySubscriber<Number>  bs10 = BodySubscribers.fromSubscriber(new ListSubscriberX(), ListSubscriberX::getIntegerX);
+        BodySubscriber<Integer> bs11 = BodySubscribers.fromSubscriber(new ListSubscriberX(), f3);
+        BodySubscriber<Number>  bs12 = BodySubscribers.fromSubscriber(new ListSubscriberX(), f3);
+        BodySubscriber<Number>  bs13 = BodySubscribers.fromSubscriber(new ListSubscriberX(), f4);
+    }
+
+    // ---
+
+    static class NumberSubscriber implements Flow.Subscriber<List<ByteBuffer>> {
+        @Override public void onSubscribe(Flow.Subscription subscription) { }
+        @Override public void onNext(List<ByteBuffer> item) { }
+        @Override public void onError(Throwable throwable) { }
+        @Override public void onComplete() { }
+        public Number getNumber() { return null; }
+    }
+
+    static class IntegerSubscriber extends NumberSubscriber {
+        @Override public void onSubscribe(Flow.Subscription subscription) { }
+        @Override public void onNext(List<ByteBuffer> item) { }
+        @Override public void onError(Throwable throwable) { }
+        @Override public void onComplete() { }
+        public Integer getInteger() { return null; }
+    }
+
+    static class LongSubscriber extends NumberSubscriber {
+        @Override public void onSubscribe(Flow.Subscription subscription) { }
+        @Override public void onNext(List<ByteBuffer> item) { }
+        @Override public void onError(Throwable throwable) { }
+        @Override public void onComplete() { }
+        public Long getLong() { return null; }
+    }
+
+    static final Function<NumberSubscriber,Number> numMapper = sub -> sub.getNumber();
+    static final Function<IntegerSubscriber,Integer> intMapper = sub -> sub.getInteger();
+    static final Function<LongSubscriber,Long> longMapper = sub -> sub.getLong();
+
+    public void makesSureDifferentGenericSubscriberSignaturesCompile()
+        throws Exception
+    {
+        HttpClient client = null;
+        HttpRequest request = null;
+        IntegerSubscriber sub1 = new IntegerSubscriber();
+
+        HttpResponse<Integer> r1 = client.send(request, BodyHandlers.fromSubscriber(sub1, IntegerSubscriber::getInteger));
+        HttpResponse<Number>  r2 = client.send(request, BodyHandlers.fromSubscriber(sub1, IntegerSubscriber::getInteger));
+        HttpResponse<Number>  r3 = client.send(request, BodyHandlers.fromSubscriber(sub1, NumberSubscriber::getNumber));
+        HttpResponse<Integer> r4 = client.send(request, BodyHandlers.fromSubscriber(sub1, intMapper));
+        HttpResponse<Number>  r5 = client.send(request, BodyHandlers.fromSubscriber(sub1, intMapper));
+        HttpResponse<Number>  r6 = client.send(request, BodyHandlers.fromSubscriber(sub1, numMapper));
+
+        // compiles but makes little sense. Just what you get with any usage of `? super`
+        final Function<Object,Number> objectMapper = sub -> 1;
+        client.sendAsync(request, BodyHandlers.fromSubscriber(sub1, objectMapper));
+
+        // does not compile, as expected ( uncomment to see )
+        //HttpResponse<Number> r7 = client.send(request, BodyHandler.fromSubscriber(sub1, longMapper));
+    }
 }