import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XMLResource;

/**
 * @author Ivan Lagunov
 */
public class TestEmptyXmlBug {
    private static final String DB_DRIVER_CLASSNAME = "net.cfoster.sedna.DatabaseImpl";
    private static final String DB_URL = "xmldb:sedna://localhost/test";
    private static final String DB_USERNAME = "SYSTEM";
    private static final String DB_PASSWORD = "MANAGER";

    private Collection rootCollection;

    public static void main(String[] args) throws Exception {
        TestEmptyXmlBug test = new TestEmptyXmlBug();

        test.setUp();
        test.test();
        test.tearDown();
    }

    public void setUp() throws Exception {
        Class<?> c = Class.forName(DB_DRIVER_CLASSNAME);
        Database database = (Database) c.newInstance();
        DatabaseManager.registerDatabase(database);
        rootCollection = DatabaseManager.getCollection(DB_URL, DB_USERNAME, DB_PASSWORD);
    }

    public void tearDown() throws XMLDBException {
        rootCollection.close();
    }

    public void test() throws XMLDBException {
        uploadXml(rootCollection, "test.xml", "");
        uploadXml(rootCollection, "test.xml", "<test/>");
    }

    public void uploadXml(Collection collection, String xmlName, String xmlContents) throws XMLDBException {
        XMLResource resource = (XMLResource) collection.createResource(xmlName, XMLResource.RESOURCE_TYPE);
        resource.setContent(xmlContents);
        collection.storeResource(resource);
    }
}
