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

Strict verification of resource deletion in TemporaryFolder rule #1044

Closed
wants to merge 2 commits into from

Conversation

npathai
Copy link
Contributor

@npathai npathai commented Dec 10, 2014

PR that adds support of strict verification of deleted resources when using TemporaryFolder rule #1001

@@ -65,7 +65,7 @@ protected void before() throws Throwable {
/**
* Override to tear down your specific external resource.
*/
protected void after() {
protected void after() throws Throwable {
Copy link
Member

Choose a reason for hiding this comment

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

We can't change the signature of published methods

@kcooney kcooney self-assigned this Dec 10, 2014
@npathai npathai force-pushed the Issue1001 branch 2 times, most recently from fc4be6a to 982bb48 Compare December 15, 2014 06:05
@npathai
Copy link
Contributor Author

npathai commented Dec 15, 2014

@kcooney I have updated the code please review.
Some points:

  1. I have named the parameter assuredDeletion. Looked promising to me.
  2. I have kept the Builder because firstly the option is optional. If we don't go with the builder then will have to create overloads of constructors which accept parentFolder with parameter and other that accepted just this parameter. That looked messy. IMHO Builder is better.
  3. Have not checked whether the parameter is set after apply is called, because ExpectedException does not check for it. And I think that would complicate the code. I am unable to understand the cons if we allow the flag to be set after apply.
  4. Help me in deciding which exception to throw when cleanup fails? I have used IllegalStateException which I know is incorrect. But we need some RuntimeException? Please suggest. We may create a custom exception, but I don't know if that's a good idea.

@kcooney
Copy link
Member

kcooney commented Dec 15, 2014

@marcphilipp you were the one who suggested we shouldn't add a builder. Could you respond to that part?

private boolean assureDeletion;

/**
*
Copy link
Member

Choose a reason for hiding this comment

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

Could you add something useful here? Note you don't need to add Javadoc for params if they are abious based on the method Javadoc. Somewhere we should document want the parent folder is used for

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kcooney Done.

@npathai npathai force-pushed the Issue1001 branch 3 times, most recently from 68864cb to 54dc9ca Compare December 16, 2014 08:15
@npathai
Copy link
Contributor Author

npathai commented Dec 16, 2014

@kcooney Incorporated all the suggested changes. Only the mutability part is remaining to be discussed and finalized. Will take care of style guide in future.
Mental note: Follow google style guide

recursiveDelete(folder);
if (!tryDelete()) {
if (isDeletionAssured()) {
throw new IllegalStateException("Unable to clean up temporary folder " + folder);
Copy link
Member

Choose a reason for hiding this comment

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

IOException?

Edit: Not a good idea. We need an unchecked exception.

Copy link
Member

Choose a reason for hiding this comment

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

The only other things that comes to mind is to use an AssertionError or even assertTrue("Unable to clean up temporary folder " + folder, isDeletionAssured()). What do you think?

On the other hand, I could live with IllegalStateException, too.

Copy link
Member

Choose a reason for hiding this comment

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

IllegalStateException doesn't sound right to me because it "signals that a method has been invoked at an illegal or inappropriate time"

I think we should either throw AssertionError or RuntimeException. In either case, we could introduce a custom exception in the future.

Let's go with AssertionError. I have a sight preference for using fail() vs. assertTrue() here

@marcphilipp
Copy link
Member

Builder

I am not sure how many people use the TemporaryFolder(File) constructor. If you don't, it's a bit wordy to use now:

@Rule public TemporaryFolder temporaryFolder = TemporaryFolder.builder().assureDeletion().build();

compared to a static factory method

@Rule public TemporaryFolder temporaryFolder = TemporaryFolder.assureDeletion();

We could do both but that might be confusing, too. I'd say let's keep the builder and drop the static factory method.

Mutability

Sorry for the confusion. @kcooney has convinced me in the discussion in #1001 that rules should rather not allow to have their behavior changed after they are constructed (ExpectedException being a notable exception to this rule). Thus, please make assureDeletion final and remove setter (and getter).

@npathai
Copy link
Contributor Author

npathai commented Dec 16, 2014

@marcphilipp I have made assureDeletion final and removed getter/setter. For now I have left IllegalStateException as it is. Let's see what @kcooney has to say on this.

Behavior

  1. TemporaryFolder.builder().assureDeletion().build() will be the only way to opt for verification and there would be no constructor
  2. assureDeletion will be applicable for all test cases and cannot be opted for a single test case

@@ -29,15 +29,86 @@
public class TemporaryFolder extends ExternalResource {
private final File parentFolder;
private File folder;

private final boolean assureDeletion;
Copy link
Member

Choose a reason for hiding this comment

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

style-nit: let's but final fields before mutable fields. Thanks

@npathai
Copy link
Contributor Author

npathai commented Dec 17, 2014

@kcooney I have used fail() to report failure.
Some questions:

  1. Should we deprecate the constructor with parentFolder in favor of Builder?
  2. Should I put @since annotation? But in which milestone are we going to ship this feature?

@kcooney
Copy link
Member

kcooney commented Dec 19, 2014

The @since Javadoc tags for the new classes and methods should all be @since 4.13. If we decide not to have a 4.13 and jump to 5.0, we'll go back and update them. Thanks.

/**
* Builds an instance of {@link TemporaryFolder}.
*/
public static class Builder {
Copy link
Member

Choose a reason for hiding this comment

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

For the Timeout rule, we gave the Builder a protected constructor. I think the reason was because most users should use the builder() static method, but if someone wants to subclass Timeout they should be allowed to subclass the Builder.

Perhaps we should make this Builder have a protected constructor, too.

@npathai
Copy link
Contributor Author

npathai commented Dec 19, 2014

@kcooney All done :)

@marcphilipp
Copy link
Member

LGTM!

One last thing: Please add Javadoc to TemporaryFolder that mentions the new feature (assureDeletion) and shows how to use the builder.

public void testStrictVerificationFailure() {
PrintableResult result = testResult(HasTempFolderWithAssuredDeletion.class);
assertThat(result, failureCountIs(1));
assertThat(result.toString(), CoreMatchers.containsString("Unable to clean up temporary folder"));
Copy link
Member

Choose a reason for hiding this comment

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

Please add a static import for containsString.

@npathai
Copy link
Contributor Author

npathai commented Dec 22, 2014

@marcphilipp Done. Should I also add this feature to 4.13 release notes? Also we need to update the Rules wiki which still says that deletion is guaranteed.

@marcphilipp
Copy link
Member

@NarendraPathai Thanks! Yes, we should update the release notes in the wiki after the merge and update the Rules wiki page (good catch!).

@kcooney Can you please take a look at the Javadoc as a native speaker?

@kcooney
Copy link
Member

kcooney commented Dec 23, 2014

I'm on vacation, but I'll take a look at the Javadoc and merge this within a week.

Thanks for all of the work on this!

@npathai
Copy link
Contributor Author

npathai commented Dec 26, 2014

@kcooney @marcphilipp Thanks to you too for supporting.

@kcooney kcooney closed this in de92695 Jan 4, 2015
@kcooney
Copy link
Member

kcooney commented Jan 4, 2015

Did some very minor style cleanup, and some cleanup of the tests, and merged.

@NarendraPathai could you update the release notes? Thanks!

@npathai
Copy link
Contributor Author

npathai commented Jan 5, 2015

@kcooney Yes I will do it on Tuesday. Just a reminder that wiki section needs to be updated too.

@npathai
Copy link
Contributor Author

npathai commented Jan 8, 2015

@kcooney I have updated the release notes. Should I also update the wiki?

@kcooney
Copy link
Member

kcooney commented Jan 8, 2015

@NarendraPathai yes please

@marcphilipp
Copy link
Member

@NarendraPathai Please mention on the wiki page that the new feature will be available from 4.13 on.

@npathai
Copy link
Contributor Author

npathai commented Jan 12, 2015

@marcphilipp Yes for sure. I will add it today.

@npathai
Copy link
Contributor Author

npathai commented Jan 12, 2015

@kcooney @marcphilipp Done.

@marcphilipp
Copy link
Member

Thanks!

@stefanbirkner stefanbirkner added this to the 4.13 milestone Apr 13, 2015
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.

4 participants