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