001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.sql;
022:
023: import java.sql.SQLException;
024: import java.sql.Savepoint;
025: import java.util.Map;
026: import java.util.TreeMap;
027:
028: import net.sf.hajdbc.Database;
029: import net.sf.hajdbc.DatabaseCluster;
030: import net.sf.hajdbc.MockDatabase;
031: import net.sf.hajdbc.util.reflect.ProxyFactory;
032:
033: import org.easymock.EasyMock;
034: import org.testng.annotations.AfterMethod;
035: import org.testng.annotations.BeforeClass;
036: import org.testng.annotations.Test;
037:
038: /**
039: * @author Paul Ferraro
040: *
041: */
042: @SuppressWarnings({"unchecked","nls"})
043: public class TestSavepoint implements Savepoint {
044: private DatabaseCluster cluster = EasyMock
045: .createStrictMock(DatabaseCluster.class);
046: private Savepoint savepoint1 = EasyMock
047: .createStrictMock(java.sql.Savepoint.class);
048: private Savepoint savepoint2 = EasyMock
049: .createStrictMock(java.sql.Savepoint.class);
050: private SQLProxy parent = EasyMock.createStrictMock(SQLProxy.class);
051:
052: private Database database1 = new MockDatabase("1");
053: private Database database2 = new MockDatabase("2");
054: private Savepoint savepoint;
055:
056: @BeforeClass
057: void init() throws Exception {
058: Map<Database, Savepoint> map = new TreeMap<Database, Savepoint>();
059: map.put(this .database1, this .savepoint1);
060: map.put(this .database2, this .savepoint2);
061:
062: EasyMock.expect(this .parent.getDatabaseCluster()).andReturn(
063: this .cluster);
064:
065: this .parent.addChild(EasyMock
066: .isA(SavepointInvocationHandler.class));
067:
068: this .replay();
069:
070: this .savepoint = ProxyFactory.createProxy(Savepoint.class,
071: new SavepointInvocationHandler(null, this .parent,
072: EasyMock.createMock(Invoker.class), map));
073:
074: this .verify();
075: this .reset();
076: }
077:
078: private Object[] objects() {
079: return new Object[] { this .cluster, this .savepoint1,
080: this .savepoint2, this .parent };
081: }
082:
083: void replay() {
084: EasyMock.replay(this .objects());
085: }
086:
087: void verify() {
088: EasyMock.verify(this .objects());
089: }
090:
091: @AfterMethod
092: void reset() {
093: EasyMock.reset(this .objects());
094: }
095:
096: /**
097: * @see java.sql.Savepoint#getSavepointId()
098: */
099: @Test
100: public int getSavepointId() throws SQLException {
101: EasyMock.expect(this .cluster.isActive()).andReturn(true);
102:
103: EasyMock.expect(this .savepoint1.getSavepointId()).andReturn(1);
104:
105: this .replay();
106:
107: int result = this .savepoint.getSavepointId();
108:
109: this .verify();
110:
111: assert result == 1;
112:
113: return result;
114: }
115:
116: /**
117: * @see java.sql.Savepoint#getSavepointName()
118: */
119: @Test
120: public String getSavepointName() throws SQLException {
121: EasyMock.expect(this .cluster.isActive()).andReturn(true);
122:
123: EasyMock.expect(this .savepoint1.getSavepointName()).andReturn(
124: "");
125:
126: this .replay();
127:
128: String result = this .savepoint.getSavepointName();
129:
130: this .verify();
131:
132: assert result.equals("");
133:
134: return result;
135: }
136: }
|