Search in sources :

Example 21 with Journal

use of net.viperfish.journal.framework.Journal in project vsDiaryWriter by shilongdai.

the class OperationTest method testDeleteEntryOperation.

@Test
public void testDeleteEntryOperation() throws FailToSyncEntryException {
    cleanUp();
    Journal toAdd = new Journal();
    toAdd.setContent("test");
    toAdd.setDate(new Date());
    toAdd.setSubject("test");
    Journal result = db.addEntry(toAdd);
    Long id = result.getId();
    toAdd.setId(id);
    result = db.getEntry(id);
    Assert.assertEquals(true, result.equals(toAdd));
    new DeleteEntryOperation(id).execute();
    result = db.getEntry(id);
    Assert.assertEquals(null, result);
    Assert.assertEquals(false, indexer.contains(id));
    cleanUp();
}
Also used : Journal(net.viperfish.journal.framework.Journal) Date(java.util.Date) Test(org.junit.Test)

Example 22 with Journal

use of net.viperfish.journal.framework.Journal in project vsDiaryWriter by shilongdai.

the class OperationTest method testExportOperation.

@Test
public void testExportOperation() throws FailToSyncEntryException {
    cleanUp();
    addEntries(20, "toExport");
    List<Journal> all = db.getAll();
    for (Journal i : all) {
        i.setId(null);
    }
    try {
        String expectedOutput = new JsonGenerator().toJson(all.toArray(new Journal[1]));
        new ExportJournalOperation("test.txt").execute();
        IOFile exportFile = new IOFile(new File("test.txt"), new TextIOStreamHandler());
        String actualOutput = exportFile.read(StandardCharsets.UTF_16);
        Assert.assertEquals(expectedOutput, actualOutput);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    cleanUp();
}
Also used : JsonGenerator(net.viperfish.framework.serialization.JsonGenerator) TextIOStreamHandler(net.viperfish.framework.file.TextIOStreamHandler) Journal(net.viperfish.journal.framework.Journal) IOException(java.io.IOException) IOFile(net.viperfish.framework.file.IOFile) File(java.io.File) IOFile(net.viperfish.framework.file.IOFile) Test(org.junit.Test)

Example 23 with Journal

use of net.viperfish.journal.framework.Journal in project vsDiaryWriter by shilongdai.

the class OperationTest method testChangeConfig.

@Test
public void testChangeConfig() throws FailToSyncEntryException {
    cleanUp();
    Map<String, String> testConfig = new HashMap<>();
    addEntries(10);
    testConfig.put("viperfish.secure.encrytion.algorithm", "DES");
    testConfig.put(ConfigMapping.DB_COMPONENT, "H2Database");
    new ChangeConfigurationOperation(testConfig).execute();
    Assert.assertEquals("DES", Configuration.getString("viperfish.secure.encrytion.algorithm"));
    Assert.assertEquals("H2Database", Configuration.getString(ConfigMapping.DB_COMPONENT));
    initComponents();
    Assert.assertEquals(((JournalEncryptionWrapper) db).getDb().getClass(), ((JournalEncryptionWrapper) EntryDatabases.INSTANCE.getEntryDatabase("H2Database")).getDb().getClass());
    for (Journal i : db.getAll()) {
        Assert.assertEquals("test", i.getContent());
    }
    setupConfig();
    initComponents();
    cleanUp();
}
Also used : HashMap(java.util.HashMap) JournalEncryptionWrapper(net.viperfish.journal.framework.provider.JournalEncryptionWrapper) Journal(net.viperfish.journal.framework.Journal) Test(org.junit.Test)

Example 24 with Journal

use of net.viperfish.journal.framework.Journal in project vsDiaryWriter by shilongdai.

the class OperationTest method addEntries.

private List<Long> addEntries(int howMany, String allContent) throws FailToSyncEntryException {
    List<Long> resultList = new LinkedList<>();
    for (int i = 0; i < howMany; ++i) {
        Journal j = new Journal();
        j.setContent(allContent);
        j.setSubject(allContent);
        Journal result = db.addEntry(j);
        resultList.add(result.getId());
    }
    return resultList;
}
Also used : Journal(net.viperfish.journal.framework.Journal) LinkedList(java.util.LinkedList)

Example 25 with Journal

use of net.viperfish.journal.framework.Journal in project vsDiaryWriter by shilongdai.

the class OperationTest method testGetDateRange.

@Test
public void testGetDateRange() throws FailToSyncEntryException {
    cleanUp();
    Calendar cal = Calendar.getInstance();
    cal.set(1990, 0, 1);
    Date lowerBound = cal.getTime();
    cal.set(1990, 1, 12);
    Date upperBound = cal.getTime();
    cal.set(1990, 0, 22);
    Date inBound = cal.getTime();
    Journal valid = new Journal();
    valid.setDate(inBound);
    valid.setSubject("valid entry");
    valid.setContent("valid content");
    db.addEntry(valid);
    cal.set(1990, 2, 4);
    Date outBound = cal.getTime();
    Journal invalid = new Journal();
    invalid.setDate(outBound);
    invalid.setContent("out of bound");
    invalid.setSubject("out of bound");
    db.addEntry(invalid);
    GetDateRangeOperation range = new GetDateRangeOperation(lowerBound, upperBound);
    range.execute();
    Set<JournalPointer> result = range.getResult();
    Assert.assertEquals(true, result.contains(new JournalPointer(valid)));
    Assert.assertEquals(false, result.contains(new JournalPointer(invalid)));
}
Also used : JournalPointer(net.viperfish.journal.framework.JournalPointer) Calendar(java.util.Calendar) Journal(net.viperfish.journal.framework.Journal) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Journal (net.viperfish.journal.framework.Journal)35 Test (org.junit.Test)13 Date (java.util.Date)8 File (java.io.File)7 JournalPointer (net.viperfish.journal.framework.JournalPointer)6 IOException (java.io.IOException)4 FailToSyncEntryException (net.viperfish.journal.framework.errors.FailToSyncEntryException)4 CipherException (net.viperfish.journal.framework.errors.CipherException)3 OperationErrorException (net.viperfish.journal.framework.errors.OperationErrorException)3 LinkedList (java.util.LinkedList)2 TreeSet (java.util.TreeSet)2 IOFile (net.viperfish.framework.file.IOFile)2 TextIOStreamHandler (net.viperfish.framework.file.TextIOStreamHandler)2 DatabaseTest (net.viperfish.journal.framework.DatabaseTest)2 FailToStoreCredentialException (net.viperfish.journal.framework.errors.FailToStoreCredentialException)2 ByteBuffer (java.nio.ByteBuffer)1 DateFormat (java.text.DateFormat)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 JsonGenerator (net.viperfish.framework.serialization.JsonGenerator)1