1616// under the License.
1717package com .cloud .upgrade .dao ;
1818
19+ import static org .mockito .Matchers .startsWith ;
1920import static org .mockito .Matchers .any ;
2021import static org .mockito .Matchers .anyString ;
2122import static org .mockito .Matchers .contains ;
2728
2829import java .sql .Connection ;
2930import java .sql .PreparedStatement ;
31+ import java .sql .ResultSet ;
3032import java .sql .SQLException ;
3133
3234import org .apache .log4j .Logger ;
35+ import org .junit .Assert ;
3336import org .junit .Before ;
3437import org .junit .Test ;
3538import org .junit .runner .RunWith ;
@@ -49,6 +52,9 @@ public class DatabaseAccessObjectTest {
4952 @ Mock
5053 private Logger loggerMock ;
5154
55+ @ Mock
56+ private ResultSet resultSetMock ;
57+
5258 private final DatabaseAccessObject dao = new DatabaseAccessObject ();
5359
5460 @ Before
@@ -83,6 +89,61 @@ public void testDropKeyWhenConnectionIsNull() throws Exception {
8389 dao .dropKey (conn , tableName , key , isForeignKey );
8490 }
8591
92+ @ Test
93+ public void generateIndexNameTest () {
94+ String indexName = dao .generateIndexName ("mytable" ,"mycolumn" );
95+ Assert .assertEquals ( "i_mytable__mycolumn" , indexName );
96+ }
97+
98+ @ Test
99+ public void indexExistsFalseTest () throws Exception {
100+ when (resultSetMock .next ()).thenReturn (false );
101+ when (connectionMock .prepareStatement (startsWith ("SHOW INDEXES FROM" ))).thenReturn (preparedStatementMock );
102+ when (preparedStatementMock .executeQuery ()).thenReturn (resultSetMock );
103+
104+ Connection conn = connectionMock ;
105+ String tableName = "mytable" ;
106+ String indexName = "myindex" ;
107+
108+ Assert .assertFalse (dao .indexExists (conn , tableName , indexName ));
109+ verify (connectionMock , times (1 )).prepareStatement (anyString ());
110+ verify (preparedStatementMock , times (1 )).executeQuery ();
111+ verify (preparedStatementMock , times (1 )).close ();
112+ }
113+
114+ @ Test
115+ public void indexExistsTrueTest () throws Exception {
116+ when (resultSetMock .next ()).thenReturn (true );
117+ when (connectionMock .prepareStatement (startsWith ("SHOW INDEXES FROM" ))).thenReturn (preparedStatementMock );
118+ when (preparedStatementMock .executeQuery ()).thenReturn (resultSetMock );
119+
120+ Connection conn = connectionMock ;
121+ String tableName = "mytable" ;
122+ String indexName = "myindex" ;
123+
124+ Assert .assertTrue (dao .indexExists (conn , tableName , indexName ));
125+ verify (connectionMock , times (1 )).prepareStatement (anyString ());
126+ verify (preparedStatementMock , times (1 )).executeQuery ();
127+ verify (preparedStatementMock , times (1 )).close ();
128+ }
129+
130+ @ Test
131+ public void createIndexTest () throws Exception {
132+ when (connectionMock .prepareStatement (startsWith ("CREATE INDEX" ))).thenReturn (preparedStatementMock );
133+ when (preparedStatementMock .execute ()).thenReturn (true );
134+
135+ Connection conn = connectionMock ;
136+ String tableName = "mytable" ;
137+ String columnName = "mycolumn" ;
138+ String indexName = "myindex" ;
139+
140+ dao .createIndex (conn , tableName , columnName , indexName );
141+ verify (connectionMock , times (1 )).prepareStatement (anyString ());
142+ verify (preparedStatementMock , times (1 )).execute ();
143+ verify (preparedStatementMock , times (1 )).close ();
144+ verify (loggerMock , times (1 )).debug ("Created index myindex" );
145+ }
146+
86147 @ Test
87148 public void testDropKeyWhenTableNameIsNull () throws Exception {
88149 SQLException sqlException = new SQLException ();
0 commit comments