@Test
  public void testConnProperties() throws Exception {
    Map<String, String> connProperties = new HashMap<>();
    connProperties.put("user", "postgres");
    connProperties.put("password", "");

    String uri = PgSQLTestServer.getInstance().getJdbcUrl().split("\\?")[0];
    Tablespace space =
        new PgSQLTablespace("t1", URI.create(uri), getJsonTablespace(connProperties));
    try {
      space.init(new TajoConf());
    } finally {
      space.close();
    }
  }
  @Test
  public void testConnPropertiesNegative() throws Exception {
    Map<String, String> connProperties = new HashMap<>();
    connProperties.put("user", "postgresX");
    connProperties.put("password", "");

    String uri = PgSQLTestServer.getInstance().getJdbcUrl().split("\\?")[0];
    Tablespace space =
        new PgSQLTablespace("t1", URI.create(uri), getJsonTablespace(connProperties));
    try {
      space.init(new TajoConf());
      fail("Must be failed");
    } catch (IOException ioe) {
      assertTrue(ioe.getCause() instanceof PSQLException);
    } finally {
      space.close();
    }
  }
public class TestPgSQLJdbcTableSpace {
  static String jdbcUrl = PgSQLTestServer.getInstance().getJdbcUrlForAdmin();

  @Test(timeout = 1000)
  public void testTablespaceHandler() throws Exception {
    assertTrue((TablespaceManager.getByName("pgsql_cluster")) instanceof PgSQLTablespace);
    assertEquals("pgsql_cluster", (TablespaceManager.getByName("pgsql_cluster").getName()));

    assertTrue((TablespaceManager.get(jdbcUrl)) instanceof PgSQLTablespace);
    assertTrue((TablespaceManager.get(jdbcUrl + "&table=tb1")) instanceof PgSQLTablespace);

    assertEquals(jdbcUrl, TablespaceManager.get(jdbcUrl).getUri().toASCIIString());
    assertTrue(
        TablespaceManager.get(jdbcUrl).getMetadataProvider() instanceof PgSQLMetadataProvider);
  }

  @Test(timeout = 1000, expected = TajoRuntimeException.class)
  public void testCreateTable() throws IOException, TajoException {
    Tablespace space = TablespaceManager.getByName("pgsql_cluster");
    space.createTable(null, false);
  }

  @Test(timeout = 1000, expected = TajoRuntimeException.class)
  public void testDropTable() throws IOException, TajoException {
    Tablespace space = TablespaceManager.getByName("pgsql_cluster");
    space.purgeTable(null);
  }

  @Test(timeout = 1000)
  public void testGetSplits() throws IOException, TajoException {
    Tablespace space = TablespaceManager.getByName("pgsql_cluster");
    MetadataProvider provider = space.getMetadataProvider();
    TableDesc table = provider.getTableDesc(null, "lineitem");
    List<Fragment> fragments = space.getSplits("lineitem", table, null);
    assertNotNull(fragments);
    assertEquals(1, fragments.size());
  }

  @Test
  public void testConnProperties() throws Exception {
    Map<String, String> connProperties = new HashMap<>();
    connProperties.put("user", "postgres");
    connProperties.put("password", "");

    String uri = PgSQLTestServer.getInstance().getJdbcUrl().split("\\?")[0];
    Tablespace space =
        new PgSQLTablespace("t1", URI.create(uri), getJsonTablespace(connProperties));
    try {
      space.init(new TajoConf());
    } finally {
      space.close();
    }
  }

  @Test
  public void testConnPropertiesNegative() throws Exception {
    Map<String, String> connProperties = new HashMap<>();
    connProperties.put("user", "postgresX");
    connProperties.put("password", "");

    String uri = PgSQLTestServer.getInstance().getJdbcUrl().split("\\?")[0];
    Tablespace space =
        new PgSQLTablespace("t1", URI.create(uri), getJsonTablespace(connProperties));
    try {
      space.init(new TajoConf());
      fail("Must be failed");
    } catch (IOException ioe) {
      assertTrue(ioe.getCause() instanceof PSQLException);
    } finally {
      space.close();
    }
  }

  public static JSONObject getJsonTablespace(Map<String, String> connProperties)
      throws IOException {
    JSONObject configElements = new JSONObject();
    configElements.put(JdbcTablespace.CONFIG_KEY_MAPPED_DATABASE, PgSQLTestServer.DATABASE_NAME);

    JSONObject connPropertiesJson = new JSONObject();
    for (Map.Entry<String, String> entry : connProperties.entrySet()) {
      connPropertiesJson.put(entry.getKey(), entry.getValue());
    }
    configElements.put(JdbcTablespace.CONFIG_KEY_CONN_PROPERTIES, connPropertiesJson);

    return configElements;
  }

  @Test
  public void testTemporaryTablespace() {
    Optional<Tablespace> ts = TablespaceManager.removeTablespaceForTest("pgsql_cluster");
    assertTrue(ts.isPresent());

    Tablespace tempTs = TablespaceManager.get(jdbcUrl);
    assertNotNull(tempTs);

    TablespaceManager.addTableSpaceForTest(ts.get());
  }
}