DAO Medge Code Examples
Back to the Main Page

Load Connector

Save/Load connector information from a file.

package au.id.medge.daotester.testsuite;

import au.id.medge.dao.adapters.MySQLAdapter;
import au.id.medge.dao.utilities.DAOConnector;
import au.id.medge.dao.utilities.DAOException;

public class LoadTest extends AbstractTestSuite {

    private static final String USERNAME = "USERNAME";
    public static String PASSWORD = "PASSWORD";

    public LoadTest() {
        super("LoadSave");
    }

    private DAOConnector getConnectorToSave() {
        DAOConnector connector = new DAOConnector();
        connector.setServerName("Test");
        connector.setDriver("org.mariadb.jdbc.Driver");
        connector.setUrl("jdbc:mysql://ginny:3306/home");
        connector.setDatabaseAdapter(new MySQLAdapter());
        connector.setUser(USERNAME);
        connector.setPassword(PASSWORD);
        connector.setServerDescription("Testing");
        connector.setRetainConnection(true);
        connector.setPreread(true);
        connector.setPostread(true);
        return connector;
    }

    private DAOConnector getConnectorToLoad() {
        DAOConnector connector = new DAOConnector();
        return connector;
    }

    private void testSave() {
        try {
            getConnectorToSave().saveTo("test.xml");
            reportSuccess("Saved connector to XML");
        } catch (DAOException ex) {
            reportFailure("Failed to save connector to XML", ex);
        }
        try {
            getConnectorToSave().saveTo("test.properties");
            reportSuccess("Saved connector to properties");
        } catch (DAOException ex) {
            reportFailure("Failed to save connector to properties", ex);
        }
    }

    private void testLoad() {
        try {
            DAOConnector connector = getConnectorToLoad();
            connector.loadFrom("test.xml");
            if (connector.getPassword() != null) {
                reportFailure("Password was set somewhere");
            }
            if (!connector.isPostread()) {
                reportFailure("Post read should be set");
            }
        } catch (DAOException ex) {
            reportFailure("Failed to load connector from XML", ex);
        }
        try {
            getConnectorToLoad().loadFrom("test.properties");
        } catch (DAOException ex) {
            reportFailure("Failed to load connector from properties", ex);
        }

    }

    @Override
    public void runTestSuite() {
        testSave();
        testLoad();
    }
Back to the Main Page