Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
7aaa7f6d5c
@ -18,7 +18,7 @@
|
||||
<aws-lambda-java-events.version>1.3.0</aws-lambda-java-events.version>
|
||||
<aws-lambda-java-core.version>1.1.0</aws-lambda-java-core.version>
|
||||
<gson.version>2.8.0</gson.version>
|
||||
<aws-java-sdk.version>1.11.154</aws-java-sdk.version>
|
||||
<aws-java-sdk.version>1.11.290</aws-java-sdk.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mockito-core.version>2.8.9</mockito-core.version>
|
||||
<assertj-core.version>3.8.0</assertj-core.version>
|
||||
|
56
aws/src/main/java/com/baeldung/s3/MultipartUpload.java
Normal file
56
aws/src/main/java/com/baeldung/s3/MultipartUpload.java
Normal file
@ -0,0 +1,56 @@
|
||||
package com.baeldung.s3;
|
||||
|
||||
import com.amazonaws.AmazonClientException;
|
||||
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
|
||||
import com.amazonaws.event.ProgressListener;
|
||||
import com.amazonaws.regions.Regions;
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
||||
import com.amazonaws.services.s3.model.PutObjectRequest;
|
||||
import com.amazonaws.services.s3.transfer.TransferManager;
|
||||
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
|
||||
import com.amazonaws.services.s3.transfer.Upload;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class MultipartUpload {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String existingBucketName = "baeldung-bucket";
|
||||
String keyName = "my-picture.jpg";
|
||||
String filePath = "documents/my-picture.jpg";
|
||||
|
||||
AmazonS3 amazonS3 = AmazonS3ClientBuilder
|
||||
.standard()
|
||||
.withCredentials(new DefaultAWSCredentialsProviderChain())
|
||||
.withRegion(Regions.DEFAULT_REGION)
|
||||
.build();
|
||||
|
||||
int maxUploadThreads = 5;
|
||||
|
||||
TransferManager tm = TransferManagerBuilder
|
||||
.standard()
|
||||
.withS3Client(amazonS3)
|
||||
.withMultipartUploadThreshold((long) (5 * 1024 * 1024))
|
||||
.withExecutorFactory(() -> Executors.newFixedThreadPool(maxUploadThreads))
|
||||
.build();
|
||||
|
||||
ProgressListener progressListener =
|
||||
progressEvent -> System.out.println("Transferred bytes: " + progressEvent.getBytesTransferred());
|
||||
|
||||
PutObjectRequest request = new PutObjectRequest(existingBucketName, keyName, new File(filePath));
|
||||
|
||||
request.setGeneralProgressListener(progressListener);
|
||||
|
||||
Upload upload = tm.upload(request);
|
||||
|
||||
try {
|
||||
upload.waitForCompletion();
|
||||
System.out.println("Upload complete.");
|
||||
} catch (AmazonClientException e) {
|
||||
System.out.println("Error occurred while uploading file");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
61
aws/src/test/java/com/baeldung/s3/MultipartUploadTest.java
Normal file
61
aws/src/test/java/com/baeldung/s3/MultipartUploadTest.java
Normal file
@ -0,0 +1,61 @@
|
||||
package com.baeldung.s3;
|
||||
|
||||
import com.amazonaws.event.ProgressListener;
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.model.PutObjectRequest;
|
||||
import com.amazonaws.services.s3.model.PutObjectResult;
|
||||
import com.amazonaws.services.s3.transfer.TransferManager;
|
||||
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
|
||||
import com.amazonaws.services.s3.transfer.Upload;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class MultipartUploadTest {
|
||||
|
||||
private static final String BUCKET_NAME = "bucket_name";
|
||||
private static final String KEY_NAME = "picture.jpg";
|
||||
|
||||
private AmazonS3 amazonS3;
|
||||
private TransferManager tm;
|
||||
private ProgressListener progressListener;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
amazonS3 = mock(AmazonS3.class);
|
||||
tm = TransferManagerBuilder
|
||||
.standard()
|
||||
.withS3Client(amazonS3)
|
||||
.withMultipartUploadThreshold((long) (5 * 1024 * 1025))
|
||||
.withExecutorFactory(() -> Executors.newFixedThreadPool(5))
|
||||
.build();
|
||||
progressListener =
|
||||
progressEvent -> System.out.println("Transferred bytes: " + progressEvent.getBytesTransferred());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUploadingFileWithTransferManager_thenVerifyUploadRequested() {
|
||||
File file = mock(File.class);
|
||||
PutObjectResult s3Result = mock(PutObjectResult.class);
|
||||
|
||||
when(amazonS3.putObject(anyString(), anyString(), (File) any())).thenReturn(s3Result);
|
||||
when(file.getName()).thenReturn(KEY_NAME);
|
||||
|
||||
PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, KEY_NAME, file);
|
||||
request.setGeneralProgressListener(progressListener);
|
||||
|
||||
Upload upload = tm.upload(request);
|
||||
|
||||
assertThat(upload).isNotNull();
|
||||
verify(amazonS3).putObject(request);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.baeldung.externalizable;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class Community implements Serializable {
|
||||
|
||||
private int id;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Community{" +
|
||||
"id=" + id +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.baeldung.externalizable;
|
||||
|
||||
import java.io.Externalizable;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInput;
|
||||
import java.io.ObjectOutput;
|
||||
|
||||
public class Country implements Externalizable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String name;
|
||||
private String capital;
|
||||
private int code;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCapital() {
|
||||
return capital;
|
||||
}
|
||||
|
||||
public void setCapital(String capital) {
|
||||
this.capital = capital;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeExternal(ObjectOutput out) throws IOException {
|
||||
out.writeUTF(name);
|
||||
out.writeUTF(capital);
|
||||
out.writeInt(code);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
|
||||
this.name = in.readUTF();
|
||||
this.capital = in.readUTF();
|
||||
this.code = in.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Country{" +
|
||||
"name='" + name + '\'' +
|
||||
", capital='" + capital + '\'' +
|
||||
", code=" + code +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.baeldung.externalizable;
|
||||
|
||||
import java.io.Externalizable;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInput;
|
||||
import java.io.ObjectOutput;
|
||||
|
||||
public class Region extends Country implements Externalizable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String climate;
|
||||
private Double population;
|
||||
private Community community;
|
||||
|
||||
public String getClimate() {
|
||||
return climate;
|
||||
}
|
||||
|
||||
public void setClimate(String climate) {
|
||||
this.climate = climate;
|
||||
}
|
||||
|
||||
public Double getPopulation() {
|
||||
return population;
|
||||
}
|
||||
|
||||
public void setPopulation(Double population) {
|
||||
this.population = population;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeExternal(ObjectOutput out) throws IOException {
|
||||
super.writeExternal(out);
|
||||
out.writeUTF(climate);
|
||||
community = new Community();
|
||||
community.setId(5);
|
||||
out.writeObject(community);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
|
||||
super.readExternal(in);
|
||||
this.climate = in.readUTF();
|
||||
community = (Community) in.readObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Region = {" +
|
||||
"country='" + super.toString() + '\'' +
|
||||
"community='" + community.toString() + '\'' +
|
||||
"climate='" + climate + '\'' +
|
||||
", population=" + population +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package com.baeldung.externalizable;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ExternalizableUnitTest {
|
||||
|
||||
private final static String OUTPUT_FILE = "externalizable.txt";
|
||||
|
||||
@Test
|
||||
public void whenSerializing_thenUseExternalizable() throws IOException, ClassNotFoundException {
|
||||
|
||||
Country c = new Country();
|
||||
c.setCapital("Yerevan");
|
||||
c.setCode(374);
|
||||
c.setName("Armenia");
|
||||
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
||||
c.writeExternal(objectOutputStream);
|
||||
|
||||
objectOutputStream.flush();
|
||||
objectOutputStream.close();
|
||||
fileOutputStream.close();
|
||||
|
||||
FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE);
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
||||
|
||||
Country c2 = new Country();
|
||||
c2.readExternal(objectInputStream);
|
||||
|
||||
objectInputStream.close();
|
||||
fileInputStream.close();
|
||||
|
||||
assertTrue(c2.getCode() == c.getCode());
|
||||
assertTrue(c2.getName().equals(c.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInheritanceSerialization_then_UseExternalizable() throws IOException, ClassNotFoundException {
|
||||
|
||||
Region r = new Region();
|
||||
r.setCapital("Yerevan");
|
||||
r.setCode(374);
|
||||
r.setName("Armenia");
|
||||
r.setClimate("Mediterranean");
|
||||
r.setPopulation(120.000);
|
||||
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
||||
r.writeExternal(objectOutputStream);
|
||||
|
||||
objectOutputStream.flush();
|
||||
objectOutputStream.close();
|
||||
fileOutputStream.close();
|
||||
|
||||
FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE);
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
||||
|
||||
Region r2 = new Region();
|
||||
r2.readExternal(objectInputStream);
|
||||
|
||||
objectInputStream.close();
|
||||
fileInputStream.close();
|
||||
|
||||
assertTrue(r2.getPopulation() == null);
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@ import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class XMLSerializeDeserializeUnitTest {
|
||||
|
||||
@ -48,6 +49,24 @@ public class XMLSerializeDeserializeUnitTest {
|
||||
assertTrue(value.getX() == 1 && value.getY() == 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenJavaGotFromXmlStrWithCapitalElem_thenCorrect() throws IOException {
|
||||
XmlMapper xmlMapper = new XmlMapper();
|
||||
SimpleBeanForCapitalizedFields value = xmlMapper.
|
||||
readValue("<SimpleBeanForCapitalizedFields><X>1</X><y>2</y></SimpleBeanForCapitalizedFields>",
|
||||
SimpleBeanForCapitalizedFields.class);
|
||||
assertTrue(value.getX() == 1 && value.getY() == 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenJavaSerializedToXmlFileWithCapitalizedField_thenCorrect() throws IOException {
|
||||
XmlMapper xmlMapper = new XmlMapper();
|
||||
xmlMapper.writeValue(new File("target/simple_bean_capitalized.xml"),
|
||||
new SimpleBeanForCapitalizedFields());
|
||||
File file = new File("target/simple_bean_capitalized.xml");
|
||||
assertNotNull(file);
|
||||
}
|
||||
|
||||
private static String inputStreamToString(InputStream is) throws IOException {
|
||||
BufferedReader br;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@ -83,3 +102,25 @@ class SimpleBean {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class SimpleBeanForCapitalizedFields {
|
||||
@JsonProperty("X")
|
||||
private int x = 1;
|
||||
private int y = 2;
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user