0

Why doesn't the java.net.URI constructor throw an exception when I pass in a string like "abc"? I've isolated it to the test case below.

import static org.junit.Assert.assertEquals;

import java.net.URI;
import java.net.URISyntaxException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;


@RunWith(JUnit4.class)
public class HelloWorldTest {
    @Test(expected=URISyntaxException.class)
    public void testAlphabeticStringWithUriConstructor()
        throws URISyntaxException {
        URI uri = new URI("abc");
    }
}

Which, when compiled and run, produces the following:

$ java -cp .:lib/junit-4.12.jar:lib/hamcrest-core-1.3.jar org.junit.runner.JUnitCore HelloWorldTest
JUnit version 4.12
.E
Time: 0.005
There was 1 failure:
1) testAlphabeticStringWithUriConstructor(HelloWorldTest)
java.lang.AssertionError: Expected exception: java.net.URISyntaxException
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:32)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:36)

FAILURES!!!
Tests run: 1,  Failures: 1

According to the javadocs for URI, the constructor java.net.URI should throw a URISyntaxException "if the given string violates RFC 2396, as augmented by the above deviations" with some minor deviations included. And the URI constructor does catch other malformed URIs. But for some reason it seems to be okay with this one. Is it possible that "abc" is a valid URI? RFC 2396 - Uniform Resource Identifier (URI): Generic Syntax does not clearly indicate it if so.

Ultimately, this is a problem because I would like to validate that a user-supplied string is a URI before attempting to make an HTTP request.

Ceasar Bautista
  • 17,413
  • 13
  • 55
  • 74

1 Answers1

2

Well, "abc" is a perfectly valid URI string. Let's say I want to refer to a file called abc in the current directory. This is the way to do it !

I guess you really wanted to use URL, which will in this case throw the following exception :

Exception in thread "main" java.net.MalformedURLException: no protocol: abc
    at java.net.URL.<init>(URL.java:586)
    at java.net.URL.<init>(URL.java:483)
    at java.net.URL.<init>(URL.java:432)
    at offlineAnalysis.Main.main(Main.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

URI is different from URL, but I'm not going to explain it there are answers for this already : What is the difference between a URI, a URL and a URN?

Community
  • 1
  • 1
Dici
  • 22,025
  • 5
  • 34
  • 74