J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4333920 4994372 |
| 27 | * @library ../../../../../sun/net/www/httptest/ |
| 28 | * @build HttpCallback HttpServer ClosedChannelList HttpTransaction |
| 29 | * @run main ChunkedEncodingWithProgressMonitorTest |
| 30 | * @summary ChunkedEncoding unit test; MeteredStream/ProgressData problem |
| 31 | */ |
| 32 | |
| 33 | import java.io.*; |
| 34 | import java.net.*; |
| 35 | import java.security.*; |
| 36 | import java.util.BitSet; |
| 37 | import sun.net.ProgressMeteringPolicy; |
| 38 | import sun.net.ProgressMonitor; |
| 39 | import sun.net.ProgressListener; |
| 40 | import sun.net.ProgressEvent; |
| 41 | |
| 42 | public class ChunkedEncodingWithProgressMonitorTest { |
| 43 | public static void main (String[] args) throws Exception { |
| 44 | ProgressMonitor.setMeteringPolicy(new MyProgressMeteringPolicy()); |
| 45 | ProgressMonitor.getDefault().addProgressListener(new MyProgressListener()); |
| 46 | ChunkedEncodingTest.test(); |
| 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 | } |