Skip to content

Commit 2ec2554

Browse files
committed
tests
1 parent e856f36 commit 2ec2554

File tree

2 files changed

+411
-109
lines changed

2 files changed

+411
-109
lines changed

src/main/java/org/scm4j/vcs/svn/SVNVCS.java

Lines changed: 71 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,28 @@
11
package org.scm4j.vcs.svn;
22

3-
import java.io.ByteArrayOutputStream;
4-
import java.io.File;
5-
import java.io.FileWriter;
6-
import java.io.UnsupportedEncodingException;
7-
import java.nio.charset.StandardCharsets;
8-
import java.util.ArrayList;
9-
import java.util.Collection;
10-
import java.util.Collections;
11-
import java.util.HashSet;
12-
import java.util.List;
13-
import java.util.Set;
14-
153
import org.apache.commons.io.FileUtils;
16-
import org.scm4j.vcs.api.IVCS;
17-
import org.scm4j.vcs.api.VCSChangeType;
18-
import org.scm4j.vcs.api.VCSCommit;
19-
import org.scm4j.vcs.api.VCSDiffEntry;
20-
import org.scm4j.vcs.api.VCSMergeResult;
21-
import org.scm4j.vcs.api.WalkDirection;
4+
import org.scm4j.vcs.api.*;
225
import org.scm4j.vcs.api.exceptions.EVCSBranchExists;
236
import org.scm4j.vcs.api.exceptions.EVCSException;
247
import org.scm4j.vcs.api.exceptions.EVCSFileNotFound;
258
import org.scm4j.vcs.api.workingcopy.IVCSLockedWorkingCopy;
269
import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
2710
import org.scm4j.vcs.api.workingcopy.IVCSWorkspace;
28-
import org.tmatesoft.svn.core.ISVNLogEntryHandler;
29-
import org.tmatesoft.svn.core.SVNCommitInfo;
30-
import org.tmatesoft.svn.core.SVNDepth;
31-
import org.tmatesoft.svn.core.SVNDirEntry;
32-
import org.tmatesoft.svn.core.SVNException;
33-
import org.tmatesoft.svn.core.SVNLogEntry;
34-
import org.tmatesoft.svn.core.SVNNodeKind;
35-
import org.tmatesoft.svn.core.SVNProperties;
36-
import org.tmatesoft.svn.core.SVNURL;
11+
import org.tmatesoft.svn.core.*;
3712
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;
3813
import org.tmatesoft.svn.core.auth.SVNAuthentication;
3914
import org.tmatesoft.svn.core.auth.SVNPasswordAuthentication;
4015
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
4116
import org.tmatesoft.svn.core.io.SVNRepository;
4217
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
43-
import org.tmatesoft.svn.core.wc.ISVNConflictHandler;
44-
import org.tmatesoft.svn.core.wc.ISVNOptions;
45-
import org.tmatesoft.svn.core.wc.SVNClientManager;
46-
import org.tmatesoft.svn.core.wc.SVNConflictChoice;
47-
import org.tmatesoft.svn.core.wc.SVNConflictDescription;
48-
import org.tmatesoft.svn.core.wc.SVNConflictResult;
49-
import org.tmatesoft.svn.core.wc.SVNCopyClient;
50-
import org.tmatesoft.svn.core.wc.SVNCopySource;
51-
import org.tmatesoft.svn.core.wc.SVNDiffClient;
52-
import org.tmatesoft.svn.core.wc.SVNRevision;
53-
import org.tmatesoft.svn.core.wc.SVNRevisionRange;
54-
import org.tmatesoft.svn.core.wc.SVNStatusType;
55-
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
56-
import org.tmatesoft.svn.core.wc.SVNWCClient;
57-
import org.tmatesoft.svn.core.wc.SVNWCUtil;
58-
import org.tmatesoft.svn.core.wc2.ISvnObjectReceiver;
59-
import org.tmatesoft.svn.core.wc2.SvnDiff;
60-
import org.tmatesoft.svn.core.wc2.SvnDiffStatus;
61-
import org.tmatesoft.svn.core.wc2.SvnDiffSummarize;
62-
import org.tmatesoft.svn.core.wc2.SvnOperationFactory;
63-
import org.tmatesoft.svn.core.wc2.SvnTarget;
18+
import org.tmatesoft.svn.core.wc.*;
19+
import org.tmatesoft.svn.core.wc2.*;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.File;
23+
import java.io.FileWriter;
24+
import java.nio.charset.StandardCharsets;
25+
import java.util.*;
6426

6527
public class SVNVCS implements IVCS {
6628
private static final int SVN_PATH_IS_NOT_WORKING_COPY_ERROR_CODE = 155007;
@@ -77,7 +39,11 @@ public class SVNVCS implements IVCS {
7739

7840
public static final String MASTER_PATH= "trunk/";
7941
public static final String BRANCHES_PATH = "branches/";
80-
private static final String SVN_VCS_TYPE_STRING = "svn";
42+
public static final String SVN_VCS_TYPE_STRING = "svn";
43+
44+
public void setClientManager(SVNClientManager clientManager) {
45+
this.clientManager = clientManager;
46+
}
8147

8248
public SVNClientManager getClientManager() {
8349
return clientManager;
@@ -86,6 +52,18 @@ public SVNClientManager getClientManager() {
8652
public ISVNOptions getOptions() {
8753
return options;
8854
}
55+
56+
public SVNURL getTrunkSVNUrl() {
57+
return trunkSVNUrl;
58+
}
59+
60+
public SVNRepository getSVNRepository() {
61+
return repository;
62+
}
63+
64+
public void setSVNRepository(SVNRepository repository) {
65+
this.repository = repository;
66+
}
8967

9068
public SVNVCS(IVCSRepositoryWorkspace repo, String user, String password) {
9169
this.repo = repo;
@@ -110,23 +88,23 @@ public SVNVCS(IVCSRepositoryWorkspace repo, String user, String password) {
11088
options, repository.getAuthenticationManager());
11189
}
11290

113-
public SVNRepository getRepository() {
114-
return repository;
115-
}
116-
117-
private SVNURL getBranchUrl(String branchPath) throws SVNException {
91+
public SVNURL getBranchUrl(String branchPath) throws Exception {
11892
return SVNURL.parseURIEncoded(repoUrl + getBranchName(branchPath));
11993
}
94+
95+
12096

12197
@Override
122-
public void createBranch(String srcBranchName, String dstBranchName, String commitMessage) {
98+
public void createBranch(String srcBranchName, String dstBranchName, String commitMessage) throws EVCSBranchExists {
99+
SVNURL fromUrl;
100+
SVNURL toUrl;
123101
try {
124-
SVNURL fromUrl = getBranchUrl(srcBranchName);
125-
SVNURL toUrl = getBranchUrl(dstBranchName);
126-
createBranch(fromUrl, toUrl, commitMessage);
127-
} catch (SVNException e) {
128-
throw new EVCSException(e);
129-
}
102+
fromUrl = getBranchUrl(srcBranchName);
103+
toUrl = getBranchUrl(dstBranchName);
104+
} catch (Exception e) {
105+
throw new RuntimeException(e);
106+
}
107+
createBranch(fromUrl, toUrl, commitMessage);
130108
}
131109

132110
public void createBranch(SVNURL fromUrl, SVNURL toUrl, String commitMessage) {
@@ -164,6 +142,8 @@ public void deleteBranch(String branchName, String commitMessage) {
164142
.doDelete(new SVNURL[] { getBranchUrl(branchName) }, commitMessage);
165143
} catch (SVNException e) {
166144
throw new EVCSException(e);
145+
} catch (Exception e) {
146+
throw new RuntimeException(e);
167147
}
168148
}
169149

@@ -225,7 +205,7 @@ public SVNWCClient getRevertClient(DefaultSVNOptions options) {
225205
return new SVNWCClient(authManager, options);
226206
}
227207

228-
public void checkout(SVNURL sourceUrl, File destPath) throws SVNException {
208+
public void checkout(SVNURL sourceUrl, File destPath) throws Exception {
229209
SVNUpdateClient updateClient = clientManager.getUpdateClient();
230210
updateClient.setIgnoreExternals(false);
231211
if (isWorkingCopyInited(destPath)) {
@@ -236,7 +216,7 @@ public void checkout(SVNURL sourceUrl, File destPath) throws SVNException {
236216
}
237217
}
238218

239-
private boolean isWorkingCopyInited(File destPath) {
219+
public boolean isWorkingCopyInited(File destPath) {
240220
try {
241221
clientManager.getStatusClient().doStatus(destPath, false);
242222
return true;
@@ -251,7 +231,7 @@ private boolean isWorkingCopyInited(File destPath) {
251231

252232
@Override
253233
public void setCredentials(String user, String password) {
254-
userPassAuth = SVNPasswordAuthentication.newInstance(user, password.toCharArray(), true, trunkSVNUrl, false);
234+
userPassAuth = SVNPasswordAuthentication.newInstance(user, password == null ? null : password.toCharArray(), true, trunkSVNUrl, false);
255235
authManager.setAuthentications(new SVNAuthentication[] {userPassAuth});
256236
}
257237

@@ -263,17 +243,17 @@ public void setProxy(String host, int port, String proxyUser, String proxyPasswo
263243

264244
@Override
265245
public String getFileContent(String branchName, String filePath, String encoding) {
266-
ByteArrayOutputStream baos = new ByteArrayOutputStream( );
246+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
267247
try {
268-
repository.getFile(new File(getBranchName(branchName), filePath).getPath().replace("\\", "/"),
248+
repository.getFile(new File(getBranchName(branchName), filePath).getPath().replace("\\", "/"),
269249
-1, new SVNProperties(), baos);
270250
return baos.toString(encoding);
271251
} catch (SVNException e) {
272252
if (e.getErrorMessage().getErrorCode().getCode() == SVN_FILE_NOT_FOUND_ERROR_CODE) {
273253
throw new EVCSFileNotFound(e);
274254
}
275255
throw new EVCSException(e);
276-
} catch (UnsupportedEncodingException e) {
256+
} catch (Exception e) {
277257
throw new RuntimeException(e);
278258
}
279259

@@ -337,7 +317,7 @@ public String getRepoUrl() {
337317
}
338318

339319
private List<VCSDiffEntry> fillUnifiedDiffs(final String srcBranchName, final String dstBranchName, List<VCSDiffEntry> entries)
340-
throws SVNException {
320+
throws Exception {
341321
List<VCSDiffEntry> res = new ArrayList<>();
342322
for (VCSDiffEntry entry : entries) {
343323
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -363,16 +343,12 @@ private List<VCSDiffEntry> fillUnifiedDiffs(final String srcBranchName, final St
363343
diff.setOutput(baos);
364344
diff.run();
365345

366-
try {
367-
res.add(new VCSDiffEntry(entry.getFilePath(), entry.getChangeType(), baos.toString("UTF-8")));
368-
} catch (UnsupportedEncodingException e) {
369-
throw new RuntimeException(e);
370-
}
346+
res.add(new VCSDiffEntry(entry.getFilePath(), entry.getChangeType(), baos.toString("UTF-8")));
371347
}
372348
return res;
373349
}
374350

375-
private SVNLogEntry getBranchFirstCommit(final String branchPath) throws SVNException {
351+
protected SVNLogEntry getBranchFirstCommit(final String branchPath) throws Exception {
376352
final List<SVNLogEntry> logEntries = new ArrayList<>();
377353
repository.log(new String[] { getBranchName(branchPath) }, -1 /* start from head descending */,
378354
0, true, true, -1, new ISVNLogEntryHandler() {
@@ -386,7 +362,7 @@ public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
386362

387363

388364
private List<VCSDiffEntry> getDiffEntries(final String srcBranchName, final String dstBranchName)
389-
throws SVNException {
365+
throws Exception {
390366
final SvnOperationFactory svnOperationFactory = new SvnOperationFactory();
391367
final SvnDiffSummarize summarizeDiff = svnOperationFactory.createDiffSummarize();
392368
final List<VCSDiffEntry> res = new ArrayList<>();
@@ -405,23 +381,25 @@ public void receive(SvnTarget target, SvnDiffStatus diffStatus) throws SVNExcept
405381
res.add(entry);
406382
}
407383

408-
private VCSChangeType SVNChangeTypeToVCSChangeType(SVNStatusType modificationType) {
409-
if (SVNStatusType.STATUS_ADDED.equals(modificationType)) {
410-
return VCSChangeType.ADD;
411-
} else if (SVNStatusType.STATUS_DELETED.equals(modificationType)) {
412-
return VCSChangeType.DELETE;
413-
} else if (SVNStatusType.STATUS_MODIFIED.equals(modificationType)) {
414-
return VCSChangeType.MODIFY;
415-
} else {
416-
return VCSChangeType.UNKNOWN;
417-
}
418-
}
384+
419385
});
420386
summarizeDiff.run();
421387

422388
return res;
423389
}
424390

391+
public VCSChangeType SVNChangeTypeToVCSChangeType(SVNStatusType modificationType) {
392+
if (SVNStatusType.STATUS_ADDED.equals(modificationType)) {
393+
return VCSChangeType.ADD;
394+
} else if (SVNStatusType.STATUS_DELETED.equals(modificationType)) {
395+
return VCSChangeType.DELETE;
396+
} else if (SVNStatusType.STATUS_MODIFIED.equals(modificationType)) {
397+
return VCSChangeType.MODIFY;
398+
} else {
399+
return VCSChangeType.UNKNOWN;
400+
}
401+
}
402+
425403
@Override
426404
public List<VCSDiffEntry> getBranchesDiff(final String srcBranchName, final String dstBranchName) {
427405
try {
@@ -452,7 +430,7 @@ public Set<String> getBranches() {
452430
}
453431
}
454432

455-
private void addTrunkIfExists(Set<String> res) {
433+
protected void addTrunkIfExists(Set<String> res) {
456434
try {
457435
if (repository.checkPath(MASTER_PATH, -1) == SVNNodeKind.DIR) {
458436
res.add(MASTER_PATH.replace("/", ""));
@@ -463,7 +441,7 @@ private void addTrunkIfExists(Set<String> res) {
463441

464442
}
465443

466-
private void listEntries(Set<String> entries, String path) throws SVNException {
444+
public void listEntries(Set<String> entries, String path) throws Exception {
467445
@SuppressWarnings("unchecked")
468446
Collection<SVNDirEntry> subEntries = repository.getDir(path, -1, null, (Collection<SVNDirEntry>) null);
469447
for (SVNDirEntry entry : subEntries) {
@@ -501,13 +479,11 @@ public String getVCSTypeString() {
501479

502480
@Override
503481
public VCSCommit removeFile(String branchName, String filePath, String commitMessage) {
504-
try {
505-
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
506-
SVNCommitInfo res = clientManager
507-
.getCommitClient()
508-
.doDelete(new SVNURL[] {getBranchUrl(branchName).appendPath(filePath, true)}, commitMessage);
509-
return new VCSCommit(Long.toString(res.getNewRevision()), commitMessage, res.getAuthor());
510-
}
482+
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
483+
SVNCommitInfo res = clientManager
484+
.getCommitClient()
485+
.doDelete(new SVNURL[] {getBranchUrl(branchName).appendPath(filePath, true)}, commitMessage);
486+
return new VCSCommit(Long.toString(res.getNewRevision()), commitMessage, res.getAuthor());
511487
} catch (SVNException e) {
512488
throw new EVCSException(e);
513489
} catch (Exception e) {

0 commit comments

Comments
 (0)