Path: blob/master/test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingWithProgressMonitorTest.java
41154 views
/*1* Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4333920 499437226* @summary ChunkedEncoding unit test; MeteredStream/ProgressData problem27* @modules java.base/sun.net28* jdk.httpserver29* @library /test/lib30* @run main ChunkedEncodingWithProgressMonitorTest31*/3233import java.net.*;34import java.util.BitSet;35import sun.net.ProgressMeteringPolicy;36import sun.net.ProgressMonitor;37import sun.net.ProgressListener;38import sun.net.ProgressEvent;3940public class ChunkedEncodingWithProgressMonitorTest {41public static void main (String[] args) throws Exception {42ProgressMonitor.setMeteringPolicy(new MyProgressMeteringPolicy());43ProgressListener listener = new MyProgressListener();44ProgressMonitor.getDefault().addProgressListener(listener);45ChunkedEncodingTest.test();46ProgressMonitor.getDefault().removeProgressListener(listener);4748if (flag.cardinality() != 3) {49throw new RuntimeException("All three methods in ProgressListener"+50" should be called. Yet the number of"+51" methods actually called are "+52flag.cardinality());53}54}5556static class MyProgressMeteringPolicy implements ProgressMeteringPolicy {57/**58* Return true if metering should be turned on for a particular network input stream.59*/60public boolean shouldMeterInput(URL url, String method) {61return true;62}6364/**65* Return update notification threshold.66*/67public int getProgressUpdateThreshold() {68return 8192;69}70}7172static BitSet flag = new BitSet(3);7374static class MyProgressListener implements ProgressListener {75/**76* Start progress.77*/78public void progressStart(ProgressEvent evt) {79System.out.println("start: received progressevent "+evt);80if (flag.nextSetBit(0) == -1)81flag.set(0);82}8384/**85* Update progress.86*/87public void progressUpdate(ProgressEvent evt) {88System.out.println("update: received progressevent "+evt);89if (flag.nextSetBit(1) == -1)90flag.set(1);91}9293/**94* Finish progress.95*/96public void progressFinish(ProgressEvent evt) {97System.out.println("finish: received progressevent "+evt);98if (flag.nextSetBit(2) == -1)99flag.set(2);100}101}102}103104105