Why java.net.URI isAbsolute() method return true for an URI that have fragment?

Viewed 69

My goal is to follow the term of absolute-URI = scheme ":" hier-part [ "?" query ] from Appendix A. Collected ABNF for URI from [RFC3986]

My understanding that is absolute-uri can't have fragment. I tested it with java.net.URI. I checked that isAbsolute() method only check for the scheme != null instead of checking the entire String.

My expected result was that the URI's isAbsolute() method return:

  1. false for Uri http://wwww.example.com/index.html#Related because it has fragment.
  2. true for Uri http://www.example.com/index.html.

My actual result was that the URI's isAbsolute() method return true.

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

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

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class RegExTest {

  @Test
  void validAbsoluteUri() throws URISyntaxException {
    URI uri1 = new URI("http://www.example.com/index.html#Related");
    Assertions.assertEquals(false, uri1.isAbsolute());
  }
}
1 Answers

The BNF in appendix A does not tell the entire story. If we read the entire specification, in particular section 4.3, we see:

Some protocol elements allow only the absolute form of a URI without a fragment identifier. For example, defining a base URI for later use by relative references calls for an absolute-URI syntax rule that does not allow a fragment.

absolute-URI = scheme ":" hier-part [ "?" query ]

(Emphasis mine.)

Some protocol elements use that form of absolute URI. http and https are not among those protocols, except when used as a base URI.

Notice that the general syntax allows absolute URIs with fragments.

Related