DATACMNS-135 - Added basic infrastructure to support historiography of data.

Added interfaces for common abstractions needed to implement historiography. A Revision is an entity associated with some RevisionMetadata. That in turn consists of a revision number and date at least and is backed by a implementation specific delegate. The Revisions wrapper class allows accessing the latest revision from a list of Revision instances no matter what order they are in. A RevisionRepository allows access the revision an entity was changed in the last time, access all Revisions of an entity as well as paged access to it.
This commit is contained in:
Oliver Gierke
2012-03-20 11:22:57 +01:00
parent 2f39b74cbc
commit f8d5e310a7
9 changed files with 688 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.history;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
* Unit tests for {@link RevisionMetadata}.
*
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class RevisionUnitTests {
@Mock
RevisionMetadata<Integer> firstMetadata, secondMetadata;
@Test
@SuppressWarnings("unchecked")
public void comparesCorrectly() {
when(firstMetadata.getRevisionNumber()).thenReturn(1);
when(secondMetadata.getRevisionNumber()).thenReturn(2);
Revision<Integer, Object> first = new Revision<Integer, Object>(firstMetadata, new Object());
Revision<Integer, Object> second = new Revision<Integer, Object>(secondMetadata, new Object());
List<Revision<Integer, Object>> revisions = Arrays.asList(second, first);
Collections.sort(revisions);
assertThat(revisions.get(0), is(first));
assertThat(revisions.get(1), is(second));
}
}

View File

@@ -0,0 +1,110 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.history;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.Iterator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
* Unit tests for {@link Revisions}.
*
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class RevisionsUnitTest {
@Mock
RevisionMetadata<Integer> first, second;
Revision<Integer, Object> firstRevision, secondRevision;
@Before
public void setUp() {
when(first.getRevisionNumber()).thenReturn(0);
when(second.getRevisionNumber()).thenReturn(10);
firstRevision = new Revision<Integer, Object>(first, new Object());
secondRevision = new Revision<Integer, Object>(second, new Object());
}
@Test
@SuppressWarnings("unchecked")
public void returnsCorrectLatestRevision() {
Revisions<Integer, Object> revisions = new Revisions<Integer, Object>(Arrays.asList(firstRevision, secondRevision));
assertThat(revisions.getLatestRevision(), is(secondRevision));
}
@Test
@SuppressWarnings("unchecked")
public void iteratesInCorrectOrder() {
Revisions<Integer, Object> revisions = new Revisions<Integer, Object>(Arrays.asList(firstRevision, secondRevision));
Iterator<Revision<Integer, Object>> iterator = revisions.iterator();
assertThat(iterator.hasNext(), is(true));
assertThat(iterator.next(), is(firstRevision));
assertThat(iterator.hasNext(), is(true));
assertThat(iterator.next(), is(secondRevision));
assertThat(iterator.hasNext(), is(false));
}
@Test
@SuppressWarnings("unchecked")
public void reversedRevisionsStillReturnsCorrectLatestRevision() {
Revisions<Integer, Object> revisions = new Revisions<Integer, Object>(Arrays.asList(firstRevision, secondRevision));
assertThat(revisions.reverse().getLatestRevision(), is(secondRevision));
}
@Test
@SuppressWarnings("unchecked")
public void iteratesReversedRevisionsInCorrectOrder() {
Revisions<Integer, Object> revisions = new Revisions<Integer, Object>(Arrays.asList(firstRevision, secondRevision));
Iterator<Revision<Integer, Object>> iterator = revisions.reverse().iterator();
assertThat(iterator.hasNext(), is(true));
assertThat(iterator.next(), is(secondRevision));
assertThat(iterator.hasNext(), is(true));
assertThat(iterator.next(), is(firstRevision));
assertThat(iterator.hasNext(), is(false));
}
@Test
@SuppressWarnings("unchecked")
public void forcesInvalidlyOrderedRevisionsToBeOrdered() {
Revisions<Integer, Object> revisions = new Revisions<Integer, Object>(Arrays.asList(secondRevision, firstRevision));
Iterator<Revision<Integer, Object>> iterator = revisions.iterator();
assertThat(iterator.hasNext(), is(true));
assertThat(iterator.next(), is(firstRevision));
assertThat(iterator.hasNext(), is(true));
assertThat(iterator.next(), is(secondRevision));
assertThat(iterator.hasNext(), is(false));
}
}