forked from liquibase/liquibase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassLoaderResourceAccessorTest.groovy
177 lines (157 loc) · 8.63 KB
/
ClassLoaderResourceAccessorTest.groovy
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package liquibase.resource
import liquibase.util.StreamUtil
import spock.lang.Specification
import spock.lang.Unroll
import java.nio.file.FileSystem
class ClassLoaderResourceAccessorTest extends Specification {
def testResourceAccessor = new ClassLoaderResourceAccessor(new URLClassLoader(
[
Thread.currentThread().getContextClassLoader().getResource("simple-files.jar"),
Thread.currentThread().getContextClassLoader().getResource("simple-files.zip"),
] as URL[],
Thread.currentThread().getContextClassLoader()))
@Unroll("#featureName: #relativeTo #streamPath")
def "getFinalPath"() {
expect:
new ClassLoaderResourceAccessor().getFinalPath(relativeTo, streamPath) == expected
where:
relativeTo | streamPath | expected
null | "com/example/test.sql" | "com/example/test.sql"
null | "/com/example/test.sql" | "com/example/test.sql"
null | "\\com\\example\\test.sql" | "com/example/test.sql"
null | "/com////example//test.sql" | "com/example/test.sql"
null | "classpath:com/example/test.sql" | "com/example/test.sql"
"com/example" | "test.sql" | "com/example/test.sql"
"/com/example/" | "test.sql" | "com/example/test.sql"
"com/example" | "/my/test.sql" | "com/example/my/test.sql"
"com/example" | "/my/test.sql" | "com/example/my/test.sql"
"com/example/other.file" | "/my/test.sql" | "com/example/my/test.sql"
"classpath:com/example/other.file" | "my/test.sql" | "com/example/my/test.sql"
"changelog.xml" | "sql/function.sql" | "sql/function.sql"
"db-change.log/changelog.xml" | "data/file.csv" | "db-change.log/data/file.csv"
}
@Unroll("#featureName: #relativeTo #streamPath")
def "openStreams, checking content"() {
given:
def streams = testResourceAccessor.openStreams(relativeTo, streamPath)
expect:
streams.size() == 1
StreamUtil.readStreamAsString(streams.iterator().next()).split(/\r?\n/)[0] == expectedContent
where:
relativeTo | streamPath | expectedContent
null | "liquibase.properties" | "# This is a sample liquibase.properties file for use by core unit tests. Its main purpose if to test the"
null | "liquibase/empty.changelog.xml" | "<databaseChangeLog xmlns=\"http://www.liquibase.org/xml/ns/dbchangelog\">"
null | "file-in-zip-root.txt" | "File in root"
null | "com/example/zip/file-in-zip.txt" | "File in zip"
null | "file-in-jar-root.txt" | "File in root"
"com/example" | "jar/file-in-jar.txt" | "File in jar"
"com/example/file-in-jar.txt" | "jar/file-in-jar.txt" | "File in jar"
}
@Unroll("#featureName: #relativeTo #streamPath")
def "openStreams, can't check content"() {
expect:
testResourceAccessor.openStreams(relativeTo, streamPath).size() == expectedSize
where:
relativeTo | streamPath | expectedSize
null | "invalid_file" | 0
"/path/to" | "/another/invalid_file" | 0
null | "com/example/everywhere/file-everywhere.txt" | 3
}
@Unroll
def "list"() {
expect:
testResourceAccessor.list(relativeTo, path, recursive, includeFiles, includeDirectories).toString() == expectedValue.toString()
where:
[relativeTo, path, recursive, includeFiles, includeDirectories, expectedValue] << [
[
null, "com/example/jar", true, true, true,
[
"com/example/jar/file-in-jar.txt",
]
],
[
null, "com/example", true, true, true,
[
"com/example/directory",
"com/example/directory/file-in-directory.txt",
"com/example/everywhere",
"com/example/everywhere/file-everywhere.txt",
"com/example/everywhere/other-file-everywhere.txt",
"com/example/file with space.txt",
"com/example/file-in-jar.txt",
"com/example/file-in-zip.txt",
"com/example/jar",
"com/example/jar/file-in-jar.txt",
"com/example/liquibase",
"com/example/liquibase/change",
"com/example/liquibase/change/ColumnConfig.class",
"com/example/liquibase/change/ComputedConfig.class",
"com/example/liquibase/change/CreateTableExampleChange.class",
"com/example/liquibase/change/DefaultConstraintConfig.class",
"com/example/liquibase/change/IdentityConfig.class",
"com/example/liquibase/change/KeyColumnConfig.class",
"com/example/liquibase/change/PrimaryKeyConfig.class",
"com/example/liquibase/change/UniqueConstraintConfig.class",
"com/example/my-logic.sql",
"com/example/shared",
"com/example/shared/file-in-jar.txt",
"com/example/shared/file-in-zip.txt",
"com/example/users.csv",
"com/example/zip",
"com/example/zip/file-in-zip.txt",
]
],
[
null, "com/example", false, true, true,
[
"com/example/directory",
"com/example/everywhere",
"com/example/file with space.txt",
"com/example/file-in-jar.txt",
"com/example/file-in-zip.txt",
"com/example/jar",
"com/example/liquibase",
"com/example/my-logic.sql",
"com/example/shared",
"com/example/users.csv",
"com/example/zip",
]
],
[
null, "com/example", false, true, false,
[
"com/example/file with space.txt",
"com/example/file-in-jar.txt",
"com/example/file-in-zip.txt",
"com/example/my-logic.sql",
"com/example/users.csv",
]
],
[
null, "com/example", false, false, true,
[
"com/example/directory",
"com/example/everywhere",
"com/example/jar",
"com/example/liquibase",
"com/example/shared",
"com/example/zip",
]
],
]
}
def close() {
setup:
FileSystem path1 = Mock()
FileSystem path2 = Mock()
when:
def testAccessor = new ClassLoaderResourceAccessor(this.getClass().getClassLoader())
testAccessor.rootPaths = null;
testAccessor.close() //no errors thrown from close() when rootPaths is null
testAccessor.rootPaths = [path1, path2]
testAccessor.close()
then:
2 * path1.close()
1 * path2.close()
}
}