1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.service;
import com.Mobile;
import com.Exception;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.easymock.EasyMock;
import org.easymock.internal.MockInvocationHandler;
import org.hsqldb.jdbc.jdbcDataSource;
import org.hsqldb.util.DatabaseManager;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
*
* @author coolcoolcool
*/
public class ServiceTest {
private Service as;
private static final String jndi = "jndi/test";
@Before
public void setUp () throws NamingException, SQLException {
as = new Service();
createJNDIContext();
createDBTable();
}
@After
public void tearDown () {
}
@Test
public void testMobiles () throws Exception {
List<Mobile> liste = null;
try {
liste = as.getMobiles();
EasyMock.expect(as.getMobiles()).andStubReturn(liste);
} catch (Exception e) {
e.getMessage());
}
// Je fais mes assertions ici
}
public static void createJNDIContext () throws NamingException {
DatabaseManager dm = new DatabaseManager();
jdbcDataSource source = new jdbcDataSource();
source.setDatabase("jdbc:hsqldb:mem:mymemdb");
source.setUser("SA");
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, MockInvocationHandler.class.getName());
Context ic = new InitialContext();
ic.bind(jndiName, source);
}
private static void createDBTable () throws NamingException, SQLException {
DataSource ds = null;
Connection conn = null;
try {
ds = (DataSource) new InitialContext().lookup(jndi);
conn = ds.getConnection();
String create = "CREATE TABLE LISTETABLE ("
+ "table1, table2, table3"
+ ")"; // mettre la requête par ici.
Statement stmt = conn.createStatement();
stmt.executeUpdate(create);
} catch (Exception e) {
} finally {
if ( conn != null) {
conn.close();
}
}
}
} |
Partager