package com.justjournal.db;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import org.apache.cayenne.access.DataContext;
import org.apache.cayenne.configuration.server.ServerRuntime;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import static org.junit.Assert.assertEquals;

public class LocationDaoTest {
    @BeforeClass
    public static void setUpClass() throws Exception {
        try {
            // Create initial context
            System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                    "org.apache.naming.java.javaURLContextFactory");
            System.setProperty(Context.URL_PKG_PREFIXES,
                    "org.apache.naming");
            InitialContext ic = new InitialContext();

            ic.createSubcontext("java:");
            ic.createSubcontext("java:comp");
            ic.createSubcontext("java:comp/env");
            ic.createSubcontext("java:comp/env/jdbc");

            // Construct DataSource
            MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
            ds.setURL("jdbc:mysql://ds9.midnightbsd.org:3306/jj");
            ds.setUser("jj");
            ds.setPassword("");

            ic.bind("java:comp/env/jdbc/jjDB", ds);

            ServerRuntime cayenneRuntime = new ServerRuntime("cayenne-JustJournalDomain.xml");
            DataContext.bindThreadObjectContext(cayenneRuntime.getContext());
        } catch (NamingException ex) {
            System.err.println(ex.getMessage());
        }

    }

    @Test
    public void testView() throws Exception {
        assertEquals(5, LocationDao.view().size());
    }
}
@@ -22,16 +22,45 @@
 al.db;%0A%0A
+import com.justjournal.Util;%0A
 import c
@@ -119,16 +119,16 @@
 Source;%0A
-
 import o
@@ -533,15 +533,10 @@
  set
-UpClass
+up
 () t
@@ -565,1107 +565,23 @@
     
-try %7B%0A            // Create initial context%0A            System.setProperty(Context.INITIAL_CONTEXT_FACTORY,%0A                    %22org.apache.naming.java.javaURLContextFactory%22);%0A            System.setProperty(Context.URL_PKG_PREFIXES,%0A                    %22org.apache.naming%22);%0A            InitialContext ic = new InitialContext();%0A%0A            ic.createSubcontext(%22java:%22);%0A            ic.createSubcontext(%22java:comp%22);%0A            ic.createSubcontext(%22java:comp/env%22);%0A            ic.createSubcontext(%22java:comp/env/jdbc%22);%0A%0A            // Construct DataSource%0A            MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();%0A            ds.setURL(%22jdbc:mysql://ds9.midnightbsd.org:3306/jj%22);%0A            ds.setUser(%22jj%22);%0A            ds.setPassword(%22%22);%0A%0A            ic.bind(%22java:comp/env/jdbc/jjDB%22, ds);%0A%0A            ServerRuntime cayenneRuntime = new ServerRuntime(%22cayenne-JustJournalDomain.xml%22);%0A            DataContext.bindThreadObjectContext(cayenneRuntime.getContext());%0A        %7D catch (NamingException ex) %7B%0A            System.err.println(ex.getMessage());%0A        %7D%0A
+Util.setupDb();
 %0A   

use util version