23
23
import java .io .FileNotFoundException ;
24
24
import java .io .IOException ;
25
25
import java .io .InputStream ;
26
+ import java .nio .charset .StandardCharsets ;
26
27
import java .util .Collection ;
27
28
import java .util .List ;
28
29
import java .util .Properties ;
@@ -143,6 +144,9 @@ private void failInit() {
143
144
* @throws IOException
144
145
*/
145
146
protected void deleteContentFromRepo (String repo ) throws IOException {
147
+ if (!isRepoExists (repo )) {
148
+ return ;
149
+ }
146
150
String fullItemUrl = url + repo + "/" ;
147
151
String itemUrl = ArtifactoryHttpClient .encodeUrl (fullItemUrl );
148
152
HttpRequestBase httpRequest = new HttpDelete (itemUrl );
@@ -155,7 +159,7 @@ protected void deleteContentFromRepo(String repo) throws IOException {
155
159
throw new FileNotFoundException ("Bad credentials for username: " + username );
156
160
}
157
161
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 ));
159
163
}
160
164
}
161
165
}
@@ -193,7 +197,7 @@ protected void createTestRepo(String repo) throws IOException {
193
197
EntityUtils .consumeQuietly (response .getEntity ());
194
198
int statusCode = response .getStatusLine ().getStatusCode ();
195
199
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 ()));
197
201
}
198
202
}
199
203
}
@@ -214,7 +218,7 @@ protected void deleteTestRepo(String repo) throws IOException {
214
218
EntityUtils .consumeQuietly (response .getEntity ());
215
219
int statusCode = response .getStatusLine ().getStatusCode ();
216
220
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 ()));
218
222
}
219
223
}
220
224
}
@@ -232,7 +236,7 @@ private String getRepoApiUrl(String repo) {
232
236
* @throws IOException
233
237
*/
234
238
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 );
236
240
spec = StringUtils .replace (spec , LOCAL_REPO_PLACEHOLDER , localRepo1 );
237
241
spec = StringUtils .replace (spec , LOCAL_REPO2_PLACEHOLDER , localRepo2 );
238
242
spec = StringUtils .replace (spec , VIRTUAL_REPO_PLACEHOLDER , virtualRepo );
@@ -306,4 +310,23 @@ public void setFiles(List<String> files) {
306
310
}
307
311
}
308
312
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
+ }
309
332
}
0 commit comments