001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dennis Ushakov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.event.WindowEvent;
023: import java.io.ByteArrayInputStream;
024: import java.io.FilterInputStream;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.InterruptedIOException;
028:
029: public class ProgressMonitorInputStreamTest extends BasicSwingTestCase {
030: private JFrame window;
031:
032: private ProgressMonitorInputStream in;
033:
034: private ByteArrayInputStream realIn;
035:
036: private byte[] bytes;
037:
038: private static class ErrorStream extends FilterInputStream {
039: protected ErrorStream(final InputStream in) {
040: super (in);
041: }
042:
043: @Override
044: public int available() throws IOException {
045: throw new IOException();
046: }
047: }
048:
049: @Override
050: public void setUp() {
051: window = new JFrame();
052: int l = 300;
053: bytes = new byte[l];
054: for (int i = 0; i < l; i++) {
055: bytes[i] = (byte) i;
056: }
057: realIn = new ByteArrayInputStream(bytes);
058: }
059:
060: @Override
061: public void tearDown() {
062: in = null;
063: realIn = null;
064: bytes = null;
065: window.dispose();
066: }
067:
068: public void testProgressMonitorInputStream() throws Exception {
069: in = new ProgressMonitorInputStream(window, "Here we go...",
070: realIn);
071: assertNotNull(in.getProgressMonitor());
072: in.read();
073: Thread.sleep(600);
074: in.skip(30);
075: assertEquals(1, window.getOwnedWindows().length);
076: in.close();
077: }
078:
079: public void testMaximum() throws Exception {
080: in = new ProgressMonitorInputStream(window, "Here we go...",
081: new ErrorStream(realIn));
082: assertEquals(0, in.getProgressMonitor().getMaximum());
083: in.read();
084: Thread.sleep(600);
085: in.skip(30);
086: Thread.sleep(600);
087: assertEquals(0, window.getOwnedWindows().length);
088: in.close();
089: }
090:
091: public void testReset() throws Exception {
092: in = new ProgressMonitorInputStream(window, "Here we go...",
093: realIn);
094: ProgressMonitor pm = in.getProgressMonitor();
095: in.read();
096: Thread.sleep(600);
097: in.skip(30);
098: in.reset();
099: assertSame(pm, in.getProgressMonitor());
100: in.close();
101: }
102:
103: public void testInterrupted() throws Exception {
104: in = new ProgressMonitorInputStream(window, "Here we go...",
105: realIn);
106: in.read();
107: Thread.sleep(600);
108: in.skip(30);
109: JDialog dialog = (JDialog) window.getOwnedWindows()[0];
110: dialog.dispatchEvent(new WindowEvent(window,
111: WindowEvent.WINDOW_CLOSING));
112: in.reset();
113: in.skip(30);
114: testExceptionalCase(new ExceptionalCase() {
115: @Override
116: public void exceptionalAction() throws Exception {
117: in.read();
118: }
119:
120: @SuppressWarnings("unchecked")
121: @Override
122: public Class expectedExceptionClass() {
123: return InterruptedIOException.class;
124: }
125: });
126: Thread.sleep(600);
127: in.close();
128: }
129: }
|