ChunkedEncodingWithProgressMonitorTest.java revision 14606:bc3775e25b52
1/*
2 * Copyright (c) 2004, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 4333920 4994372
27 * @summary ChunkedEncoding unit test; MeteredStream/ProgressData problem
28 * @modules java.base/sun.net
29 *          jdk.httpserver
30 * @run main ChunkedEncodingWithProgressMonitorTest
31 */
32
33import java.net.*;
34import java.util.BitSet;
35import sun.net.ProgressMeteringPolicy;
36import sun.net.ProgressMonitor;
37import sun.net.ProgressListener;
38import sun.net.ProgressEvent;
39
40public class ChunkedEncodingWithProgressMonitorTest {
41    public static void main (String[] args) throws Exception {
42        ProgressMonitor.setMeteringPolicy(new MyProgressMeteringPolicy());
43        ProgressListener listener = new MyProgressListener();
44        ProgressMonitor.getDefault().addProgressListener(listener);
45        ChunkedEncodingTest.test();
46        ProgressMonitor.getDefault().removeProgressListener(listener);
47
48        if (flag.cardinality() != 3) {
49            throw new RuntimeException("All three methods in ProgressListener"+
50                                       " should be called. Yet the number of"+
51                                       " methods actually called are "+
52                                       flag.cardinality());
53        }
54    }
55
56    static class MyProgressMeteringPolicy implements ProgressMeteringPolicy {
57        /**
58         * Return true if metering should be turned on for a particular network input stream.
59         */
60        public boolean shouldMeterInput(URL url, String method) {
61            return true;
62        }
63
64        /**
65         * Return update notification threshold.
66         */
67        public int getProgressUpdateThreshold() {
68            return 8192;
69        }
70    }
71
72    static BitSet flag = new BitSet(3);
73
74    static class MyProgressListener implements ProgressListener {
75        /**
76         * Start progress.
77         */
78        public void progressStart(ProgressEvent evt) {
79            System.out.println("start: received progressevent "+evt);
80            if (flag.nextSetBit(0) == -1)
81                flag.set(0);
82        }
83
84        /**
85         * Update progress.
86         */
87        public void progressUpdate(ProgressEvent evt) {
88            System.out.println("update: received progressevent "+evt);
89            if (flag.nextSetBit(1) == -1)
90                flag.set(1);
91        }
92
93        /**
94         * Finish progress.
95         */
96        public void progressFinish(ProgressEvent evt) {
97            System.out.println("finish: received progressevent "+evt);
98            if (flag.nextSetBit(2) == -1)
99                flag.set(2);
100        }
101    }
102}
103