1   package net.sourceforge.jpotpourri.util;
2   
3   import java.io.Closeable;
4   import java.io.IOException;
5   
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   
9   /**
10   * @author christoph_pickl@users.sourceforge.net
11   */
12  public final class CloseUtilTest extends AbstractUtilTestCase {
13  
14      private static final Log LOG = LogFactory.getLog(CloseUtilTest.class);
15  
16  
17  	public void testCloseNull() throws Exception {
18  		innerTestCloseNull(CloseUtil.class);
19  		innerTestCloseNull(SubCloseUtil.class);
20  	}
21  
22  	private void innerTestCloseNull(final Class<? extends CloseUtil> clazz) throws Exception {
23  		final Closeable[] methodArgs = new Closeable[] { null };
24  		invokeCloseMethod(clazz, methodArgs);
25  		// should not throw exception
26  	}
27  
28  
29  	public void testCloseException() throws Exception {
30  		innerTestCloseException(CloseUtil.class);
31  		innerTestCloseException(SubCloseUtil.class);
32  	}
33  	
34  	private void innerTestCloseException(final Class<? extends CloseUtil> clazz) throws Exception {
35  		try {
36  			LOG.info("CloseUtil will throw exception.");
37  			
38  			invokeCloseMethod(clazz, new Closeable[] { new ThrowingTestCloseable() });
39  			assertTrue(true);
40  		} catch (Exception e) {
41  			e.printStackTrace();
42  			fail(e.getMessage());
43  		}
44  	}
45  	
46  
47  	
48  	public void testCloseState() throws Exception {
49  		innerTestCloseState(CloseUtil.class);
50  		innerTestCloseState(SubCloseUtil.class);
51  	}
52  	
53  	private void innerTestCloseState(final Class<? extends CloseUtil> clazz) throws Exception {
54  		final ManagedTestCloseable closeable = new ManagedTestCloseable();
55  		final Closeable[] methodArgs = new Closeable[] { closeable };
56  		assertFalse(closeable.isClosed());
57  
58  		invokeCloseMethod(clazz, methodArgs);
59  		assertTrue(closeable.isClosed());
60  
61  		LOG.info("CloseUtil will log exception.");
62  		invokeCloseMethod(clazz, methodArgs);
63  	}
64  	
65  
66  	
67  	private static void invokeCloseMethod(final Class<? extends CloseUtil> clazz,
68  			final Closeable[] methodArgs) throws Exception {
69  		clazz.getMethod("close", Closeable[].class).invoke(null, (Object) methodArgs);
70  	}
71  
72  	
73  	/**
74  	 * @author christoph_pickl@users.sourceforge.net
75  	 */
76  	private static class ThrowingTestCloseable implements Closeable {
77  		public void close() throws IOException {
78  			throw new IOException("CloseUtilTest did throw exception - Okay.");
79  		}
80  	}
81  
82  	/**
83  	 * @author christoph_pickl@users.sourceforge.net
84  	 */
85  	private static class ManagedTestCloseable implements Closeable {
86  		private boolean closed = false;
87  		public void close() throws IOException {
88  			if(this.isClosed() == true) {
89  				throw new IOException("TestCloseable is already closed - Okay.");
90  			}
91  			this.closed = true;
92  		}
93  		public boolean isClosed() {
94  			return this.closed;
95  		}
96  	}
97  
98  	/**
99  	 * @author christoph_pickl@users.sourceforge.net
100 	 */
101 	private static class SubCloseUtil extends CloseUtil {
102 		protected SubCloseUtil() {
103 			super();
104 		}
105 		// could have some additional functionality
106 	}
107 	
108 }