Skip to content

Commit 6cb7099

Browse files
authored
Tests - Fix possible NPE
1 parent 5c9ff5b commit 6cb7099

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

Diff for: build-info-extractor/src/test/java/org/jfrog/build/extractor/clientConfiguration/util/DownloadTest.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ public class DownloadTest extends IntegrationTestsBase {
3232

3333
/**
3434
* Tests download of files - both bulk and concurrently.
35-
* @param uploadedChecksum checksums of the artifact uploaded to Artifactory, used to compare with the downloaded artifact.
36-
* @param fileName
37-
* @param fileSize
35+
*
36+
* @param uploadedChecksum - checksums of the artifact uploaded to Artifactory, used to compare with the downloaded artifact.
37+
* @param fileName - artifact name to download.
38+
* @param fileSize - artifact size to download.
3839
*/
3940
@Test(dataProvider = "testDownloadFilesProvider")
4041
public void testBulkAndConcurrentDownload(Map<String, String> uploadedChecksum, String fileName, long fileSize)
4142
throws Exception {
4243
String uriWithParams = dependenciesClient.getArtifactoryUrl() + "/" + localRepo1 + "/" + TEST_REPO_PATH + "/" + fileName;
43-
String fileDestination = tempWorkspace.getPath() + File.separatorChar + "download" + File.separatorChar + fileName;
44+
String fileDestination = tempWorkspace.getPath() + File.separatorChar + "download" + File.separatorChar + fileName;
4445

4546
DependenciesDownloaderHelper helper = new DependenciesDownloaderHelper(dependenciesClient, tempWorkspace.getPath(), log);
4647
DependenciesDownloaderHelper.ArtifactMetaData artifactMetaData = helper.downloadArtifactMetaData(uriWithParams);
@@ -72,7 +73,7 @@ public void testDownloadArtifact(Map<String, String> uploadedChecksum, String fi
7273
DependenciesDownloaderHelper dependenciesDownloaderHelper = new DependenciesDownloaderHelper(dependenciesClient, ".", log);
7374

7475
String repoUrl = dependenciesClient.getArtifactoryUrl() + "/" + localRepo1 + "/" + TEST_REPO_PATH;
75-
String targetDirPath = tempWorkspace.getPath() + File.separatorChar + "download" + File.separatorChar;
76+
String targetDirPath = tempWorkspace.getPath() + File.separatorChar + "download" + File.separatorChar;
7677
String url = repoUrl + "/" + fileName;
7778

7879
ArtifactMetaData artifactMetaData = dependenciesDownloaderHelper.downloadArtifactMetaData(url);
@@ -94,7 +95,7 @@ public void testDownloadArtifactWithoutContentLength(Map<String, String> uploade
9495
DependenciesDownloaderHelper dependenciesDownloaderHelper = new DependenciesDownloaderHelper(dependenciesClient, ".", log);
9596

9697
String repoUrl = dependenciesClient.getArtifactoryUrl() + "/" + localRepo1 + "/" + TEST_REPO_PATH;
97-
String targetDirPath = tempWorkspace.getPath() + File.separatorChar + "download" + File.separatorChar;
98+
String targetDirPath = tempWorkspace.getPath() + File.separatorChar + "download" + File.separatorChar;
9899
String url = repoUrl + "/" + fileName;
99100

100101
ArtifactMetaData artifactMetaData = dependenciesDownloaderHelper.downloadArtifactMetaData(url);
@@ -117,6 +118,9 @@ public void testDownloadArtifactWithoutContentLength(Map<String, String> uploade
117118
*/
118119
@DataProvider
119120
private Object[][] testDownloadFilesProvider() throws IOException, NoSuchAlgorithmException {
121+
if (buildInfoClient == null) {
122+
throw new IOException("tests were not initialized successfully. aborting");
123+
}
120124
Map<String, Integer> testFilesMap = new HashMap<String, Integer>() {{
121125
put("file1", MIN_SIZE_FOR_CONCURRENT_DOWNLOAD);
122126
put("file2", MIN_SIZE_FOR_CONCURRENT_DOWNLOAD - 1);

Diff for: build-info-extractor/src/testFixtures/java/org/jfrog/build/IntegrationTestsBase.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.FileNotFoundException;
2424
import java.io.IOException;
2525
import java.io.InputStream;
26+
import java.nio.charset.StandardCharsets;
2627
import java.util.Collection;
2728
import java.util.List;
2829
import java.util.Properties;
@@ -143,6 +144,9 @@ private void failInit() {
143144
* @throws IOException
144145
*/
145146
protected void deleteContentFromRepo(String repo) throws IOException {
147+
if (!isRepoExists(repo)) {
148+
return;
149+
}
146150
String fullItemUrl = url + repo + "/";
147151
String itemUrl = ArtifactoryHttpClient.encodeUrl(fullItemUrl);
148152
HttpRequestBase httpRequest = new HttpDelete(itemUrl);
@@ -155,7 +159,7 @@ protected void deleteContentFromRepo(String repo) throws IOException {
155159
throw new FileNotFoundException("Bad credentials for username: " + username);
156160
}
157161
if (statusCode < 200 || statusCode >= 300) {
158-
throw new IOException("Error deleting " + localRepo1 + ". Code: " + statusCode + " Message: " + statusLine.getReasonPhrase());
162+
throw new IOException(getRepositoryCustomErrorMessage(repo, false, statusLine));
159163
}
160164
}
161165
}
@@ -193,7 +197,7 @@ protected void createTestRepo(String repo) throws IOException {
193197
EntityUtils.consumeQuietly(response.getEntity());
194198
int statusCode = response.getStatusLine().getStatusCode();
195199
if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) {
196-
throw new IOException("Error creating repository: " + repo + ". Code: " + statusCode + " Message: " + response.getStatusLine().getReasonPhrase());
200+
throw new IOException(getRepositoryCustomErrorMessage(repo, true, response.getStatusLine()));
197201
}
198202
}
199203
}
@@ -214,7 +218,7 @@ protected void deleteTestRepo(String repo) throws IOException {
214218
EntityUtils.consumeQuietly(response.getEntity());
215219
int statusCode = response.getStatusLine().getStatusCode();
216220
if (statusCode != HttpStatus.SC_OK) {
217-
throw new IOException("Error deleting repository" + repo + ". Code: " + statusCode + " Message: " + response.getStatusLine().getReasonPhrase());
221+
throw new IOException(getRepositoryCustomErrorMessage(repo, false, response.getStatusLine()));
218222
}
219223
}
220224
}
@@ -232,7 +236,7 @@ private String getRepoApiUrl(String repo) {
232236
* @throws IOException
233237
*/
234238
protected String readSpec(File specFile, String workSpacePath) throws IOException {
235-
String spec = FileUtils.readFileToString(specFile, "UTF-8");
239+
String spec = FileUtils.readFileToString(specFile, StandardCharsets.UTF_8);
236240
spec = StringUtils.replace(spec, LOCAL_REPO_PLACEHOLDER, localRepo1);
237241
spec = StringUtils.replace(spec, LOCAL_REPO2_PLACEHOLDER, localRepo2);
238242
spec = StringUtils.replace(spec, VIRTUAL_REPO_PLACEHOLDER, virtualRepo);
@@ -306,4 +310,23 @@ public void setFiles(List<String> files) {
306310
}
307311
}
308312

313+
/***
314+
* Returns a custom exception error to be thrown when repository creation/deletion fails.
315+
* @param repo - repo name.
316+
* @param creating - creation or deletion failed.
317+
* @param statusLine - status returned.
318+
* @return - custom error message.
319+
*/
320+
private String getRepositoryCustomErrorMessage(String repo, boolean creating, StatusLine statusLine) {
321+
StringBuilder builder = new StringBuilder()
322+
.append("Error ")
323+
.append(creating ? "creating" : "deleting")
324+
.append(" '").append(repo).append("'. ")
325+
.append("Code: ").append(statusLine.getStatusCode());
326+
if (statusLine.getReasonPhrase() != null) {
327+
builder.append(" Message: ")
328+
.append(statusLine.getReasonPhrase());
329+
}
330+
return builder.toString();
331+
}
309332
}

0 commit comments

Comments
 (0)