|
| 1 | +package transfer |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +type testAdapter struct { |
| 10 | + name string |
| 11 | + dir Direction |
| 12 | +} |
| 13 | + |
| 14 | +func (a *testAdapter) Name() string { |
| 15 | + return a.name |
| 16 | +} |
| 17 | +func (a *testAdapter) Direction() Direction { |
| 18 | + return a.dir |
| 19 | +} |
| 20 | +func (a *testAdapter) Begin(maxConcurrency int, cb TransferProgressCallback, completion chan TransferResult) error { |
| 21 | + return nil |
| 22 | +} |
| 23 | +func (a *testAdapter) Add(t *Transfer) { |
| 24 | +} |
| 25 | +func (a *testAdapter) End() { |
| 26 | +} |
| 27 | +func (a *testAdapter) ClearTempStorage() error { |
| 28 | + return nil |
| 29 | +} |
| 30 | +func newTestAdapter(name string, dir Direction) TransferAdapter { |
| 31 | + return &testAdapter{name, dir} |
| 32 | +} |
| 33 | +func newRenamedTestAdapter(name string, dir Direction) TransferAdapter { |
| 34 | + return &testAdapter{"RENAMED", dir} |
| 35 | +} |
| 36 | +func resetAdapters() { |
| 37 | + uploadAdapterFuncs = make(map[string]NewTransferAdapterFunc) |
| 38 | + downloadAdapterFuncs = make(map[string]NewTransferAdapterFunc) |
| 39 | +} |
| 40 | + |
| 41 | +func testBasicAdapterExists(t *testing.T) { |
| 42 | + assert := assert.New(t) |
| 43 | + |
| 44 | + dls := GetDownloadAdapterNames() |
| 45 | + if assert.NotNil(dls) { |
| 46 | + assert.Equal([]string{"basic"}, dls) |
| 47 | + } |
| 48 | + uls := GetUploadAdapterNames() |
| 49 | + if assert.NotNil(uls) { |
| 50 | + assert.Equal([]string{"basic"}, uls) |
| 51 | + } |
| 52 | + da := NewDownloadAdapter("basic") |
| 53 | + if assert.NotNil(da) { |
| 54 | + assert.Equal("basic", da.Name()) |
| 55 | + assert.Equal(Download, da.Direction()) |
| 56 | + } |
| 57 | + ua := NewUploadAdapter("basic") |
| 58 | + if assert.NotNil(ua) { |
| 59 | + assert.Equal("basic", ua.Name()) |
| 60 | + assert.Equal(Upload, ua.Direction()) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func testAdapterRegAndOverride(t *testing.T) { |
| 65 | + assert := assert.New(t) |
| 66 | + |
| 67 | + assert.Nil(NewDownloadAdapter("test")) |
| 68 | + assert.Nil(NewUploadAdapter("test")) |
| 69 | + |
| 70 | + RegisterNewTransferAdapterFunc("test", Upload, newTestAdapter) |
| 71 | + assert.Nil(NewDownloadAdapter("test")) |
| 72 | + assert.NotNil(NewUploadAdapter("test")) |
| 73 | + |
| 74 | + RegisterNewTransferAdapterFunc("test", Download, newTestAdapter) |
| 75 | + da := NewDownloadAdapter("test") |
| 76 | + if assert.NotNil(da) { |
| 77 | + assert.Equal("test", da.Name()) |
| 78 | + assert.Equal(Download, da.Direction()) |
| 79 | + } |
| 80 | + ua := NewUploadAdapter("test") |
| 81 | + if assert.NotNil(ua) { |
| 82 | + assert.Equal("test", ua.Name()) |
| 83 | + assert.Equal(Upload, ua.Direction()) |
| 84 | + } |
| 85 | + |
| 86 | + // Test override |
| 87 | + RegisterNewTransferAdapterFunc("test", Upload, newRenamedTestAdapter) |
| 88 | + ua = NewUploadAdapter("test") |
| 89 | + if assert.NotNil(ua) { |
| 90 | + assert.Equal("RENAMED", ua.Name()) |
| 91 | + assert.Equal(Upload, ua.Direction()) |
| 92 | + } |
| 93 | + da = NewDownloadAdapter("test") |
| 94 | + if assert.NotNil(da) { |
| 95 | + assert.Equal("test", da.Name()) |
| 96 | + assert.Equal(Download, da.Direction()) |
| 97 | + } |
| 98 | + RegisterNewTransferAdapterFunc("test", Download, newRenamedTestAdapter) |
| 99 | + da = NewDownloadAdapter("test") |
| 100 | + if assert.NotNil(da) { |
| 101 | + assert.Equal("RENAMED", da.Name()) |
| 102 | + assert.Equal(Download, da.Direction()) |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments