mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
#998 git: set repository head to default branch
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package sonia.scm.repository;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.Files;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GitHeadHandlerTest {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Mock
|
||||
private GitRepositoryHandler repositoryHandler;
|
||||
|
||||
@InjectMocks
|
||||
private GitHeadHandler headHandler;
|
||||
|
||||
@Test
|
||||
public void testResolve() throws IOException {
|
||||
Repository repository = RepositoryTestData.createHeartOfGold("git");
|
||||
create(repository, "master");
|
||||
|
||||
String head = headHandler.resolve(repository);
|
||||
assertEquals("master", head);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testModify() throws IOException {
|
||||
Repository repository = RepositoryTestData.createHeartOfGold("git");
|
||||
File file = create(repository, "master");
|
||||
|
||||
headHandler.modify(repository, "develop");
|
||||
|
||||
assertEquals("ref: refs/heads/develop", Files.readFirstLine(file, Charsets.UTF_8));
|
||||
}
|
||||
|
||||
private File create(Repository repository, String head) throws IOException {
|
||||
File directory = temporaryFolder.newFolder();
|
||||
File headFile = new File(directory, "HEAD");
|
||||
Files.write(String.format("ref: refs/heads/%s\n", head), headFile, Charsets.UTF_8);
|
||||
|
||||
when(repositoryHandler.getDirectory(repository)).thenReturn(directory);
|
||||
|
||||
return headFile;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
/**
|
||||
* 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,
|
||||
@@ -13,7 +13,7 @@
|
||||
* 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
|
||||
@@ -24,34 +24,42 @@
|
||||
* 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 static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import sonia.scm.HandlerEvent;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GitRepositoryModifyListener}.
|
||||
*
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GitRepositoryModifyListenerTest {
|
||||
|
||||
@Mock
|
||||
private GitHeadResolver headResolver;
|
||||
|
||||
@Mock
|
||||
private GitHeadModifier headModifier;
|
||||
|
||||
@InjectMocks
|
||||
private GitRepositoryModifyTestListener repositoryModifyListener;
|
||||
|
||||
/**
|
||||
* Set up test object.
|
||||
*/
|
||||
@Before
|
||||
public void setUpObjectUnderTest(){
|
||||
repositoryModifyListener = new GitRepositoryModifyTestListener();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests happy path.
|
||||
@@ -62,14 +70,14 @@ public class GitRepositoryModifyListenerTest {
|
||||
old.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "master");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.MODIFY);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
|
||||
assertNotNull(repositoryModifyListener.repository);
|
||||
assertSame(current, repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests with new default branch.
|
||||
*/
|
||||
@@ -78,14 +86,14 @@ public class GitRepositoryModifyListenerTest {
|
||||
Repository old = RepositoryTestData.createHeartOfGold("git");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.MODIFY);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
|
||||
assertNotNull(repositoryModifyListener.repository);
|
||||
assertSame(current, repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests with non git repositories.
|
||||
*/
|
||||
@@ -95,13 +103,13 @@ public class GitRepositoryModifyListenerTest {
|
||||
old.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "master");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("hg");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.MODIFY);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
|
||||
assertNull(repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests without default branch.
|
||||
*/
|
||||
@@ -109,13 +117,13 @@ public class GitRepositoryModifyListenerTest {
|
||||
public void testWithoutDefaultBranch(){
|
||||
Repository old = RepositoryTestData.createHeartOfGold("git");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.MODIFY);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
|
||||
assertNull(repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests with non modify event.
|
||||
*/
|
||||
@@ -125,13 +133,13 @@ public class GitRepositoryModifyListenerTest {
|
||||
old.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "master");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.CREATE);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
|
||||
assertNull(repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests with non git repositories.
|
||||
*/
|
||||
@@ -144,20 +152,52 @@ public class GitRepositoryModifyListenerTest {
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.MODIFY);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
|
||||
assertNull(repositoryModifyListener.repository);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testModifyRepositoryHead() {
|
||||
Repository old = RepositoryTestData.createHeartOfGold("git");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
when(headResolver.resolve(current)).thenReturn("master");
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.MODIFY);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
verify(headModifier).modify(current, "develop");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithEqualHeads() {
|
||||
Repository old = RepositoryTestData.createHeartOfGold("git");
|
||||
Repository current = RepositoryTestData.createHeartOfGold("git");
|
||||
current.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "develop");
|
||||
|
||||
when(headResolver.resolve(current)).thenReturn("develop");
|
||||
|
||||
RepositoryModificationEvent event = new RepositoryModificationEvent(current, old, HandlerEvent.MODIFY);
|
||||
repositoryModifyListener.handleEvent(event);
|
||||
|
||||
verify(headModifier, never()).modify(current, "develop");
|
||||
}
|
||||
|
||||
private static class GitRepositoryModifyTestListener extends GitRepositoryModifyListener {
|
||||
|
||||
|
||||
private Repository repository;
|
||||
|
||||
|
||||
public GitRepositoryModifyTestListener(GitHeadResolver headResolver, GitHeadModifier headModifier) {
|
||||
super(headResolver, headModifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void sendClearRepositoryCacheEvent(Repository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user