-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TREQ-21: Throw FileNotFoundException when resource is not found #2
Conversation
SourcePond
commented
Jun 8, 2017
- Check on construction time whether the URL points to a local resource
- Always throw a FileNotFoundException when the URL points to a local resource and url.openConnection throws an IOException
- Added OSGi tests
This pull-request replaces #1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @SourcePond, the PR looks awesome.
I've added some trivial comments regarding the project's code style and so on…
Otherwise I need some time to check out the tests, as you're added new testing framework/libraries…
My other question was that you were making remote url protocols pluggable, at least in how we detect which protocols are local or not…
What else is required to add a protocol to work with url.openConnecton()
?
I'm thinking that if REMOTE_PROTOCOLS can be extended by a system property, then it should be documented, including an example use-case or what else needs to be done for additional protocols.
tiles-request-api/pom.xml
Outdated
<url.version>2.5.2</url.version> | ||
<felix.version>5.6.4</felix.version> | ||
<slf4j-api.version>1.7.25</slf4j-api.version> | ||
<logback.version>1.2.2</logback.version> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trivial:
We don't specify dependency versions by maven properties in the tiles projects.
Please put these in with the dependency elements.
import java.net.*; | ||
import java.util.HashSet; | ||
import java.util.Locale; | ||
import java.util.Set; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trivial:
the import order was correct how it was. the project puts java.* imports first, and doesn't wildcard imports.
* {@link IOException} will be thrown directly. If a url has a local protocol, then any {@link IOException} | ||
* will be caught and transformed into a {@link FileNotFoundException}. | ||
*/ | ||
static final String REMOTE_PROTOCOLS_PROPERTY = "tiles.remoteProtocols"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trivial:
according to Java's declaration order, this should appear before LOG
is declared.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
String protocolsProp = getProperty(REMOTE_PROTOCOLS_PROPERTY); | ||
if (protocolsProp != null) { | ||
for (String protocol : protocolsProp.split(";")) { | ||
remoteProtocols.add(protocol.trim()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is adding a feature to the code. it will need a bit more documentation, and be a bit clearer in the issue and commit message, as currently it's titled as a bug fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perfect. thanks @SourcePond
/** the URL where the contents can be found. */ | ||
private URL url; | ||
/** if the URL matches a file, this is the file. */ | ||
private File file; | ||
/** if the URL points to a local resource */ | ||
private boolean local; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think we can make these fields final
?
I added some documentation which should also explain how an additional remote protocol can be added to the system. Additionally, I enhanced the task a bit so it should explain the problem in more detail. |
Thanks @SourcePond. Very much appreciated! Especially the response time :-) |
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.lang.reflect.Field; | ||
import java.net.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trivial: no wildcards please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, I adjusted my IDE settings and it should not happen again now ;-)
try { | ||
resource.getInputStream(); | ||
} catch (FileNotFoundException e) { | ||
// expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this is expected, then shouldn't the method be annotated @Test(expected=FileNotFoundException.class)
?
} catch (FileNotFoundException e) { | ||
fail("FileNotFoundException not allowed here"); | ||
} catch (IOException e) { | ||
assertEquals(EXPECTED_MESSAGE, e.getMessage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment here. shouldn't we also rethrow, and make it @Test(expected=IOException.class)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion, we should catch the IOException here, and, should assert that it was actually thrown by the TestUrlStreamHandler. This should make sure that the test does not accidentally pass even when an unexpected IOException occurs.
resource.getLastModified(); | ||
fail("Exception expected"); | ||
} catch (FileNotFoundException e) { | ||
// expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment about expected exceptions.
resource.getInputStream(); | ||
fail("Exception expected"); | ||
} catch (FileNotFoundException e) { | ||
// expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment about expected exceptions.
i've finished my review and apart from the comments about |
@SourcePond i'm happy to merge this. thanks for the contribution! Before I merge it, would you mind squashing the commits to one commit, writing up a summarised message (that includes a reference to this PR)? |
- Separate between local and remote URL protocols, check on construction time whether the URL points to a local resource - Always throw a FileNotFoundException when the URL points to a local resource and url.openConnection throws an IOException - Added system property 'tiles.remoteProtocols' which can be used to specify additional remote protocols - Added OSGi tests including PAX-EXAM dependencies - PR: apache#2
I squashed all changes into 1 commit, I'm glad that I could help |
Has been committed upstream. |
TREQ-21: Convert IOException to FileNotFoundException on local protocols - Separate between local and remote URL protocols, check on construction time whether the URL points to a local resource - Always throw a FileNotFoundException when the URL points to a local resource and url.openConnection throws an IOException - Added system property 'tiles.remoteProtocols' which can be used to specify additional remote protocols - Added OSGi tests including PAX-EXAM dependencies references: - pull request: #2 - ticket: https://issues.apache.org/jira/browse/TREQ-21 Contributed by Roland Hauser ( https://github.com/SourcePond ) This closes #2 git-svn-id: https://svn.apache.org/repos/asf/tiles/request/trunk@1799291 13f79535-47bb-0310-9956-ffa450edef68