11package com .aliyun .oss .integrationtests ;
22
3+ import com .aliyun .oss .event .ProgressEvent ;
34import com .aliyun .oss .model .*;
45import org .junit .Assert ;
56import org .junit .Test ;
910
1011public 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