/** * Returns the {@link RevisionRange} whose major value of {@code from} {@link Revision} is greater than * or equal to the major value of {@code to} {@link Revision}. * * @throws IllegalStateException if the {@code from} and {@code to} {@link Revision}s are in the * different state. They should be either absolute or relative. */ public RevisionRange toDescending() { if (isAscending()) { return new RevisionRange(to, from); } return this; }
/** * Returns the {@link RevisionRange} whose major value of {@code from} {@link Revision} is greater than * or equal to the major value of {@code to} {@link Revision}. * * @throws IllegalStateException if the {@code from} and {@code to} {@link Revision}s are in the * different state. They should be either absolute or relative. */ public RevisionRange toDescending() { if (isAscending()) { return new RevisionRange(to, from); } return this; }
/** * Returns the {@link RevisionRange} whose major value of {@code from} {@link Revision} is greater than * or equal to the major value of {@code to} {@link Revision}. * * @throws IllegalStateException if the {@code from} and {@code to} {@link Revision}s are in the * different state. They should be either absolute or relative. */ public RevisionRange toDescending() { if (isAscending()) { return new RevisionRange(to, from); } return this; }
/** * Returns the {@link RevisionRange} whose major value of {@code from} {@link Revision} is lower than * or equal to the major value of {@code to} {@link Revision}. * * @throws IllegalStateException if the {@code from} and {@code to} {@link Revision}s are in the * different state. They should be either absolute or relative. */ public RevisionRange toAscending() { if (isAscending() || from.equals(to)) { return this; } return new RevisionRange(to, from); }
/** * Returns the {@link RevisionRange} whose major value of {@code from} {@link Revision} is lower than * or equal to the major value of {@code to} {@link Revision}. * * @throws IllegalStateException if the {@code from} and {@code to} {@link Revision}s are in the * different state. They should be either absolute or relative. */ public RevisionRange toAscending() { if (isAscending() || from.equals(to)) { return this; } return new RevisionRange(to, from); }
/** * Returns the {@link RevisionRange} whose major value of {@code from} {@link Revision} is lower than * or equal to the major value of {@code to} {@link Revision}. * * @throws IllegalStateException if the {@code from} and {@code to} {@link Revision}s are in the * different state. They should be either absolute or relative. */ public RevisionRange toAscending() { if (isAscending() || from.equals(to)) { return this; } return new RevisionRange(to, from); }
@Override public RevisionRange normalizeNow(Revision from, Revision to) { final int baseMajor = cachedHeadRevision().major(); return new RevisionRange(normalizeNow(from, baseMajor), normalizeNow(to, baseMajor)); }
@Override public RevisionRange normalizeNow(Revision from, Revision to) { final int baseMajor = cachedHeadRevision().major(); return new RevisionRange(normalizeNow(from, baseMajor), normalizeNow(to, baseMajor)); }
@Override public RevisionRange normalizeNow(Revision from, Revision to) { final int baseMajor = cachedHeadRevision().major(); return new RevisionRange(normalizeNow(from, baseMajor), normalizeNow(to, baseMajor)); }
@Test public void finaLatestRevisionHead() { final Repository repo = newCachingRepo(); final Revision actualHeadRev = new Revision(2); doReturn(new RevisionRange(actualHeadRev, actualHeadRev)) .when(delegateRepo).normalizeNow(HEAD, HEAD); assertThat(repo.findLatestRevision(HEAD, "/**").join()).isNull(); verify(delegateRepo, never()).findLatestRevision(any(), any()); verifyNoMoreInteractions(delegateRepo); }
@Test public void findLatestRevision() { final Repository repo = setMockNames(newCachingRepo()); doReturn(new RevisionRange(INIT, new Revision(2))).when(delegateRepo).normalizeNow(INIT, HEAD); // Uncached when(delegateRepo.findLatestRevision(any(), any())).thenReturn(completedFuture(new Revision(2))); assertThat(repo.findLatestRevision(INIT, "/**").join()).isEqualTo(new Revision(2)); verify(delegateRepo).findLatestRevision(INIT, "/**"); verifyNoMoreInteractions(delegateRepo); // Cached clearInvocations(delegateRepo); assertThat(repo.findLatestRevision(INIT, "/**").join()).isEqualTo(new Revision(2)); verify(delegateRepo, never()).findLatestRevision(any(), any()); verifyNoMoreInteractions(delegateRepo); }
@Test public void watchFastPath() { final Repository repo = setMockNames(newCachingRepo()); doReturn(new RevisionRange(INIT, new Revision(2))).when(delegateRepo).normalizeNow(INIT, HEAD); // Uncached when(delegateRepo.findLatestRevision(any(), any())).thenReturn(completedFuture(new Revision(2))); assertThat(repo.watch(INIT, "/**").join()).isEqualTo(new Revision(2)); verify(delegateRepo).findLatestRevision(INIT, "/**"); verify(delegateRepo, never()).watch(any(), any(String.class)); verifyNoMoreInteractions(delegateRepo); // Cached clearInvocations(delegateRepo); assertThat(repo.watch(INIT, "/**").join()).isEqualTo(new Revision(2)); verify(delegateRepo, never()).findLatestRevision(any(), any()); verify(delegateRepo, never()).watch(any(), any(String.class)); verifyNoMoreInteractions(delegateRepo); }
@Test public void findLatestRevisionNull() { final Repository repo = setMockNames(newCachingRepo()); doReturn(new RevisionRange(INIT, new Revision(2))).when(delegateRepo).normalizeNow(INIT, HEAD); // Uncached when(delegateRepo.findLatestRevision(any(), any())).thenReturn(completedFuture(null)); assertThat(repo.findLatestRevision(INIT, "/**").join()).isNull(); verify(delegateRepo).findLatestRevision(INIT, "/**"); verifyNoMoreInteractions(delegateRepo); // Cached clearInvocations(delegateRepo); assertThat(repo.findLatestRevision(INIT, "/**").join()).isNull(); verify(delegateRepo, never()).findLatestRevision(any(), any()); verifyNoMoreInteractions(delegateRepo); }
@Test public void watchSlowPath() { final Repository repo = setMockNames(newCachingRepo()); doReturn(new RevisionRange(INIT, new Revision(2))).when(delegateRepo).normalizeNow(INIT, HEAD); final CompletableFuture<Revision> delegateWatchFuture = new CompletableFuture<>(); when(delegateRepo.findLatestRevision(any(), any())).thenReturn(completedFuture(null)); when(delegateRepo.watch(any(), any(String.class))).thenReturn(delegateWatchFuture); // Make sure the future returned by CachingRepository.watch() depends on // the future returned by delegateRepo.watch(). final CompletableFuture<Revision> watchFuture = repo.watch(INIT, "/**"); assertThat(watchFuture).isNotDone(); delegateWatchFuture.complete(new Revision(3)); assertThat(watchFuture.join()).isSameAs(delegateWatchFuture.join()); verify(delegateRepo).findLatestRevision(INIT, "/**"); verify(delegateRepo).watch(INIT, "/**"); verifyNoMoreInteractions(delegateRepo); }
@Test public void singleDiff() { final Repository repo = setMockNames(newCachingRepo()); final Query<String> query = Query.ofText("/foo.txt"); final Change<String> change = Change.ofTextUpsert(query.path(), "bar"); doReturn(new RevisionRange(10, 1)).when(delegateRepo).normalizeNow(HEAD, INIT); doReturn(new RevisionRange(10, 1)).when(delegateRepo).normalizeNow(new Revision(10), INIT); doReturn(new RevisionRange(10, 1)).when(delegateRepo).normalizeNow(new Revision(-1), new Revision(-10)); doReturn(new RevisionRange(10, 1)).when(delegateRepo).normalizeNow(new Revision(10), new Revision(-10)); // Uncached when(delegateRepo.diff(any(), any(), any(Query.class))).thenReturn(completedFuture(change)); assertThat(repo.diff(HEAD, INIT, query).join()).isEqualTo(change); verify(delegateRepo).diff(INIT, new Revision(10), query); verifyNoMoreInteractions(delegateRepo); // Cached clearInvocations(delegateRepo); assertThat(repo.diff(HEAD, new Revision(-10), query).join()).isEqualTo(change); assertThat(repo.diff(HEAD, INIT, query).join()).isEqualTo(change); assertThat(repo.diff(new Revision(10), new Revision(-10), query).join()).isEqualTo(change); assertThat(repo.diff(new Revision(10), INIT, query).join()).isEqualTo(change); verify(delegateRepo, never()).diff(any(), any(), any(Query.class)); verifyNoMoreInteractions(delegateRepo); }
@Test public void multiDiff() { final Repository repo = setMockNames(newCachingRepo()); final Map<String, Change<?>> changes = ImmutableMap.of( "/foo.txt", Change.ofTextUpsert("/foo.txt", "bar")); doReturn(new RevisionRange(10, 1)).when(delegateRepo).normalizeNow(HEAD, INIT); doReturn(new RevisionRange(10, 1)).when(delegateRepo).normalizeNow(new Revision(10), INIT); doReturn(new RevisionRange(10, 1)).when(delegateRepo).normalizeNow(new Revision(-1), new Revision(-10)); doReturn(new RevisionRange(10, 1)).when(delegateRepo).normalizeNow(new Revision(10), new Revision(-10)); // Uncached when(delegateRepo.diff(any(), any(), any(String.class))).thenReturn(completedFuture(changes)); assertThat(repo.diff(HEAD, INIT, "/**").join()).isEqualTo(changes); verify(delegateRepo).diff(INIT, new Revision(10), "/**"); verifyNoMoreInteractions(delegateRepo); // Cached clearInvocations(delegateRepo); assertThat(repo.diff(HEAD, new Revision(-10), "/**").join()).isEqualTo(changes); assertThat(repo.diff(HEAD, INIT, "/**").join()).isEqualTo(changes); assertThat(repo.diff(new Revision(10), new Revision(-10), "/**").join()).isEqualTo(changes); assertThat(repo.diff(new Revision(10), INIT, "/**").join()).isEqualTo(changes); verify(delegateRepo, never()).diff(any(), any(), any(Query.class)); verifyNoMoreInteractions(delegateRepo); }
@Test public void revisionRange() { RevisionRange range = new RevisionRange(2, 4); assertThat(range.toDescending()).isEqualTo(new RevisionRange(4, 2)); range = new RevisionRange(revisionTen, INIT); assertThat(range.toAscending()).isEqualTo(new RevisionRange(INIT, revisionTen)); assertThat(range.toDescending()).isSameAs(range); range = new RevisionRange(revisionTen, revisionTen); range = new RevisionRange(revisionNegativeTen, revisionNegativeThree); assertThat(range.toAscending()).isSameAs(range); assertThat(range.toDescending()).isEqualTo( new RevisionRange(revisionNegativeThree, revisionNegativeTen)); final RevisionRange relativeRange = new RevisionRange(INIT, HEAD);
@Test public void history() { final Repository repo = setMockNames(newCachingRepo()); final List<Commit> commits = ImmutableList.of( new Commit(new Revision(3), SYSTEM, "third", "", Markup.MARKDOWN), new Commit(new Revision(3), SYSTEM, "second", "", Markup.MARKDOWN), new Commit(new Revision(3), SYSTEM, "first", "", Markup.MARKDOWN)); doReturn(new RevisionRange(3, 1)).when(delegateRepo).normalizeNow(HEAD, INIT); doReturn(new RevisionRange(3, 1)).when(delegateRepo).normalizeNow(HEAD, new Revision(-3)); doReturn(new RevisionRange(3, 1)).when(delegateRepo).normalizeNow(new Revision(3), new Revision(-3)); doReturn(new RevisionRange(3, 1)).when(delegateRepo).normalizeNow(new Revision(3), INIT); // Uncached when(delegateRepo.history(any(), any(), any(), anyInt())).thenReturn(completedFuture(commits)); assertThat(repo.history(HEAD, INIT, "/**", Integer.MAX_VALUE).join()).isEqualTo(commits); verify(delegateRepo).history(new Revision(3), INIT, "/**", 3); verifyNoMoreInteractions(delegateRepo); // Cached clearInvocations(delegateRepo); assertThat(repo.history(HEAD, new Revision(-3), "/**", 3).join()).isEqualTo(commits); assertThat(repo.history(HEAD, INIT, "/**", 4).join()).isEqualTo(commits); assertThat(repo.history(new Revision(3), new Revision(-3), "/**", 5).join()).isEqualTo(commits); assertThat(repo.history(new Revision(3), INIT, "/**", 6).join()).isEqualTo(commits); verify(delegateRepo, never()).history(any(), any(), any(), anyInt()); verifyNoMoreInteractions(delegateRepo); }