1 /* 2 * Copyright (c) 2023 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.networknt.schema; 17 18 import static org.junit.jupiter.api.Assertions.*; 19 20 import org.junit.jupiter.api.Test; 21 22 class AbsoluteIriTest { 23 24 @Test absolute()25 void absolute() { 26 AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json"); 27 assertEquals("classpath:resource", iri.resolve("classpath:resource").toString()); 28 } 29 30 @Test resolveNull()31 void resolveNull() { 32 AbsoluteIri iri = new AbsoluteIri(null); 33 assertEquals("test.json", iri.resolve("test.json").toString()); 34 } 35 36 @Test relativeAtDocument()37 void relativeAtDocument() { 38 AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json"); 39 assertEquals("http://www.example.org/foo/test.json", iri.resolve("test.json").toString()); 40 } 41 42 @Test relativeAtDirectory()43 void relativeAtDirectory() { 44 AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/"); 45 assertEquals("http://www.example.org/foo/test.json", iri.resolve("test.json").toString()); 46 } 47 48 @Test relativeAtRoot()49 void relativeAtRoot() { 50 AbsoluteIri iri = new AbsoluteIri("http://www.example.org"); 51 assertEquals("http://www.example.org/test.json", iri.resolve("test.json").toString()); 52 } 53 54 @Test relativeAtRootWithTrailingSlash()55 void relativeAtRootWithTrailingSlash() { 56 AbsoluteIri iri = new AbsoluteIri("http://www.example.org/"); 57 assertEquals("http://www.example.org/test.json", iri.resolve("test.json").toString()); 58 } 59 60 @Test relativeAtRootWithSchemeSpecificPart()61 void relativeAtRootWithSchemeSpecificPart() { 62 AbsoluteIri iri = new AbsoluteIri("classpath:resource"); 63 assertEquals("classpath:resource/test.json", iri.resolve("test.json").toString()); 64 } 65 66 @Test relativeParentWithSchemeSpecificPart()67 void relativeParentWithSchemeSpecificPart() { 68 AbsoluteIri iri = new AbsoluteIri("classpath:resource/hello/world/testing.json"); 69 assertEquals("classpath:resource/test.json", iri.resolve("../../test.json").toString()); 70 } 71 72 @Test rootAbsoluteAtDocument()73 void rootAbsoluteAtDocument() { 74 AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json"); 75 assertEquals("http://www.example.org/test.json", iri.resolve("/test.json").toString()); 76 } 77 78 @Test rootAbsoluteAtDirectory()79 void rootAbsoluteAtDirectory() { 80 AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/"); 81 assertEquals("http://www.example.org/test.json", iri.resolve("/test.json").toString()); 82 } 83 84 @Test rootAbsoluteAtRoot()85 void rootAbsoluteAtRoot() { 86 AbsoluteIri iri = new AbsoluteIri("http://www.example.org"); 87 assertEquals("http://www.example.org/test.json", iri.resolve("/test.json").toString()); 88 } 89 90 @Test rootAbsoluteAtRootWithTrailingSlash()91 void rootAbsoluteAtRootWithTrailingSlash() { 92 AbsoluteIri iri = new AbsoluteIri("http://www.example.org/"); 93 assertEquals("http://www.example.org/test.json", iri.resolve("/test.json").toString()); 94 } 95 96 @Test rootAbsoluteAtRootSchemeSpecificPart()97 void rootAbsoluteAtRootSchemeSpecificPart() { 98 AbsoluteIri iri = new AbsoluteIri("classpath:resource"); 99 assertEquals("classpath:resource/test.json", iri.resolve("/test.json").toString()); 100 } 101 102 @Test schemeClasspath()103 void schemeClasspath() { 104 assertEquals("classpath", AbsoluteIri.of("classpath:resource/test.json").getScheme()); 105 } 106 107 @Test schemeHttps()108 void schemeHttps() { 109 assertEquals("https", AbsoluteIri.of("https://www.example.org").getScheme()); 110 } 111 112 @Test schemeNone()113 void schemeNone() { 114 assertEquals("", AbsoluteIri.of("relative").getScheme()); 115 } 116 117 @Test schemeUrn()118 void schemeUrn() { 119 assertEquals("urn", AbsoluteIri.of("urn:isbn:1234567890").getScheme()); 120 } 121 122 } 123