Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Hafner committed Dec 22, 2024
1 parent 106a765 commit 287d3c1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

public class CommitInstantsLog {

public static final long NO_COMMIT_INSTANT = 0;
private static HashMap<VersionedEntryIdentifier, Long> lastCommit = new HashMap<>();


Expand All @@ -36,7 +37,7 @@ public void setOrUpdateLastCommit( long entryIdentifier, long version, long inst

public long getLastCommit( long entryIdentifier, long version ) {
VersionedEntryIdentifier identifier = getIdentifier( entryIdentifier, version );
return lastCommit.get( identifier );
return lastCommit.getOrDefault(identifier, NO_COMMIT_INSTANT);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,81 +18,85 @@

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CommitInstantsLogTest {

CommitInstantsLog log;


@BeforeEach
void setUp() {
log = new CommitInstantsLog();
}


@Test
void testSetOrUpdateLastCommitAndGetLastCommit() {
long entryId = 1L;
long version = 1L;
long instant = 1000L;

log.setOrUpdateLastCommit(entryId, version, instant);
long retrievedInstant = log.getLastCommit(entryId, version);
log.setOrUpdateLastCommit( entryId, version, instant );
long retrievedInstant = log.getLastCommit( entryId, version );

assertEquals(instant, retrievedInstant, "The retrieved instant should match the updated instant.");
assertEquals( instant, retrievedInstant, "The retrieved instant should match the updated instant." );
}


@Test
void testSetOrUpdateLastCommitOverridesPreviousCommit() {
long entryId = 1L;
long version = 1L;
long firstInstant = 1000L;
long secondInstant = 2000L;

log.setOrUpdateLastCommit(entryId, version, firstInstant);
log.setOrUpdateLastCommit(entryId, version, secondInstant);
long retrievedInstant = log.getLastCommit(entryId, version);
log.setOrUpdateLastCommit( entryId, version, firstInstant );
log.setOrUpdateLastCommit( entryId, version, secondInstant );
long retrievedInstant = log.getLastCommit( entryId, version );

assertEquals(secondInstant, retrievedInstant, "The retrieved instant should match the most recent update.");
assertEquals( secondInstant, retrievedInstant, "The retrieved instant should match the most recent update." );
}


@Test
void testGetLastCommitForNonexistentEntry() {
long entryId = 1L;
long version = 1L;

Exception exception = assertThrows(NullPointerException.class, () -> {
log.getLastCommit(entryId, version);
});
long retrievedInstant = log.getLastCommit( entryId, version );

assertNotNull(exception, "An exception should be thrown for a nonexistent entry.");
assertEquals( CommitInstantsLog.NO_COMMIT_INSTANT, retrievedInstant );
}


@Test
void testRemoveEntry() {
long entryId = 1L;
long version = 1L;
long instant = 1000L;

log.setOrUpdateLastCommit(entryId, version, instant);
log.removeEntry(entryId, version);
log.setOrUpdateLastCommit( entryId, version, instant );
log.removeEntry( entryId, version );

long retrievedInstant = log.getLastCommit( entryId, version );

assertThrows(NullPointerException.class, () -> {
log.getLastCommit(entryId, version);
}, "An exception should be thrown after removing the entry.");
assertEquals( CommitInstantsLog.NO_COMMIT_INSTANT, retrievedInstant );
}


@Test
void testRemoveEntryForNonexistentEntry() {
long entryId = 1L;
long version = 1L;

assertDoesNotThrow(() -> log.removeEntry(entryId, version),
"Removing a nonexistent entry should not throw an exception.");
assertDoesNotThrow( () -> log.removeEntry( entryId, version ),
"Removing a nonexistent entry should not throw an exception." );
}


@Test
void testMultipleEntries() {
long entryId1 = 1L;
Expand All @@ -103,13 +107,13 @@ void testMultipleEntries() {
long version2 = 2L;
long instant2 = 2000L;

log.setOrUpdateLastCommit(entryId1, version1, instant1);
log.setOrUpdateLastCommit(entryId2, version2, instant2);
log.setOrUpdateLastCommit( entryId1, version1, instant1 );
log.setOrUpdateLastCommit( entryId2, version2, instant2 );

assertEquals(instant1, log.getLastCommit(entryId1, version1),
"The first entry's instant should match its updated value.");
assertEquals(instant2, log.getLastCommit(entryId2, version2),
"The second entry's instant should match its updated value.");
assertEquals( instant1, log.getLastCommit( entryId1, version1 ),
"The first entry's instant should match its updated value." );
assertEquals( instant2, log.getLastCommit( entryId2, version2 ),
"The second entry's instant should match its updated value." );
}

}

0 comments on commit 287d3c1

Please sign in to comment.