1   package uba.db.jdbc;
2   
3   import java.sql.ResultSet;
4   import java.sql.SQLException;
5   import java.util.HashMap;
6   import java.util.Map;
7   
8   import junit.framework.TestCase;
9   
10  /***
11   * @version $Revision: 1.1.1.1 $
12   */
13  public class SocketConnectionTest extends TestCase {
14      private SocketConnection connection;
15  
16      protected void setUp() throws Exception {
17          connection = new uba.db.jdbc.SocketConnection();
18      }
19  
20      public void testCreateStatement() throws Exception {
21          java.sql.Statement result = connection.createStatement();
22          assertNotNull(result);
23      }
24  
25      public void testCreatePreparedStatement() throws Exception {
26          java.sql.PreparedStatement result = connection
27                  .prepareStatement("select * from r1");
28          assertNotNull(result);
29      }
30  
31      public void testNativeSQLWithoutSpecialCharacters() throws Exception {
32          String expectedResult = "select * from t";
33          String result = connection.nativeSQL("select * from t");
34          assertEquals(result, expectedResult);
35      }
36  
37      public void testNativeSQLWithDate() throws SQLException {
38          try {
39              connection.nativeSQL("{d '2005-04-13'}");
40              fail("Esta version no soporta DATE, cuando las soporte deberia retornar: DATE '2005-04-13'");
41          } catch (DateDatatypeUnsupportedException e) {}
42      }
43  
44      public void testNativeSQLWithCall() throws SQLException {
45          try {
46              connection.nativeSQL("{call xxx}");
47              fail("Las llamadas a stored procedures no estan soportadas en esta vesrsion del servidor");
48          } catch (StoredProceduresUnsupportedException e) {}
49      }
50  
51      public void testNativeSQLWithCallAndReturn() throws SQLException {
52          try {
53              connection.nativeSQL("{?=call xxx}");
54              fail("Las llamadas a stored procedures no estan soportadas en esta vesrsion del servidor");
55          } catch (StoredProceduresUnsupportedException e) {}
56      }
57  
58      public void testNativeSQLWithChar() throws SQLException {
59          String expectedResult = "'{d \'2005-04-13\'}'";
60          String result = connection.nativeSQL("'{d \'2005-04-13\'}'");
61          assertEquals(result, expectedResult);
62      }
63  
64      public void testAutocommitAccessing() throws SQLException {
65          assertTrue("Por default en JDBC autoCommit es true", connection.getAutoCommit());
66          try {
67              connection.setAutoCommit(false);
68              fail("Esta version del servidor no soporta transacciones");
69          } catch (TransactionsUnsupportedExeption e) {}
70      }
71  
72      public void testClose() throws SQLException {
73          assertFalse(connection.isClosed());
74          connection.close();
75          assertTrue(connection.isClosed());
76      }
77  
78      public void testGetMetaData() throws SQLException {
79          assertNotNull(connection.getMetaData());
80      }
81  
82      public void testReadOnlyAccessors() throws Exception {
83          assertFalse(connection.isReadOnly());
84          connection.setReadOnly(true);
85          assertTrue(connection.isReadOnly());
86      }
87  
88      public void testSetCatalog() {
89          try {
90              connection.setCatalog("un catalogo");
91          } catch (Exception e) {
92              fail("Los catalogos no estan soportados, y segun el API de JDBC esto no debe generar error");
93          }
94      }
95  
96      public void testGetCatalog() throws Exception {
97          assertNull("Los catalogos no estan soportados, se debe retornar null", connection
98                  .getCatalog());
99      }
100 
101     public void testSetTransactionIsolation() throws Exception {
102         try {
103             connection.setTransactionIsolation(java.sql.Connection.TRANSACTION_NONE);
104             fail("Esta version del servidor no soporta transacciones");
105         } catch (TransactionsUnsupportedExeption e) {}
106     }
107 
108     public void testGetWarningsWithCleanState() throws Exception {
109         assertNull(connection.getWarnings());
110     }
111 
112     public void testGetWarningsWhenClosed() throws Exception {
113         connection.close();
114         try {
115             connection.getWarnings();
116             fail("La conexion fue cerrada, por lo que getWarnnings deberia generar una excepcion");
117         } catch (ConnectionClosedException e) {}
118     }
119 
120     public void testGetWarningsAfterOperationsThatFailsSilent() throws Exception {
121         connection.getTransactionIsolation();
122         assertEquals(connection.getWarnings().getClass(),
123                      TransactionsUnsupportedWarning.class);
124         connection.setCatalog("bla");
125         assertEquals(connection.getWarnings().getNextWarning().getClass(),
126                      CatalogsUnsupportedWarning.class);
127     }
128 
129     public void testClearWarnings() throws Exception {
130         connection.getTransactionIsolation();
131         assertEquals(connection.getWarnings().getClass(),
132                      TransactionsUnsupportedWarning.class);
133         connection.clearWarnings();
134         assertNull(connection.getWarnings());
135     }
136 
137     public void testCreateStatementWithNonDefaultOptions() throws Exception {
138         try {
139             connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
140                                        ResultSet.CONCUR_READ_ONLY);
141             fail("ResultSet.TYPE_SCROLL_INSENSITIVE no esta soportado en esta version");
142         } catch (UnsupportedResultSetTypeException e) {}
143 
144         try {
145             connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
146                                        ResultSet.CONCUR_READ_ONLY);
147             fail("ResultSet.TYPE_SCROLL_SENSITIVE no esta soportado en esta version");
148         } catch (UnsupportedResultSetTypeException e) {}
149 
150         try {
151             connection.createStatement(ResultSet.TYPE_FORWARD_ONLY,
152                                        ResultSet.CONCUR_UPDATABLE);
153             fail("ResultSet.CONCUR_UPDATABLE no esta soportado en esta version");
154         } catch (UnsupportedResultSetConcurrencyModeException e) {}
155     }
156 
157     public void testPrepareStatementWithNonDefaultOptions() throws Exception {
158         try {
159             connection.prepareStatement("select * from t",
160                                         ResultSet.TYPE_SCROLL_INSENSITIVE,
161                                         ResultSet.CONCUR_READ_ONLY);
162             fail("ResultSet.TYPE_SCROLL_INSENSITIVE no esta soportado en esta version");
163         } catch (UnsupportedResultSetTypeException e) {}
164 
165         try {
166             connection.prepareStatement("select * from t",
167                                         ResultSet.TYPE_SCROLL_SENSITIVE,
168                                         ResultSet.CONCUR_READ_ONLY);
169             fail("ResultSet.TYPE_SCROLL_SENSITIVE no esta soportado en esta version");
170         } catch (UnsupportedResultSetTypeException e) {}
171 
172         try {
173             connection.prepareStatement("select * from t",
174                                         ResultSet.TYPE_FORWARD_ONLY,
175                                         ResultSet.CONCUR_UPDATABLE);
176             fail("ResultSet.CONCUR_UPDATABLE no esta soportado en esta version");
177         } catch (UnsupportedResultSetConcurrencyModeException e) {}
178     }
179 
180     public void testTestPrepareCall() throws Exception {
181         try {
182             connection.prepareCall("call");
183             fail("Esta version del servidor no soporta stored procedures");
184         } catch (StoredProceduresUnsupportedException e) {}
185     }
186 
187     public void testGetTypeMapAccessors() throws Exception {
188         Map typeMap = new HashMap();
189         assertTrue(connection.getTypeMap().isEmpty());
190         connection.setTypeMap(typeMap);
191         assertEquals(typeMap, connection.getTypeMap());
192     }
193 
194     public void testHoldability() throws Exception {
195         assertEquals(connection.getHoldability(), ResultSet.CLOSE_CURSORS_AT_COMMIT);
196         connection.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
197         assertEquals(connection.getHoldability(), ResultSet.HOLD_CURSORS_OVER_COMMIT);
198     }
199 
200     public void testSavepoint() throws Exception {
201         try {
202             connection.setSavepoint();
203             fail("Esta version del servidor no soporta transacciones");
204         } catch (TransactionsUnsupportedExeption e) {}
205 
206         try {
207             connection.setSavepoint("savePointName");
208             fail("Esta version del servidor no soporta transacciones");
209         } catch (TransactionsUnsupportedExeption e) {}
210     }
211 
212     public void testRollback() throws Exception {
213         try {
214             connection.rollback();
215             fail("Esta version del servidor no soporta transacciones");
216         } catch (TransactionsUnsupportedExeption e) {}
217     }
218 
219     public void testReleaseSavepoint() throws Exception {
220         try {
221             connection.releaseSavepoint(null);
222             fail("Esta version del servidor no soporta transacciones");
223         } catch (TransactionsUnsupportedExeption e) {}
224     }
225 }