Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

SourcePond
Copy link

  • 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

@SourcePond
Copy link
Author

This pull-request replaces #1

Copy link
Member

@michaelsembwever michaelsembwever left a 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.

<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>
Copy link
Member

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;
Copy link
Member

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";
Copy link
Member

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.

Copy link
Member

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());
Copy link
Member

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.

Copy link
Member

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;
Copy link
Member

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?

@SourcePond
Copy link
Author

SourcePond commented Jun 13, 2017

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.

@michaelsembwever
Copy link
Member

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.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trivial: no wildcards please.

Copy link
Author

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
Copy link
Member

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());
Copy link
Member

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)

Copy link
Author

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
Copy link
Member

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
Copy link
Member

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.

@michaelsembwever
Copy link
Member

i've finished my review and apart from the comments about @Test(expected=…) this is 👍 from me.

@michaelsembwever
Copy link
Member

@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
@SourcePond
Copy link
Author

SourcePond commented Jun 19, 2017

I squashed all changes into 1 commit, I'm glad that I could help

@michaelsembwever
Copy link
Member

Has been committed upstream.

asfgit pushed a commit that referenced this pull request Jun 20, 2017
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
@asfgit asfgit closed this in 586d164 Jun 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants