public void testOverride() throws Exception { Subclasser sub = new Subclasser(); String actual = sub.overrideConnection(); String expected = "" + sub.NEWLINE + sub.INDENT + "public Connection getConnection() throws SQLException {" + sub.NEWLINE + sub.INDENT + sub.INDENT + "return P6SpyDriverCore.wrapConnection(super.getConnection());" + sub.NEWLINE + sub.INDENT + "};" + sub.NEWLINE + sub.NEWLINE + sub.INDENT + "public Connection getConnection(String username, String password) throws SQLException {" + sub.NEWLINE + sub.INDENT + sub.INDENT + "return P6SpyDriverCore.wrapConnection(super.getConnection(username, password));" + sub.NEWLINE + sub.INDENT + "};" + sub.NEWLINE + ""; assertEquals(expected, actual); }
protected void compareFiles(Subclasser sub) throws Exception { sub.createSubClass(); String name = sub.getOutputName(); File expected = new File("etc", name + ".java"); File actual = sub.getOutputFile(); compareFiles(expected, actual); }
public void testBadClass() throws Exception { final Subclasser sub = new Subclasser(); // Protectable is JUnit class Protectable p = new Protectable() { public void protect() throws Exception { sub.createSubClass(); } }; chkException(p, "must not be null"); sub.setParentClass(this.getClass()); chkException(p, "instanceof javax.sql.DataSource"); }
public void testWriteConstructors() throws Exception { final Subclasser sub = new Subclasser(); Protectable p = new Protectable() { public void protect() throws Exception { sub.writeConstructors(); } }; chkException(p, null); sub.setParentClass(javax.sql.DataSource.class); chkException(p, "interface"); sub.setParentClass(com.p6spy.engine.spy.P6DataSource.class); String expected = "" + sub.NEWLINE + sub.INDENT + "public P6P6DataSource (javax.sql.DataSource p0) {" + sub.NEWLINE + sub.INDENT + sub.INDENT + "super( p0);" + sub.NEWLINE + sub.INDENT + "}" + sub.NEWLINE + sub.INDENT + "public P6P6DataSource () {" + sub.NEWLINE + sub.INDENT + sub.INDENT + "super();" + sub.NEWLINE + sub.INDENT + "}" + ""; String actual = sub.writeConstructors(); assertEquals(expected, actual); // sometimes JUnit truncs the strings... if that's the case with your // version, then you can uncomment this line to get out own // full listing of the strings in their variance // assertEquals("Expected constructor like: " + sub.NEWLINE + expected + sub.NEWLINE + " but // found " + sub.NEWLINE + actual + sub.NEWLINE, expected, actual); }
public void testWriteHeader() throws Exception { final Subclasser sub = new Subclasser(); // Protectable is JUnit class Protectable p = new Protectable() { public void protect() throws Exception { sub.writeHeader(); } }; chkException(p, null); sub.setParentClass(com.p6spy.engine.spy.P6DataSource.class); String actual = sub.writeHeader(); // com.p6spy.engine.spy is imported twice since // we're sublcassing the p6spy datasource. String expected = "" + "// this class generated by class com.p6spy.engine.common.Subclasser" + sub.NEWLINE + sub.NEWLINE + "package " + sub.DEFAULT_PACKAGE + ";" + sub.NEWLINE + sub.NEWLINE + "import com.p6spy.engine.spy.*;" + sub.NEWLINE + "import java.sql.*;" + sub.NEWLINE + "import javax.sql.*;" + sub.NEWLINE + "import com.p6spy.engine.spy.*;" + sub.NEWLINE + sub.NEWLINE + sub.NEWLINE + "public class P6P6DataSource extends com.p6spy.engine.spy.P6DataSource {" + sub.NEWLINE + ""; assertEquals(sub.NEWLINE + expected, sub.NEWLINE + actual); }
public void testString() throws Exception { Subclasser sub = new Subclasser(); Class clazz = this.getClass(); String packageName = clazz.getPackage().getName(); String className = clazz.getName(); // NB, this will all break if you change the package structure :) // pretty unlikely, but just in case it starts to fail, keep it in mind String expectedName = "P6TestSubclasser"; assertEquals(expectedName, sub.baseName(className)); // this one's a little trickier... since it could fail // depending on your architecture. So put in this terrible // switchlike hack here String expectedPath = null; if (sub.DELIMITER.equals("/")) { expectedPath = "com/p6spy/engine/test"; } else if (sub.DELIMITER.equals("\\")) { expectedPath = "com\\p6spy\\engine\\test"; } else if (sub.DELIMITER.equals(":")) { expectedPath = "com:p6spy:engine:test"; } if (expectedPath == null) { fail( "Unexpected file separator: " + sub.DELIMITER + ". Please expand the test class to test for this file separator."); } assertEquals(expectedPath, sub.packToDir(packageName)); // now check the default file String newName = "ThisIsATestClass"; sub.setOutputPackage(packageName); sub.setOutputName(newName); File actualFile = sub.getOutputFile(); File expectedFile = new File("scratch" + sub.DELIMITER + expectedPath, newName + ".java"); assertEquals(expectedFile, actualFile); }