Skip to content

Conversation

@kennethmcfarland
Copy link
Contributor

for factory methods and constructors.

@keith-turner keith-turner self-assigned this Mar 12, 2019
@SuppressWarnings("unused")
public class PageLoaderTest {

@Test
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be @Test(expected = NullPointerException.class)?

Copy link
Contributor

Choose a reason for hiding this comment

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

@kpm1985 do you know if this is still an issue?

public void testDeleteFailsWithNullUrl() {
try {
PageLoader loader = PageLoader.deletePage(null);
} catch (MalformedURLException e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this catch be dropped?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

If the exception is not expected you can make the test throw exception. If it is actually thrown then the test will fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if this is correct but did a simple refactor where it catches Exception e and test if its an instance of a NPE and if yes throws NPE.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking of the following.

@Test(expected = NullPointerException.class)
public void testDeletePageThrowsNPEifNullURL() throws MalformedURLException {	
  PageLoader.deletePage((URL)null);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I gotcha now. I fixed it to throws instead of catch, and did the same for the other methods. I added throws to the last test for clarity even though it catches all exceptions, as a hint to the reader.

public void testDeletePage() {
URL url = new URL("","","",0,false,false);
try {
PageLoader loader = PageLoader.deletePage(url);
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the intent of this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Try to see if deletePage() works with an empty URL. Will refactor with a better method name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The method has been given a vastly better descriptive name that does not require guesswork.

}
}

@Test
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be @Test(expected = NullPointerException.class)? Also if that change is made seem like all of the catch blocks could be dropped.

Copy link
Contributor

Choose a reason for hiding this comment

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

@kpm1985 do you know if this is still an issue?

import webindex.core.models.Page;
import webindex.core.models.URL;

@SuppressWarnings("unused")
Copy link
Member

Choose a reason for hiding this comment

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

There's no reason to suppress this warning, since this is easily avoidable. For example, replace:

Page p = null;
PageLoader loader = PageLoader.updatePage(p);

with:

PageLoader loader = PageLoader.updatePage((Page) null);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, fixed.

Added Apache License header
Renamed methods with better descriptives
Removed unused local variable warnings
@kennethmcfarland
Copy link
Contributor Author

kennethmcfarland commented Mar 15, 2019

I wrote the test because I noticed load(Tx, Ctx) doesn't check for null args. The deletePage(URL url) requires url not be null but doesn't care if its a url representing "", ie empty or blank. The updatePage(Page page) method requires the url is not "" or blank/empty but doesn't care if it gets a null page (leading to NPE).

Improve handling of unexpected exceptions and cast them to the
appropriate type
@kennethmcfarland
Copy link
Contributor Author

kennethmcfarland commented Mar 16, 2019

I created a separate issue for the refactoring of PageLoader so that it can pass these tests. It can be found in #9

Change method signature to eliminate catch blocks
loader.load(null, null);
} catch (Exception e) {
if(e instanceof NullPointerException)
throw new NullPointerException(e.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand why this is catching and then rethrowing a NPE. Why even catch it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello Keith, I did that because loader class throws Exception, but I was trying to determine when it's because of a NullPointer to load(tx,ctx) rather than just the exception load already throws, I fixed it and removed it.

@keith-turner
Copy link
Contributor

@kpm1985 the changes are looking good now. So does this test not pass as it is?

@kennethmcfarland
Copy link
Contributor Author

That is correct, I just run it in Eclipse and get a couple failing tests.

@kennethmcfarland
Copy link
Contributor Author

@keith-turner The two tests that are failing are testUpdatePageWithNullPage and testLoadWithNullTxCtx.
The first failing test comes from here https://github.com/apache/fluo-examples/blob/47f1bb42969345b444f5c0e4c8c4a36ec93aab14/webindex/modules/data/src/main/java/webindex/data/fluo/PageLoader.java#L44-L50

Where I think a simple Objects.requireNonNull(page) could do the trick.

The second comes from here: https://github.com/apache/fluo-examples/blob/47f1bb42969345b444f5c0e4c8c4a36ec93aab14/webindex/modules/data/src/main/java/webindex/data/fluo/PageLoader.java#L60-L80

And Objects.requireNonNull() could again do the trick.

@kennethmcfarland
Copy link
Contributor Author

@keith-turner @ctubbsii Anybody have some feedback? Does this look ok now?

@ctubbsii ctubbsii changed the base branch from master to main August 11, 2020 19:29
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.

3 participants