Skip to content

Commit 333dfe2

Browse files
committed
add ut
1 parent d923b3e commit 333dfe2

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

src/main/java/com/aliyun/oss/common/parser/RequestMarshallers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ public byte[] marshall(SelectObjectRequest request) {
525525
xmlBody.append("<RecordDelimiter>" + BinaryUtil.toBase64String(csvInputFormat.getRecordDelimiter().getBytes()) + "</RecordDelimiter>");
526526
xmlBody.append("<FieldDelimiter>" + BinaryUtil.toBase64String(csvInputFormat.getFieldDelimiter().toString().getBytes()) + "</FieldDelimiter>");
527527
xmlBody.append("<QuoteCharacter>" + BinaryUtil.toBase64String(csvInputFormat.getQuoteChar().toString().getBytes()) + "</QuoteCharacter>");
528-
xmlBody.append("<Comments>" + BinaryUtil.toBase64String(csvInputFormat.getCommentChar().toString().getBytes()) + "</Comments>");
528+
xmlBody.append("<CommentCharacter>" + BinaryUtil.toBase64String(csvInputFormat.getCommentChar().toString().getBytes()) + "</CommentCharacter>");
529529

530530
if (request.getLineRange() != null) {
531531
xmlBody.append("<Range>" + request.lineRangeToString(request.getLineRange()) + "</Range>");

src/main/java/com/aliyun/oss/model/CreateSelectMetaInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private void readFrame() throws IOException {
147147
throw new IOException("Oss Select create meta encounter error code: " + status + ", message: " + error);
148148
}
149149

150-
selectObjectMetadata.setCsvObjectMetadata(
150+
selectObjectMetadata.withCsvObjectMetadata(
151151
new SelectObjectMetadata.CSVObjectMetadata()
152152
.withSplits(ByteBuffer.wrap(splitBytes).getInt())
153153
.withTotalLines(ByteBuffer.wrap(totalLineBytes).getLong()));

src/main/java/com/aliyun/oss/model/OutputSerialization.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88
public class OutputSerialization implements Serializable {
99
private CSVFormat csvOutputFormat = new CSVFormat();
10-
private String compressionType = CompressionType.NONE.name();
1110
private boolean keepAllColumns = false;
1211
private boolean payloadCrcEnabled = false;
1312
private boolean outputRawData = false;
@@ -26,19 +25,6 @@ public OutputSerialization withCsvOutputFormat(CSVFormat csvFormat) {
2625
return this;
2726
}
2827

29-
public String getCompressionType() {
30-
return compressionType;
31-
}
32-
33-
public void setCompressionType(CompressionType compressionType) {
34-
this.compressionType = compressionType.name();
35-
}
36-
37-
public OutputSerialization withCompressionType(CompressionType compressionType) {
38-
setCompressionType(compressionType);
39-
return this;
40-
}
41-
4228
public boolean isKeepAllColumns() {
4329
return keepAllColumns;
4430
}

src/test/java/com/aliyun/oss/integrationtests/SelectObjectTest.java

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.aliyun.oss.integrationtests;
22

3+
import com.aliyun.oss.event.ProgressEvent;
34
import com.aliyun.oss.model.*;
45
import org.junit.Assert;
56
import org.junit.Test;
@@ -9,6 +10,12 @@
910

1011
public class SelectObjectTest extends TestBase {
1112

13+
private static class CustomProgressListener implements com.aliyun.oss.event.ProgressListener {
14+
public void progressChanged(ProgressEvent progressEvent) {
15+
System.out.println(progressEvent.getBytes());
16+
}
17+
}
18+
1219
@Test
1320
public void testGetSelectObjectMetadata() {
1421
final String valid = "get-select-object-metadata-valid";
@@ -20,6 +27,8 @@ public void testGetSelectObjectMetadata() {
2027
ossClient.putObject(bucketName, valid, new ByteArrayInputStream(validContent.getBytes()));
2128
SelectObjectMetadata validSelectObjectMetadata = ossClient.createSelectObjectMetadata(
2229
new CreateSelectObjectMetadataRequest(bucketName, valid)
30+
.withOverwrite(true)
31+
.withSelectProgressListener(new CustomProgressListener())
2332
.withInputSerialization(new InputSerialization().withCsvInputFormat(new CSVFormat())));
2433
Assert.assertEquals(5, validSelectObjectMetadata.getCsvObjectMetadata().getTotalLines());
2534
Assert.assertEquals(1, validSelectObjectMetadata.getCsvObjectMetadata().getSplits());
@@ -47,50 +56,72 @@ public void testSelectObject() throws IOException {
4756
final String key = "get-select-object-metadata-valid";
4857
final String content = "name,school,company,age\n" +
4958
"Lora Francis,School,Staples Inc,27\n" +
59+
"#Lora Francis,School,Staples Inc,27\n" +
5060
"Eleanor Little,School,\"Conectiv, Inc\",43\n" +
5161
"Rosie Hughes,School,Western Gas Resources Inc,44\n" +
5262
"Lawrence Ross,School,MetLife Inc.,24\n";
5363
ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
5464

5565
SelectObjectRequest selectObjectRequest =
5666
new SelectObjectRequest(bucketName, key)
57-
.withInputSerialization(new InputSerialization().withCsvInputFormat(
58-
new CSVFormat().withHeaderInfo(CSVFormat.Header.Ignore)))
59-
.withOutputSerialization(new OutputSerialization().withCsvOutputFormat(new CSVFormat()));
60-
selectObjectRequest.setExpression("select * from ossobject");
67+
.withSelectProgressListener(new CustomProgressListener())
68+
.withSkipPartialDataRecord(false)
69+
.withInputSerialization(new InputSerialization()
70+
.withCompressionType(CompressionType.NONE)
71+
.withCsvInputFormat(
72+
new CSVFormat().withRecordDelimiter("\n")
73+
.withQuoteChar("\"")
74+
.withFieldDelimiter(",")
75+
.withCommentChar("#")
76+
.withHeaderInfo(CSVFormat.Header.Ignore)))
77+
.withOutputSerialization(new OutputSerialization()
78+
.withOutputHeader(false)
79+
.withOutputRawData(false)
80+
.withCrcEnabled(true)
81+
.withKeepAllColumns(true)
82+
.withCsvOutputFormat(new CSVFormat()))
83+
.withExpression("select * from ossobject");
6184
OSSObject ossObject = ossClient.selectObject(selectObjectRequest);
6285
byte[] buffer = new byte[1024];
6386
int bytesRead;
6487
int off = 0;
65-
while ((bytesRead = ossObject.getObjectContent().read(buffer, off, 1024 - off)) != -1) {
66-
off += bytesRead;
88+
while ((bytesRead = ossObject.getObjectContent().read()) != -1) {
89+
buffer[off++] = (byte)bytesRead;
6790
}
6891

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

7194
ossClient.createSelectObjectMetadata(
7295
new CreateSelectObjectMetadataRequest(bucketName, key)
7396
.withInputSerialization(new InputSerialization().withCsvInputFormat(new CSVFormat())));
7497

7598
selectObjectRequest.setLineRange(1, 3);
99+
selectObjectRequest.getOutputSerialization().withKeepAllColumns(true);
100+
selectObjectRequest.getOutputSerialization().withCrcEnabled(false);
76101
OSSObject rangeOssObject = ossClient.selectObject(selectObjectRequest);
102+
try {
103+
rangeOssObject.getObjectContent().available();
104+
Assert.fail("select object input stream does not support available() operation");
105+
} catch (Exception e) {
106+
107+
}
108+
77109
off = 0;
78-
while ((bytesRead = rangeOssObject.getObjectContent().read(buffer, off, 1024 - off)) != -1) {
110+
while ((bytesRead = rangeOssObject.getObjectContent().read(buffer)) != -1) {
79111
off += bytesRead;
80112
}
81113
Assert.assertEquals(new String(buffer, 0, off),
82114
"Lora Francis,School,Staples Inc,27\n" +
83-
"Eleanor Little,School,\"Conectiv, Inc\",43\n" +
84-
"Rosie Hughes,School,Western Gas Resources Inc,44\n");
115+
"Eleanor Little,School,\"Conectiv, Inc\",43\n");
85116

86-
selectObjectRequest.setLineRange(5, 10);
117+
selectObjectRequest.withLineRange(6, 10);
87118
try {
88119
ossClient.selectObject(selectObjectRequest);
89120
Assert.fail("invalid line range for select object request");
90121
} catch (Exception e) {
91122
}
92123

93-
selectObjectRequest.setSplitRange(5, 10);
124+
selectObjectRequest.withSplitRange(5, 10);
94125
try {
95126
ossClient.selectObject(selectObjectRequest);
96127
Assert.fail("both split range and line range have been set for select object request");

0 commit comments

Comments
 (0)