-
-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathSubversionSCMUnitTest.java
More file actions
135 lines (107 loc) · 5.24 KB
/
Copy pathSubversionSCMUnitTest.java
File metadata and controls
135 lines (107 loc) · 5.24 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
package hudson.scm;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyMapOf;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.remoting.VirtualChannel;
import hudson.scm.SubversionSCM.ModuleLocation;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
/**
* Unit tests for {@link SubversionSCM}.
*
* ({@link SubversionSCMTest} is more like an integration test)
*
* @author kutzi
*/
public class SubversionSCMUnitTest {
@Test
@Bug(12113)
public void testLocalDirectoryIsExpandedWithEnvVars() {
FilePath root = new FilePath((VirtualChannel)null, "root");
EnvVars envVars = new EnvVars();
envVars.put("BRANCH", "test");
SubversionSCM scm = new SubversionSCM("dummyUrl");
FilePath resolvedRoot = scm._getModuleRoot(root, "$BRANCH/someMorePath", envVars);
String expected = String.format("root%stest/someMorePath", System.getProperty("file.separator"));
Assert.assertEquals(expected, resolvedRoot.getRemote());
}
@SuppressWarnings("deprecation")
@Test
public void shouldSetEnvironmentVariablesWithSingleSvnModule() throws IOException {
// GIVEN an scm with a single module location
SubversionSCM scm = mockSCMForBuildEnvVars();
ModuleLocation[] singleLocation = new ModuleLocation[] {new ModuleLocation("/remotepath", "")};
when(scm.getLocations(any(EnvVars.class), any(AbstractBuild.class))).thenReturn(singleLocation);
Map<String, Long> revisions = new HashMap<String, Long>();
revisions.put("/remotepath", 4711L);
when(scm.parseSvnRevisionFile(any(AbstractBuild.class))).thenReturn(revisions);
// WHEN envVars are build
AbstractBuild<?,?> build = mock(AbstractBuild.class);
Map<String, String> envVars = new HashMap<String, String>();
scm.buildEnvVars(build, envVars);
// THEN: we have the (legacy) SVN_URL and SVN_REVISION vars
assertThat(envVars.get("SVN_URL"), is("/remotepath"));
assertThat(envVars.get("SVN_REVISION"), is("4711"));
// AND: also the index-based vars
assertThat(envVars.get("SVN_URL_1"), is("/remotepath"));
assertThat(envVars.get("SVN_REVISION_1"), is("4711"));
}
@SuppressWarnings("deprecation")
@Test
public void shouldSetEnvironmentVariablesWithMultipleSvnModules() throws IOException {
// GIVEN an scm with a 2 module locations
SubversionSCM scm = mockSCMForBuildEnvVars();
ModuleLocation[] locations = new ModuleLocation[] {
new ModuleLocation("/remotepath1", ""),
new ModuleLocation("/remotepath2", "")};
when(scm.getLocations(any(EnvVars.class), any(AbstractBuild.class))).thenReturn(locations);
Map<String, Long> revisions = new HashMap<String, Long>();
revisions.put("/remotepath1", 4711L);
revisions.put("/remotepath2", 42L);
when(scm.parseSvnRevisionFile(any(AbstractBuild.class))).thenReturn(revisions);
// WHEN envVars are build
AbstractBuild<?,?> build = mock(AbstractBuild.class);
Map<String, String> envVars = new HashMap<String, String>();
scm.buildEnvVars(build, envVars);
// THEN: we have the SVN_URL_n and SVN_REVISION_n vars
assertThat(envVars.get("SVN_URL_1"), is("/remotepath1"));
assertThat(envVars.get("SVN_REVISION_1"), is("4711"));
assertThat(envVars.get("SVN_URL_2"), is("/remotepath2"));
assertThat(envVars.get("SVN_REVISION_2"), is("42"));
}
@SuppressWarnings("deprecation")
@Test
public void shouldSetEnvironmentVariablesMaximumRevision() throws IOException {
// GIVEN an scm with a 2 module locations
SubversionSCM scm = mockSCMForBuildEnvVars();
ModuleLocation[] singleLocation = new ModuleLocation[] {new ModuleLocation("/remotepath", "")};
when(scm.getLocations(any(EnvVars.class), any(AbstractBuild.class))).thenReturn(singleLocation);
Map<String, Long> revisions = new HashMap<String, Long>();
revisions.put("/remotepath", 4711L);
revisions.put("/remotepath2", 42L);
revisions.put("/remotepath3", 9920L);
when(scm.parseSvnRevisionFile(any(AbstractBuild.class))).thenReturn(revisions);
// WHEN envVars are build
AbstractBuild<?,?> build = mock(AbstractBuild.class);
Map<String, String> envVars = new HashMap<String, String>();
scm.buildEnvVars(build, envVars);
// THEN: we have the SVN_MAX_REVISION var
assertThat(envVars.get("SVN_MAX_REVISION"), is("9920"));
}
private SubversionSCM mockSCMForBuildEnvVars() {
SubversionSCM scm = mock(SubversionSCM.class);
doCallRealMethod().when(scm).buildEnvVars(any(AbstractBuild.class), anyMapOf(String.class, String.class));
return scm;
}
}