java - Cannot find protocol 'resource' in URL constructor -
i've implemented class read rss 2.0 , atom 1.0 feeds. want write unit tests in order verify functionality. here feed reader section of code:
private string readfeed(final string url) throws ioexception { final stringbuilder builder = new stringbuilder(); final url feedurl = new url(url); final bufferedreader in = new bufferedreader( new inputstreamreader(feedurl.openstream())); string input; while ((input = in.readline()) != null) { builder.append(input); } in.close(); return builder.tostring(); } after research, figured best way test have sample feed xml file in project resources directory.
i've created example file "resources/rss2-0.xml"
i'm sending in following value readfeed function, "resource:///rss2-0.xml", , keep receiving java.net.malformedurlexception: unknown protocol: resource
this first time using url pathway load resource. can tell, resource seems should valid protocol. have ideas may doing wrong or other ways go this?
if want deal path using local file system, path class best suited task.
an object may used locate file in file system. typically represent system dependent file path.
you can use :
path path = filesystems.getdefault().getpath("/resources/rss2-0.xml"); bufferedreader reader = files.newbufferedreader(path, standardcharsets.utf_8); if want deal url, protocol you're looking "file". file:///rss2-0.xml instead of resource:///rss2-0.xml , file:/resources/rss2-0.xml exact.
note in case, indeed have deal urls sooner or later, when working on local tests, using path class save troubles. if want alternative, try uri class. since uri identifier (see difference between uri , url) can identify either url or path may serve bridge between production code deal urls , test code path class best put in use.
for example :
public interface feedreader { string readfeed(final uri uri); } and 2 implementations, 1 testing locally :
public class localfeedreader implements feedreader { @override public string readfeed(final uri uri) { // uri -> path // dealing path target local rss2-0.xml file } } and 1 production code :
public class webfeedreader implements feedreader { @override public string readfeed(final uri uri) { // uri -> url // dealing url target real resources } }
Comments
Post a Comment