-
Notifications
You must be signed in to change notification settings - Fork 26
/
SvnMirrorCommandTest.java
122 lines (102 loc) · 4.58 KB
/
SvnMirrorCommandTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.repository.spi;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import sonia.scm.repository.RepositoryTestData;
import sonia.scm.repository.api.MirrorCommandResult;
import sonia.scm.repository.api.SimpleUsernamePasswordCredential;
import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.io.IOException;
import java.util.function.Consumer;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(MockitoJUnitRunner.class)
public class SvnMirrorCommandTest extends AbstractSvnCommandTestBase {
@Mock
private X509TrustManager trustManager;
private SvnContext emptyContext;
@Before
public void bendContextToNewRepository() throws IOException, SVNException {
emptyContext = createEmptyContext();
}
@Test
public void shouldDoInitialMirror() {
MirrorCommandResult result = callMirror(emptyContext, repositoryDirectory, c -> {
});
assertThat(result.isSuccess()).isTrue();
}
@Test
public void shouldDoMirrorUpdate() throws SVNException, IOException {
File newRepoDir = tempFolder.newFolder();
SVNRepositoryFactory.createLocalRepository(newRepoDir, true, true);
MirrorCommandResult result = callMirrorUpdate(emptyContext, createContext().getDirectory());
assertThat(result.isSuccess()).isTrue();
assertThat(result.getLog()).contains("Updated from revision 0 to revision 1");
}
@Test
public void shouldUseCredentials() throws IOException, SVNException {
File newRepoDir = tempFolder.newFolder();
SVNRepositoryFactory.createLocalRepository(newRepoDir, true, true);
MirrorCommandResult result = callMirror(emptyContext, newRepoDir, createCredential("svnadmin", "secret"));
assertThat(result.isSuccess()).isTrue();
}
private MirrorCommandResult callMirrorUpdate(SvnContext context, File source) {
MirrorCommandRequest request = new MirrorCommandRequest();
SvnMirrorCommand command = createMirrorCommand(context, source, request);
return command.update(request);
}
private MirrorCommandResult callMirror(SvnContext context, File source, Consumer<MirrorCommandRequest> consumer) {
MirrorCommandRequest request = new MirrorCommandRequest();
SvnMirrorCommand command = createMirrorCommand(context, source, request);
consumer.accept(request);
return command.mirror(request);
}
private SvnMirrorCommand createMirrorCommand(SvnContext context, File source, MirrorCommandRequest request) {
return new SvnMirrorCommand(context, trustManager, c ->
{
try {
request.setSourceUrl(SVNURL.fromFile(c).toString());
return SVNURL.fromFile(source);
} catch (SVNException e) {
throw new IllegalStateException(e);
}
});
}
private Consumer<MirrorCommandRequest> createCredential(String username, String password) {
return request -> request.setCredentials(singletonList(new SimpleUsernamePasswordCredential(username, password.toCharArray())));
}
private SvnContext createEmptyContext() throws SVNException, IOException {
File dir = tempFolder.newFolder();
SVNRepositoryFactory.createLocalRepository(dir, true, true);
return new SvnContext(RepositoryTestData.createHappyVerticalPeopleTransporter(), dir);
}
}