import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;
import com.mysql.jdbc.util.BaseBugReport;

/**
 * Tests conversion of Well Known Text to MySql geometry and back
 */
public class GeometryBugReport extends BaseBugReport {
    
    public void runTest() throws Exception {
        Connection c = getConnection();
        PreparedStatement toGeom = c.prepareStatement("select GeomFromText(?)");
        PreparedStatement toText = c.prepareStatement("select AsText(?)");
        Statement s = c.createStatement();
        String inText = "POINT(146.67596278 -36.54368233)";
        // First assert that the problem is not at the server end
        ResultSet rs = s.executeQuery("select AsText(GeomFromText('" + inText + "'))");
        rs.next();
        String outText = rs.getString(1);
        rs.close();
        assertTrue("Server side only\n In: " + inText + "\nOut: " + outText, inText.equals(outText));
        // Now bring a binary geometry object to the client and send it back
        toGeom.setString(1, inText);
        rs = toGeom.executeQuery();
        rs.next();
        // Return a binary geometry object from the WKT
        Object geom = rs.getObject(1);
        rs.close();
        toText.setObject(1, geom);
        rs = toText.executeQuery();
        rs.next();
        // Return WKT from the binary geometry
        outText = rs.getString(1);
        rs.close();
        assertTrue("Server to client and back\n In: " + inText + "\nOut: " + outText, inText.equals(outText));
    }
    public void setUp() throws Exception {}
    public void tearDown() throws Exception {}
    
    public static void main(String[] args) {
        try {
            new GeometryBugReport().run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
