001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.container.test.adapters;
027:
028: import junit.framework.TestCase;
029: import org.jicarilla.container.Factory;
030: import org.jicarilla.container.JicarillaException;
031: import org.jicarilla.container.JicarillaInstantiationException;
032: import org.jicarilla.container.adapters.AbstractAdapter;
033:
034: /**
035: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
036: * @version $Id: AbstracAdapterTestCase.java,v 1.1 2004/03/17 14:14:17 lsimons Exp $
037: */
038: public class AbstracAdapterTestCase extends TestCase {
039: public static class Adapter extends AbstractAdapter {
040: public Adapter(Factory factory) {
041: super (factory);
042: }
043:
044: public Object getInstance() {
045: return m_factory.newInstance();
046: }
047: }
048:
049: public static class LoggingFactory implements Factory {
050: public boolean newInstanceCalled = false;
051: public boolean releaseInstanceCalled = false;
052:
053: public Object newInstance() {
054: newInstanceCalled = true;
055: return new Object();
056: }
057:
058: public void releaseInstance(Object o) throws Exception {
059: releaseInstanceCalled = true;
060: }
061: }
062:
063: public void testEverything() throws Exception {
064: LoggingFactory factory = new LoggingFactory();
065: Adapter adapter = new Adapter(factory);
066:
067: Object instance = adapter.getInstance();
068: adapter.releaseInstance(instance);
069:
070: assertTrue(factory.newInstanceCalled);
071: assertTrue(factory.releaseInstanceCalled);
072:
073: factory = new LoggingFactory();
074: adapter = new Adapter(factory);
075: adapter.releaseInstance(null);
076: assertTrue(factory.releaseInstanceCalled);
077: }
078:
079: public void testExceptions() {
080: Throwable t = null;
081: try {
082: new Adapter(null);
083: } catch (AssertionError th) {
084: t = th;
085: }
086: assertNotNull(t);
087: }
088:
089: public void testHandleUnExpectedException() {
090: JicarillaException ex = new JicarillaException();
091: try {
092: AbstractAdapter.handleUnexpectedException(ex);
093: fail("Expected an exception!");
094: } catch (Throwable th) {
095: assertEquals(ex, th);
096: }
097:
098: Error e = new Error();
099: try {
100: AbstractAdapter.handleUnexpectedException(e);
101: fail("Expected an exception!");
102: } catch (Throwable th) {
103: assertEquals(e, th);
104: }
105:
106: Exception exc = new Exception();
107: try {
108: AbstractAdapter.handleUnexpectedException(exc);
109: fail("Expected an exception!");
110: } catch (Throwable th) {
111: assertTrue(th instanceof JicarillaInstantiationException);
112: assertEquals(exc, th.getCause());
113: }
114:
115: }
116:
117: public void testHandleUnExpectedExceptionFromSubClass() {
118: LoggingFactory factory = new LoggingFactory();
119: Adapter adapter = new Adapter(factory);
120:
121: JicarillaException ex = new JicarillaException();
122: try {
123: adapter.handleUnexpectedException(ex);
124: fail("Expected an exception!");
125: } catch (Throwable th) {
126: assertEquals(ex, th);
127: }
128:
129: Error e = new Error();
130: try {
131: adapter.handleUnexpectedException(e);
132: fail("Expected an exception!");
133: } catch (Throwable th) {
134: assertEquals(e, th);
135: }
136:
137: Exception exc = new Exception();
138: try {
139: adapter.handleUnexpectedException(exc);
140: fail("Expected an exception!");
141: } catch (Throwable th) {
142: assertTrue(th instanceof JicarillaInstantiationException);
143: assertEquals(exc, th.getCause());
144: }
145:
146: }
147: }
|