mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 06:55:47 +01:00
merge with branch 1.x
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
/***
|
||||
* Copyright (c) 2015, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* https://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import org.eclipse.jgit.api.GarbageCollectCommand;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.junit.Test;
|
||||
import static org.mockito.Mockito.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GitGcTask}.
|
||||
*
|
||||
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GitGcTaskTest
|
||||
{
|
||||
|
||||
@Mock
|
||||
private RepositoryManager manager;
|
||||
|
||||
@Mock
|
||||
private RepositoryDirectoryHandler handler;
|
||||
|
||||
@Mock
|
||||
private GarbageCollectCommand gcc;
|
||||
|
||||
@Mock
|
||||
private Git git;
|
||||
|
||||
private GitGcTask task;
|
||||
|
||||
/**
|
||||
* Setup mocks for tests.
|
||||
*
|
||||
* @throws GitAPIException
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws GitAPIException
|
||||
{
|
||||
when(git.gc()).thenReturn(gcc);
|
||||
when(gcc.getStatistics()).thenReturn(new Properties());
|
||||
when(gcc.call()).thenReturn(new Properties());
|
||||
when(manager.getHandler(GitRepositoryHandler.TYPE_NAME)).thenReturn(handler);
|
||||
task = new GitGcTask(manager){
|
||||
|
||||
@Override
|
||||
protected Git open(File file) throws IOException
|
||||
{
|
||||
return git;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link GitGcTask#run()}.
|
||||
*
|
||||
* @throws GitAPIException
|
||||
*/
|
||||
@Test
|
||||
public void testRun() throws GitAPIException
|
||||
{
|
||||
// prepare repositories for task
|
||||
Repository unhealthy = mock(Repository.class);
|
||||
when(unhealthy.getType()).thenReturn("git");
|
||||
when(unhealthy.isHealthy()).thenReturn(Boolean.FALSE);
|
||||
|
||||
Repository invalid = mock(Repository.class);
|
||||
when(unhealthy.getType()).thenReturn("git");
|
||||
when(unhealthy.isValid()).thenReturn(Boolean.FALSE);
|
||||
|
||||
List<Repository> repositories = Lists.newArrayList(
|
||||
RepositoryTestData.create42Puzzle("git"),
|
||||
RepositoryTestData.createHeartOfGold("hg"),
|
||||
unhealthy,
|
||||
invalid
|
||||
);
|
||||
when(manager.getAll()).thenReturn(repositories);
|
||||
|
||||
// run
|
||||
task.run();
|
||||
|
||||
// gc command should only be called once
|
||||
verify(gcc).getStatistics();
|
||||
verify(gcc).call();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,14 +43,22 @@ import static org.junit.Assert.*;
|
||||
|
||||
import java.io.File;
|
||||
import sonia.scm.store.ConfigurationStoreFactory;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import sonia.scm.schedule.Scheduler;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GitRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase
|
||||
{
|
||||
|
||||
@Mock
|
||||
private Scheduler scheduler;
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -90,7 +98,7 @@ public class GitRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase
|
||||
File directory)
|
||||
{
|
||||
GitRepositoryHandler repositoryHandler = new GitRepositoryHandler(factory,
|
||||
new DefaultFileSystem());
|
||||
new DefaultFileSystem(), scheduler);
|
||||
|
||||
repositoryHandler.init(contextProvider);
|
||||
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* Copyright (c) 2014, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import sonia.scm.HandlerEventType;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GitRepositoryModifyListener}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitRepositoryModifyListenerTest {
|
||||
|
||||
private GitRepositoryModifyTestListener repositoryModifyListener;
|
||||
|
||||
/**
|
||||
* Set up test object.
|
||||
*/
|
||||
@Before
|
||||
public void setUpObjectUnderTest(){
|
||||
repositoryModifyListener = new GitRepositoryModifyTestListener();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests happy path.
|
||||
*/
|
||||
@Test
|
||||
public void testHandleEvent() {
|
||||
Repository old = RepositoryTestData.createHeartOfGold("git");
|
||||
old.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "master");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(HandlerEventType.MODIFY, current, old);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
assertNotNull(repositoryModifyListener.repository);
|
||||
assertSame(current, repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests with new default branch.
|
||||
*/
|
||||
@Test
|
||||
public void testWithNewDefaultBranch() {
|
||||
Repository old = RepositoryTestData.createHeartOfGold("git");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(HandlerEventType.MODIFY, current, old);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
assertNotNull(repositoryModifyListener.repository);
|
||||
assertSame(current, repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests with non git repositories.
|
||||
*/
|
||||
@Test
|
||||
public void testNonGitRepository(){
|
||||
Repository old = RepositoryTestData.createHeartOfGold("hg");
|
||||
old.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "master");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("hg");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(HandlerEventType.MODIFY, current, old);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
assertNull(repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests without default branch.
|
||||
*/
|
||||
@Test
|
||||
public void testWithoutDefaultBranch(){
|
||||
Repository old = RepositoryTestData.createHeartOfGold("git");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(HandlerEventType.MODIFY, current, old);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
assertNull(repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests with non modify event.
|
||||
*/
|
||||
@Test
|
||||
public void testNonModifyEvent(){
|
||||
Repository old = RepositoryTestData.createHeartOfGold("git");
|
||||
old.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "master");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(HandlerEventType.CREATE, current, old);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
assertNull(repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests with non git repositories.
|
||||
*/
|
||||
@Test
|
||||
public void testNoModification(){
|
||||
Repository old = RepositoryTestData.createHeartOfGold("git");
|
||||
old.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "master");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "master");
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(HandlerEventType.MODIFY, current, old);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
assertNull(repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
private static class GitRepositoryModifyTestListener extends GitRepositoryModifyListener {
|
||||
|
||||
private Repository repository;
|
||||
|
||||
@Override
|
||||
protected void sendClearRepositoryCacheEvent(Repository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -53,16 +53,18 @@ import static org.mockito.Mockito.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* Unit tests for {@link GitUtil}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitUtilTest
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
* Tests {@link GitUtil#checkBranchName(org.eclipse.jgit.lib.Repository, java.lang.String)} with invalid name.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@@ -76,8 +78,7 @@ public class GitUtilTest
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
* Tests {@link GitUtil#checkBranchName(org.eclipse.jgit.lib.Repository, java.lang.String)}.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@@ -92,32 +93,28 @@ public class GitUtilTest
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws GitAPIException
|
||||
* @throws IOException
|
||||
* Tests {@link GitUtil#getTagName(java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOpenJava7() throws GitAPIException, IOException
|
||||
{
|
||||
File dir = temp.newFolder();
|
||||
|
||||
Git.init().setDirectory(dir).setBare(true).call();
|
||||
|
||||
org.eclipse.jgit.lib.Repository repo = GitUtil.open(dir);
|
||||
|
||||
assertThat(repo.getFS().getClass().getName(), containsString("Java7"));
|
||||
public void testGetTagName(){
|
||||
assertNull(GitUtil.getTagName("refs/head/master"));
|
||||
assertEquals("1.0.0", GitUtil.getTagName("refs/tags/1.0.0"));
|
||||
assertEquals("super/1.0.0", GitUtil.getTagName("refs/tags/super/1.0.0"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link GitUtil#isBranch(java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testIsBranchName(){
|
||||
assertTrue(GitUtil.isBranch("refs/heads/master"));
|
||||
assertTrue(GitUtil.isBranch("refs/heads/feature/super"));
|
||||
assertFalse(GitUtil.isBranch(""));
|
||||
assertFalse(GitUtil.isBranch(null));
|
||||
assertFalse(GitUtil.isBranch("refs/tags/1.0.0"));
|
||||
assertFalse(GitUtil.isBranch("refs/heads"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param directory
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private org.eclipse.jgit.lib.Repository mockRepo(File directory)
|
||||
{
|
||||
org.eclipse.jgit.lib.Repository repo =
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
/***
|
||||
* Copyright (c) 2015, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* https://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
package sonia.scm.repository.api;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.eclipse.jgit.transport.ReceiveCommand;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GitHookBranchProvider}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GitHookBranchProviderTest {
|
||||
|
||||
@Mock
|
||||
private ReceiveCommand command;
|
||||
|
||||
private List<ReceiveCommand> commands;
|
||||
|
||||
/**
|
||||
* Prepare mocks for upcoming test.
|
||||
*/
|
||||
@Before
|
||||
public void setUpMocks(){
|
||||
commands = Lists.newArrayList(command);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link GitHookBranchProvider#getCreatedOrModified()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetCreatedOrModified(){
|
||||
List<ReceiveCommand.Type> types = Arrays.asList(
|
||||
ReceiveCommand.Type.CREATE, ReceiveCommand.Type.UPDATE, ReceiveCommand.Type.UPDATE_NONFASTFORWARD
|
||||
);
|
||||
for ( ReceiveCommand.Type type : types ){
|
||||
checkCreatedOrModified(type);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkCreatedOrModified(ReceiveCommand.Type type){
|
||||
GitHookBranchProvider provider = createGitHookBranchProvider(type, "refs/heads/hello");
|
||||
assertThat(provider.getCreatedOrModified(), Matchers.contains("hello"));
|
||||
assertThat(provider.getDeletedOrClosed(), empty());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link GitHookBranchProvider#getDeletedOrClosed()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetDeletedOrClosed(){
|
||||
GitHookBranchProvider provider = createGitHookBranchProvider(ReceiveCommand.Type.DELETE, "refs/heads/hello");
|
||||
assertThat(provider.getDeletedOrClosed(), Matchers.contains("hello"));
|
||||
assertThat(provider.getCreatedOrModified(), empty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link GitHookBranchProvider} with a tag instead of a branch.
|
||||
*/
|
||||
@Test
|
||||
public void testWithTag(){
|
||||
GitHookBranchProvider provider = createGitHookBranchProvider(ReceiveCommand.Type.CREATE, "refs/tags/1.0.0");
|
||||
assertThat(provider.getCreatedOrModified(), empty());
|
||||
assertThat(provider.getDeletedOrClosed(), empty());
|
||||
}
|
||||
|
||||
private GitHookBranchProvider createGitHookBranchProvider(ReceiveCommand.Type type, String refName){
|
||||
when(command.getType()).thenReturn(type);
|
||||
when(command.getRefName()).thenReturn(refName);
|
||||
return new GitHookBranchProvider(commands);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
/***
|
||||
* Copyright (c) 2015, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* https://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
package sonia.scm.repository.api;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import java.util.List;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.transport.ReceiveCommand;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.mockito.stubbing.OngoingStubbing;
|
||||
import sonia.scm.repository.Tag;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GitHookTagProvider}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GitHookTagProviderTest {
|
||||
|
||||
@Mock
|
||||
private ReceiveCommand command;
|
||||
|
||||
private List<ReceiveCommand> commands;
|
||||
|
||||
/**
|
||||
* Set up mocks for upcoming tests.
|
||||
*/
|
||||
@Before
|
||||
public void setUpMocks(){
|
||||
commands = Lists.newArrayList(command);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link GitHookTagProvider#getCreatedTags()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetCreatedTags() {
|
||||
String revision = "b2002b64013e54b78eac251df0672bd5d6a83aa7";
|
||||
GitHookTagProvider provider = createProvider(ReceiveCommand.Type.CREATE, "refs/tags/1.0.0", revision);
|
||||
|
||||
assertTag("1.0.0", revision, provider.getCreatedTags());
|
||||
assertThat(provider.getDeletedTags(), empty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link GitHookTagProvider#getDeletedTags()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetDeletedTags() {
|
||||
String revision = "b2002b64013e54b78eac251df0672bd5d6a83aa7";
|
||||
GitHookTagProvider provider = createProvider(ReceiveCommand.Type.DELETE, "refs/tags/1.0.0", revision);
|
||||
|
||||
assertThat(provider.getCreatedTags(), empty());
|
||||
assertTag("1.0.0", revision, provider.getDeletedTags());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link GitHookTagProvider} with a branch ref instead of a tag.
|
||||
*/
|
||||
@Test
|
||||
public void testWithBranch(){
|
||||
String revision = "b2002b64013e54b78eac251df0672bd5d6a83aa7";
|
||||
GitHookTagProvider provider = createProvider(ReceiveCommand.Type.CREATE, "refs/heads/1.0.0", revision);
|
||||
|
||||
assertThat(provider.getCreatedTags(), empty());
|
||||
assertThat(provider.getDeletedTags(), empty());
|
||||
}
|
||||
|
||||
private void assertTag(String name, String revision, List<Tag> tags){
|
||||
assertNotNull(tags);
|
||||
assertFalse(tags.isEmpty());
|
||||
assertEquals(1, tags.size());
|
||||
Tag tag = tags.get(0);
|
||||
assertEquals(name, tag.getName());
|
||||
assertEquals(revision, tag.getRevision());
|
||||
}
|
||||
|
||||
private GitHookTagProvider createProvider(ReceiveCommand.Type type, String ref, String id){
|
||||
OngoingStubbing<ObjectId> ongoing;
|
||||
if (type == ReceiveCommand.Type.CREATE){
|
||||
ongoing = when(command.getNewId());
|
||||
} else {
|
||||
ongoing = when(command.getOldId());
|
||||
}
|
||||
ongoing.thenReturn(ObjectId.fromString(id));
|
||||
|
||||
when(command.getType()).thenReturn(type);
|
||||
when(command.getRefName()).thenReturn(ref);
|
||||
|
||||
return new GitHookTagProvider(commands);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -45,14 +45,42 @@ import static org.junit.Assert.*;
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
import sonia.scm.repository.GitConstants;
|
||||
|
||||
/**
|
||||
*
|
||||
* Unit tests for {@link GitBlameCommand}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitBlameCommandTest extends AbstractGitCommandTestBase
|
||||
{
|
||||
|
||||
/**
|
||||
* Tests blame command with default branch.
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
@Test
|
||||
public void testDefaultBranch() throws IOException, RepositoryException {
|
||||
// without default branch, the repository head should be used
|
||||
BlameCommandRequest request = new BlameCommandRequest();
|
||||
request.setPath("a.txt");
|
||||
|
||||
BlameResult result = createCommand().getBlameResult(request);
|
||||
assertNotNull(result);
|
||||
assertEquals(2, result.getTotal());
|
||||
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getLine(0).getRevision());
|
||||
assertEquals("fcd0ef1831e4002ac43ea539f4094334c79ea9ec", result.getLine(1).getRevision());
|
||||
|
||||
// set default branch and test again
|
||||
repository.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "test-branch");
|
||||
result = createCommand().getBlameResult(request);
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.getTotal());
|
||||
assertEquals("3f76a12f08a6ba0dc988c68b7f0b2cd190efc3c4", result.getLine(0).getRevision());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -48,13 +48,51 @@ import static org.junit.Assert.*;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.List;
|
||||
import sonia.scm.repository.GitConstants;
|
||||
|
||||
/**
|
||||
*
|
||||
* Unit tests for {@link GitBrowseCommand}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitBrowseCommandTest extends AbstractGitCommandTestBase
|
||||
{
|
||||
|
||||
/**
|
||||
* Test browse command with default branch.
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
@Test
|
||||
public void testDefaultBranch() throws IOException, RepositoryException {
|
||||
// without default branch, the repository head should be used
|
||||
BrowserResult result = createCommand().getBrowserResult(new BrowseCommandRequest());
|
||||
assertNotNull(result);
|
||||
|
||||
List<FileObject> foList = result.getFiles();
|
||||
assertNotNull(foList);
|
||||
assertFalse(foList.isEmpty());
|
||||
assertEquals(4, foList.size());
|
||||
|
||||
assertEquals("a.txt", foList.get(0).getName());
|
||||
assertEquals("b.txt", foList.get(1).getName());
|
||||
assertEquals("c", foList.get(2).getName());
|
||||
assertEquals("f.txt", foList.get(3).getName());
|
||||
|
||||
// set default branch and fetch again
|
||||
repository.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "test-branch");
|
||||
result = createCommand().getBrowserResult(new BrowseCommandRequest());
|
||||
assertNotNull(result);
|
||||
|
||||
foList = result.getFiles();
|
||||
assertNotNull(foList);
|
||||
assertFalse(foList.isEmpty());
|
||||
assertEquals(2, foList.size());
|
||||
|
||||
assertEquals("a.txt", foList.get(0).getName());
|
||||
assertEquals("c", foList.get(1).getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
|
||||
@@ -44,14 +44,36 @@ import static org.junit.Assert.*;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import sonia.scm.repository.GitConstants;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GitCatCommand}.
|
||||
*
|
||||
* TODO add not found test
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitCatCommandTest extends AbstractGitCommandTestBase
|
||||
{
|
||||
|
||||
/**
|
||||
* Tests cat command with default branch.
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
@Test
|
||||
public void testDefaultBranch() throws IOException, RepositoryException {
|
||||
// without default branch, the repository head should be used
|
||||
CatCommandRequest request = new CatCommandRequest();
|
||||
request.setPath("a.txt");
|
||||
|
||||
assertEquals("a\nline for blame", execute(request));
|
||||
|
||||
// set default branch for repository and check again
|
||||
repository.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "test-branch");
|
||||
assertEquals("a and b", execute(request));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
|
||||
@@ -49,14 +49,48 @@ import static org.junit.Assert.*;
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import sonia.scm.repository.GitConstants;
|
||||
|
||||
/**
|
||||
*
|
||||
* Unit tests for {@link GitLogCommand}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitLogCommandTest extends AbstractGitCommandTestBase
|
||||
{
|
||||
|
||||
/**
|
||||
* Tests log command with the usage of a default branch.
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws GitAPIException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
@Test
|
||||
public void testGetDefaultBranch() throws IOException, GitAPIException, RepositoryException {
|
||||
// without default branch, the repository head should be used
|
||||
ChangesetPagingResult result = createCommand().getChangesets(new LogCommandRequest());
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(4, result.getTotal());
|
||||
assertEquals("fcd0ef1831e4002ac43ea539f4094334c79ea9ec", result.getChangesets().get(0).getId());
|
||||
assertEquals("86a6645eceefe8b9a247db5eb16e3d89a7e6e6d1", result.getChangesets().get(1).getId());
|
||||
assertEquals("592d797cd36432e591416e8b2b98154f4f163411", result.getChangesets().get(2).getId());
|
||||
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getChangesets().get(3).getId());
|
||||
|
||||
// set default branch and fetch again
|
||||
repository.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "test-branch");
|
||||
|
||||
result = createCommand().getChangesets(new LogCommandRequest());
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(3, result.getTotal());
|
||||
assertEquals("3f76a12f08a6ba0dc988c68b7f0b2cd190efc3c4", result.getChangesets().get(0).getId());
|
||||
assertEquals("592d797cd36432e591416e8b2b98154f4f163411", result.getChangesets().get(1).getId());
|
||||
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getChangesets().get(2).getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -51,7 +51,8 @@ import static org.junit.Assert.assertNotNull;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* Unit tests for {@link OutgoingCommand}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitOutgoingCommandTest extends AbstractRemoteCommandTestBase
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer. 2. Redistributions in
|
||||
* binary form must reproduce the above copyright notice, this list of
|
||||
* conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
|
||||
* nor the names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.web;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
|
||||
*/
|
||||
public class GitUserAgentProviderTest
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testParseUserAgent()
|
||||
{
|
||||
assertEquals(GitUserAgentProvider.GIT, parse("git/1.7.9.5"));
|
||||
assertEquals(GitUserAgentProvider.MSYSGIT, parse("git/1.8.3.msysgit.0"));
|
||||
assertNull(parse("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param v
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private UserAgent parse(String v)
|
||||
{
|
||||
return provider.parseUserAgent(
|
||||
Strings.nullToEmpty(v).toLowerCase(Locale.ENGLISH));
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private final GitUserAgentProvider provider = new GitUserAgentProvider();
|
||||
}
|
||||
Reference in New Issue
Block a user