mirror of
				https://github.com/scm-manager/scm-manager.git
				synced 2025-10-31 10:35:56 +01:00 
			
		
		
		
	implement SvnWorkDirFactory
(grafted from 7c6f871d771d61c6835d052e2380af49368fe6f5)
This commit is contained in:
		| @@ -0,0 +1,9 @@ | ||||
| package sonia.scm.repository; | ||||
|  | ||||
| import sonia.scm.repository.spi.SvnContext; | ||||
| import sonia.scm.repository.util.WorkdirFactory; | ||||
|  | ||||
| import java.io.File; | ||||
|  | ||||
| public interface SvnWorkDirFactory extends WorkdirFactory<File, File, SvnContext> { | ||||
| } | ||||
| @@ -0,0 +1,63 @@ | ||||
| package sonia.scm.repository.spi; | ||||
|  | ||||
| import org.apache.commons.lang.exception.CloneFailedException; | ||||
| import org.tmatesoft.svn.core.SVNException; | ||||
| import org.tmatesoft.svn.core.SVNURL; | ||||
| import org.tmatesoft.svn.core.wc2.SvnCheckout; | ||||
| import org.tmatesoft.svn.core.wc2.SvnOperationFactory; | ||||
| import org.tmatesoft.svn.core.wc2.SvnTarget; | ||||
| import sonia.scm.repository.Repository; | ||||
| import sonia.scm.repository.SvnWorkDirFactory; | ||||
| import sonia.scm.repository.util.SimpleWorkdirFactory; | ||||
| import sonia.scm.repository.util.WorkdirProvider; | ||||
|  | ||||
| import javax.inject.Inject; | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
|  | ||||
| public class SimpleSvnWorkDirFactory extends SimpleWorkdirFactory<File, File, SvnContext> implements SvnWorkDirFactory { | ||||
|  | ||||
|   @Inject | ||||
|   public SimpleSvnWorkDirFactory(WorkdirProvider workdirProvider) { | ||||
|     super(workdirProvider); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   protected Repository getScmRepository(SvnContext context) { | ||||
|     return null; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   protected void closeRepository(File workingCopy) { | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   protected void closeWorkdirInternal(File workdir) { | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   protected ParentAndClone<File, File> cloneRepository(SvnContext context, File workingCopy, String initialBranch) throws IOException { | ||||
|  | ||||
|     final SvnOperationFactory svnOperationFactory = new SvnOperationFactory(); | ||||
|  | ||||
|     SVNURL source; | ||||
|     try { | ||||
|       source = SVNURL.fromFile(context.getDirectory()); | ||||
|     } catch (SVNException ex) { | ||||
|       throw new CloneFailedException(ex.getMessage()); | ||||
|     } | ||||
|  | ||||
|     try { | ||||
|       final SvnCheckout checkout = svnOperationFactory.createCheckout(); | ||||
|       checkout.setSingleTarget(SvnTarget.fromFile(workingCopy)); | ||||
|       checkout.setSource(SvnTarget.fromURL(source)); | ||||
|       checkout.run(); | ||||
|     } catch (SVNException ex) { | ||||
|       throw new CloneFailedException(ex.getMessage()); | ||||
|     } finally { | ||||
|       svnOperationFactory.dispose(); | ||||
|     } | ||||
|  | ||||
|     return new ParentAndClone<>(workingCopy, workingCopy); | ||||
|   } | ||||
| } | ||||
| @@ -0,0 +1,69 @@ | ||||
| package sonia.scm.repository.spi; | ||||
|  | ||||
| import org.junit.Before; | ||||
| import org.junit.Rule; | ||||
| import org.junit.Test; | ||||
| import org.junit.rules.TemporaryFolder; | ||||
| import org.tmatesoft.svn.core.SVNException; | ||||
| import sonia.scm.repository.util.WorkdirProvider; | ||||
| import sonia.scm.repository.util.WorkingCopy; | ||||
|  | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
|  | ||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||
|  | ||||
| public class SimpleSvnWorkDirFactoryTest extends AbstractSvnCommandTestBase { | ||||
|  | ||||
|   @Rule | ||||
|   public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||||
|  | ||||
|   // keep this so that it will not be garbage collected (Transport keeps this in a week reference) | ||||
|   private WorkdirProvider workdirProvider; | ||||
|  | ||||
|   @Before | ||||
|   public void initWorkDirProvider() throws IOException { | ||||
|     workdirProvider = new WorkdirProvider(temporaryFolder.newFolder()); | ||||
|   } | ||||
|  | ||||
|   @Test | ||||
|   public void shouldCheckoutLatestRevision() throws SVNException, IOException { | ||||
|     SimpleSvnWorkDirFactory factory = new SimpleSvnWorkDirFactory(workdirProvider); | ||||
|  | ||||
|     try (WorkingCopy<File, File> workingCopy = factory.createWorkingCopy(createContext(), null)) { | ||||
|       assertThat(new File(workingCopy.getWorkingRepository(), "a.txt")) | ||||
|         .exists() | ||||
|         .isFile() | ||||
|         .hasContent("a and b\nline for blame test"); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Test | ||||
|   public void cloneFromPoolshouldNotBeReused() { | ||||
|     SimpleSvnWorkDirFactory factory = new SimpleSvnWorkDirFactory(workdirProvider); | ||||
|  | ||||
|     File firstDirectory; | ||||
|     try (WorkingCopy<File, File> workingCopy = factory.createWorkingCopy(createContext(), null)) { | ||||
|       firstDirectory = workingCopy.getDirectory(); | ||||
|     } | ||||
|     try (WorkingCopy<File, File> workingCopy = factory.createWorkingCopy(createContext(), null)) { | ||||
|       File secondDirectory = workingCopy.getDirectory(); | ||||
|       assertThat(secondDirectory).isNotEqualTo(firstDirectory); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Test | ||||
|   public void shouldDeleteCloneOnClose() { | ||||
|     SimpleSvnWorkDirFactory factory = new SimpleSvnWorkDirFactory(workdirProvider); | ||||
|  | ||||
|     File directory; | ||||
|     File workingRepository; | ||||
|     try (WorkingCopy<File, File> workingCopy = factory.createWorkingCopy(createContext(), null)) { | ||||
|       directory = workingCopy.getDirectory(); | ||||
|       workingRepository = workingCopy.getWorkingRepository(); | ||||
|  | ||||
|     } | ||||
|     assertThat(directory).doesNotExist(); | ||||
|     assertThat(workingRepository).doesNotExist(); | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user