Skip to content

Commit

Permalink
fix/test: better tests of type scheme uri validation
Browse files Browse the repository at this point in the history
* remove type before validation and add back after validation
  • Loading branch information
bogovicj committed Apr 30, 2024
1 parent 61d2751 commit d9c250e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import ij.IJ;
import ij.ImageJ;
import ij.gui.ProgressBar;
import net.imglib2.util.Pair;
import se.sawano.java.text.AlphanumericComparator;

import org.janelia.saalfeldlab.n5.CachedGsonKeyValueN5Reader;
Expand All @@ -36,6 +37,8 @@
import org.janelia.saalfeldlab.n5.DataType;
import org.janelia.saalfeldlab.n5.N5Exception;
import org.janelia.saalfeldlab.n5.universe.N5DatasetDiscoverer;
import org.janelia.saalfeldlab.n5.universe.N5Factory;
import org.janelia.saalfeldlab.n5.universe.N5Factory.StorageFormat;
import org.janelia.saalfeldlab.n5.N5Reader;
import org.janelia.saalfeldlab.n5.N5URI;
import org.janelia.saalfeldlab.n5.universe.N5TreeNode;
Expand Down Expand Up @@ -74,7 +77,7 @@
import java.awt.Insets;
import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.text.Collator;
import java.text.ParseException;
Expand Down Expand Up @@ -943,16 +946,31 @@ protected static class UriValidator extends AbstractFormatter {
private static final long serialVersionUID = 6765664180035018335L;

@Override
public Object stringToValue(String input) throws ParseException {
public Object stringToValue(String inputArg) throws ParseException {

if (input == null || input.isEmpty())
if (inputArg == null || inputArg.isEmpty())
return null;

URI returnVal;
String fmt = null;
String input = inputArg;
try {

final Pair<StorageFormat, URI> fmtAndUri = N5Factory.StorageFormat.parseUri(input);

final StorageFormat format = fmtAndUri.getA();
if (format != null)
fmt = format.toString().toLowerCase() + ":";

input = fmtAndUri.getB().toString();

} catch (URISyntaxException e) {}

N5URI n5uri = null;
try {
URI uri = new URI(input.trim());
if (uri.isAbsolute())
return uri.normalize();
return addTypeScheme(fmt, uri.normalize());
else {
n5uri = new N5URI(uri);
}
Expand All @@ -966,15 +984,24 @@ public Object stringToValue(String input) throws ParseException {
uri.getQuery() == null ? null : n5uri.getGroupPath(),
uri.getFragment() == null ? null : n5uri.getAttributePath());

return pathOnly.resolve(queryFragmentOnly).getURI().normalize();
return addTypeScheme(fmt, pathOnly.resolve(queryFragmentOnly).getURI().normalize());
} else
return Paths.get(input.trim()).normalize().toUri();
return addTypeScheme(fmt, Paths.get(input.trim()).normalize().toUri());

} catch (Throwable ignore) {}

throw new ParseException("input " + input + " not a valid URI", 0);
}

private URI addTypeScheme(String typeScheme, URI uri) {

if (typeScheme == null)
return uri;
else
return URI.create(typeScheme + uri.toString());

}

@Override
public String valueToString(Object arg) throws ParseException {

Expand Down
28 changes: 21 additions & 7 deletions src/test/java/org/janelia/saalfeldlab/n5/ui/TestUriValidation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.net.URI;
import java.nio.file.Path;
Expand Down Expand Up @@ -74,23 +75,36 @@ public void testUriValidator() throws ParseException {
assertIsUriCreate("https gcs path, query", "https://storage.googleapis.com/a/b/c?d/e", urival);
assertIsUriCreate("https gcs path, query, frament", "https://storage.googleapis.com/a/b/c?d/e#f/g", urival);

assertIsUriCreate("zarr: path", "zarr:/a/b/c", urival);
assertIsUriCreate("zarr: path, query", "zarr:/a/b/c?d/e", urival);
assertIsUriCreate("zarr: path, query fragment", "zarr:/a/b/c?d/e#f/g", urival);

assertIsUriCreate("zarr:file: path", "zarr:file:///a/b/c", urival);
assertIsUriCreate("zarr:file: path, query", "zarr:file:///a/b/c?d/e", urival);
assertIsUriCreate("zarr:file: path, query fragment", "zarr:file:///a/b/c?d/e#f/g", urival);
assertIsPathGetType("zarr: path", "zarr:", "/a/b/c", urival);
assertIsUriCreateType("zarr:file: path", "zarr:", "file:///a/b/c", urival);
assertIsUriCreateType("zarr:file: path, query", "zarr:", "file:///a/b/c?d/e", urival);
assertIsUriCreateType("zarr:file: path, query fragment", "zarr:", "file:///a/b/c?d/e#f/g", urival);
}

private void assertIsUriCreate(String message, String s, UriValidator urival) throws ParseException {

assertEquals(message, URI.create(s).normalize(), urival.stringToValue(s));
}

private void assertIsUriCreateType(String message, String typeScheme, String s, UriValidator urival) throws ParseException {

final URI val = (URI)urival.stringToValue(typeScheme + s);
assertTrue(message + " starts with typescheme", val.toString().startsWith(typeScheme));
final URI uriNoType = URI.create(val.toString().replaceFirst("^" + typeScheme, ""));
assertEquals(message, URI.create(s).normalize(), uriNoType);
}

private void assertIsPathGet(String message, String s, UriValidator urival) throws ParseException {

assertEquals(message, Paths.get(s).toUri().normalize(), urival.stringToValue(s));
}

private void assertIsPathGetType(String message, String typeScheme, String s, UriValidator urival) throws ParseException {

final URI val = (URI)urival.stringToValue(typeScheme + s);
assertTrue(message + " starts with typescheme", val.toString().startsWith(typeScheme));
final URI uriNoType = URI.create(val.toString().replaceFirst("^" + typeScheme, ""));
assertEquals(message, Paths.get(s).toUri().normalize(), uriNoType);
}

}

0 comments on commit d9c250e

Please sign in to comment.