/* simple test of getDataSource() which assumes addDataSource() works. */
  public void testGetDataSource() {
    SPDataSource dbcs = new JDBCDataSource(target);
    dbcs.setName("cows");
    target.addDataSource(dbcs);

    SPDataSource gotDbcs = target.getDataSource("cows");
    assertNotNull(gotDbcs);
    assertSame(dbcs, gotDbcs);
  }
  /*
   * Test method for 'ca.sqlpower.architect.PlDotIni.write(OutputStream)'
   */
  public void testWriteOutputStream() throws IOException {
    String orig = makePlIniString();
    InputStream in = makeInputStream(orig);
    target.read(in);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    target.write(out);
    assertEquals(orig, out.toString());
  }
  public void testSQLTypeRead() throws Exception {
    testRead();
    UserDefinedSQLType sqlType = target.getSQLType("VARCHAR");
    assertNotNull(sqlType);
    assertEquals("VARCHAR", sqlType.getName());
    assertEquals(BasicSQLType.TEXT, sqlType.getBasicType());
    assertEquals("Test Type", sqlType.getDescription());
    assertEquals(12, (int) sqlType.getType());

    UserDefinedSQLType sqlTypeInList = target.getSQLTypes().get(0);
    assertEquals(sqlType, sqlTypeInList);
  }
  /**
   * Tests that any items coming before the first section get written out when we re-save the file.
   */
  public void testNotLoseItemsWithoutSection() throws Exception {
    testRead();

    File tmp2 = File.createTempFile("pl.out", null);
    target.write(tmp2);

    PlDotIni reread = new PlDotIni();
    reread.read(tmp2);
    Object sect = reread.getSection(0);
    assertEquals(PlDotIni.Section.class, sect.getClass());
    assertNull(((PlDotIni.Section) sect).getName());
  }
  /* Ensures that the read() method parses data source types properly,
   * assuming getDataSourceType() works.
   */
  public void testReadDataSourceType() throws IOException {
    testRead();

    JDBCDataSourceType dst = target.getDataSourceType("my db type");
    assertNotNull(dst);
    assertEquals(dst.getProperty("silly property"), "diddle");

    JDBCDataSourceType dst2 = target.getDataSourceType("my other db type");
    assertNotNull(dst2);
    assertEquals(dst2.getProperty("silly property"), "fiddle");
    assertSame(dst, dst2.getParentType());
  }
  public void testReRead() throws IOException {
    target.read(makeInputStream(makePlIniString()));

    String orig = plIniToString(target);

    // re-reading should just merge in all the same stuff and leave object unchanged
    target.read(makeInputStream(makePlIniString()));

    String after = plIniToString(target);

    assertEquals(orig, after);
  }
  public void testDatabaseTypeParentDoesntExist() throws Exception {
    testRead();

    JDBCDataSourceType dbType = target.getDataSourceType("my other db type");
    dbType.putProperty(JDBCDataSourceType.PARENT_TYPE_NAME, "non existant parent");

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    target.write(out);

    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    try {
      target.read(in);
      fail("Parent type didn't exist but no exception was thrown.");
    } catch (IllegalStateException ex) {
      // test passed
    }
  }
 private static String plIniToString(PlDotIni plini) {
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < plini.getSectionCount(); i++) {
     Object o = plini.getSection(i);
     if (o instanceof PlDotIni.Section) {
       sb.append(((PlDotIni.Section) o).getPropertiesMap().toString());
     } else if (o instanceof SPDataSource) {
       sb.append(((SPDataSource) o).getPropertiesMap().toString());
     } else if (o instanceof JDBCDataSourceType) {
       sb.append(((JDBCDataSourceType) o).getProperties().toString());
     } else if (o instanceof UserDefinedSQLType) {
       sb.append(plini.createSQLTypePropertiesMap(((UserDefinedSQLType) o)).toString());
     } else {
       throw new IllegalArgumentException("Unknown pl.ini section type: " + o);
     }
     sb.append("\n");
   }
   return sb.toString();
 }
  public void testRemoveDsType() {
    final JDBCDataSourceType newType = new JDBCDataSourceType();
    target.addDataSourceType(newType);
    int oldCount = target.getDataSourceTypes().size();

    assertFalse(target.removeDataSourceType(null));
    assertEquals(oldCount, target.getDataSourceTypes().size());

    assertFalse(target.removeDataSourceType(new JDBCDataSourceType()));
    assertEquals(oldCount, target.getDataSourceTypes().size());

    assertTrue(target.removeDataSourceType(newType));
    assertEquals(oldCount - 1, target.getDataSourceTypes().size());
  }
  /* ensures we read all sections in the correct order */
  public void testReadSections() throws Exception {
    testRead();
    assertEquals(6, target.getSectionCount());

    // the nameless first section
    Object s = target.getSection(0);
    assertEquals(PlDotIni.Section.class, s.getClass());
    assertEquals(null, ((PlDotIni.Section) s).getName());

    s = target.getSection(1);
    assertEquals(JDBCDataSourceType.class, s.getClass());

    s = target.getSection(2);
    assertEquals(JDBCDataSourceType.class, s.getClass());

    s = target.getSection(3);
    assertEquals(PlDotIni.Section.class, s.getClass());

    s = target.getSection(4);
    assertEquals(JDBCDataSource.class, s.getClass());

    s = target.getSection(5);
    assertEquals(UserDefinedSQLType.class, s.getClass());
  }
 /*
  * Test method for 'ca.sqlpower.architect.PlDotIni.read(File)'
  */
 public void testRead() throws IOException {
   target.read(makeInputStream(makePlIniString()));
   assertTrue(target.getConnections().size() > 0);
 }
 /** Ensures every data source child of this pl.ini has the right parent pointer. */
 public void testParentOfDataSources() throws Exception {
   testRead();
   for (SPDataSource ds : target.getConnections()) {
     assertSame(target, ds.getParentCollection());
   }
 }
 public void testAddDsType() {
   int oldCount = target.getDataSourceTypes().size();
   target.addDataSourceType(new JDBCDataSourceType());
   assertEquals(oldCount + 1, target.getDataSourceTypes().size());
 }
 public void testHookUpDataSourceToParentType() throws Exception {
   testRead();
   JDBCDataSource ds = target.getDataSource(FUN_DATASOURCE_NAME, JDBCDataSource.class);
   assertNotNull(ds);
   assertNotNull(ds.getParentType());
 }
 /*
  * Test method for 'ca.sqlpower.architect.PlDotIni.getConnections()'
  */
 public void testGetConnections() throws IOException {
   testRead();
   List l = target.getConnections();
   assertEquals(1, l.size());
   assertEquals(target.getDataSource(FUN_DATASOURCE_NAME), l.get(0));
 }
 /*
  * Ensures the read() method reads data sources properly,
  * assuming getDataSource() works properly
  */
 public void testReadDataSource() throws IOException {
   testRead();
   SPDataSource ds = target.getDataSource(FUN_DATASOURCE_NAME);
   assertNotNull(ds);
 }