Skip to content

Commit 8fd1c56

Browse files
danccdancc
dancc
authored and
dancc
committed
Ninth and final pass at enabling PMD.
1 parent 177af70 commit 8fd1c56

24 files changed

+778
-711
lines changed

src/main/java/com/cdancy/bitbucket/rest/domain/branch/Type.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ public static Type create(final TypeId id,
5757
final String prefix,
5858
final Boolean enabled) {
5959

60+
final boolean isEnabled = (enabled != null) ? enabled : false; //NOPMD
6061
return new AutoValue_Type(id,
6162
displayName,
6263
prefix,
63-
enabled || false);
64+
isEnabled);
6465
}
6566
}

src/test/java/com/cdancy/bitbucket/rest/BaseBitbucketMockTest.java

+42-29
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@
4747
public class BaseBitbucketMockTest {
4848

4949
protected final String restBasePath = "/rest/api/";
50-
protected String provider;
50+
protected final String provider;
5151
private final JsonParser parser = new JsonParser();
5252

5353
public BaseBitbucketMockTest() {
5454
provider = "bitbucket";
5555
}
5656

57-
public BitbucketApi api(URL url) {
57+
public BitbucketApi api(final URL url) {
5858
return ContextBuilder.newBuilder(provider).endpoint(url.toString()).overrides(setupProperties())
5959
.buildApi(BitbucketApi.class);
6060
}
6161

6262
protected Properties setupProperties() {
63-
Properties properties = new Properties();
63+
final Properties properties = new Properties();
6464
properties.setProperty(Constants.PROPERTY_MAX_RETRIES, "0");
6565
return properties;
6666
}
@@ -73,7 +73,7 @@ protected Properties setupProperties() {
7373
* if unable to start/play server
7474
*/
7575
public static MockWebServer mockWebServer() throws IOException {
76-
MockWebServer server = new MockWebServer();
76+
final MockWebServer server = new MockWebServer();
7777
server.play();
7878
return server;
7979
}
@@ -85,26 +85,26 @@ public static MockWebServer mockWebServer() throws IOException {
8585
* String representation of a given resource
8686
* @return payload in String form
8787
*/
88-
public String payloadFromResource(String resource) {
88+
public String payloadFromResource(final String resource) {
8989
try {
9090
return new String(toStringAndClose(getClass().getResourceAsStream(resource)).getBytes(Charsets.UTF_8));
9191
} catch (IOException e) {
9292
throw Throwables.propagate(e);
9393
}
9494
}
9595

96-
private static Map<String, String> extractParams(String path) {
96+
private static Map<String, String> extractParams(final String path) {
9797

98-
int qmIndex = path.indexOf('?');
98+
final int qmIndex = path.indexOf('?');
9999
if (qmIndex <= 0) {
100100
return ImmutableMap.of();
101101
}
102102

103-
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
103+
final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
104104

105-
String[] params = path.substring(qmIndex + 1).split("&");
106-
for (String param : params) {
107-
String[] keyValue = param.split("=", 2);
105+
final String[] params = path.substring(qmIndex + 1).split("&");
106+
for (final String param : params) {
107+
final String[] keyValue = param.split("=", 2);
108108
if (keyValue.length > 1) {
109109
builder.put(keyValue[0], keyValue[1]);
110110
}
@@ -113,49 +113,62 @@ private static Map<String, String> extractParams(String path) {
113113
return builder.build();
114114
}
115115

116-
protected RecordedRequest assertSent(MockWebServer server, String method, String path) throws InterruptedException {
116+
protected RecordedRequest assertSent(final MockWebServer server,
117+
final String method,
118+
final String path) throws InterruptedException {
119+
117120
return assertSent(server, method, path, ImmutableMap.<String, Void> of());
118121
}
119122

120-
protected RecordedRequest assertSent(MockWebServer server, String method, String expectedPath, Map<String, ?> queryParams)
121-
throws InterruptedException {
123+
protected RecordedRequest assertSent(final MockWebServer server,
124+
final String method,
125+
final String expectedPath,
126+
final Map<String, ?> queryParams) throws InterruptedException {
122127

123-
RecordedRequest request = server.takeRequest();
128+
final RecordedRequest request = server.takeRequest();
124129
assertThat(request.getMethod()).isEqualTo(method);
125-
assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaType.APPLICATION_JSON);
130+
assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(APPLICATION_JSON);
126131

127-
String path = request.getPath();
128-
String rawPath = path.contains("?") ? path.substring(0, path.indexOf('?')) : path;
132+
final String path = request.getPath();
133+
final String rawPath = path.contains("?") ? path.substring(0, path.indexOf('?')) : path;
129134
assertThat(rawPath).isEqualTo(expectedPath);
130135

131-
Map<String, String> normalizedParams = Maps.transformValues(queryParams, Functions.toStringFunction());
136+
final Map<String, String> normalizedParams = Maps.transformValues(queryParams, Functions.toStringFunction());
132137
assertThat(normalizedParams).isEqualTo(extractParams(path));
133138

134139
return request;
135140
}
136141

137-
protected RecordedRequest assertSent(MockWebServer server, String method, String path, String json)
138-
throws InterruptedException {
139-
RecordedRequest request = assertSent(server, method, path);
142+
protected RecordedRequest assertSent(final MockWebServer server,
143+
final String method,
144+
final String path,
145+
final String json) throws InterruptedException {
146+
147+
final RecordedRequest request = assertSent(server, method, path);
140148
assertThat(APPLICATION_JSON).isEqualTo(request.getHeader("Content-Type"));
141149
assertThat(parser.parse(json)).isEqualTo(parser.parse(request.getUtf8Body()));
142150
return request;
143151
}
144152

145-
protected RecordedRequest assertSentWithFormData(MockWebServer server, String method, String path, String body)
146-
throws InterruptedException {
147-
RecordedRequest request = server.takeRequest();
153+
protected RecordedRequest assertSentWithFormData(final MockWebServer server,
154+
final String method,
155+
final String path,
156+
final String body) throws InterruptedException {
157+
158+
final RecordedRequest request = server.takeRequest();
148159
assertThat(request.getMethod()).isEqualTo(method);
149160
assertThat(request.getPath()).isEqualTo(path);
150161
assertThat(request.getUtf8Body()).isEqualTo(body);
151-
assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaType.APPLICATION_JSON);
162+
assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(APPLICATION_JSON);
152163
assertThat(request.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.APPLICATION_FORM_URLENCODED);
153164
return request;
154165
}
155166

156-
protected RecordedRequest assertSentAcceptText(MockWebServer server, String method, String path)
157-
throws InterruptedException {
158-
RecordedRequest request = server.takeRequest();
167+
protected RecordedRequest assertSentAcceptText(final MockWebServer server,
168+
final String method,
169+
final String path) throws InterruptedException {
170+
171+
final RecordedRequest request = server.takeRequest();
159172
assertThat(request.getMethod()).isEqualTo(method);
160173
assertThat(request.getPath()).isEqualTo(path);
161174
assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaType.TEXT_PLAIN);

src/test/java/com/cdancy/bitbucket/rest/GeneratedTestContents.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ public class GeneratedTestContents {
3737
* @param repository previously created Repository.
3838
* @param projectPreviouslyExists whether the test suite created or user passed in.
3939
*/
40-
public GeneratedTestContents(Project project, Repository repository, boolean projectPreviouslyExists) {
40+
public GeneratedTestContents(final Project project,
41+
final Repository repository,
42+
final boolean projectPreviouslyExists) {
43+
4144
this.project = project;
4245
this.repository = repository;
4346
this.projectPreviouslyExists = projectPreviouslyExists;

src/test/java/com/cdancy/bitbucket/rest/TestUtilities.java

+32-22
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
*/
4646
public class TestUtilities {
4747

48+
private static final String GIT_COMMAND = "git";
49+
private static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz".toCharArray();
50+
4851
private static User defaultUser;
4952

5053
/**
@@ -69,7 +72,7 @@ public static synchronized User getDefaultUser(final String credential, final Bi
6972
final UserPage userPage = api.adminApi().listUsers(username, null, null);
7073
assertThat(userPage).isNotNull();
7174
assertThat(userPage.size() > 0).isTrue();
72-
for (User user : userPage.values()) {
75+
for (final User user : userPage.values()) {
7376
if (username.equals(user.slug())) {
7477
defaultUser = user;
7578
break;
@@ -108,7 +111,7 @@ public static String executionToString(final List<String> args, final Path worki
108111
* @return Path pointing at generated file.
109112
* @throws Exception if file could not be written.
110113
*/
111-
public static Path initGeneratedFile(Path baseDir) throws Exception {
114+
public static Path initGeneratedFile(final Path baseDir) throws Exception {
112115
assertThat(baseDir).isNotNull();
113116
assertThat(baseDir.toFile().isDirectory()).isTrue();
114117

@@ -188,7 +191,7 @@ public static synchronized GeneratedTestContents initGeneratedTestContents(final
188191

189192
generateGitContentsAndPush(generatedFileDir, generatedEndpoint);
190193

191-
} catch (Exception e) {
194+
} catch (final Exception e) {
192195
throw Throwables.propagate(e);
193196
}
194197

@@ -202,48 +205,55 @@ public static synchronized GeneratedTestContents initGeneratedTestContents(final
202205
* @param gitRepoURL git repository URL with embedded credentials.
203206
* @throws Exception if git repository could not be created or files added.
204207
*/
205-
public static void generateGitContentsAndPush(File gitDirectory, String gitRepoURL) throws Exception {
208+
public static void generateGitContentsAndPush(final File gitDirectory, final String gitRepoURL) throws Exception {
206209

207210
// 1.) initialize git repository
208-
String initGit = TestUtilities.executionToString(Arrays.asList("git", "init"), gitDirectory.toPath());
211+
final String initGit = TestUtilities.executionToString(Arrays.asList(GIT_COMMAND, "init"), gitDirectory.toPath());
209212
System.out.println("git-init: " + initGit.trim());
210213

211214
// 2.) create some random files and commit them
212215
for (int i = 0; i < 3; i++) {
213216
Path genFile = initGeneratedFile(gitDirectory.toPath());
214-
String addGit = TestUtilities.executionToString(Arrays.asList("git", "add", genFile.toFile().getPath()), gitDirectory.toPath());
217+
String addGit = TestUtilities.executionToString(Arrays.asList(GIT_COMMAND, "add", genFile.toFile().getPath()), gitDirectory.toPath());
215218
System.out.println("git-add-1: " + addGit.trim());
216-
String commitGit = TestUtilities.executionToString(Arrays.asList("git", "commit", "-m", "added"), gitDirectory.toPath());
219+
String commitGit = TestUtilities.executionToString(Arrays.asList(GIT_COMMAND, "commit", "-m", "added"), gitDirectory.toPath());
217220
System.out.println("git-commit-1: " + commitGit.trim());
218221

219222
// edit file again and create another commit
220223
genFile = Files.write(genFile, Arrays.asList(randomString()), Charset.forName("UTF-8"));
221-
addGit = TestUtilities.executionToString(Arrays.asList("git", "add", genFile.toFile().getPath()), gitDirectory.toPath());
224+
addGit = TestUtilities.executionToString(Arrays.asList(GIT_COMMAND, "add", genFile.toFile().getPath()), gitDirectory.toPath());
222225
System.out.println("git-add-2: " + addGit.trim());
223-
commitGit = TestUtilities.executionToString(Arrays.asList("git", "commit", "-m", "added"), gitDirectory.toPath());
226+
commitGit = TestUtilities.executionToString(Arrays.asList(GIT_COMMAND, "commit", "-m", "added"), gitDirectory.toPath());
224227
System.out.println("git-commit-2: " + commitGit.trim());
225228
}
226229

227230
// 3.) push changes to remote repository
228-
String pushGit = TestUtilities.executionToString(Arrays.asList("git", "push", "--set-upstream", gitRepoURL, "master"), gitDirectory.toPath());
231+
final String pushGit = TestUtilities.executionToString(Arrays.asList(GIT_COMMAND,
232+
"push",
233+
"--set-upstream",
234+
gitRepoURL,
235+
"master"), gitDirectory.toPath());
229236
System.out.println("git-push: " + pushGit);
230237

231238
// 4.) create branch
232-
String generatedBranchName = randomString();
233-
String branchGit = TestUtilities.executionToString(Arrays.asList("git", "checkout", "-b", generatedBranchName), gitDirectory.toPath());
239+
final String generatedBranchName = randomString();
240+
final String branchGit = TestUtilities.executionToString(Arrays.asList(GIT_COMMAND,
241+
"checkout", "-b",
242+
generatedBranchName),
243+
gitDirectory.toPath());
234244
System.out.println("git-branch: " + branchGit.trim());
235245

236246

237247
// 5.) generate random file for new branch
238-
Path genFile = initGeneratedFile(gitDirectory.toPath());
239-
String addGit = TestUtilities.executionToString(Arrays.asList("git", "add", genFile.toFile().getPath()), gitDirectory.toPath());
248+
final Path genFile = initGeneratedFile(gitDirectory.toPath());
249+
final String addGit = TestUtilities.executionToString(Arrays.asList(GIT_COMMAND, "add", genFile.toFile().getPath()), gitDirectory.toPath());
240250
System.out.println("git-branch-add: " + addGit.trim());
241-
String commitGit = TestUtilities.executionToString(Arrays.asList("git", "commit", "-m", "added"), gitDirectory.toPath());
251+
final String commitGit = TestUtilities.executionToString(Arrays.asList(GIT_COMMAND, "commit", "-m", "added"), gitDirectory.toPath());
242252
System.out.println("git-branch-commit: " + commitGit.trim());
243253

244254
// 6.) push branch
245-
List<String> args = Arrays.asList("git", "push", "-u", gitRepoURL, generatedBranchName);
246-
String pushBranchGit = TestUtilities.executionToString(args, gitDirectory.toPath());
255+
final List<String> args = Arrays.asList(GIT_COMMAND, "push", "-u", gitRepoURL, generatedBranchName);
256+
final String pushBranchGit = TestUtilities.executionToString(args, gitDirectory.toPath());
247257
System.out.println("git-branch-push: " + pushBranchGit);
248258
}
249259

@@ -253,7 +263,8 @@ public static void generateGitContentsAndPush(File gitDirectory, String gitRepoU
253263
* @param api BitbucketApi object
254264
* @param generatedTestContents to terminate.
255265
*/
256-
public static synchronized void terminateGeneratedTestContents(final BitbucketApi api, final GeneratedTestContents generatedTestContents) {
266+
public static synchronized void terminateGeneratedTestContents(final BitbucketApi api,
267+
final GeneratedTestContents generatedTestContents) {
257268
assertThat(api).isNotNull();
258269
assertThat(generatedTestContents).isNotNull();
259270

@@ -281,11 +292,10 @@ public static synchronized void terminateGeneratedTestContents(final BitbucketAp
281292
* @return random String.
282293
*/
283294
public static String randomStringLettersOnly() {
284-
char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
285-
StringBuilder sb = new StringBuilder();
286-
Random random = new Random();
295+
final StringBuilder sb = new StringBuilder();
296+
final Random random = new Random();
287297
for (int i = 0; i < 10; i++) {
288-
char randomChar = chars[random.nextInt(chars.length)];
298+
final char randomChar = CHARS[random.nextInt(CHARS.length)];
289299
sb.append(randomChar);
290300
}
291301
return sb.toString().toUpperCase();

src/test/java/com/cdancy/bitbucket/rest/features/AdminApiLiveTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class AdminApiLiveTest extends BaseBitbucketApiLiveTest {
3030

3131
@Test
3232
public void testListUsersByGroup() {
33-
UserPage userPage = api().listUsersByGroup(defaultBitbucketGroup, null, null, null);
33+
final UserPage userPage = api().listUsersByGroup(defaultBitbucketGroup, null, null, null);
3434
assertThat(userPage).isNotNull();
3535
assertThat(userPage.size() > 0).isTrue();
3636
}

src/test/java/com/cdancy/bitbucket/rest/features/AdminApiMockTest.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
@Test(groups = "unit", testName = "AdminApiMockTest")
3434
public class AdminApiMockTest extends BaseBitbucketMockTest {
3535

36+
private final String limitKeyword = "limit";
37+
private final String startKeyword = "start";
38+
private final String restApiPath = "/rest/api/";
39+
private final String getMethod = "GET";
40+
3641
private final String localContext = "test";
3742

3843
public void testGetListUserByGroup() throws Exception {
@@ -49,8 +54,8 @@ public void testGetListUserByGroup() throws Exception {
4954
assertThat(up.size() == 2).isTrue();
5055
assertThat(up.values().get(0).slug().equals("bob123")).isTrue();
5156

52-
final Map<String, ?> queryParams = ImmutableMap.of("context", localContext, "limit", 2, "start", 0);
53-
assertSent(server, "GET", "/rest/api/" + BitbucketApiMetadata.API_VERSION
57+
final Map<String, ?> queryParams = ImmutableMap.of("context", localContext, limitKeyword, 2, startKeyword, 0);
58+
assertSent(server, getMethod, restApiPath + BitbucketApiMetadata.API_VERSION
5459
+ "/admin/groups/more-members", queryParams);
5560
} finally {
5661
server.shutdown();
@@ -69,8 +74,8 @@ public void testGetListUserByGroupOnError() throws Exception {
6974
assertThat(up).isNotNull();
7075
assertThat(up.errors()).isNotEmpty();
7176

72-
final Map<String, ?> queryParams = ImmutableMap.of("context", localContext, "limit", 2, "start", 0);
73-
assertSent(server, "GET", "/rest/api/" + BitbucketApiMetadata.API_VERSION
77+
final Map<String, ?> queryParams = ImmutableMap.of("context", localContext, limitKeyword, 2, startKeyword, 0);
78+
assertSent(server, getMethod, restApiPath + BitbucketApiMetadata.API_VERSION
7479
+ "/admin/groups/more-members", queryParams);
7580
} finally {
7681
server.shutdown();
@@ -90,8 +95,8 @@ public void testListUsers() throws Exception {
9095
assertThat(up.size() == 1).isTrue();
9196
assertThat(up.values().get(0).slug().equals("jcitizen")).isTrue();
9297

93-
final Map<String, ?> queryParams = ImmutableMap.of("filter", "jcitizen", "limit", 2, "start", 0);
94-
assertSent(server, "GET", "/rest/api/" + BitbucketApiMetadata.API_VERSION
98+
final Map<String, ?> queryParams = ImmutableMap.of("filter", "jcitizen", limitKeyword, 2, startKeyword, 0);
99+
assertSent(server, getMethod, restApiPath + BitbucketApiMetadata.API_VERSION
95100
+ "/admin/users", queryParams);
96101
} finally {
97102
baseApi.close();
@@ -110,8 +115,8 @@ public void testListUsersOnError() throws Exception {
110115
assertThat(up).isNotNull();
111116
assertThat(up.errors()).isNotEmpty();
112117

113-
final Map<String, ?> queryParams = ImmutableMap.of("filter", "blah%20blah", "limit", 2, "start", 0);
114-
assertSent(server, "GET", "/rest/api/" + BitbucketApiMetadata.API_VERSION
118+
final Map<String, ?> queryParams = ImmutableMap.of("filter", "blah%20blah", limitKeyword, 2, startKeyword, 0);
119+
assertSent(server, getMethod, restApiPath + BitbucketApiMetadata.API_VERSION
115120
+ "/admin/users", queryParams);
116121
} finally {
117122
baseApi.close();

0 commit comments

Comments
 (0)