-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
SwiftUnderFileSystemTest.java
111 lines (102 loc) · 4.02 KB
/
SwiftUnderFileSystemTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.underfs.swift;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.reflect.Whitebox;
/**
* Tests for the private helper methods in {@link SwiftUnderFileSystem} that do not require a
* Swift backend.
*/
public class SwiftUnderFileSystemTest {
private SwiftUnderFileSystem mMockSwiftUnderFileSystem;
private String mMockContainerName = "test-container";
private String mMockContainerPrefix = "swift://" + mMockContainerName + "/";
/**
* Sets up the mock before a test runs.
*/
@Before
public final void before() {
mMockSwiftUnderFileSystem = PowerMockito.mock(SwiftUnderFileSystem.class);
Whitebox.setInternalState(mMockSwiftUnderFileSystem, "mContainerName", mMockContainerName);
Whitebox.setInternalState(mMockSwiftUnderFileSystem, "mContainerPrefix", mMockContainerPrefix);
}
/**
* Tests the {@link SwiftUnderFileSystem#addFolderSuffixIfNotPresent(String)} (String)} method.
*/
@Test
public void addFolderSuffixIfNotPresentTest() throws Exception {
String input1 = "a/b";
String input2 = "/a/b";
String input3 = "a/b/";
String input4 = "/a/b/";
String result1 = Whitebox.invokeMethod(mMockSwiftUnderFileSystem,
"addFolderSuffixIfNotPresent", input1);
String result2 = Whitebox.invokeMethod(mMockSwiftUnderFileSystem,
"addFolderSuffixIfNotPresent", input2);
String result3 = Whitebox.invokeMethod(mMockSwiftUnderFileSystem,
"addFolderSuffixIfNotPresent", input3);
String result4 = Whitebox.invokeMethod(mMockSwiftUnderFileSystem,
"addFolderSuffixIfNotPresent", input4);
Assert.assertEquals(result1, "a/b/");
Assert.assertEquals(result2, "/a/b/");
Assert.assertEquals(result3, "a/b/");
Assert.assertEquals(result4, "/a/b/");
}
/**
* Tests the {@link SwiftUnderFileSystem#stripFolderSuffixIfPresent(String)} method.
*/
@Test
public void stripFolderSuffixIfPresentTest() throws Exception {
String input1 = mMockContainerPrefix;
String input2 = mMockContainerPrefix + "dir/file";
String input3 = mMockContainerPrefix + "dir/";
String result1 =
Whitebox.invokeMethod(mMockSwiftUnderFileSystem, "stripFolderSuffixIfPresent", input1);
String result2 =
Whitebox.invokeMethod(mMockSwiftUnderFileSystem, "stripFolderSuffixIfPresent", input2);
String result3 =
Whitebox.invokeMethod(mMockSwiftUnderFileSystem, "stripFolderSuffixIfPresent", input3);
Assert.assertEquals(mMockContainerPrefix, result1 + "/");
Assert.assertEquals(mMockContainerPrefix + "dir/file", result2);
Assert.assertEquals(mMockContainerPrefix + "dir", result3);
}
/**
* Tests the {@link SwiftUnderFileSystem#stripPrefixIfPresent(String)} method.
*/
@Test
public void stripPrefixIfPresentTest() throws Exception {
String[] inputs = new String[]{
"swift://" + mMockContainerName,
mMockContainerPrefix,
mMockContainerPrefix + "file",
mMockContainerPrefix + "dir/file",
"swift://test-container-wrong/dir/file",
"dir/file",
"/dir/file",
};
String[] results = new String[]{
"swift://" + mMockContainerName,
"",
"file",
"dir/file",
"swift://test-container-wrong/dir/file",
"dir/file",
"dir/file",
};
for (int i = 0; i < inputs.length; i++) {
Assert.assertEquals(results[i], Whitebox.invokeMethod(mMockSwiftUnderFileSystem,
"stripPrefixIfPresent", inputs[i]));
}
}
}