Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
wujinhu committed Sep 15, 2018
1 parent d923b3e commit 333dfe2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ public byte[] marshall(SelectObjectRequest request) {
xmlBody.append("<RecordDelimiter>" + BinaryUtil.toBase64String(csvInputFormat.getRecordDelimiter().getBytes()) + "</RecordDelimiter>");
xmlBody.append("<FieldDelimiter>" + BinaryUtil.toBase64String(csvInputFormat.getFieldDelimiter().toString().getBytes()) + "</FieldDelimiter>");
xmlBody.append("<QuoteCharacter>" + BinaryUtil.toBase64String(csvInputFormat.getQuoteChar().toString().getBytes()) + "</QuoteCharacter>");
xmlBody.append("<Comments>" + BinaryUtil.toBase64String(csvInputFormat.getCommentChar().toString().getBytes()) + "</Comments>");
xmlBody.append("<CommentCharacter>" + BinaryUtil.toBase64String(csvInputFormat.getCommentChar().toString().getBytes()) + "</CommentCharacter>");

if (request.getLineRange() != null) {
xmlBody.append("<Range>" + request.lineRangeToString(request.getLineRange()) + "</Range>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void readFrame() throws IOException {
throw new IOException("Oss Select create meta encounter error code: " + status + ", message: " + error);
}

selectObjectMetadata.setCsvObjectMetadata(
selectObjectMetadata.withCsvObjectMetadata(
new SelectObjectMetadata.CSVObjectMetadata()
.withSplits(ByteBuffer.wrap(splitBytes).getInt())
.withTotalLines(ByteBuffer.wrap(totalLineBytes).getLong()));
Expand Down
14 changes: 0 additions & 14 deletions src/main/java/com/aliyun/oss/model/OutputSerialization.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
public class OutputSerialization implements Serializable {
private CSVFormat csvOutputFormat = new CSVFormat();
private String compressionType = CompressionType.NONE.name();
private boolean keepAllColumns = false;
private boolean payloadCrcEnabled = false;
private boolean outputRawData = false;
Expand All @@ -26,19 +25,6 @@ public OutputSerialization withCsvOutputFormat(CSVFormat csvFormat) {
return this;
}

public String getCompressionType() {
return compressionType;
}

public void setCompressionType(CompressionType compressionType) {
this.compressionType = compressionType.name();
}

public OutputSerialization withCompressionType(CompressionType compressionType) {
setCompressionType(compressionType);
return this;
}

public boolean isKeepAllColumns() {
return keepAllColumns;
}
Expand Down
55 changes: 43 additions & 12 deletions src/test/java/com/aliyun/oss/integrationtests/SelectObjectTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.aliyun.oss.integrationtests;

import com.aliyun.oss.event.ProgressEvent;
import com.aliyun.oss.model.*;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -9,6 +10,12 @@

public class SelectObjectTest extends TestBase {

private static class CustomProgressListener implements com.aliyun.oss.event.ProgressListener {
public void progressChanged(ProgressEvent progressEvent) {
System.out.println(progressEvent.getBytes());
}
}

@Test
public void testGetSelectObjectMetadata() {
final String valid = "get-select-object-metadata-valid";
Expand All @@ -20,6 +27,8 @@ public void testGetSelectObjectMetadata() {
ossClient.putObject(bucketName, valid, new ByteArrayInputStream(validContent.getBytes()));
SelectObjectMetadata validSelectObjectMetadata = ossClient.createSelectObjectMetadata(
new CreateSelectObjectMetadataRequest(bucketName, valid)
.withOverwrite(true)
.withSelectProgressListener(new CustomProgressListener())
.withInputSerialization(new InputSerialization().withCsvInputFormat(new CSVFormat())));
Assert.assertEquals(5, validSelectObjectMetadata.getCsvObjectMetadata().getTotalLines());
Assert.assertEquals(1, validSelectObjectMetadata.getCsvObjectMetadata().getSplits());
Expand Down Expand Up @@ -47,50 +56,72 @@ public void testSelectObject() throws IOException {
final String key = "get-select-object-metadata-valid";
final String content = "name,school,company,age\n" +
"Lora Francis,School,Staples Inc,27\n" +
"#Lora Francis,School,Staples Inc,27\n" +
"Eleanor Little,School,\"Conectiv, Inc\",43\n" +
"Rosie Hughes,School,Western Gas Resources Inc,44\n" +
"Lawrence Ross,School,MetLife Inc.,24\n";
ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));

SelectObjectRequest selectObjectRequest =
new SelectObjectRequest(bucketName, key)
.withInputSerialization(new InputSerialization().withCsvInputFormat(
new CSVFormat().withHeaderInfo(CSVFormat.Header.Ignore)))
.withOutputSerialization(new OutputSerialization().withCsvOutputFormat(new CSVFormat()));
selectObjectRequest.setExpression("select * from ossobject");
.withSelectProgressListener(new CustomProgressListener())
.withSkipPartialDataRecord(false)
.withInputSerialization(new InputSerialization()
.withCompressionType(CompressionType.NONE)
.withCsvInputFormat(
new CSVFormat().withRecordDelimiter("\n")
.withQuoteChar("\"")
.withFieldDelimiter(",")
.withCommentChar("#")
.withHeaderInfo(CSVFormat.Header.Ignore)))
.withOutputSerialization(new OutputSerialization()
.withOutputHeader(false)
.withOutputRawData(false)
.withCrcEnabled(true)
.withKeepAllColumns(true)
.withCsvOutputFormat(new CSVFormat()))
.withExpression("select * from ossobject");
OSSObject ossObject = ossClient.selectObject(selectObjectRequest);
byte[] buffer = new byte[1024];
int bytesRead;
int off = 0;
while ((bytesRead = ossObject.getObjectContent().read(buffer, off, 1024 - off)) != -1) {
off += bytesRead;
while ((bytesRead = ossObject.getObjectContent().read()) != -1) {
buffer[off++] = (byte)bytesRead;
}

Assert.assertEquals(new String(buffer, 0, off), content.substring(content.indexOf("\n") + 1));
Assert.assertEquals(new String(buffer, 0, off), content.substring(content.indexOf("#L") + 1));

ossClient.createSelectObjectMetadata(
new CreateSelectObjectMetadataRequest(bucketName, key)
.withInputSerialization(new InputSerialization().withCsvInputFormat(new CSVFormat())));

selectObjectRequest.setLineRange(1, 3);
selectObjectRequest.getOutputSerialization().withKeepAllColumns(true);
selectObjectRequest.getOutputSerialization().withCrcEnabled(false);
OSSObject rangeOssObject = ossClient.selectObject(selectObjectRequest);
try {
rangeOssObject.getObjectContent().available();
Assert.fail("select object input stream does not support available() operation");
} catch (Exception e) {

}

off = 0;
while ((bytesRead = rangeOssObject.getObjectContent().read(buffer, off, 1024 - off)) != -1) {
while ((bytesRead = rangeOssObject.getObjectContent().read(buffer)) != -1) {
off += bytesRead;
}
Assert.assertEquals(new String(buffer, 0, off),
"Lora Francis,School,Staples Inc,27\n" +
"Eleanor Little,School,\"Conectiv, Inc\",43\n" +
"Rosie Hughes,School,Western Gas Resources Inc,44\n");
"Eleanor Little,School,\"Conectiv, Inc\",43\n");

selectObjectRequest.setLineRange(5, 10);
selectObjectRequest.withLineRange(6, 10);
try {
ossClient.selectObject(selectObjectRequest);
Assert.fail("invalid line range for select object request");
} catch (Exception e) {
}

selectObjectRequest.setSplitRange(5, 10);
selectObjectRequest.withSplitRange(5, 10);
try {
ossClient.selectObject(selectObjectRequest);
Assert.fail("both split range and line range have been set for select object request");
Expand Down

0 comments on commit 333dfe2

Please sign in to comment.