1
1
package com .emc .object .s3 ;
2
2
3
- import com .emc .object .ObjectConfig ;
4
- import com .emc .object .s3 .bean .ListObjectsResult ;
5
- import com .emc .object .s3 .bean .S3Object ;
6
3
import com .emc .object .s3 .jersey .S3JerseyClient ;
7
4
import com .emc .object .s3 .request .CreateBucketRequest ;
8
5
import com .emc .object .s3 .request .PutObjectRequest ;
9
6
import com .emc .object .util .FaultInjectionStream ;
7
+ import com .emc .util .ConcurrentJunitRunner ;
10
8
import com .sun .jersey .api .client .ClientHandlerException ;
11
9
import com .sun .jersey .client .urlconnection .URLConnectionClientHandler ;
12
10
import org .apache .commons .codec .digest .DigestUtils ;
13
- import org .junit .*;
14
- import org .slf4j .Logger ;
15
- import org .slf4j .LoggerFactory ;
11
+ import org .junit .After ;
12
+ import org .junit .Assert ;
13
+ import org .junit .Ignore ;
14
+ import org .junit .Test ;
15
+ import org .junit .runner .RunWith ;
16
16
17
17
import javax .xml .bind .DatatypeConverter ;
18
18
import java .io .ByteArrayInputStream ;
19
19
import java .io .IOException ;
20
20
import java .io .InputStream ;
21
- import java .net .URI ;
22
21
import java .util .Random ;
23
22
24
- public class WriteTruncationTest {
25
- public static final Logger log = LoggerFactory .getLogger (WriteTruncationTest .class );
26
-
27
- static final String BUCKET_NAME = "ecs-object-client-write-truncation-test" ;
23
+ @ RunWith (ConcurrentJunitRunner .class )
24
+ public class WriteTruncationTest extends AbstractS3ClientTest {
28
25
static final int OBJECT_RETENTION_PERIOD = 15 ; // 15 seconds
29
26
static final int MOCK_OBJ_SIZE = 5 * 1024 * 1024 ; // 5MB
30
27
31
- S3Client s3Client ;
32
- S3Client s3JvmClient ;
28
+ S3Client jvmClient ;
33
29
final Random random = new Random ();
34
30
35
- @ Before
36
- public void setup () throws Exception {
37
- S3Config s3Config = AbstractS3ClientTest .s3ConfigFromProperties ();
38
- s3Config .setRetryEnabled (false );
31
+ @ Override
32
+ protected S3Client createS3Client () throws Exception {
33
+ S3Config s3Config = createS3Config ().withRetryEnabled (false );
34
+ this .jvmClient = new S3JerseyClient (s3Config , new URLConnectionClientHandler ());
35
+ return new S3JerseyClient (createS3Config ().withRetryEnabled (false ));
36
+ }
39
37
40
- String proxy = s3Config .getPropAsString (ObjectConfig .PROPERTY_PROXY_URI );
41
- if (proxy != null ) {
42
- URI proxyUri = new URI (proxy );
43
- System .setProperty ("http.proxyHost" , proxyUri .getHost ());
44
- System .setProperty ("http.proxyPort" , "" + proxyUri .getPort ());
45
- }
38
+ @ Override
39
+ protected String getTestBucketPrefix () {
40
+ return "s3-write-truncation-test" ;
41
+ }
46
42
47
- s3Client = new S3JerseyClient (s3Config );
48
- s3JvmClient = new S3JerseyClient (s3Config , new URLConnectionClientHandler ());
43
+ @ Override
44
+ protected void createBucket (String bucketName ) {
45
+ // create bucket with retention period and D@RE enabled
46
+ client .createBucket (new CreateBucketRequest (getTestBucket ())
47
+ .withRetentionPeriod (OBJECT_RETENTION_PERIOD )
48
+ .withEncryptionEnabled (true ));
49
+ }
49
50
51
+ @ Override
52
+ protected void cleanUpBucket (String bucketName ) {
50
53
try {
51
- // create bucket with retention period and D@RE enabled
52
- s3Client .createBucket (new CreateBucketRequest (BUCKET_NAME )
53
- .withRetentionPeriod (OBJECT_RETENTION_PERIOD )
54
- .withEncryptionEnabled (true ));
55
- } catch (S3Exception e ) {
56
- if (!e .getErrorCode ().equals ("BucketAlreadyExists" )) throw e ;
54
+ Thread .sleep (OBJECT_RETENTION_PERIOD * 1000 ); // wait for retention to expire
55
+ } catch (InterruptedException ignored ) {
57
56
}
57
+ super .cleanUpBucket (bucketName );
58
+ }
59
+
60
+ @ After
61
+ public void shutdownJvmClient () {
62
+ if (jvmClient != null ) jvmClient .destroy ();
58
63
}
59
64
60
65
@ Test
@@ -126,7 +131,7 @@ void testTruncatedWrite(boolean useApacheClient,
126
131
ExceptionType exceptionType ,
127
132
int delayBeforeException ,
128
133
boolean sendContentMd5 ) {
129
- S3Client s3Client = useApacheClient ? this .s3Client : this .s3JvmClient ;
134
+ S3Client s3Client = useApacheClient ? this .client : this .jvmClient ;
130
135
131
136
String key = String .format ("read-%s%s-%s%stest" ,
132
137
delayBeforeException > 0 ? "delayed-" : "" ,
@@ -153,7 +158,7 @@ void testTruncatedWrite(boolean useApacheClient,
153
158
badStream .setSecondDelayBeforeThrowing (delayBeforeException );
154
159
155
160
try {
156
- s3Client .putObject (new PutObjectRequest (BUCKET_NAME , key , badStream ).withObjectMetadata (metadata ));
161
+ s3Client .putObject (new PutObjectRequest (getTestBucket () , key , badStream ).withObjectMetadata (metadata ));
157
162
Assert .fail ("exception in input stream did not throw an exception" );
158
163
} catch (ClientHandlerException e ) {
159
164
if (exceptionType == ExceptionType .RuntimeException ) {
@@ -170,35 +175,7 @@ void testTruncatedWrite(boolean useApacheClient,
170
175
} catch (InterruptedException ignored ) {
171
176
}
172
177
173
- Assert .assertEquals (0 , s3Client .listObjects (BUCKET_NAME ).getObjects ().size ());
174
- }
175
-
176
- @ After
177
- public void teardown () {
178
- if (s3Client == null ) return ;
179
-
180
- try {
181
- Thread .sleep (OBJECT_RETENTION_PERIOD * 1000 ); // wait for retention to expire
182
- } catch (InterruptedException ignored ) {
183
- }
184
-
185
- try {
186
- ListObjectsResult listing = null ;
187
- do {
188
- if (listing == null ) listing = s3Client .listObjects (BUCKET_NAME );
189
- else listing = s3Client .listMoreObjects (listing );
190
-
191
- for (final S3Object summary : listing .getObjects ()) {
192
- s3Client .deleteObject (BUCKET_NAME , summary .getKey ());
193
- }
194
- } while (listing .isTruncated ());
195
-
196
- s3Client .deleteBucket (BUCKET_NAME );
197
- } catch (RuntimeException e ) {
198
- log .error ("could not delete bucket " + BUCKET_NAME , e );
199
- } finally {
200
- s3Client .destroy ();
201
- }
178
+ Assert .assertEquals (0 , s3Client .listObjects (getTestBucket ()).getObjects ().size ());
202
179
}
203
180
204
181
enum ExceptionType {
0 commit comments