01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.commons.io.testtools;
18:
19: import java.io.IOException;
20: import java.io.OutputStream;
21:
22: import junit.framework.AssertionFailedError;
23:
24: import org.apache.commons.io.output.ProxyOutputStream;
25:
26: /**
27: * Helper class for checking behaviour of IO classes.
28: *
29: * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
30: */
31: public class YellOnFlushAndCloseOutputStream extends ProxyOutputStream {
32:
33: private boolean yellForFlush;
34: private boolean yellForClose;
35:
36: /**
37: * @param proxy OutputStream to delegate to.
38: * @param yellForFlush True if flush() is forbidden
39: * @param yellForClose True if close() is forbidden
40: */
41: public YellOnFlushAndCloseOutputStream(OutputStream proxy,
42: boolean yellForFlush, boolean yellForClose) {
43: super (proxy);
44: this .yellForFlush = yellForFlush;
45: this .yellForClose = yellForClose;
46: }
47:
48: /** @see java.io.OutputStream#flush() */
49: public void flush() throws IOException {
50: if (yellForFlush) {
51: throw new AssertionFailedError(
52: "flush() was called on OutputStream");
53: }
54: super .flush();
55: }
56:
57: /** @see java.io.OutputStream#close() */
58: public void close() throws IOException {
59: if (yellForClose) {
60: throw new AssertionFailedError(
61: "close() was called on OutputStream");
62: }
63: super .close();
64: }
65:
66: public void off() {
67: yellForFlush = false;
68: yellForClose = false;
69: }
70:
71: }
|