### Question:
DatabaseMetadataUtil { public static DatabaseMetadata getMetadata( Connection conn, boolean includeCatalogs, boolean includeSchemas ) throws Exception { try { DatabaseMetadata result = new DatabaseMetadata( ); ResultSet rs; DatabaseMetaData sqlMetadata = conn.getMetaData( ); result.setDatabaseType( DatabaseMetadataUtil.getDatabaseType( sqlMetadata.getDatabaseProductName() ) ); result.setDatabaseProductName( sqlMetadata.getDatabaseProductName( ) ); result.setDatabaseProductVersion( sqlMetadata.getDatabaseProductVersion( ) ); result.setDriverName( sqlMetadata.getDriverName( ) ); result.setDriverVersion( sqlMetadata.getDriverVersion( ) ); result.setDriverMajorVersion( sqlMetadata.getDriverMajorVersion() ); result.setDriverMinorVersion( sqlMetadata.getDriverMinorVersion( ) ); if ( includeCatalogs ) { List< CatalogMetadata > catalogs = new ArrayList<>( ); rs = sqlMetadata.getCatalogs( ); while ( rs.next( ) ) { catalogs.add( new CatalogMetadata( rs.getString( "TABLE_CAT" ) ) ); } rs.close( ); result.setCatalogs( catalogs ); } if ( includeSchemas ) { List< SchemaMetadata > schemas = new ArrayList<>( ); rs = sqlMetadata.getSchemas( ); while ( rs.next( ) ) { schemas.add( new SchemaMetadata( rs.getString( "TABLE_CATALOG" ), rs.getString( "TABLE_SCHEM" ) ) ); } rs.close( ); result.setSchemas( schemas ); } return result; } catch ( Exception e ) { throw new Exception( "It was not possible to read database metadata due to the following error: " + e.getMessage( ) ); } finally { try { conn.close( ); } catch ( Exception e ) { } } }  static DatabaseMetadata getMetadata( Connection conn,
                                                boolean includeCatalogs,
                                                boolean includeSchemas ); static List< TableMetadata > findTables( Connection conn,
                                                    String schema,
                                                    String tableNamePattern,
                                                    DatabaseMetadata.TableType... types ); static DatabaseMetadata.DatabaseType getDatabaseType( String dbProductName );  }### Answer:
@Test public void testGetMetadata( ) throws Exception { when( conn.getMetaData( ) ).thenReturn( sqlDatabaseMetaData ); when( sqlDatabaseMetaData.getDatabaseProductName( ) ).thenReturn( DATA_BASE_PRODUCT_NAME ); when( sqlDatabaseMetaData.getDatabaseProductVersion( ) ).thenReturn( DATA_BASE_PRODUCT_VERSION ); when( sqlDatabaseMetaData.getDriverName( ) ).thenReturn( DRIVER_NAME ); when( sqlDatabaseMetaData.getDriverVersion( ) ).thenReturn( DRIVER_VERSION ); when( sqlDatabaseMetaData.getDriverMajorVersion( ) ).thenReturn( DRIVER_MAJOR_VERSION ); when( sqlDatabaseMetaData.getDriverMinorVersion( ) ).thenReturn( DRIVER_MINOR_VERSION ); when( sqlDatabaseMetaData.getCatalogs( ) ).thenReturn( catalogsRs ); when( sqlDatabaseMetaData.getSchemas( ) ).thenReturn( schemasRs ); DatabaseMetadata result = DatabaseMetadataUtil.getMetadata( conn, true, true ); assertEquals( databaseMetadata, result ); assertTrue( catalogsRs.isClosed( ) ); assertTrue( schemasRs.isClosed( ) ); verify( conn, times( 1 ) ).close( ); }