1 /*
   2  * Copyright (c) 2004, 2012, 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  * @run main ChunkedEncodingWithProgressMonitorTest
  28  * @summary ChunkedEncoding unit test; MeteredStream/ProgressData problem
  29  */
  30 
  31 import java.net.*;
  32 import java.util.BitSet;
  33 import sun.net.ProgressMeteringPolicy;
  34 import sun.net.ProgressMonitor;
  35 import sun.net.ProgressListener;
  36 import sun.net.ProgressEvent;
  37 
  38 public class ChunkedEncodingWithProgressMonitorTest {
  39     public static void main (String[] args) throws Exception {
  40         ProgressMonitor.setMeteringPolicy(new MyProgressMeteringPolicy());
  41         ProgressListener listener = new MyProgressListener();
  42         ProgressMonitor.getDefault().addProgressListener(listener);
  43         ChunkedEncodingTest.test();
  44         ProgressMonitor.getDefault().removeProgressListener(listener);
  45 
  46         if (flag.cardinality() != 3) {
  47             throw new RuntimeException("All three methods in ProgressListener"+
  48                                        " should be called. Yet the number of"+
  49                                        " methods actually called are "+
  50                                        flag.cardinality());
  51         }
  52     }
  53 
  54     static class MyProgressMeteringPolicy implements ProgressMeteringPolicy {
  55         /**
  56          * Return true if metering should be turned on for a particular network input stream.
  57          */
  58         public boolean shouldMeterInput(URL url, String method) {
  59             return true;
  60         }
  61 
  62         /**
  63          * Return update notification threshold.
  64          */
  65         public int getProgressUpdateThreshold() {
  66             return 8192;
  67         }
  68     }
  69 
  70     static BitSet flag = new BitSet(3);
  71 
  72     static class MyProgressListener implements ProgressListener {
  73         /**
  74          * Start progress.
  75          */
  76         public void progressStart(ProgressEvent evt) {
  77             System.out.println("start: received progressevent "+evt);
  78             if (flag.nextSetBit(0) == -1)
  79                 flag.set(0);
  80         }
  81 
  82         /**
  83          * Update progress.
  84          */
  85         public void progressUpdate(ProgressEvent evt) {
  86             System.out.println("update: received progressevent "+evt);
  87             if (flag.nextSetBit(1) == -1)
  88                 flag.set(1);
  89         }
  90 
  91         /**
  92          * Finish progress.
  93          */
  94         public void progressFinish(ProgressEvent evt) {
  95             System.out.println("finish: received progressevent "+evt);
  96             if (flag.nextSetBit(2) == -1)
  97                 flag.set(2);
  98         }
  99     }
 100 }