Expose URLYinTextSource 60/109660/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 7 Jan 2024 14:49:59 +0000 (15:49 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 7 Jan 2024 15:22:23 +0000 (16:22 +0100)
Eliminate YinTextSource static methods in favor of exposing
URLYinTextSource.

JIRA: YANGTOOLS-1561
Change-Id: I444478a417988f4532310f52d1e73bed236932e8
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/URLYinTextSource.java [moved from model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/ResourceYinTextSource.java with 61% similarity]
model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YinTextSource.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/yin/YinFileStmtTest.java

similarity index 61%
rename from model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/ResourceYinTextSource.java
rename to model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/URLYinTextSource.java
index 3c9b1b1dcefc03f01095fe2dc2a7ed3b3736b667..20aa0cf765c0cb83ce5da1794ca546974b44b681 100644 (file)
@@ -14,37 +14,47 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.opendaylight.yangtools.concepts.Delegator;
 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
 
 /**
- * A resource-backed {@link YinTextSource}.
+ * A {@link YinTextSource}.backed by a {@link URL}.
  */
-final class ResourceYinTextSource extends YinTextSource implements Delegator<URL> {
-    private final @NonNull URL url;
+@NonNullByDefault
+public class URLYinTextSource extends YinTextSource implements Delegator<URL> {
+    private final URL url;
 
-    ResourceYinTextSource(final SourceIdentifier sourceId, final URL url) {
+    public URLYinTextSource(final SourceIdentifier sourceId, final URL url) {
         super(sourceId);
         this.url = requireNonNull(url);
     }
 
-    @Override
-    public URL getDelegate() {
-        return url;
+    public URLYinTextSource(final URL url) {
+        this(SourceIdentifier.ofYinFileName(extractFileName(url.getPath())), url);
     }
 
     @Override
-    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
-        return super.addToStringAttributes(toStringHelper).add("url", url);
+    public final URL getDelegate() {
+        return url;
     }
 
     @Override
-    public InputStream openStream() throws IOException {
+    public final InputStream openStream() throws IOException {
         return url.openStream();
     }
 
     @Override
-    public String symbolicName() {
+    public final @NonNull String symbolicName() {
         return url.toString();
     }
+
+    @Override
+    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+        return super.addToStringAttributes(toStringHelper).add("url", url);
+    }
+
+    private static String extractFileName(final String path) {
+        return path.substring(path.lastIndexOf('/') + 1);
+    }
 }
index 24f6a248c529ebe4909446a20304e407907c2793..4e36cde7e51f24e46ad3fd446351b8f510d159e2 100644 (file)
@@ -12,7 +12,6 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.io.ByteSource;
-import com.google.common.io.Resources;
 import java.io.InputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -65,10 +64,4 @@ public abstract class YinTextSource extends ByteSource implements YinSourceRepre
         }
         throw new IllegalArgumentException("Supplied path " + path + " is not a regular file");
     }
-
-    public static @NonNull YinTextSource forResource(final Class<?> clazz, final String resourceName) {
-        final String fileName = resourceName.substring(resourceName.lastIndexOf('/') + 1);
-        return new ResourceYinTextSource(SourceIdentifier.ofYinFileName(fileName),
-            Resources.getResource(clazz, resourceName));
-    }
 }
index 644c2ed455f61f6e960a88689d42ff80aa815b3c..f8c24cd4df17d13c7bc29c4dd8679680e74b8472 100644 (file)
@@ -7,15 +7,14 @@
  */
 package org.opendaylight.yangtools.yang.stmt.yin;
 
-import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.assertThat;
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.IOException;
 import org.junit.jupiter.api.Test;
-import org.opendaylight.yangtools.yang.model.spi.source.YinTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.URLYinTextSource;
 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
@@ -26,7 +25,6 @@ import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
 import org.xml.sax.SAXException;
 
 class YinFileStmtTest {
-
     private static final StatementStreamSource YIN_FILE = createSource("test.yin");
     private static final StatementStreamSource EXT_FILE = createSource("extension.yin");
     private static final StatementStreamSource EXT_USE_FILE = createSource("extension-use.yin");
@@ -36,7 +34,7 @@ class YinFileStmtTest {
     private static StatementStreamSource createSource(final String name) {
         try {
             return YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
-                YinTextSource.forResource(YinFileStmtTest.class, "/semantic-statement-parser/yin/" + name)));
+                new URLYinTextSource(YinFileStmtTest.class.getResource("/semantic-statement-parser/yin/" + name))));
         } catch (SAXException | IOException e) {
             throw new IllegalArgumentException(e);
         }
@@ -62,6 +60,6 @@ class YinFileStmtTest {
         var reactor = RFC7950Reactors.defaultReactor().newBuild().addSource(INVALID_YIN_FILE_2);
         final var cause = assertThrows(SomeModifiersUnresolvedException.class, reactor::buildEffective).getCause();
         assertInstanceOf(SourceException.class, cause);
-        assertThat(cause.getMessage(), startsWith("Key argument 'testing-string testing-string' contains duplicates"));
+        assertThat(cause.getMessage()).startsWith("Key argument 'testing-string testing-string' contains duplicates");
     }
 }
\ No newline at end of file