{"record":{"id":"385dbbe6e004900d","repo":"junit-team/junit5","slug":"failed-to-create-a-java-net-uri-from-uri","errorCode":null,"errorMessage":"Failed to create a java.net.URI from: ${uri}","messagePattern":"Failed to create a java\\.net\\.URI from: (.+?)","errorType":"validation","errorClass":"PreconditionViolationException","httpStatus":null,"severity":"error","filePath":"junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/DiscoverySelectors.java","lineNumber":86,"sourceCode":"\n\t/**\n\t * Create a {@code UriSelector} for the supplied URI.\n\t *\n\t * @param uri the URI to select; never {@code null} or blank\n\t * @see UriSelector\n\t * @see #selectUri(URI)\n\t * @see #selectFile(String)\n\t * @see #selectFile(File)\n\t * @see #selectDirectory(String)\n\t * @see #selectDirectory(File)\n\t */\n\tpublic static UriSelector selectUri(String uri) {\n\t\tPreconditions.notBlank(uri, \"URI must not be null or blank\");\n\t\ttry {\n\t\t\treturn new UriSelector(new URI(uri));\n\t\t}\n\t\tcatch (URISyntaxException ex) {\n\t\t\tthrow new PreconditionViolationException(\"Failed to create a java.net.URI from: \" + uri, ex);\n\t\t}\n\t}\n\n\t/**\n\t * Create a {@code UriSelector} for the supplied {@link URI}.\n\t *\n\t * @param uri the URI to select; never {@code null}\n\t * @see UriSelector\n\t * @see #selectUri(String)\n\t * @see #selectFile(String)\n\t * @see #selectFile(File)\n\t * @see #selectDirectory(String)\n\t * @see #selectDirectory(File)\n\t */\n\tpublic static UriSelector selectUri(URI uri) {\n\t\tPreconditions.notNull(uri, \"URI must not be null\");\n\t\treturn new UriSelector(uri);\n\t}","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/junit-team/junit5/blob/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/DiscoverySelectors.java#L68-L104","documentation":"DiscoverySelectors.selectUri(String) constructs a java.net.URI from a string; if the string violates RFC 3986 (URISyntaxException), it is wrapped as PreconditionViolationException. The string must already be non-blank (checked just before). This guards selector construction before any discovery runs.","triggerScenarios":"Calling selectUri(string) with a syntactically invalid URI: missing scheme, illegal characters (spaces, unencoded brackets), a path needing percent-encoding, or a malformed authority. URISyntaxException's reason points at the offending position.","commonSituations":"Passing a file path with spaces as a URI without percent-encoding; missing scheme ('host/path' instead of 'file://...'); copy-pasted URIs with stray characters; building URIs by string concatenation without URI-building utilities.","solutions":["Use java.net.URI or URI.Builder/Path.toUri() to construct the string rather than concatenation, so encoding is handled.","Percent-encode spaces and reserved characters; ensure a scheme is present (e.g. 'file://', 'http://').","Prefer selectUri(URI) overload (passing a pre-built URI) to surface syntax errors at construction time near the source.","Inspect URISyntaxException position from the cause to locate the bad character."],"exampleFix":"// before\nUriSelector s = selectUri(\"my file path with spaces.xml\");\n// after\nUriSelector s = selectUri(new File(\"my file path with spaces.xml\").toURI().toString());\n// or\nUriSelector s = selectUri(new URI(\"file\", null, \"/my%20file%20path.xml\", null));","handlingStrategy":"validation","validationCode":"// Validate URI syntax before constructing the selector.\nString uriStr = userInput;\ntry { new java.net.URI(uriStr); } catch (java.net.URISyntaxException e) {\n  throw new IllegalArgumentException(\"Invalid URI for selectUri: \" + e.getMessage(), e);\n}\n// or use a real URI directly\n","typeGuard":null,"tryCatchPattern":"try { return selectUri(new URI(raw)); } catch (URISyntaxException e) { /* convert/escape or ask caller to fix */ }","preventionTips":["Construct URIs with java.net.URI or Path.toUri() rather than string concatenation.","Prefer the selectUri(URI) overload so syntax errors surface at construction time near the source.","Percent-encode spaces and reserved characters; always include a scheme."],"tags":["discovery","uri","selector","engine"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}