DAO Medge Code Examples
Back to the Main Page

Main Class

The main class, with public static final void main(String[] args)!

package au.id.medge.daotester;

import au.id.medge.daotester.testsuite.AbstractTestSuite;
import au.id.medge.daotester.testsuite.DefaultOrderByTest;
import au.id.medge.daotester.testsuite.ExecuteTest;
import au.id.medge.daotester.testsuite.FullSelectTest;
import au.id.medge.daotester.testsuite.IntegerAsStringTest;
import au.id.medge.daotester.testsuite.LoadTest;
import au.id.medge.daotester.testsuite.MultiKeyTests;
import au.id.medge.daotester.testsuite.MultipleTableSelectTests;
import au.id.medge.daotester.testsuite.UpdateTests;
import java.util.ArrayList;
import java.util.List;

public class DAOTester {
    private final List<AbstractTestSuite> tests = new ArrayList<>();

    private DAOTester() {        
        tests.add(new DefaultOrderByTest());
        tests.add(new MultipleTableSelectTests());        
        tests.add(new UpdateTests());
        tests.add(new LoadTest());
        tests.add(new ExecuteTest());        
        tests.add(new MultiKeyTests());        
        tests.add(new IntegerAsStringTest());        
        tests.add(new FullSelectTest());
    }

    private void runAllTests() {
        for (AbstractTestSuite at : tests) {
            at.runTestSuite();
        }
    }

    public static void main(String[] args) {
        DAOTester daoTester = new DAOTester();
        daoTester.runAllTests();
    }

}

DAO/DAOConnector Class

The DAO builder DAOConnector builder class

package au.id.medge.daotester.dao;

import au.id.medge.dao.DAO;
import au.id.medge.dao.adapters.MySQLAdapter;
import au.id.medge.dao.utilities.DAOConnector;
import au.id.medge.daotester.DAOTester;
import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class DAOTesterDAO {
    
    public static final String USER_NAME = "USERNAME";
    public static final String PASSWORD = "PASSWORD";
    
    private static final String LOGGER_FILE = "log.results.log";
    private static final long LOG_SIZE = 1024 * 1024 * 100;

    public static Logger LOGGER;

    private final DAO dao;

    // User name is NOT the log on user name that is USERNAME
    public DAOTesterDAO(String userName) {
        dao = new DAO(buildConnector(), userName);
    }

    private DAOConnector buildConnector() {
        DAOConnector connector = new DAOConnector();
        connector.setServerName("Test");
        connector.setDriver("org.mariadb.jdbc.Driver");
        connector.setUrl("jdbc:mysql://server:3306/db");
        connector.setDatabaseAdapter(new MySQLAdapter());
        connector.setUser(USER_NAME);
        connector.setPassword(PASSWORD);
        connector.setServerDescription("Testing");
        connector.setRetainConnection(true);
        connector.setPreread(true);
        connector.setPostread(true);
        connector.setLogger(buildLogger());
        return connector;
    }

    public synchronized DAO getDAO() {
        return dao;
    }
    
    private Logger buildLogger() {
        if (LOGGER == null) {
            System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT.%1$tL] [%4$-7s] %5$s%6$s%n");
            LOGGER = Logger.getLogger("TestLogger");
            File file = new File(LOGGER_FILE);
            try {
                for(Handler handler : LOGGER.getHandlers()) {
                    LOGGER.removeHandler(handler);
                }
                LOGGER.setUseParentHandlers(false);
                Handler fileHandler = new FileHandler(file.getCanonicalPath(), LOG_SIZE, 10, true);

                SimpleFormatter sf = new SimpleFormatter();
                fileHandler.setFormatter(sf);
                LOGGER.addHandler(fileHandler);
            } catch(IOException ex) {
                ex.printStackTrace(System.err);
            }
            LOGGER.setLevel(Level.INFO);
        }
        return LOGGER;
    }

}
Back to the Main Page