KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > util > ResourceCloserTest


1 package net.sf.uitags.util;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.Reader JavaDoc;
6
7 import junit.framework.TestCase;
8
9 public class ResourceCloserTest extends TestCase {
10   public void testDoesNotFailOnNullResource() {
11     Reader JavaDoc reader = null;
12     ResourceCloser.close(reader);
13
14     InputStream JavaDoc inputStream = null;
15     ResourceCloser.close(inputStream);
16   }
17
18   public void testDoesNotFailOnExceptionCausedByClosingResource() {
19     ResourceCloser.close(new MockReader());
20     ResourceCloser.close(new MockInputStream());
21   }
22
23
24
25   /////////////////////////////////////////////////
26
////////// Mock classes to aid testing //////////
27
/////////////////////////////////////////////////
28

29   private static final class MockReader extends Reader JavaDoc {
30     public void close() throws IOException JavaDoc {
31       throw new IOException JavaDoc(
32           "Thrown by a test method that always throws an exception.");
33     }
34
35     public int read(char[] cbuf, int off, int len) throws IOException JavaDoc {
36       return 0;
37     }
38   }
39
40   private static final class MockInputStream extends InputStream JavaDoc {
41     public void close() throws IOException JavaDoc {
42       throw new IOException JavaDoc(
43           "Thrown by a test method that always throws an exception.");
44     }
45
46     public int read() throws IOException JavaDoc {
47       return 0;
48     }
49   }
50 }
51
Popular Tags