8
8
import org .junit .Test ;
9
9
10
10
import java .net .URI ;
11
+ import java .util .LinkedHashSet ;
12
+ import java .util .Set ;
11
13
12
14
import static org .junit .Assert .assertArrayEquals ;
13
15
import static org .junit .Assert .assertEquals ;
18
20
public abstract class AbstractKeyValueAccessTest {
19
21
20
22
protected abstract KeyValueAccess newKeyValueAccess (URI root );
23
+
21
24
protected KeyValueAccess newKeyValueAccess () {
25
+
22
26
return newKeyValueAccess (tempUri ());
23
27
}
24
28
25
29
protected abstract URI tempUri ();
26
30
27
31
protected URI [] testURIs (final URI base ) {
32
+
33
+ final Set <URI > testUris = new LinkedHashSet <>();
34
+ /*add the base uri as a test case */
35
+ testUris .add (base );
36
+
37
+ /* NOTE: Java 8 doesn't behave well with URIs with empty path when resolving against a path.
38
+ * See KeyValueAccess#compose for more details.
39
+ * In tests with that as a base URI, resolve against `/` first.
40
+ * Should be unnecessary in Java 21*/
41
+ final URI testUri = base .getPath ().isEmpty () ? base .resolve ("/" ) : base ;
28
42
final URI [] pathParts = new URI []{
29
- N5URI .getAsUri ("test/path/file" ), // typical path, with leading and trailing slash
30
- N5URI .getAsUri ("test/path/file/" ), // typical path, with leading and trailing slash
31
- N5URI .getAsUri ("/test/path/file/" ), // typical path, with leading and trailing slash
32
- N5URI .getAsUri ("file" ), // single path
33
- N5URI .getAsUri ("file/" ), // single path
34
- N5URI .getAsUri ("/file/" ), // single path
43
+ N5URI .getAsUri ("test/path/file" ), // typical path, with no leading or trailing slashes
44
+ N5URI .getAsUri ("test/path/file/" ), // typical path, with trailing slash
45
+ N5URI .getAsUri ("/test/path/file" ), // typical path, with leading slash
46
+ N5URI .getAsUri ("/test/path/file/" ), // typical path, with leading and trailing slash
47
+ N5URI .getAsUri ("file" ), // single path
48
+ N5URI .getAsUri ("file/" ), // single path
49
+ N5URI .getAsUri ("/file" ), // single path
50
+ N5URI .getAsUri ("/file/" ), // single path
35
51
N5URI .getAsUri ("path/w i t h/spaces" ),
36
52
N5URI .getAsUri ("uri/illegal%character" ),
37
- N5URI .getAsUri ("/" ),
38
- N5URI .getAsUri ("" )
53
+ N5URI .getAsUri ("/" ), // root path
54
+ N5URI .getAsUri ("" ) // empty path
39
55
};
40
- final URI [] testUris = new URI [pathParts .length ];
41
- for (int i = 0 ; i < pathParts .length ; i ++) {
42
- final URI pathPart = pathParts [i ];
43
- testUris [i ] = base .resolve (pathPart );
56
+ for (final URI pathPart : pathParts ) {
57
+ testUris .add (testUri .resolve (pathPart ));
44
58
}
45
- return testUris ;
59
+ return testUris . toArray ( new URI [ 0 ]) ;
46
60
}
47
61
48
62
protected String [][] testPathComponents (final URI base ) {
@@ -81,7 +95,7 @@ protected void testComponentsAtLocation(URI testRoot) {
81
95
82
96
final String [] components = access .components (testPaths [i ].getPath ());
83
97
84
- assertArrayEquals ("Failure at Index " + i , expectedComponents [i ], components );
98
+ assertArrayEquals ("Failure at Index " + i , expectedComponents [i ], components );
85
99
}
86
100
}
87
101
@@ -96,11 +110,12 @@ protected void testComposeAtLocation(URI uri) {
96
110
97
111
for (int i = 0 ; i < testPathComponents .length ; ++i ) {
98
112
testPathComponents [i ] = testPathComponents [i ].clone ();
99
- final URI baseUri = testUris [i ].resolve ("/" );
113
+ /* Don't add the "/" if the input uri path is empty. just use it. Otherwise, remove the parts and start with "/" */
114
+ final URI baseUri = uri .getPath ().isEmpty () ? uri : testUris [i ].resolve ("/" );
100
115
final String [] components = testPathComponents [i ];
101
- final String stringUriFromComponents = access .compose (baseUri , components );
102
- final URI uriFromComponents = N5URI . getAsUri ( stringUriFromComponents );
103
- assertEquals ("Failure at Index " + i , testUris [ i ], uriFromComponents );
116
+ final String actualCompose = access .compose (baseUri , components );
117
+ final String expectedCompose = access . compose ( testUris [ i ] );
118
+ assertEquals ("Failure at Index " + i , expectedCompose , actualCompose );
104
119
}
105
120
}
106
121
@@ -111,21 +126,83 @@ public void testComponents() {
111
126
}
112
127
113
128
@ Test
114
- public void testComponentsAtRoot () {
129
+ public void testComponentsWithPathSlash () {
130
+
131
+ final URI uriWithPathSlash = setUriPath (tempUri (), "/" );
132
+ testComponentsAtLocation (uriWithPathSlash );
133
+ }
134
+
135
+ @ Test
136
+ public void testComponentsWithPathEmpty () {
115
137
116
- URI root = tempUri (). resolve ( "/ " );
117
- testComponentsAtLocation (root );
138
+ final URI uriWithPathEmpty = setUriPath ( tempUri (), " " );
139
+ testComponentsAtLocation (uriWithPathEmpty );
118
140
}
119
141
120
142
@ Test
121
143
public void testCompose () {
122
- testComposeAtLocation (tempUri ());
144
+
145
+ final URI uri = tempUri ();
146
+ testComposeAtLocation (uri );
147
+
148
+ final KeyValueAccess kva = newKeyValueAccess ();
149
+ final URI uriWithPath = setUriPath (uri , "/foo" );
150
+ assertEquals ("Non-empty Path" , "/foo" , uriWithPath .getPath ());
151
+ assertComposeEquals ("Non-empty Path, no empty or slash in components" , kva , uriWithPath , "/foo/bar/baz" , "bar" , "baz" );
152
+ assertComposeEquals ("Non-empty Path, first components leading slash" , kva , uriWithPath , "/bar/baz" , "/bar" , "baz" );
153
+ assertComposeEquals ("Non-empty Path, first components slash only" , kva , uriWithPath ,"/bar/baz" , "/" , "bar" , "baz" );
154
+ assertComposeEquals ("Non-empty Path, first components slash only" , kva , uriWithPath ,"/foo/bar/baz" , "" , "bar" , "baz" );
155
+ assertComposeEquals ("Non-empty Path, first components slash only" , kva , uriWithPath ,"/bar/baz" , "" , "/bar" , "baz" );
156
+ assertComposeEquals ("Non-empty Path, null and empty inner components" , kva , uriWithPath ,"/foo/bar/baz" , "bar" , null , "" , "baz" );
157
+ assertComposeEquals ("Non-empty Path, null and empty inner components" , kva , uriWithPath ,"/bar/baz" , "/bar" , null , "" , "baz" );
158
+ }
159
+
160
+ @ Test
161
+ public void testComposeWithPathSlash () {
162
+
163
+ final URI uriWithSlashRoot = setUriPath (tempUri (), "/" );
164
+ assertEquals ("Root (/) Path" , "/" , uriWithSlashRoot .getPath ());
165
+ testComposeAtLocation (uriWithSlashRoot );
166
+
167
+ final KeyValueAccess kva = newKeyValueAccess ();
168
+ assertComposeEquals ("Root (/) Path, no empty or slash in components" , kva , uriWithSlashRoot , "/bar/baz" , "bar" , "baz" );
169
+ assertComposeEquals ("Root (/) Path, first components leading slash" , kva , uriWithSlashRoot , "/bar/baz" , "/bar" , "baz" );
170
+ assertComposeEquals ("Root (/) Path, first components slash only" , kva , uriWithSlashRoot ,"/bar/baz" , "/" , "bar" , "baz" );
171
+ assertComposeEquals ("Root (/) Path, first components slash only" , kva , uriWithSlashRoot ,"/bar/baz" , "" , "bar" , "baz" );
172
+ assertComposeEquals ("Root (/) Path, first components slash only" , kva , uriWithSlashRoot ,"/bar/baz" , "" , "/bar" , "baz" );
173
+ assertComposeEquals ("Root (/) Path, null and empty inner components" , kva , uriWithSlashRoot ,"/bar/baz" , "bar" , null , "" , "baz" );
174
+ assertComposeEquals ("Root (/) Path, null and empty inner components" , kva , uriWithSlashRoot ,"/bar/baz" , "/bar" , null , "" , "baz" );
123
175
}
124
176
125
177
@ Test
126
- public void testComposeAtRoot () {
178
+ public void testComposeWithPathEmpty () {
179
+
180
+ final URI uriWithEmptyRoot = setUriPath (tempUri (), "" );
181
+ assertEquals ("Empty Path" , "" , uriWithEmptyRoot .getPath ());
182
+ testComposeAtLocation (uriWithEmptyRoot );
183
+
184
+ final KeyValueAccess kva = newKeyValueAccess ();
185
+ assertComposeEquals ("Root (/) Path, no empty or slash in components" , kva , uriWithEmptyRoot , "/bar/baz" , "bar" , "baz" );
186
+ assertComposeEquals ("Root (/) Path, first components leading slash" , kva , uriWithEmptyRoot , "/bar/baz" , "/bar" , "baz" );
187
+ assertComposeEquals ("Root (/) Path, first components slash only" , kva , uriWithEmptyRoot ,"/bar/baz" , "/" , "bar" , "baz" );
188
+ assertComposeEquals ("Root (/) Path, first components slash only" , kva , uriWithEmptyRoot ,"/bar/baz" , "" , "bar" , "baz" );
189
+ assertComposeEquals ("Root (/) Path, first components slash only" , kva , uriWithEmptyRoot ,"/bar/baz" , "" , "/bar" , "baz" );
190
+ assertComposeEquals ("Root (/) Path, null and empty inner components" , kva , uriWithEmptyRoot ,"/bar/baz" , "bar" , null , "" , "baz" );
191
+ assertComposeEquals ("Root (/) Path, null and empty inner components" , kva , uriWithEmptyRoot ,"/bar/baz" , "/bar" , null , "" , "baz" );
192
+ }
193
+
194
+ public void assertComposeEquals (String reason , KeyValueAccess kva , URI uri , String absoluteExpectedPath , String ... components ) {
195
+ String actual = kva .compose (uri , components );
196
+ String expected = kva .compose (uri .resolve (absoluteExpectedPath ));
197
+ assertEquals (reason , expected , actual );
198
+ }
199
+
200
+ public URI setUriPath (final URI uri , final String path ) {
127
201
128
- final URI root = tempUri ().resolve ("/" );
129
- testComposeAtLocation (root );
202
+ final URI tempUri = uri .resolve ("/" );
203
+ final String newUri = tempUri .toString ().replaceAll (tempUri .getPath () + "$" , path );
204
+ final URI uriWithNewPath = N5URI .getAsUri (newUri );
205
+ assertEquals ("setUriPath failed" , path , uriWithNewPath .getPath ());
206
+ return uriWithNewPath ;
130
207
}
131
208
}
0 commit comments