use of cn.edu.zju.acm.onlinejudge.bean.Configuration in project zoj by licheng.
the class ConfigurationPersistenceImplTest method testGetConfigurations.
/**
* Tests getConfigurations method
* @throws Exception to JUnit
*/
public void testGetConfigurations() throws Exception {
ConfigurationPersistenceImpl persistence = new ConfigurationPersistenceImpl();
List configurations = persistence.getConfigurations();
assertEquals("size is wrong", 3, configurations.size());
Set nameSet = new HashSet(Arrays.asList(new String[] { "n1", "n2", "n3" }));
Set valueSet = new HashSet(Arrays.asList(new String[] { "v1", "v2", "v3" }));
Set descSet = new HashSet(Arrays.asList(new String[] { "d1", "d2", "d3" }));
for (Iterator it = configurations.iterator(); it.hasNext(); ) {
Configuration configuration = (Configuration) it.next();
assertTrue("wrong name", nameSet.contains(configuration.getName()));
assertTrue("wrong value", valueSet.contains(configuration.getValue()));
assertTrue("wrong description", descSet.contains(configuration.getDescription()));
nameSet.remove(configuration.getName());
valueSet.remove(configuration.getValue());
descSet.remove(configuration.getDescription());
}
}
use of cn.edu.zju.acm.onlinejudge.bean.Configuration in project zoj by licheng.
the class ConfigurationPersistenceImplTest method testSetConfigurations2.
/**
* Tests setConfigurations method
* @throws Exception to JUnit
*/
public void testSetConfigurations2() throws Exception {
List configurations = new ArrayList();
Configuration configuration1 = new Configuration();
configuration1.setName("n1");
configuration1.setValue("nv1");
configuration1.setDescription("nd1");
Configuration configuration4 = new Configuration();
configuration4.setName("n4");
configuration4.setValue("v4");
configuration4.setDescription("d4");
configurations.add(configuration1);
configurations.add(configuration4);
ConfigurationPersistenceImpl persistence = new ConfigurationPersistenceImpl();
persistence.setConfigurations(configurations, 1);
configurations = persistence.getConfigurations();
assertEquals("size is wrong", 4, configurations.size());
Set nameSet = new HashSet(Arrays.asList(new String[] { "n1", "n2", "n3", "n4" }));
Set valueSet = new HashSet(Arrays.asList(new String[] { "nv1", "v2", "v3", "v4" }));
Set descSet = new HashSet(Arrays.asList(new String[] { "nd1", "d2", "d3", "d4" }));
for (Iterator it = configurations.iterator(); it.hasNext(); ) {
Configuration configuration = (Configuration) it.next();
assertTrue("wrong name", nameSet.contains(configuration.getName()));
assertTrue("wrong value", valueSet.contains(configuration.getValue()));
assertTrue("wrong description", descSet.contains(configuration.getDescription()));
nameSet.remove(configuration.getName());
valueSet.remove(configuration.getValue());
descSet.remove(configuration.getDescription());
}
}
use of cn.edu.zju.acm.onlinejudge.bean.Configuration in project zoj by licheng.
the class ConfigurationPersistenceImplTest method setUp.
/**
* Setup.
* @throws Exception to JUnit
*/
protected void setUp() throws Exception {
DatabaseHelper.resetAllTables(false);
List configurations = new ArrayList();
Configuration configuration1 = new Configuration();
configuration1.setName("n1");
configuration1.setValue("v1");
configuration1.setDescription("d1");
Configuration configuration2 = new Configuration();
configuration2.setName("n2");
configuration2.setValue("v2");
configuration2.setDescription("d2");
Configuration configuration3 = new Configuration();
configuration3.setName("n3");
configuration3.setValue("v3");
configuration3.setDescription("d3");
configurations.add(configuration1);
configurations.add(configuration2);
configurations.add(configuration3);
new ConfigurationPersistenceImpl().setConfigurations(configurations, 1);
}
use of cn.edu.zju.acm.onlinejudge.bean.Configuration in project zoj by licheng.
the class ConfigurationPersistenceImplTest method testSetConfigurations1.
/**
* Tests setConfigurations method
* @throws Exception to JUnit
*/
public void testSetConfigurations1() throws Exception {
List configurations = new ArrayList();
Configuration configuration4 = new Configuration();
configuration4.setName("n4");
configuration4.setValue("v4");
configuration4.setDescription("d4");
Configuration configuration5 = new Configuration();
configuration5.setName("n5");
configuration5.setValue("v5");
configuration5.setDescription("d5");
configurations.add(configuration4);
configurations.add(configuration5);
ConfigurationPersistenceImpl persistence = new ConfigurationPersistenceImpl();
persistence.setConfigurations(configurations, 1);
configurations = persistence.getConfigurations();
assertEquals("size is wrong", 5, configurations.size());
Set nameSet = new HashSet(Arrays.asList(new String[] { "n1", "n2", "n3", "n4", "n5" }));
Set valueSet = new HashSet(Arrays.asList(new String[] { "v1", "v2", "v3", "v4", "v5" }));
Set descSet = new HashSet(Arrays.asList(new String[] { "d1", "d2", "d3", "d4", "d5" }));
for (Iterator it = configurations.iterator(); it.hasNext(); ) {
Configuration configuration = (Configuration) it.next();
assertTrue("wrong name", nameSet.contains(configuration.getName()));
assertTrue("wrong value", valueSet.contains(configuration.getValue()));
assertTrue("wrong description", descSet.contains(configuration.getDescription()));
nameSet.remove(configuration.getName());
valueSet.remove(configuration.getValue());
descSet.remove(configuration.getDescription());
}
}
use of cn.edu.zju.acm.onlinejudge.bean.Configuration in project zoj by licheng.
the class ConfigurationPersistenceImpl method setConfigurations.
/**
* <p>
* Stores the given list of Configuration instances to persistence layer.
* </p>
*
* @param configurations
* a list of Configuration instances to store
* @param user
* the id of the user who made this modification.
* @throws PersistenceException
* wrapping a persistence implementation specific exception
* @throws NullPointerException
* if configurations is null
* @throws IllegalArgumentException
* if configurations contains invalid or duplicate element
*/
public void setConfigurations(List<Configuration> configurations, long user) throws PersistenceException {
if (configurations.size() == 0) {
return;
}
Set<String> nameSet = new HashSet<String>();
for (Configuration configuration : configurations) {
String name = configuration.getName();
if (!nameSet.add(name)) {
throw new IllegalArgumentException("configurations contains duplicate element");
}
}
Connection conn = null;
try {
conn = Database.createConnection();
conn.setAutoCommit(false);
PreparedStatement ps = null;
Set<String> existingConfigurations = new HashSet<String>();
try {
// get existing configurations
ps = conn.prepareStatement(ConfigurationPersistenceImpl.GET_CONFIGURATION_NAMES + Database.createValues(nameSet));
ResultSet rs = ps.executeQuery();
while (rs.next()) {
existingConfigurations.add(rs.getString(DatabaseConstants.CONFIGURATION_NAME));
}
} finally {
Database.dispose(ps);
}
// update
for (Configuration configuration : configurations) {
try {
if (existingConfigurations.contains(configuration.getName())) {
ps = conn.prepareStatement(ConfigurationPersistenceImpl.UPDATE_CONFIGURATION);
ps.setString(5, configuration.getName());
ps.setString(1, configuration.getValue());
ps.setString(2, configuration.getDescription());
ps.setLong(3, user);
ps.setTimestamp(4, new Timestamp(new Date().getTime()));
} else {
ps = conn.prepareStatement(ConfigurationPersistenceImpl.INSERT_CONFIGURATION);
ps.setString(1, configuration.getName());
ps.setString(2, configuration.getValue());
ps.setString(3, configuration.getDescription());
ps.setLong(4, user);
ps.setTimestamp(5, new Timestamp(new Date().getTime()));
ps.setLong(6, user);
ps.setTimestamp(7, new Timestamp(new Date().getTime()));
}
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
}
conn.commit();
} catch (SQLException e) {
Database.rollback(conn);
throw new PersistenceException("Error.", e);
} finally {
Database.dispose(conn);
}
}
Aggregations