text:
package lib

import (
	"fmt"
	"io/ioutil"
	"net/url"
	"os"
	"sort"
	"strconv"
	"strings"

	oss "github.com/aliyun/aliyun-oss-go-sdk/oss"
	. "gopkg.in/check.v1"
)

// test list buckets
func (s *OssutilCommandSuite) TestListLoadConfig(c *C) {
	command := "ls"
	var args []string
	str := ""
	limitedNum := strconv.FormatInt(-1, 10)
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"stsToken":        &str,
		"configFile":      &configFile,
		"limitedNum":      &limitedNum,
	}
	showElapse, err := cm.RunCommand(command, args, options)
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	args = []string{"oss://"}
	options = OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"stsToken":        &str,
		"configFile":      &configFile,
		"limitedNum":      &limitedNum,
	}
	showElapse, err = cm.RunCommand(command, args, options)
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)
}

func (s *OssutilCommandSuite) TestListNotExistConfigFile(c *C) {
	command := "ls"
	var args []string
	str := ""
	cfile := "notexistfile"
	limitedNum := strconv.FormatInt(-1, 10)
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"stsToken":        &str,
		"configFile":      &cfile,
		"limitedNum":      &limitedNum,
	}
	showElapse, err := cm.RunCommand(command, args, options)
	c.Assert(err, NotNil)
	c.Assert(showElapse, Equals, false)
}

func (s *OssutilCommandSuite) TestListErrConfigFile(c *C) {
	cfile := randStr(10)
	s.createFile(cfile, content, c)

	command := "ls"
	var args []string
	str := ""
	limitedNum := strconv.FormatInt(-1, 10)
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"stsToken":        &str,
		"configFile":      &cfile,
		"limitedNum":      &limitedNum,
	}
	showElapse, err := cm.RunCommand(command, args, options)
	c.Assert(err, NotNil)
	c.Assert(showElapse, Equals, false)

	os.Remove(cfile)
}

func (s *OssutilCommandSuite) TestListConfigFile(c *C) {
	cfile := randStr(10)
	data := fmt.Sprintf("[Credentials]\nendpoint=%s\naccessKeyID=%s\naccessKeySecret=%s\n", endpoint, accessKeyID, accessKeySecret)
	s.createFile(cfile, data, c)

	command := "ls"
	var args []string
	str := ""
	limitedNum := strconv.FormatInt(-1, 10)
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"stsToken":        &str,
		"configFile":      &cfile,
		"limitedNum":      &limitedNum,
	}
	showElapse, err := cm.RunCommand(command, args, options)
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	os.Remove(cfile)
}

func (s *OssutilCommandSuite) TestListWithBucketEndpoint(c *C) {
	bucketName := bucketNameExist

	cfile := randStr(10)
	data := fmt.Sprintf("[Credentials]\nendpoint=%s\naccessKeyID=%s\naccessKeySecret=%s\n[Bucket-Endpoint]\n%s=%s", "abc", accessKeyID, accessKeySecret, bucketName, endpoint)
	s.createFile(cfile, data, c)

	command := "ls"
	args := []string{CloudURLToString(bucketName, "")}
	str := ""
	limitedNum := strconv.FormatInt(-1, 10)
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"stsToken":        &str,
		"configFile":      &cfile,
		"limitedNum":      &limitedNum,
	}
	showElapse, err := cm.RunCommand(command, args, options)
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	os.Remove(cfile)
}

func (s *OssutilCommandSuite) TestListWithBucketCname(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)

	cfile := randStr(10)
	data := fmt.Sprintf("[Credentials]\nendpoint=%s\naccessKeyID=%s\naccessKeySecret=%s\n[Bucket-Endpoint]\n%s=%s\n[Bucket-Cname]\n%s=%s", "abc", accessKeyID, accessKeySecret, bucketName, "abc", bucketName, bucketName+"."+endpoint)
	s.createFile(cfile, data, c)

	command := "ls"
	args := []string{CloudURLToString(bucketName, "")}
	str := ""
	limitedNum := strconv.FormatInt(-1, 10)
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"stsToken":        &str,
		"configFile":      &cfile,
		"limitedNum":      &limitedNum,
	}
	showElapse, err := cm.RunCommand(command, args, options)
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	os.Remove(cfile)
	s.removeBucket(bucketName, true, c)
}

// list objects with not exist bucket
func (s *OssutilCommandSuite) TestListObjectsBucketNotExist(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	command := "ls"
	args := []string{CloudURLToString(bucketName, "")}
	str := ""
	limitedNum := strconv.FormatInt(-1, 10)
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"stsToken":        &str,
		"configFile":      &configFile,
		"limitedNum":      &limitedNum,
	}
	showElapse, err := cm.RunCommand(command, args, options)
	c.Assert(err, NotNil)
	c.Assert(showElapse, Equals, false)
}

// list objects
func (s *OssutilCommandSuite) TestListObjects(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)

	// put objects
	num := 3
	for i := 0; i < num; i++ {
		object := fmt.Sprintf("lstest:#%d", i)
		s.putObject(bucketName, object, uploadFileName, c)
	}

	object := "another_object"
	s.putObject(bucketName, object, uploadFileName, c)

	objectStat := s.getStat(bucketName, object, c)
	c.Assert(objectStat[StatACL], Equals, "default")
	c.Assert(len(objectStat["Etag"]), Equals, 32)
	c.Assert(objectStat["Last-Modified"] != "", Equals, true)
	c.Assert(objectStat[StatOwner] != "", Equals, true)

	//put directories
	num1 := 2
	for i := 0; i < num1; i++ {
		object := fmt.Sprintf("lstest:#%d/", i)
		s.putObject(bucketName, object, uploadFileName, c)

		object = fmt.Sprintf("lstest:#%d/%d/", i, i)
		s.putObject(bucketName, object, uploadFileName, c)
	}

	// "ls oss://bucket -s"
	//objects := s.listObjects(bucketName, "", true, false, false, false, c)
	//c.Assert(len(objects), Equals, num + 2*num1 + 1)

	// "ls oss://bucket/prefix -s"
	objects := s.listObjects(bucketName, "lstest:", "ls -s", c)
	c.Assert(len(objects), Equals, num+2*num1)

	// "ls oss://bucket/prefix"
	objects = s.listObjects(bucketName, "lstest:#", "ls - ", c)
	c.Assert(len(objects), Equals, num+2*num1)

	// "ls oss://bucket/prefix -d"
	objects = s.listObjects(bucketName, "lstest:#", "ls -d", c)
	c.Assert(len(objects), Equals, num+num1)

	objects = s.listObjects(bucketName, "lstest:#1/", "ls -d", c)
	c.Assert(len(objects), Equals, 2)

	s.removeBucket(bucketName, true, c)
}

func (s *OssutilCommandSuite) TestListObjectsWithPayer(c *C) {
	s.createFile(uploadFileName, content, c)
	bucketName := payerBucket
	objectName := randStr(10)

	//put object, with --payer=requester
	args := []string{uploadFileName, CloudURLToString(bucketName, objectName)}
	showElapse, err := s.rawCPWithPayer(args, false, true, false, DefaultBigFileThreshold, "requester")
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	args = []string{CloudURLToString(bucketName, objectName)}
	showElapse, err = s.rawList(args, "ls - ", OptionPair{Key: "payer", Value: "requester"}, OptionPair{Key: "endpoint", Value: payerBucketEndPoint})
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)
}

func (s *OssutilCommandSuite) TestListMultipartUploadsWithPayer(c *C) {
	bucketName := payerBucket

	// list buckets with -m
	args := []string{CloudURLToString(bucketName, "")}
	showElapse, err := s.rawList(args, "ls -m", OptionPair{Key: "payer", Value: "requester"}, OptionPair{Key: "endpoint", Value: payerBucketEndPoint})
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)
}

func (s *OssutilCommandSuite) TestListObjectWithPayerInvalidPayer(c *C) {
	bucketName := payerBucket
	invalidPayer := randStr(10)

	// list buckets with -m
	args := []string{CloudURLToString(bucketName, "")}
	showElapse, err := s.rawList(args, "ls - ", OptionPair{Key: "payer", Value: invalidPayer})
	c.Assert(err, NotNil)
	c.Assert(showElapse, Equals, false)
}

func (s *OssutilCommandSuite) TestErrList(c *C) {
	showElapse, err := s.rawList([]string{"../"}, "ls -s")
	c.Assert(err, NotNil)
	c.Assert(showElapse, Equals, false)

	// not exist bucket
	bucketName := bucketNamePrefix + randLowStr(10)
	showElapse, err = s.rawList([]string{CloudURLToString(bucketName, "")}, "ls -d")
	c.Assert(err, NotNil)
	c.Assert(showElapse, Equals, false)

	// list buckets with -d
	showElapse, err = s.rawList([]string{"oss://"}, "ls -d")
	c.Assert(err, NotNil)
	c.Assert(showElapse, Equals, false)
}

func (s *OssutilCommandSuite) TestListIDKey(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)

	cfile := randStr(10)
	data := fmt.Sprintf("[Credentials]\nendpoint=%s\naccessKeyID=%s\naccessKeySecret=%s\n[Bucket-Endpoint]\n%s=%s[Bucket-Cname]\n%s=%s", "abc", "def", "ghi", bucketName, "abc", bucketName, "abc")
	s.createFile(cfile, data, c)

	command := "ls"
	str := ""
	args := []string{"oss://"}
	limitedNum := strconv.FormatInt(-1, 10)
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"stsToken":        &str,
		"configFile":      &cfile,
		"limitedNum":      &limitedNum,
	}
	showElapse, err := cm.RunCommand(command, args, options)
	c.Assert(err, NotNil)

	options = OptionMapType{
		"endpoint":        &endpoint,
		"accessKeyID":     &accessKeyID,
		"accessKeySecret": &accessKeySecret,
		"stsToken":        &str,
		"configFile":      &cfile,
		"limitedNum":      &limitedNum,
	}
	showElapse, err = cm.RunCommand(command, args, options)
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	os.Remove(cfile)

	s.removeBucket(bucketName, true, c)
}

func (s *OssutilCommandSuite) TestListBucketIDKey(c *C) {
	bucketName := bucketNameExist

	cfile := randStr(10)
	data := fmt.Sprintf("[Credentials]\nendpoint=%s\naccessKeyID=%s\naccessKeySecret=%s\n[Bucket-Endpoint]\n%s=%s[Bucket-Cname]\n%s=%s", "abc", "def", "ghi", bucketName, "abc", bucketName, "abc")
	s.createFile(cfile, data, c)

	command := "ls"
	str := ""
	args := []string{CloudURLToString(bucketName, "")}
	limitedNum := strconv.FormatInt(-1, 10)
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"stsToken":        &str,
		"configFile":      &cfile,
		"limitedNum":      &limitedNum,
	}
	showElapse, err := cm.RunCommand(command, args, options)
	c.Assert(err, NotNil)

	options = OptionMapType{
		"endpoint":        &endpoint,
		"accessKeyID":     &accessKeyID,
		"accessKeySecret": &accessKeySecret,
		"stsToken":        &str,
		"configFile":      &cfile,
		"limitedNum":      &limitedNum,
	}
	showElapse, err = cm.RunCommand(command, args, options)
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	os.Remove(cfile)
	cfile = randStr(20)

	// list without config file
	options = OptionMapType{
		"endpoint":        &endpoint,
		"accessKeyID":     &accessKeyID,
		"accessKeySecret": &accessKeySecret,
		"stsToken":        &str,
		"configFile":      &cfile,
		"limitedNum":      &limitedNum,
	}
	showElapse, err = cm.RunCommand(command, args, options)
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)
}

// list multipart
func (s *OssutilCommandSuite) TestListMultipartUploads(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)
	// "rm -arf oss://bucket/"
	command := "rm"
	args := []string{CloudURLToString(bucketName, "")}
	str := ""
	ok := true
	limitedNum := strconv.FormatInt(-1, 10)
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"stsToken":        &str,
		"configFile":      &configFile,
		"recursive":       &ok,
		"force":           &ok,
		"allType":         &ok,
		"limitedNum":      &limitedNum,
	}
	cm.RunCommand(command, args, options)

	object := "TestMultipartObjectLs"
	s.putObject(bucketName, object, uploadFileName, c)

	// list object
	objects := s.listObjects(bucketName, object, "ls - ", c)
	c.Assert(len(objects), Equals, 1)
	c.Assert(objects[0], Equals, object)

	bucket, err := copyCommand.command.ossBucket(bucketName)

	lmr_origin, e := bucket.ListMultipartUploads(oss.Prefix(object))
	c.Assert(e, IsNil)

	for i := 0; i < 20; i++ {
		_, err = bucket.InitiateMultipartUpload(object)
		c.Assert(err, IsNil)
	}

	lmr, e := bucket.ListMultipartUploads(oss.Prefix(object))
	c.Assert(e, IsNil)
	c.Assert(len(lmr.Uploads), Equals, 20+len(lmr_origin.Uploads))

	// list multipart: ls oss://bucket/object
	objects = s.listObjects(bucketName, object, "ls - ", c)
	c.Assert(len(objects), Equals, 1)
	c.Assert(objects[0], Equals, object)

	// list multipart: ls -m oss://bucket/object
	objects = s.listObjects(bucketName, object, "ls -m", c)
	c.Assert(len(objects), Equals, 20)

	// list all type object: ls -a oss://bucket/object
	objects = s.listObjects(bucketName, object, "ls -a", c)
	c.Assert(len(objects), Equals, 21)

	// list multipart: ls -am oss://bucket/object
	objects = s.listObjects(bucketName, object, "ls -am", c)
	c.Assert(len(objects), Equals, 21)

	// list multipart: ls -ms oss://bucket/object
	objects = s.listObjects(bucketName, object, "ls -ms", c)
	c.Assert(len(objects), Equals, 20)

	// list multipart: ls -as oss://bucket/object
	objects = s.listObjects(bucketName, object, "ls -as", c)
	c.Assert(len(objects), Equals, 21)

	lmr, e = bucket.ListMultipartUploads(oss.Prefix(object))
	c.Assert(e, IsNil)
	c.Assert(len(lmr.Uploads), Equals, 20+len(lmr_origin.Uploads))

	s.removeBucket(bucketName, true, c)
}

func (s *OssutilCommandSuite) TestListMultipartUploadsError(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)

	cfile := randStr(10)
	data := fmt.Sprintf("[Credentials]\nendpoint=%s\naccessKeyID=%s\naccessKeySecret=%s", endpoint, "abc", accessKeySecret)
	s.createFile(cfile, data, c)

	command := "ls"
	var args []string
	str := ""
	limitedNum := strconv.FormatInt(-1, 10)
	ok := true
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"stsToken":        &str,
		"configFile":      &cfile,
		"limitedNum":      &limitedNum,
		"multipart":       &ok,
	}
	showElapse, err := cm.RunCommand(command, args, options)
	c.Assert(err, NotNil)
	c.Assert(showElapse, Equals, false)

	os.Remove(cfile)

	s.removeBucket(bucketName, true, c)
}

func (s *OssutilCommandSuite) TestListLimitedMarker(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)

	bucketName1 := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName1, c)

	// list bucket
	buckets := s.listLimitedMarker("", "", "ls ", -1, "", "", c)
	c.Assert(FindPos(bucketName, buckets) != -1, Equals, true)

	// list bucket
	buckets = s.listLimitedMarker("", "", "ls ", 0, "", "", c)
	c.Assert(len(buckets), Equals, 0)

	// list bucket
	buckets = s.listLimitedMarker("", "", "ls ", 1, "", "", c)
	c.Assert(len(buckets), Equals, 1)

	buckets = s.listLimitedMarker("", "", "ls ", -1, "t", "", c)
	c.Assert(FindPos(bucketName, buckets), Equals, -1)

	buckets = s.listLimitedMarker("", "", "ls ", -1, "", "t", c)
	c.Assert(FindPos(bucketName, buckets) != -1, Equals, true)

	objectPrefix := "中文"
	for i := 0; i < 5; i++ {
		s.putObject(bucketName, fmt.Sprintf("%s%d", objectPrefix, i), uploadFileName, c)
	}

	bucket, err := listCommand.command.ossBucket(bucketName)

	for i := 0; i < 5; i++ {
		_, err = bucket.InitiateMultipartUpload(fmt.Sprintf("%s%d", objectPrefix, 0))
		c.Assert(err, IsNil)
	}

	lmr, err := bucket.ListMultipartUploads(oss.Prefix(fmt.Sprintf("%s%d", objectPrefix, 0)))
	c.Assert(err, IsNil)
	uploadIDs := []string{}
	for _, uploadID := range lmr.Uploads {
		uploadIDs = append(uploadIDs, uploadID.UploadID)
	}
	c.Assert(len(uploadIDs), Equals, 5)

	sort.Strings(uploadIDs)

	// list object with limitedNum
	objects := s.listLimitedMarker(bucketName, "", "ls ", -1, "", "", c)
	c.Assert(len(objects), Equals, 5)

	objects = s.listLimitedMarker(bucketName, "", "ls -a ", -1, "", "", c)
	c.Assert(len(objects), Equals, 10)

	objects = s.listLimitedMarker(bucketName, "", "ls -m ", -1, "", "", c)
	c.Assert(len(objects), Equals, 5)

	// normal list
	objects = s.listLimitedMarker(bucketName, "", "ls ", 6, "", "", c)
	c.Assert(len(objects), Equals, 5)

	objects = s.listLimitedMarker(bucketName, "", "ls -d", 6, "", "", c)
	c.Assert(len(objects), Equals, 5)

	objects = s.listLimitedMarker(bucketName, "", "ls ", 2, "", "", c)
	c.Assert(len(objects), Equals, 2)

	objects = s.listLimitedMarker(bucketName, "", "ls -d", 2, "", "", c)
	c.Assert(len(objects), Equals, 2)

	objects = s.listLimitedMarker(bucketName, "", "ls ", 0, "", "", c)
	c.Assert(len(objects), Equals, 0)

	objects = s.listLimitedMarker(bucketName, "", "ls ", 2, fmt.Sprintf("%s%d", objectPrefix, 1), "", c)
	c.Assert(len(objects), Equals, 2)

	objects = s.listLimitedMarker(bucketName, "", "ls ", 2, fmt.Sprintf("%s%d", objectPrefix, 3), "", c)
	c.Assert(len(objects), Equals, 1)

	objects = s.listLimitedMarker(bucketName, "", "ls ", 2, fmt.Sprintf("%s%d", objectPrefix, 3), "t", c)
	c.Assert(len(objects), Equals, 1)
	c.Assert(objects[0], Equals, fmt.Sprintf("%s%d", objectPrefix, 4))

	// list -m
	objects = s.listLimitedMarker(bucketName, "", "ls -m", 6, "", "", c)
	c.Assert(len(objects), Equals, 5)

	objects = s.listLimitedMarker(bucketName, "", "ls -m", 2, "", "", c)
	c.Assert(len(objects), Equals, 2)

	objects = s.listLimitedMarker(bucketName, "", "ls -m", 2, "", uploadIDs[0], c)
	c.Assert(len(objects), Equals, 2)

	objects = s.listLimitedMarker(bucketName, "", "ls -md", 2, "", uploadIDs[0], c)
	c.Assert(len(objects), Equals, 2)

	objects = s.listLimitedMarker(bucketName, "", "ls -m", 10, "", uploadIDs[1], c)
	c.Assert(len(objects), Equals, 5)

	objects = s.listLimitedMarker(bucketName, "", "ls -m --encoding-type url", 10, "", url.QueryEscape(uploadIDs[1]), c)
	c.Assert(len(objects), Equals, 5)

	objects = s.listLimitedMarker(bucketName, "", "ls -m", 10, objectPrefix, uploadIDs[1], c)
	c.Assert(len(objects), Equals, 5)

	objects = s.listLimitedMarker(bucketName, "", "ls -m", 10, fmt.Sprintf("%s%d", objectPrefix, 3), uploadIDs[1], c)
	c.Assert(len(objects), Equals, 0)

	objects = s.listLimitedMarker(bucketName, "", "ls -m", 2, objectPrefix, uploadIDs[2], c)
	c.Assert(len(objects), Equals, 2)

	// list -a
	objects = s.listLimitedMarker(bucketName, "", "ls -a", 6, "", "", c)
	c.Assert(len(objects), Equals, 6)

	objects = s.listLimitedMarker(bucketName, "", "ls -a", 11, "", "", c)
	c.Assert(len(objects), Equals, 10)

	objects = s.listLimitedMarker(bucketName, "", "ls -a", 20, fmt.Sprintf("%s%d", objectPrefix, 0), uploadIDs[2], c)
	c.Assert(len(objects), Equals, 6)

	objects = s.listLimitedMarker(bucketName, "", "ls -a", 5, fmt.Sprintf("%s%d", objectPrefix, 0), uploadIDs[2], c)
	c.Assert(len(objects), Equals, 5)

	objects = s.listLimitedMarker(bucketName, "", "ls -a", 20, fmt.Sprintf("%s%d", objectPrefix, 3), "", c)
	c.Assert(len(objects), Equals, 1)

	objects = s.listLimitedMarker(bucketName, "", "ls -a", 20, url.QueryEscape(fmt.Sprintf("%s%d", objectPrefix, 3)), "", c)
	c.Assert(len(objects), Equals, 10)

	objects = s.listLimitedMarker(bucketName, "", "ls -a --encoding-type url", 20, url.QueryEscape(fmt.Sprintf("%s%d", objectPrefix, 3)), "", c)
	c.Assert(len(objects), Equals, 1)

	objects = s.listLimitedMarker(bucketName, "", "ls -a", 4, fmt.Sprintf("%s%d", objectPrefix, 0), uploadIDs[0], c)
	c.Assert(len(objects), Equals, 4)

	objects = s.listLimitedMarker(bucketName, "", "ls -a --encoding-type url", 4, url.QueryEscape(fmt.Sprintf("%s%d", objectPrefix, 0)), url.QueryEscape(uploadIDs[0]), c)
	c.Assert(len(objects), Equals, 4)

	objects = s.listLimitedMarker(bucketName, "", "ls -a", -1, url.QueryEscape(fmt.Sprintf("%s%d", objectPrefix, 0)), url.QueryEscape(uploadIDs[0]), c)
	c.Assert(len(objects), Equals, 10)

	objects = s.listLimitedMarker(bucketName, "", "ls -a --encoding-type url", -1, url.QueryEscape(fmt.Sprintf("%s%d", objectPrefix, 0)), url.QueryEscape(uploadIDs[0]), c)
	c.Assert(len(objects), Equals, 8)

	objects = s.listLimitedMarker(bucketName, "", "ls -a", 20, "中文t", uploadIDs[0], c)
	c.Assert(len(objects), Equals, 0)

	s.removeBucket(bucketName, true, c)
	s.removeBucket(bucketName1, true, c)
}

func (s *OssutilCommandSuite) TestListURLEncoding(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)

	object := "^M特殊字符 加上空格 test"
	s.putObject(bucketName, object, uploadFileName, c)

	urlObject := url.QueryEscape(object)
	c.Assert(object != urlObject, Equals, true)

	// list object
	objects := s.listLimitedMarker(bucketName, "", "ls ", -1, "", "", c)
	c.Assert(len(objects), Equals, 1)
	c.Assert(objects[0], Equals, object)

	objects = s.listLimitedMarker(bucketName, urlObject, "ls ", -1, "", "", c)
	c.Assert(len(objects), Equals, 0)

	objects = s.listLimitedMarker(bucketName, urlObject, "ls --encoding-type url -as", -1, "", "", c)
	c.Assert(len(objects), Equals, 1)
	c.Assert(objects[0], Equals, object)

	// remove object
	_, err := s.removeWrapper("rm -f", bucketName, urlObject, c)
	c.Assert(err, IsNil)

	objects = s.listLimitedMarker(bucketName, urlObject, "ls --encoding-type url -s", -1, "", "", c)
	c.Assert(len(objects), Equals, 1)
	c.Assert(objects[0], Equals, object)

	// remove object
	_, err = s.removeWrapper("rm --encoding-type url -f", bucketName, urlObject, c)
	c.Assert(err, IsNil)

	objects = s.listLimitedMarker(bucketName, urlObject, "ls --encoding-type url -s", -1, "", "", c)
	c.Assert(len(objects), Equals, 0)

	s.putObject(bucketName, object, uploadFileName, c)

	// test url encode marker
	object1 := "^M特殊字符 加上空格 test1"
	s.putObject(bucketName, object1, uploadFileName, c)

	urlObject1 := url.QueryEscape(object1)
	c.Assert(object1 != urlObject1, Equals, true)

	objects = s.listLimitedMarker(bucketName, "", "ls --encoding-type url -as", -1, "", "", c)
	c.Assert(len(objects), Equals, 2)

	objects = s.listLimitedMarker(bucketName, "", "ls --encoding-type url -as", -1, urlObject, "", c)
	c.Assert(len(objects), Equals, 1)
	c.Assert(objects[0], Equals, object1)

	_, err = s.rawListLimitedMarker([]string{"oss%3a%2f%2f"}, "ls --encoding-type url", -1, "", "")
	c.Assert(err, NotNil)

	bucketName1 := url.QueryEscape("中文")
	_, err = s.rawListLimitedMarker([]string{"oss://" + bucketName1}, "ls --encoding-type url", -1, "", "")
	c.Assert(err, NotNil)

	_, err = s.rawListLimitedMarker([]string{"oss://" + bucketName}, "ls --encoding-type url", -1, "", "")
	c.Assert(err, IsNil)

	s.removeBucket(bucketName, true, c)
}

func (s *OssutilCommandSuite) TestListObjectfilterInclude(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)
	bucketStr := CloudURLToString(bucketName, "")

	dir := "ossutil-test-dir-" + randLowStr(5)
	subdir := "dir1"
	contents := map[string]string{}
	filenames := s.createTestFiles(dir, subdir, c, contents)

	// upload files
	args := []string{dir, bucketStr}
	cmdline := []string{"ossutil", "cp", dir, bucketStr, "-rf"}
	showElapse, err := s.rawCPWithFilter(args, true, true, false, DefaultBigFileThreshold, CheckpointDir, cmdline, "", "")
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	// ls files
	// e.g., ossutil ls oss://tempb4/ --include "*.jpg" --include "*.txt"
	cmdline = []string{"ossutil", "ls", bucketStr, "--include", "*.jpg", "--include", "*.txt"}
	limitedNum := strconv.FormatInt(-1, 10)
	lsArgs := []string{CloudURLToString(bucketName, "")}
	str := ""
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"configFile":      &configFile,
		"limitedNum":      &limitedNum,
	}

	testOutFileName := "ossutil-test-outfile-" + randLowStr(5)
	testOutFile, _ := os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	oldStdout := os.Stdout
	os.Stdout = testOutFile
	os.Args = cmdline
	_, err = cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()
	os.Stdout = oldStdout
	os.Args = []string{}

	fileBody, err := ioutil.ReadFile(testOutFileName)
	c.Assert(err, IsNil)

	// Verify
	files := filterStrsWithInclude(filenames, "*.jpg")
	for _, filename := range files {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, true)
	}

	files = filterStrsWithInclude(filenames, "*.txt")
	for _, filename := range files {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, true)
	}

	files = filterStrsWithInclude(filenames, "*.rtf")
	for _, filename := range files {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, false)
	}

	// cleanup
	os.Remove(testOutFileName)
	os.RemoveAll(dir)
	s.removeBucket(bucketName, true, c)
}

func (s *OssutilCommandSuite) TestListObjectfilterExclude(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)
	bucketStr := CloudURLToString(bucketName, "")

	dir := "ossutil-test-dir-" + randLowStr(5)
	subdir := "dir1"
	contents := map[string]string{}
	filenames := s.createTestFiles(dir, subdir, c, contents)

	// upload files
	args := []string{dir, bucketStr}
	cmdline := []string{"ossutil", "cp", dir, bucketStr, "-rf"}
	showElapse, err := s.rawCPWithFilter(args, true, true, false, DefaultBigFileThreshold, CheckpointDir, cmdline, "", "")
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	// ls files
	cmdline = []string{"ossutil", "ls", bucketStr, "--exclude", "*.jpg"}
	limitedNum := strconv.FormatInt(-1, 10)
	lsArgs := []string{CloudURLToString(bucketName, "")}
	str := ""
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"configFile":      &configFile,
		"limitedNum":      &limitedNum,
	}

	testOutFileName := "ossutil-test-outfile-" + randLowStr(5)
	testOutFile, _ := os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	oldStdout := os.Stdout
	os.Stdout = testOutFile
	os.Args = cmdline
	_, err = cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()
	os.Stdout = oldStdout
	os.Args = []string{}

	fileBody, err := ioutil.ReadFile(testOutFileName)
	c.Assert(err, IsNil)

	// Verify
	files := filterStrsWithInclude(filenames, "*.jpg")
	for _, filename := range files {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, false)
	}

	files = filterStrsWithInclude(filenames, "*.txt")
	for _, filename := range files {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, true)
	}

	files = filterStrsWithInclude(filenames, "*.rtf")
	for _, filename := range files {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, true)
	}

	// cleanup
	os.Remove(testOutFileName)
	os.RemoveAll(dir)
	s.removeBucket(bucketName, true, c)
}

func (s *OssutilCommandSuite) TestListDirectoryfilterInclude(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)
	bucketStr := CloudURLToString(bucketName, "")

	// directory1
	dir1 := "ossutil-test-dir-" + randLowStr(5)
	subdir1 := "dir1"
	contents1 := map[string]string{}
	filenames1 := s.createTestFiles(dir1, subdir1, c, contents1)

	// directory2
	dir2 := "ossutil-test-dir-" + randLowStr(5)
	subdir2 := "dir2"
	contents2 := map[string]string{}
	filenames2 := s.createTestFiles(dir2, subdir2, c, contents2)

	// upload directory1
	args := []string{dir1, bucketStr + "/" + dir1}
	cmdline := []string{"ossutil", "cp", dir1, bucketStr + "/" + dir1, "-rf"}
	showElapse, err := s.rawCPWithFilter(args, true, true, false, DefaultBigFileThreshold, CheckpointDir, cmdline, "", "")
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	// upload directory2
	args = []string{dir2, bucketStr + "/" + dir2}
	cmdline = []string{"ossutil", "cp", dir2, bucketStr + "/" + dir2, "-rf"}
	showElapse, err = s.rawCPWithFilter(args, true, true, false, DefaultBigFileThreshold, CheckpointDir, cmdline, "", "")
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	// ls files
	// e.g., ossutil ls oss://tempb4/ --include "*.jpg" --include dir1
	cmdline = []string{"ossutil", "ls", bucketStr, "--include"}
	strFilter := "*" + dir1
	cmdline = append(cmdline, strFilter)

	limitedNum := strconv.FormatInt(-1, 10)
	lsArgs := []string{CloudURLToString(bucketName, "")}
	str := ""
	bDirectory := true
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"configFile":      &configFile,
		"limitedNum":      &limitedNum,
		"directory":       &bDirectory,
	}

	testOutFileName := "ossutil-test-outfile-" + randLowStr(5)
	testOutFile, _ := os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	oldStdout := os.Stdout
	os.Stdout = testOutFile
	os.Args = cmdline
	_, err = cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()
	os.Stdout = oldStdout
	os.Args = []string{}

	fileBody, err := ioutil.ReadFile(testOutFileName)
	c.Assert(err, IsNil)

	// Verify
	c.Assert(strings.Contains(string(fileBody), dir1), Equals, true)
	for _, filename := range filenames1 {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, false)
	}

	c.Assert(strings.Contains(string(fileBody), dir2), Equals, false)
	for _, filename := range filenames2 {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, false)
	}

	// cleanup
	os.Remove(testOutFileName)
	os.RemoveAll(dir1)
	os.RemoveAll(dir2)
	s.removeBucket(bucketName, true, c)
}

func (s *OssutilCommandSuite) TestListDirectoryfilterExclude(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)
	bucketStr := CloudURLToString(bucketName, "")

	// directory1
	dir1 := "ossutil-test-dir-" + randLowStr(5)
	subdir1 := "dir1"
	contents1 := map[string]string{}
	filenames1 := s.createTestFiles(dir1, subdir1, c, contents1)

	// directory2
	dir2 := "ossutil-test-dir-" + randLowStr(5)
	subdir2 := "dir2"
	contents2 := map[string]string{}
	filenames2 := s.createTestFiles(dir2, subdir2, c, contents2)

	// upload directory1
	args := []string{dir1, bucketStr + "/" + dir1}
	cmdline := []string{"ossutil", "cp", dir1, bucketStr + "/" + dir1, "-rf"}
	showElapse, err := s.rawCPWithFilter(args, true, true, false, DefaultBigFileThreshold, CheckpointDir, cmdline, "", "")
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	// upload directory2
	args = []string{dir2, bucketStr + "/" + dir2}
	cmdline = []string{"ossutil", "cp", dir2, bucketStr + "/" + dir2, "-rf"}
	showElapse, err = s.rawCPWithFilter(args, true, true, false, DefaultBigFileThreshold, CheckpointDir, cmdline, "", "")
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	// ls files
	// e.g., ossutil ls oss://tempb4/ --include "*.jpg" --include dir1
	cmdline = []string{"ossutil", "ls", bucketStr, "--exclude"}
	strFilter := "*" + dir1
	cmdline = append(cmdline, strFilter)

	limitedNum := strconv.FormatInt(-1, 10)
	lsArgs := []string{CloudURLToString(bucketName, "")}
	str := ""
	bDirectory := true
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"configFile":      &configFile,
		"limitedNum":      &limitedNum,
		"directory":       &bDirectory,
	}

	testOutFileName := "ossutil-test-outfile-" + randLowStr(5)
	testOutFile, _ := os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	oldStdout := os.Stdout
	os.Stdout = testOutFile
	os.Args = cmdline
	_, err = cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()
	os.Stdout = oldStdout
	os.Args = []string{}

	fileBody, err := ioutil.ReadFile(testOutFileName)
	c.Assert(err, IsNil)

	// Verify
	c.Assert(strings.Contains(string(fileBody), dir1), Equals, false)
	for _, filename := range filenames1 {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, false)
	}

	c.Assert(strings.Contains(string(fileBody), dir2), Equals, true)
	for _, filename := range filenames2 {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, false)
	}

	// cleanup
	os.Remove(testOutFileName)
	os.RemoveAll(dir1)
	os.RemoveAll(dir2)
	s.removeBucket(bucketName, true, c)
}

func (s *OssutilCommandSuite) TestListPartfilterInclude(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)
	bucketStr := CloudURLToString(bucketName, "")

	bucket, err := makeBucketCommand.command.ossBucket(bucketName)

	// object jpg
	object1 := "ossutil-test-object-" + randLowStr(5) + ".jpg"
	_, err = bucket.InitiateMultipartUpload(object1)
	c.Assert(err, IsNil)

	// object png
	object2 := "ossutil-test-object-" + randLowStr(5) + ".png"
	_, err = bucket.InitiateMultipartUpload(object2)
	c.Assert(err, IsNil)

	// ls files
	cmdline := []string{"ossutil", "ls", "-m", bucketStr, "--include", "*.jpg"}

	limitedNum := strconv.FormatInt(-1, 10)
	lsArgs := []string{CloudURLToString(bucketName, "")}
	str := ""
	bPart := true
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"configFile":      &configFile,
		"limitedNum":      &limitedNum,
		"multipart":       &bPart,
	}

	testOutFileName := "ossutil-test-outfile-" + randLowStr(5)
	testOutFile, _ := os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	oldStdout := os.Stdout
	os.Stdout = testOutFile
	os.Args = cmdline
	_, err = cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()
	os.Stdout = oldStdout
	os.Args = []string{}

	fileBody, err := ioutil.ReadFile(testOutFileName)
	c.Assert(err, IsNil)

	// Verify
	c.Assert(strings.Contains(string(fileBody), object1), Equals, true)
	c.Assert(strings.Contains(string(fileBody), object2), Equals, false)

	// cleanup
	os.Remove(testOutFileName)
	s.removeBucket(bucketName, true, c)
}

func (s *OssutilCommandSuite) TestListPartfilterExclude(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)
	bucketStr := CloudURLToString(bucketName, "")

	bucket, err := makeBucketCommand.command.ossBucket(bucketName)

	// object jpg
	object1 := "ossutil-test-object-" + randLowStr(5) + ".jpg"
	_, err = bucket.InitiateMultipartUpload(object1)
	c.Assert(err, IsNil)

	// object png
	object2 := "ossutil-test-object-" + randLowStr(5) + ".png"
	_, err = bucket.InitiateMultipartUpload(object2)
	c.Assert(err, IsNil)

	// ls files
	cmdline := []string{"ossutil", "ls", "-m", bucketStr, "--exclude", "*.jpg"}

	limitedNum := strconv.FormatInt(-1, 10)
	lsArgs := []string{CloudURLToString(bucketName, "")}
	str := ""
	bPart := true
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"configFile":      &configFile,
		"limitedNum":      &limitedNum,
		"multipart":       &bPart,
	}

	testOutFileName := "ossutil-test-outfile-" + randLowStr(5)
	testOutFile, _ := os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	oldStdout := os.Stdout
	os.Stdout = testOutFile
	os.Args = cmdline
	_, err = cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()
	os.Stdout = oldStdout
	os.Args = []string{}

	fileBody, err := ioutil.ReadFile(testOutFileName)
	c.Assert(err, IsNil)

	// Verify
	c.Assert(strings.Contains(string(fileBody), object1), Equals, false)
	c.Assert(strings.Contains(string(fileBody), object2), Equals, true)

	// cleanup
	os.Remove(testOutFileName)
	s.removeBucket(bucketName, true, c)
}

// list objects versions
func (s *OssutilCommandSuite) TestListObjectVersionsNormal(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)
	s.putBucketVersioning(bucketName, string(oss.VersionEnabled), c)

	fileName := "test-ossutil-file-" + randLowStr(10)
	content := randLowStr(10)
	s.createFile(fileName, content, c)

	// put 3 objects
	numObject := 3
	for i := 0; i < numObject; i++ {
		object := fmt.Sprintf("lstest:#%d", i)
		s.putObject(bucketName, object, fileName, c)
	}

	// put 1 object
	numObject++
	object := "another_object" + randLowStr(5)
	s.putObject(bucketName, object, fileName, c)

	//put 2 directories
	numDir := 2
	for i := 0; i < numDir; i++ {
		object := fmt.Sprintf("lstest:#%d/", i)
		s.putObject(bucketName, object, fileName, c)
	}

	allVersions := true
	limitedNum := strconv.FormatInt(-1, 10)
	lsArgs := []string{CloudURLToString(bucketName, "")}
	str := ""
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"configFile":      &configFile,
		"allVersions":     &allVersions,
		"limitedNum":      &limitedNum,
	}

	testOutFileName := "ossutil-test-outfile-" + randLowStr(5)
	testOutFile, _ := os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	oldStdout := os.Stdout
	os.Stdout = testOutFile
	_, err := cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()

	fileBody, err := ioutil.ReadFile(testOutFileName)
	c.Assert(err, IsNil)
	c.Assert(strings.Contains(string(fileBody), "VERSIONID"), Equals, true)
	c.Assert(strings.Contains(string(fileBody), "COMMON-PREFIX"), Equals, false)

	strTotal := fmt.Sprintf("Object Number is: %d", numObject+numDir)
	c.Assert(strings.Contains(string(fileBody), strTotal), Equals, true)

	// ls oss://bucket -d
	directory := true
	options["directory"] = &directory
	testOutFile, _ = os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	os.Stdout = testOutFile
	_, err = cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()

	fileBody, err = ioutil.ReadFile(testOutFileName)
	c.Assert(err, IsNil)
	c.Assert(strings.Contains(string(fileBody), "VERSIONID"), Equals, true)
	c.Assert(strings.Contains(string(fileBody), "COMMON-PREFIX"), Equals, true)

	// rm all object
	recursive := true
	force := true
	deleteOptions := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"stsToken":        &str,
		"configFile":      &configFile,
		"recursive":       &recursive,
		"force":           &force,
	}
	rmArgs := []string{CloudURLToString(bucketName, "")}
	_, err = cm.RunCommand("rm", rmArgs, deleteOptions)
	c.Assert(err, IsNil)

	// list object again
	delete(options, "directory")
	testOutFile, _ = os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	os.Stdout = testOutFile
	_, err = cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()

	os.Stdout = oldStdout

	fileBody, err = ioutil.ReadFile(testOutFileName)
	strTotal = fmt.Sprintf("Object Number is: %d", (numObject+numDir)*2)
	c.Assert(strings.Contains(string(fileBody), strTotal), Equals, true)

	os.Remove(fileName)
	os.Remove(testOutFileName)
	s.removeBucket(bucketName, true, c)
}

// list objects versions
// list objects versions
func (s *OssutilCommandSuite) TestListObjectVersionsMarker(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)
	s.putBucketVersioning(bucketName, string(oss.VersionEnabled), c)

	fileName := "test-ossutil-file-" + randLowStr(10)
	content := randLowStr(10)
	s.createFile(fileName, content, c)

	object := "test-ossutil-object" + randLowStr(10)

	// default max-key 100
	numObject := 200
	for i := 0; i < numObject; i++ {
		s.putObject(bucketName, object, fileName, c)
	}

	allVersions := true
	limitedNum := strconv.FormatInt(-1, 10)
	lsArgs := []string{CloudURLToString(bucketName, "")}
	str := ""
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"configFile":      &configFile,
		"allVersions":     &allVersions,
		"limitedNum":      &limitedNum,
	}

	testOutFileName := "ossutil-test-outfile-" + randLowStr(5)
	testOutFile, _ := os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	oldStdout := os.Stdout
	os.Stdout = testOutFile
	_, err := cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()

	fileBody, err := ioutil.ReadFile(testOutFileName)
	c.Assert(err, IsNil)
	c.Assert(strings.Contains(string(fileBody), "VERSIONID"), Equals, true)
	c.Assert(strings.Contains(string(fileBody), "COMMON-PREFIX"), Equals, false)

	strTotal := fmt.Sprintf("Object Number is: %d", numObject)
	c.Assert(strings.Contains(string(fileBody), strTotal), Equals, true)

	// test for limit num
	limitedNum = strconv.FormatInt(int64(numObject/4), 10)
	saveLimit := numObject / 4
	testOutFile, _ = os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	os.Stdout = testOutFile
	_, err = cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()

	fileBody, err = ioutil.ReadFile(testOutFileName)
	c.Assert(err, IsNil)
	strTotal = fmt.Sprintf("Object Number is: %d", saveLimit)
	c.Assert(strings.Contains(string(fileBody), strTotal), Equals, true)

	//get all versions info
	bucket, err := listCommand.command.ossBucket(bucketName)
	c.Assert(err, IsNil)

	maxKeys := oss.MaxKeys(1000)
	listResult, err := listCommand.command.ossListObjectVersionsRetry(bucket, maxKeys)
	c.Assert(len(listResult.ObjectVersions), Equals, numObject)

	selectVersionId := listResult.ObjectVersions[int(saveLimit)].VersionId

	// test for key-marker version-id-mark
	limitedNum = strconv.FormatInt(-1, 10)
	options["marker"] = &object
	options["versionIdMarker"] = &selectVersionId

	testOutFile, _ = os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	os.Stdout = testOutFile
	_, err = cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()

	os.Stdout = oldStdout

	fileBody, err = ioutil.ReadFile(testOutFileName)
	strTotal = fmt.Sprintf("Object Number is: %d", numObject-saveLimit-1)
	c.Assert(strings.Contains(string(fileBody), strTotal), Equals, true)

	os.Remove(fileName)
	os.Remove(testOutFileName)
	s.removeBucket(bucketName, true, c)
}

func (s *OssutilCommandSuite) TestListObjectfilterIncludeVersions(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)
	s.putBucketVersioning(bucketName, string(oss.VersionEnabled), c)

	bucketStr := CloudURLToString(bucketName, "")

	dir := "ossutil-test-dir-" + randLowStr(5)
	subdir := "dir1"
	contents := map[string]string{}
	filenames := s.createTestFiles(dir, subdir, c, contents)

	// upload files
	args := []string{dir, bucketStr}
	cmdline := []string{"ossutil", "cp", dir, bucketStr, "-rf"}
	showElapse, err := s.rawCPWithFilter(args, true, true, false, DefaultBigFileThreshold, CheckpointDir, cmdline, "", "")
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	// "rm oss://bucket/prefix -r -f"
	// remove object
	s.removeObjects(bucketName, "re", true, true, c)

	// ls files
	// e.g., ossutil ls oss://tempb4/ --include "*.jpg" --include "*.txt"
	cmdline = []string{"ossutil", "ls", bucketStr, "--include", "*.jpg", "--include", "*.txt"}
	limitedNum := strconv.FormatInt(-1, 10)
	lsArgs := []string{CloudURLToString(bucketName, "")}
	allVersions := true
	str := ""
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"configFile":      &configFile,
		"limitedNum":      &limitedNum,
		"allVersions":     &allVersions,
	}

	testOutFileName := "ossutil-test-outfile-" + randLowStr(5)
	testOutFile, _ := os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	oldStdout := os.Stdout
	os.Stdout = testOutFile
	os.Args = cmdline
	_, err = cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()
	os.Stdout = oldStdout
	os.Args = []string{}

	fileBody, err := ioutil.ReadFile(testOutFileName)
	c.Assert(err, IsNil)

	// Verify
	files := filterStrsWithInclude(filenames, "*.jpg")
	for _, filename := range files {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, true)
	}

	files = filterStrsWithInclude(filenames, "*.txt")
	for _, filename := range files {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, true)
	}

	files = filterStrsWithInclude(filenames, "*.rtf")
	for _, filename := range files {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, false)
	}

	// cleanup
	os.Remove(testOutFileName)
	os.RemoveAll(dir)
	s.removeBucket(bucketName, true, c)
}

func (s *OssutilCommandSuite) TestListDirectoryfilterIncludeVersions(c *C) {
	bucketName := bucketNamePrefix + randLowStr(10)
	s.putBucket(bucketName, c)
	s.putBucketVersioning(bucketName, string(oss.VersionEnabled), c)

	bucketStr := CloudURLToString(bucketName, "")

	// directory1
	dir1 := "ossutil-test-dir-" + randLowStr(5)
	subdir1 := "dir1"
	contents1 := map[string]string{}
	filenames1 := s.createTestFiles(dir1, subdir1, c, contents1)

	// directory2
	dir2 := "ossutil-test-dir-" + randLowStr(5)
	subdir2 := "dir2"
	contents2 := map[string]string{}
	filenames2 := s.createTestFiles(dir2, subdir2, c, contents2)

	// upload directory1
	args := []string{dir1, bucketStr + "/" + dir1}
	cmdline := []string{"ossutil", "cp", dir1, bucketStr + "/" + dir1, "-rf"}
	showElapse, err := s.rawCPWithFilter(args, true, true, false, DefaultBigFileThreshold, CheckpointDir, cmdline, "", "")
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	// upload directory2
	args = []string{dir2, bucketStr + "/" + dir2}
	cmdline = []string{"ossutil", "cp", dir2, bucketStr + "/" + dir2, "-rf"}
	showElapse, err = s.rawCPWithFilter(args, true, true, false, DefaultBigFileThreshold, CheckpointDir, cmdline, "", "")
	c.Assert(err, IsNil)
	c.Assert(showElapse, Equals, true)

	// "rm oss://bucket/prefix -r -f"
	// remove object
	s.removeObjects(bucketName, "re", true, true, c)

	// ls files
	// e.g., ossutil ls oss://tempb4/ --include "*.jpg" --include dir1
	cmdline = []string{"ossutil", "ls", bucketStr, "--include"}
	strFilter := "*" + dir1
	cmdline = append(cmdline, strFilter)

	limitedNum := strconv.FormatInt(-1, 10)
	lsArgs := []string{CloudURLToString(bucketName, "")}
	str := ""
	bDirectory := true
	allVersions := true
	options := OptionMapType{
		"endpoint":        &str,
		"accessKeyID":     &str,
		"accessKeySecret": &str,
		"configFile":      &configFile,
		"limitedNum":      &limitedNum,
		"directory":       &bDirectory,
		"allVersions":     &allVersions,
	}

	testOutFileName := "ossutil-test-outfile-" + randLowStr(5)
	testOutFile, _ := os.OpenFile(testOutFileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0664)
	oldStdout := os.Stdout
	os.Stdout = testOutFile
	os.Args = cmdline
	_, err = cm.RunCommand("ls", lsArgs, options)
	c.Assert(err, IsNil)
	testOutFile.Close()
	os.Stdout = oldStdout
	os.Args = []string{}

	fileBody, err := ioutil.ReadFile(testOutFileName)
	c.Assert(err, IsNil)

	// Verify
	c.Assert(strings.Contains(string(fileBody), dir1), Equals, true)
	for _, filename := range filenames1 {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, false)
	}

	c.Assert(strings.Contains(string(fileBody), dir2), Equals, false)
	for _, filename := range filenames2 {
		c.Assert(strings.Contains(string(fileBody), filename), Equals, false)
	}

	// cleanup
	os.Remove(testOutFileName)
	os.RemoveAll(dir1)
	os.RemoveAll(dir2)
	s.removeBucket(bucketName, true, c)
}


text:
"""
Example implementation of image model in TensorFlow
that can be trained and deployed on Cloud ML Engine
"""

import argparse
import json
import os
import model

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  # Input Arguments
  parser.add_argument(
    '--train_batch_size',
    help='Batch size for training steps',
    type=int,
    default=256
  )
  parser.add_argument(
    '--learning_rate',
    help='Initial learning rate for training',
    type=float,
    default=0.01
  )
  parser.add_argument(
    '--train_steps',
    help="""\
      Steps to run the training job for. A step is one batch-size,\
      """,
    type=int,
    default=100
  )
  parser.add_argument(
    '--num_train_images',
    help='Number of training images',
    type=int,
    required=True
  )
  parser.add_argument(
    '--num_eval_images',
    help='Number of validation images',
    type=int,
    required=True
  )
  parser.add_argument(
    '--output_dir',
    help='GCS location to write checkpoints and export models',
    required=True
  )
  parser.add_argument(
    '--train_data_path',
    help='location of train file containing training tfrecords',
    required=True
  )
  parser.add_argument(
    '--eval_data_path',
    help='location of eval file containing evaluation tfrecords',
    required=True
  )
  parser.add_argument(
    '--job-dir',
    help='this model ignores this field, but it is required by gcloud',
    default='junk'
  )

  # for Cloud TPU
  parser.add_argument(
    '--use_tpu',
    help=('If specified, use TPU to execute the model for training and evaluation.'
          ' Else use whatever devices are available to'
          ' TensorFlow by default (e.g. CPU and GPU)'),
    dest='use_tpu',
    action='store_true')

  parser.add_argument(
    '--tpu', default=None,
    help='The Cloud TPU to use for training. This should be either the name '
         'used when creating the Cloud TPU, or a grpc://ip.address.of.tpu:8470 url.')

  parser.add_argument(
    '--project', default=None,
    help='Project name for the Cloud TPU-enabled project. If not specified, we '
         'will attempt to automatically detect the GCE project from metadata.')

  parser.add_argument(
    '--tpu_zone', default=None,
    help='GCE zone where the Cloud TPU is located in. If not specified, we '
         'will attempt to automatically detect the GCE project from metadata.')

  # optional hyperparameters used by cnn
  parser.add_argument(
    '--ksize1',
    help='kernel size of first layer for CNN',
    type=int,
    default=5)
  parser.add_argument(
    '--ksize2',
    help='kernel size of second layer for CNN',
    type=int,
    default=5)
  parser.add_argument(
    '--nfil1',
    help='number of filters in first layer for CNN',
    type=int,
    default=10)
  parser.add_argument(
    '--nfil2',
    help='number of filters in second layer for CNN',
    type=int,
    default=20)
  parser.add_argument(
    '--dprob',
    help='dropout probability for CNN',
    type=float,
    default=0.25)
  parser.add_argument(
    '--batch_norm',
    help='if specified, do batch_norm for CNN',
    dest='batch_norm',
    action='store_true')

  # boolean flags
  parser.set_defaults(use_tpu=False, batch_norm=False)

  args = parser.parse_args()
  hparams = args.__dict__
  print(hparams)

  output_dir = hparams.pop('output_dir')
  # Append trial_id to path for hptuning
  output_dir = os.path.join(
    output_dir,
    json.loads(
      os.environ.get('TF_CONFIG', '{}')
    ).get('task', {}).get('trial', '')
  )

  # Run the training job
  model.train_and_evaluate(output_dir, hparams)

redpajama_set_name: RedPajamaGithub


text:
class ApplicationController < ActionController::Base
  protect_from_forgery

  before_action :set_locale
  before_action :set_request_host
  before_action :set_current_region
  before_action :check_cookie_consent

  def authenticate_admin!
    return if current_profile&.admin?

    redirect_to profiles_url, notice: I18n.t('flash.profiles.no_permission')
  end

  def after_sign_in_path_for(resource)
    profile_url(resource) || root_path
  end

  def render_footer?
    false
  end
  helper_method(:render_footer?)

  def set_request_host
    Thread.current[:request_host] = request.host
  end

  def request_host
    Thread.current[:request_host]
  end
  helper_method :request_host

  def set_current_region
    @current_region = validate_region($1.to_sym) if request.host =~ %r((.+)\.#{current_domain})
  end

  def current_domain
    ENV['DOMAIN'] or Rails.env.development? ? 'speakerinnen.local' : 'speakerinnen.org'
  end

  def validate_region(region)
    region if region == :vorarlberg

    # countries.include?(region) || states.include?(region)
  end

  attr_reader :current_region
  helper_method :current_region

  private

  def build_missing_translations(object)
    I18n.available_locales.each do |locale|
      object.translations.build(locale: locale) unless object.translated_locales.include?(locale)
    end
  end

  def set_locale
    desired_locale = request.headers['HTTP_ACCEPT_LANGUAGE'].to_s[0..1] == 'de' ? 'de' : 'en'
    I18n.locale = params[:locale] || desired_locale
  end

  def default_url_options
    options = super
    options.update(locale: I18n.locale)
    options
  end

  def cookie_consent_given?
    cookies[:cookie_consent]
  end

  def check_cookie_consent
    if params[:allow_cookies] == 'true'
      cookies[:cookie_consent] = {
        value: true,
        expires: 1.year.from_now,
        domain: (Rails.env.staging? ? nil : :all)
      }
      reset_session
      redirect_back(fallback_location: root_path)
    elsif !cookie_consent_given?
      request.session_options[:skip] = true
    end
  end

end


text:
package org.broadinstitute.hellbender.tools.walkers.haplotypecaller.graphs;

import org.broadinstitute.hellbender.utils.test.BaseTest;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.*;

public final class LowWeightChainPrunerUnitTest extends BaseTest {
    @DataProvider(name = "pruneChainsData")
    public Object[][] makePruneChainsData() {
        List<Object[]> tests = new ArrayList<>();

        final SeqVertex v1 = new SeqVertex("A");
        final SeqVertex v2 = new SeqVertex("C");
        final SeqVertex v3 = new SeqVertex("G");
        final SeqVertex v4 = new SeqVertex("T");
        final SeqVertex v5 = new SeqVertex("AA");
        final SeqVertex v6 = new SeqVertex("CC");

        for ( final int edgeWeight : Arrays.asList(1, 2, 3) ) {
            for ( final int pruneFactor : Arrays.asList(1, 2, 3, 4) ) {
                for ( final boolean isRef : Arrays.asList(true, false)) {
                    { // just an isolated chain
                        final int nExpected = edgeWeight < pruneFactor && ! isRef ? 3 : 0;
                        SeqGraph graph = new SeqGraph(11);
                        graph.addVertices(v1, v2, v3);
                        graph.addEdges(() -> new BaseEdge(isRef, edgeWeight), v1, v2, v3);
                        tests.add(new Object[]{"combinatorial", graph, pruneFactor, nExpected > 0 ? Collections.emptySet() : graph.vertexSet()});
                    }
                }
            }
        }

        { // connects to ref chain
            SeqGraph graph = new SeqGraph(11);
            graph.addVertices(v1, v2, v3);
            graph.addVertices(v4, v5);
            graph.addEdges(() -> new BaseEdge(true, 1), v4, v5);
            graph.addEdges(() -> new BaseEdge(false, 1), v4, v1, v2, v3, v5);
            tests.add(new Object[]{"bad internal branch", graph, 2, new HashSet<>(Arrays.asList(v4, v5))});
        }

        { // has bad cycle
            SeqGraph graph = new SeqGraph(11);
            graph.addVertices(v1, v2, v3, v4);
            graph.addEdges(() -> new BaseEdge(false, 1), v4, v1, v2, v3, v1);
            // note that we'll remove v4 because it's low weight
            tests.add(new Object[]{"has bad cycle", graph, 2, Collections.emptySet()});
        }

        { // has good cycle
            SeqGraph graph = new SeqGraph(111);
            graph.addVertices(v1, v2, v3, v4);
            graph.addEdges(() -> new BaseEdge(false, 3), v4, v1, v2, v3, v1);
            // note that we'll remove v4 because it's low weight
            tests.add(new Object[]{"has good cycle", graph, 2, graph.vertexSet()});
        }

        { // has branch
            SeqGraph graph = new SeqGraph(11);
            graph.addVertices(v1, v2, v3, v4, v5, v6);
            graph.addEdges(() -> new BaseEdge(false, 1), v1, v2, v3, v4, v6);
            graph.addEdges(() -> new BaseEdge(false, 1), v1, v2, v3, v5, v6);
            tests.add(new Object[]{"has two bad branches", graph, 2, Collections.emptySet()});
        }

        { // middle vertex above threshold => no one can be removed
            SeqGraph graph = new SeqGraph(11);
            graph.addVertices(v1, v2, v3, v4, v5);
            graph.addEdges(() -> new BaseEdge(false, 1), v1, v2);
            graph.addEdges(() -> new BaseEdge(false, 3), v2, v3);
            graph.addEdges(() -> new BaseEdge(false, 1), v3, v4, v5);
            tests.add(new Object[]{"middle vertex above factor", graph, 2, graph.vertexSet()});
        }

        { // the branching node has value > pruneFactor
            SeqGraph graph = new SeqGraph(11);
            graph.addVertices(v1, v2, v3, v4, v5, v6);
            graph.addEdges(() -> new BaseEdge(false, 3), v1, v2);
            graph.addEdges(() -> new BaseEdge(false, 3), v2, v3);
            graph.addEdges(() -> new BaseEdge(false, 1), v3, v4, v6);
            graph.addEdges(() -> new BaseEdge(false, 3), v2, v5, v6);
            tests.add(new Object[]{"branch node greater than pruneFactor", graph, 2, graph.vertexSet()});
        }

        { // A single isolated chain with weights all below pruning should be pruned
            SeqGraph graph = new SeqGraph(11);
            graph.addVertices(v1, v2, v3, v4, v5);
            graph.addEdges(() -> new BaseEdge(false, 1), v1, v2, v3);
            graph.addEdges(() -> new BaseEdge(false, 5), v4, v5);
            tests.add(new Object[]{"isolated chain", graph, 2, new LinkedHashSet<>(Arrays.asList(v4, v5))});
        }

        { // A chain with weights all below pruning should be pruned, even if it connects to another good chain
            SeqGraph graph = new SeqGraph(11);
            graph.addVertices(v1, v2, v3, v4, v5, v6);
            graph.addEdges(() -> new BaseEdge(false, 1), v1, v2, v3, v5);
            graph.addEdges(() -> new BaseEdge(false, 5), v4, v5, v6);
            tests.add(new Object[]{"bad chain branching into good one", graph, 2, new HashSet<>(Arrays.asList(v4, v5, v6))});
        }

        return tests.toArray(new Object[][]{});
    }

    @Test(dataProvider = "pruneChainsData", enabled = true)
    public void testPruneChains(final String name, final SeqGraph graph, final int pruneFactor, final Set<SeqVertex> remainingVertices) {
        final Set<SeqVertex> copy = new HashSet<>(remainingVertices);
//        graph.printGraph(new File("in.dot"), 0);
        final LowWeightChainPruner<SeqVertex, BaseEdge> pruner = new LowWeightChainPruner<>(pruneFactor);
        pruner.pruneLowWeightChains(graph);
//        graph.printGraph(new File("out.dot"), 0);
        Assert.assertEquals(graph.vertexSet(), copy);
    }
}


text:
/*************************************************************************************************/

#ifndef BOOST_GIL_EXTENSION_IO_TARGA_IO_WRITER_BACKEND_HPP
#define BOOST_GIL_EXTENSION_IO_TARGA_IO_WRITER_BACKEND_HPP

////////////////////////////////////////////////////////////////////////////////////////
/// \file
/// \brief
/// \author Christian Henning \n
///
/// \date 2012 \n
///
////////////////////////////////////////////////////////////////////////////////////////

#include <boost/gil/extension/io/targa_tags.hpp>

namespace boost { namespace gil {

#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
#pragma warning(push)
#pragma warning(disable:4512) //assignment operator could not be generated
#endif

///
/// TARGA Writer Backend
///
template< typename Device >
struct writer_backend< Device
                     , targa_tag
                     >
{
public:

    typedef targa_tag format_tag_t;

public:

    writer_backend( const Device&                        io_dev
                  , const image_write_info< targa_tag >& info
                  )
    : _io_dev( io_dev )
    , _info( info )
    {}

public:

    Device _io_dev;

    image_write_info< targa_tag > _info;
};

#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
#pragma warning(pop)
#endif

} // namespace gil
} // namespace boost

#endif // BOOST_GIL_EXTENSION_IO_TARGA_IO_WRITER_BACKEND_HPP


text:
"""Tests for components.

This test can be used to ensure that a component is self contained by replacing
the component in grr/test_data/ with the real component.
"""
import os
import StringIO
import zipfile

from grr.client import comms
from grr.client.client_actions import components
from grr.lib import aff4
from grr.lib import flags
from grr.lib import test_lib
from grr.lib.rdfvalues import client as rdf_client

class MockHTTPManager(object):

  def __init__(self, token):
    self.token = token

  def OpenServerEndpoint(self, url):
    fd = aff4.FACTORY.Open("aff4:/web" + url, token=self.token)
    return comms.HTTPObject(data=fd.read(100e6), code=200)

class MockClientWorker(object):
  """Mock client worker."""

  def __init__(self, token):
    self.http_manager = MockHTTPManager(token)

class TestComponents(test_lib.EmptyActionTest):
  """Test the ability to load arbitrary components."""

  # This component will leave its mark on the os module when imported.
  component_payload = """
import os
os._grr_component_was_here = True
"""

  def setUp(self):
    super(TestComponents, self).setUp()

    # Create a mock component.
    fp = StringIO.StringIO()
    new_zip_file = zipfile.ZipFile(fp, mode="w")
    new_zip_file.writestr("mock_mod.py", self.component_payload)
    new_zip_file.close()

    self.component = test_lib.WriteComponent(
        name="mock_component",
        version="1.0",
        token=self.token,
        modules=["mock_mod"],
        raw_data=fp.getvalue())

  def testComponentLoading(self):
    """Ensure we can load the component."""
    message = rdf_client.LoadComponent(summary=self.component.summary)

    # The client uses its build_environment configuration to call the correct
    # version of the component. It is normally populated by the build system but
    # in this test we set it to a known value which is the same as we created
    # earlier.
    with test_lib.ConfigOverrider({
        "Client.build_environment": self.component.build_system.signature(),
        "Client.component_path": os.path.join(self.temp_dir, "components")
    }):
      self.RunAction(
          components.LoadComponent,
          message,
          grr_worker=MockClientWorker(self.token))

      # Make sure that the component was loaded.
      self.assertTrue(getattr(os, "_grr_component_was_here", False))

def main(argv):
  # Run the full test suite
  test_lib.GrrTestProgram(argv=argv)

if __name__ == "__main__":
  flags.StartMain(main)


text:
module YARD
  module Spellcheck
    # yard-spellcheck VERSION
    VERSION = '0.2.0'
  end
end


text:
package com.ongres.pgdeploy.core;

import com.ongres.pgdeploy.core.exceptions.NonWritableDestinationException;
import com.ongres.pgdeploy.core.exceptions.UnreachableBinariesException;
import com.ongres.pgdeploy.core.pgversion.PostgresMajorVersion;
import org.junit.After;
import org.junit.Test;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

public class AbstractPostgresInstallationSupplierUnzipFoldersTest {

  private PostgresInstallationSupplier supplier;
  private Path path;

  private Path writablePath = Paths.get("installation");

  private Path nonWritablePath = Paths.get("nonWritable");

  private List<String> existingZip =
      Arrays.asList("src", "test", "resources", "binaries", "bin.zip");

  private List<String> nonExistingZip =
      Arrays.asList("qwerqwer", "bin.zip");

  private List<PostgresInstallationFolder> allFolders =
      Arrays.asList(PostgresInstallationFolder.BIN,
          PostgresInstallationFolder.INCLUDE,
          PostgresInstallationFolder.LIB,
          PostgresInstallationFolder.SHARE);

  private List<PostgresInstallationFolder> someFolders =
      Arrays.asList(PostgresInstallationFolder.BIN,
          PostgresInstallationFolder.LIB);

  private PostgresInstallationSupplier setup(List<String> folders) {

    RelativeRoute route = new RelativeRoute(folders);

    return new MockedPostgresInstallationSupplier(
        PostgresMajorVersion.fromString("10").get(), 0, new Platform("Linux", "x64"), null, route);

  }

  @After
  public void tearDown() {
    if(Files.exists(path))
    {
      deleteFolder(path.toFile());
    }

  }

  public static void deleteFolder(File folder) {
    File[] files = folder.listFiles();
    if(files!=null) { //some JVMs return null for empty dirs
      for(File f: files) {
        if(f.isDirectory()) {
          deleteFolder(f);
        } else {
          f.delete();
        }
      }
    }
    folder.delete();
  }

  @Test
  public void unzipFoldersEverythingOk() throws Exception {
    supplier = setup(existingZip);

    path = writablePath;

    supplier.unpackFolders(path, allFolders);
    supplier.checkInstallation(path, allFolders);

  }

  @Test(expected = UnreachableBinariesException.class)
  public void unzipFoldersNonExistingZip() throws Exception {
    supplier = setup(nonExistingZip);

    path = writablePath;

    supplier.unpackFolders(path, allFolders);

  }

  @Test(expected = NonWritableDestinationException.class)
  public void unzipFoldersNonWritableDestination() throws Exception {
    supplier = setup(existingZip);

    path = nonWritablePath;

    Set<PosixFilePermission> perms =
        PosixFilePermissions.fromString("rw-rw-rw-");
    FileAttribute<Set<PosixFilePermission>> attr =
        PosixFilePermissions.asFileAttribute(perms);
    Files.createDirectory(path, attr);

    supplier.unpackFolders(path, allFolders);

  }

  @Test
  public void unzipFoldersNotAllOptions() throws Exception {
    supplier = setup(existingZip);

    path = writablePath;

    supplier.unpackFolders(path, someFolders);
    supplier.checkInstallation(path, someFolders);

  }

}


text:
class DeploysController < ApplicationController
  authorize_actions_for Deploy
  protect_from_forgery except: :create
  skip_before_filter :verify_authenticity_token, only: :create

  def create
    @app = App.find_by! api_key: params[:api_key]
    @deploy = @app.deploys.create!(default_deploy || heroku_deploy)
    render :xml => @deploy
  end

  private
    def default_deploy
      if params[:deploy]
        {
          :username     => params[:deploy][:local_username],
          :environment  => params[:deploy][:rails_env],
          :repository   => params[:deploy][:scm_repository],
          :revision     => params[:deploy][:scm_revision],
          :message      => params[:deploy][:message]
        }
      end
    end

    # handle Heroku's HTTP post deployhook format
    def heroku_deploy
      {
        :username     => params[:user],
        :environment  => params[:rack_env].try(:downcase) || params[:app],
        :repository   => "git@heroku.com:#{params[:app]}.git",
        :revision     => params[:head],
      }
    end

end


text:
/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
/*
 * This code was generated by https://github.com/googleapis/google-api-java-client-services/
 * Modify at your own risk.
 */

package com.google.api.services.containeranalysis.v1beta1.model;

/**
 * Basis describes the base image portion (Note) of the DockerImage relationship. Linked occurrences
 * are derived from this or an equivalent image via:   FROM  Or an equivalent reference, e.g. a tag
 * of the resource_url.
 *
 *
 This is the Java data model class that specifies how to parse/serialize into the JSON that is
 * transmitted over HTTP when working with the Container Analysis API. For a detailed explanation
 * see:
 * https://developers.google.com/api-client-library/java/google-http-java-client/json
 *

 *
 * @author Google, Inc.
 */
@SuppressWarnings("javadoc")
public final class Basis extends com.google.api.client.json.GenericJson {

  /**
   * Required. Immutable. The fingerprint of the base image.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private Fingerprint fingerprint;

  /**
   * Required. Immutable. The resource_url for the resource representing the basis of associated
   * occurrence images.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.lang.String resourceUrl;

  /**
   * Required. Immutable. The fingerprint of the base image.
   * @return value or {@code null} for none
   */
  public Fingerprint getFingerprint() {
    return fingerprint;
  }

  /**
   * Required. Immutable. The fingerprint of the base image.
   * @param fingerprint fingerprint or {@code null} for none
   */
  public Basis setFingerprint(Fingerprint fingerprint) {
    this.fingerprint = fingerprint;
    return this;
  }

  /**
   * Required. Immutable. The resource_url for the resource representing the basis of associated
   * occurrence images.
   * @return value or {@code null} for none
   */
  public java.lang.String getResourceUrl() {
    return resourceUrl;
  }

  /**
   * Required. Immutable. The resource_url for the resource representing the basis of associated
   * occurrence images.
   * @param resourceUrl resourceUrl or {@code null} for none
   */
  public Basis setResourceUrl(java.lang.String resourceUrl) {
    this.resourceUrl = resourceUrl;
    return this;
  }

  @Override
  public Basis set(String fieldName, Object value) {
    return (Basis) super.set(fieldName, value);
  }

  @Override
  public Basis clone() {
    return (Basis) super.clone();
  }

}


text:
describe "routes/api/links" do
  before(:all) do
    ActiveRecord::FixtureSet.create_fixtures(File.join(File.dirname(__FILE__), "..", "fixtures"), "links")
  end

  include AuthHelper
  before(:each) do
    http_login
  end

  def app
    Sinatra::Application
  end

  context "/api/links" do
    it "should respond with json" do
      get "/api/links"
      expect(last_response).to be_ok
      expect(last_response.content_type).to eq("application/json")
    end

    it "should respond with all tags" do
      get "/api/links"
      links = JSON.parse(last_response.body)
      expect(links.size).to eq(Link.all.size)
    end
  end
end


text:
package com.yuyh.sprintnba.retrofit.okhttp;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author yuyh.
 * @date 2016/8/2.
 */
public class CommonParamsInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {

        Request oldRequest = chain.request();

        HttpUrl.Builder authorizedUrlBuilder = oldRequest.url()
                .newBuilder()
                .scheme(oldRequest.url().scheme())
                .host(oldRequest.url().host())
                .addQueryParameter("appver", "1.0.2.2")
                .addQueryParameter("appvid", "1.0.2.2")
                .addQueryParameter("network", "wifi");

        Request newRequest = oldRequest.newBuilder()
                .method(oldRequest.method(), oldRequest.body())
                .url(authorizedUrlBuilder.build())
                .build();

        return chain.proceed(newRequest);
    }
}


text:
<!DOCTYPE html>

    <form id="searchForm"
          class="form-search form-inline col-sm-12"
          role="form"
          th:action="@{${searchUrl}}"
          method="post"
       >

                <input type="text"
                       id="searchParameter"
                       name="searchParameter"
                       th:value="${searchParameter}"
                       class="search-query form-control col-sm-10"
                       th:placeholder='#{${placeholderTerm}}'
                        />

                    <button type="submit" class="btn btn-info col-sm-12">

                            common.search

                    </button>

    </form>