【问题标题】:Why doesn't the java.net.URI constructor throw a URISyntaxException for alphabetic strings?为什么 java.net.URI 构造函数不为字母字符串抛出 URISyntaxException?
【发布时间】:2016-01-08 20:51:51
【问题描述】:

为什么当我传入像“abc”这样的字符串时,java.net.URI 构造函数不会抛出异常?我已将其隔离到下面的测试用例中。

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");
    }
}

编译和运行时,会产生以下结果:

$ 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

根据javadocs for URI,构造函数java.net.URI 应该抛出URISyntaxException“如果给定的字符串违反了RFC 2396,如上述偏差所增强”,包括一些小的偏差。并且 URI 构造函数确实捕获了其他格式错误的 URI。但由于某种原因,这似乎没问题。 “abc”是否有可能是一个有效的 URI? RFC 2396 - Uniform Resource Identifier (URI): Generic Syntax 没有明确说明是否有。

最终,这是一个问题,因为我想在尝试发出 HTTP 请求之前验证用户提供的字符串是 URI。

【问题讨论】:

    标签: java uri


    【解决方案1】:

    嗯,"abc" 是一个完全有效的 URI 字符串。假设我想引用当前目录中名为abc 的文件。这就是它的方法!

    我猜你真的想使用 URL,在这种情况下会抛出以下异常:

    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 与 URL 不同,但我不打算解释它已经有答案了:What is the difference between a URI, a URL and a URN?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      • 2020-07-27
      • 1970-01-01
      相关资源
      最近更新 更多