Fix auto commit issues. Fix null issues. Add test.

This commit is contained in:
Christian P. MOMON 2013-09-02 18:41:18 +02:00
parent adf0f3a86c
commit 99377b9f8f
16 changed files with 1146 additions and 270 deletions

View file

@ -4,7 +4,7 @@
<classpathentry kind="src" path="test"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.1.jar"/>
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
<classpathentry kind="lib" path="lib/junit-4.11.jar"/>
<classpathentry kind="lib" path="lib/junit-4.11.jar" sourcepath="lib/junit-4.11-sources.jar"/>
<classpathentry kind="lib" path="lib/log4j-1.2.17.jar"/>
<classpathentry kind="lib" path="lib/slf4j-api-1.7.5.jar"/>
<classpathentry kind="lib" path="lib/slf4j-log4j12-1.7.5.jar"/>
@ -21,5 +21,6 @@
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/devinsy-utils-0.2.4.jar"/>
<classpathentry kind="lib" path="lib/mysql-jdbc-5.0.8.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

BIN
lib/mysql-jdbc-5.0.8.jar Normal file

Binary file not shown.

View file

@ -49,8 +49,7 @@ public class Element {
* @param value
*/
public Element(final String key, final String subkey, final String value) {
updateValue(value);
this.creationDate = this.editionDate;
update(value);
this.archiveDate = null;
this.key = key;
this.subkey = subkey;
@ -127,9 +126,14 @@ public class Element {
this.value = value;
}
public void updateValue(final String value) {
public void update(final String value) {
//
setValue(value);
//
this.editionDate = new Date();
//
if (value == null) {
this.size = 0;
this.digest = null;
@ -137,5 +141,10 @@ public class Element {
this.size = this.value.length();
this.digest = DigestUtils.md5Hex(this.value);
}
//
if (this.creationDate == null) {
this.creationDate = this.editionDate;
}
}
}

View file

@ -15,6 +15,8 @@
*/
package fr.devinsy.sikevadb;
import java.util.Date;
import fr.devinsy.util.StringList;
/**
@ -43,6 +45,30 @@ public class FileSikevaDB implements SikevaDB {
}
@Override
public void clearAllArchive() throws Exception {
// TODO Auto-generated method stub
}
@Override
public void clearArchive(final Date beforeDate) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void clearArchive(final int maxDays) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void clearDatabase() throws Exception {
// TODO Auto-generated method stub
}
@Override
public void close() {
// TODO Auto-generated method stub
@ -115,6 +141,18 @@ public class FileSikevaDB implements SikevaDB {
return null;
}
@Override
public StringList getAllKeys() throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public StringList getAllSubkeys(final String key) throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public StringList getAllValues(final String key) {
// TODO Auto-generated method stub
@ -163,6 +201,18 @@ public class FileSikevaDB implements SikevaDB {
return null;
}
@Override
public StringList getKeys() throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public StringList getSubkeys(final String key) throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public String getValue(final String key) throws Exception {
// TODO Auto-generated method stub
@ -240,4 +290,10 @@ public class FileSikevaDB implements SikevaDB {
// TODO Auto-generated method stub
}
@Override
public void removeAll(final String key) throws Exception {
// TODO Auto-generated method stub
}
}

File diff suppressed because it is too large Load diff

View file

@ -15,6 +15,8 @@
*/
package fr.devinsy.sikevadb;
import java.util.Date;
import fr.devinsy.util.StringList;
/**
@ -30,6 +32,14 @@ public interface SikevaDB {
public void archive(final String key, final String subkey) throws Exception;
public void clearAllArchive() throws Exception;
public void clearArchive(final Date beforeDate) throws Exception;
public void clearArchive(final int maxDays) throws Exception;
public void clearDatabase() throws Exception;
public void close() throws Exception;
public long countOfAllElements() throws Exception;
@ -54,6 +64,10 @@ public interface SikevaDB {
public Elements getAllElements(String key, String subkey) throws Exception;
public StringList getAllKeys() throws Exception;
public StringList getAllSubkeys(String key) throws Exception;
public StringList getAllValues(String key) throws Exception;
public StringList getAllValues(String key, String subkey) throws Exception;
@ -70,6 +84,10 @@ public interface SikevaDB {
public Elements getElements(String key, String subkey) throws Exception;
public StringList getKeys() throws Exception;
public StringList getSubkeys(String key) throws Exception;
public String getValue(String key) throws Exception;
public String getValue(String key, String subkey) throws Exception;
@ -95,4 +113,6 @@ public interface SikevaDB {
public void remove(final String key) throws Exception;
public void remove(final String key, final String subkey) throws Exception;
public void removeAll(final String key) throws Exception;
}

View file

@ -37,16 +37,16 @@ public class SikevaDBFactory {
/**
*
* @param host
* @param port
* @param driverClassName
* @param url
* @param login
* @param password
* @return
*/
public static SikevaDB get(final String host, final String port, final String login, final String password) {
public static SikevaDB get(final String driverClassName, final String url, final String login, final String password) {
SikevaDB result;
result = new SQLSikevaDB(host, port, login, password);
result = new SQLSikevaDB(driverClassName, url, login, password);
//
return result;

View file

@ -1,40 +0,0 @@
package fr.devinsy.sikevadb;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
*
* @author Christian P. Momon
*/
public class AlphaTest {
static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(AlphaTest.class);
/**
*
*/
@Before
public void before() {
BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.ERROR);
}
/**
*
*/
@Test
public void testFoo01() {
//
logger.debug("===== test starting...");
//
Assert.assertEquals('a', 'a');
//
logger.debug("===== test done.");
}
}

View file

@ -0,0 +1,358 @@
package fr.devinsy.sikevadb;
import java.sql.SQLException;
import java.util.Date;
import javax.naming.NamingException;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import fr.devinsy.util.StringList;
/**
* ?profileSQL=true
*
* @author Christian P. Momon
*/
public class SQLSikevaDBTest {
static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SQLSikevaDBTest.class);
private SQLSikevaDB database;
/**
*
*/
@After
public void after() {
if (this.database != null) {
this.database.close();
}
}
/**
* @throws NamingException
* @throws SQLException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*
*/
@Before
public void before() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, NamingException {
BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.ERROR);
this.database = new SQLSikevaDB("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/sikevadb-test", "sikevadb-test", "12345678");
this.database.open();
}
/**
* @throws Exception
*
*/
@Test
public void testClearArchive01() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
Element element = new Element();
element.setKey("alpha");
element.setSubkey(null);
element.setSize(10);
element.setDigest("qsdkfqskjf");
element.setCreationDate(new Date());
element.setEditionDate(new Date());
element.setArchiveDate(new Date(new Date().getTime() - 10 * 24 * 60 * 60 * 1000));
element.setValue("bonjour");
database.put(element);
database.put("alpha", "bravo", "toto");
database.clearArchive(5);
Assert.assertEquals(1, database.countOfElements());
Assert.assertEquals(0, database.countOfArchivedElements());
Assert.assertEquals(1, database.countOfAllElements());
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testClearArchive02() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
Element element = new Element();
element.setKey("alpha");
element.setSubkey(null);
element.setSize(10);
element.setDigest("qsdkfqskjf");
element.setCreationDate(new Date());
element.setEditionDate(new Date());
element.setArchiveDate(new Date(new Date().getTime() - 10 * 24 * 60 * 60 * 1000));
element.setValue("bonjour");
database.put(element);
database.put("alpha", "bravo", "toto");
database.clearArchive(new Date(new Date().getTime() - 5 * 24 * 60 * 60 * 1000));
Assert.assertEquals(1, database.countOfElements());
Assert.assertEquals(0, database.countOfArchivedElements());
Assert.assertEquals(1, database.countOfAllElements());
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testPut01() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
String source = "bonjour";
database.put("alpha01", source);
String target = database.getValue("alpha01");
Assert.assertEquals(source, target);
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testPut02() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
String source = "bonjour";
database.put("alpha01", source);
String target = database.getValue("alpha01");
Assert.assertEquals(source, target);
target = database.getValue("alpha01", null);
Assert.assertEquals(source, target);
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testPut03() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
String source = "bonjour";
database.put("alpha01", null, source);
String target = database.getValue("alpha01");
Assert.assertEquals(source, target);
target = database.getValue("alpha01", null);
Assert.assertEquals(source, target);
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testPut04() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
String source = "bonjour";
database.put("alpha02", "bravo", source);
String target = database.getValue("alpha02", "bravo");
Assert.assertEquals(source, target);
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testPut05() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
String source = "bonjour";
database.put("alpha01", source);
String target = database.getValue("alpha01");
Assert.assertEquals(source, target);
String source2 = "au revoir";
database.put("alpha01", source2);
target = database.getValue("alpha01");
Assert.assertEquals(source2, target);
StringList targets = database.getArchivedValues("alpha01");
Assert.assertEquals(1, targets.size());
Assert.assertEquals(source, targets.get(0));
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testPut06() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
String source = "bonjour";
database.put("alpha01", "bravo", source);
String target = database.getValue("alpha01", "bravo");
Assert.assertEquals(source, target);
String source2 = "au revoir";
database.put("alpha01", "bravo", source2);
target = database.getValue("alpha01", "bravo");
Assert.assertEquals(source2, target);
StringList targets = database.getArchivedValues("alpha01", "bravo");
Assert.assertEquals(1, targets.size());
Assert.assertEquals(source, targets.get(0));
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testRemove01() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
String source = "bonjour";
database.put("alpha01", "bravo", source);
String target = database.getValue("alpha01", "bravo");
Assert.assertEquals(source, target);
String source2 = "au revoir";
database.put("alpha01", "bravo", source2);
target = database.getValue("alpha01", "bravo");
Assert.assertEquals(source2, target);
StringList targets = database.getArchivedValues("alpha01", "bravo");
Assert.assertEquals(1, targets.size());
Assert.assertEquals(source, targets.get(0));
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testSize01() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
Assert.assertEquals(0, database.countOfElements());
Assert.assertEquals(0, database.countOfArchivedElements());
Assert.assertEquals(0, database.countOfAllElements());
database.put("alpha01", "qlskjfmlqja");
database.put("alpha01", "qlskjfmlqjb");
database.put("alpha02", "qlskjfmlqj");
database.put("alpha03", "qlskjfmlqj");
database.put("alpha04", "qlskjfmlqj");
database.put("alpha05", "qlskjfmlqj");
database.archive("alpha02");
database.remove("alpha03");
database.put("alpha01", "bravo1", "qlskjfmlqja");
database.put("alpha01", "bravo1", "qlskjfmlqjb");
database.put("alpha01", "bravo2", "qlskjfmlqj");
database.put("alpha01", "bravo3", "qlskjfmlqj");
database.put("alpha01", "bravo4", "qlskjfmlqj");
database.put("alpha01", "bravo5", "qlskjfmlqj");
database.archive("alpha01", "bravo2");
database.remove("alpha01", "bravo3");
// System.out.println(database.countOfElements() + " " +
// database.countOfArchivedElements() + " " +
// database.countOfAllElements());
Assert.assertEquals(6, database.countOfElements());
Assert.assertEquals(4, database.countOfArchivedElements());
Assert.assertEquals(10, database.countOfAllElements());
database.clearDatabase();
Assert.assertEquals(0, database.countOfElements());
Assert.assertEquals(0, database.countOfArchivedElements());
Assert.assertEquals(0, database.countOfAllElements());
//
logger.debug("===== test done.");
}
}

View file

@ -0,0 +1,12 @@
<dataset>
<ELEMENTS
TOPKEY="alpha"
SUBKEY=NULL
SIZE="10"
DIGEST="7c12772809c1c0c3deda6103b10fdfa0"
CREATION_DATE="2013-01-01 18:00:00.000"
EDITION_DATE="2013-01-01 18:00:00.000"
ARCHIVE_DATE="2013-01-01 18:00:00.000"
VALUE="1234567890"
/>
</dataset>