package org.junit.rules;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.failureCountIs;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;

import java.io.IOException;

import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.results.PrintableResult;

public class TemporaryFolderRuleAssuredDeletionTest {

    private static class StubTemporaryFolder extends TemporaryFolder {
        private StubTemporaryFolder(Builder builder) {
            super(builder);
        }

        /**
         * Don't need to create as we are overriding deletion.
         */
        @Override
        public void create() throws IOException {
        }

        /**
         * Simulates failure to clean-up temporary folder.
         */
        @Override
        protected boolean tryDelete() {
            return false;
        }

        public static Builder builder() {
            return new TemporaryFolder.Builder() {
                @Override
                public TemporaryFolder build() {
                    return new StubTemporaryFolder(this);
               }
            };
        }
    }

    public static class HasTempFolderWithAssuredDeletion {
        @Rule public TemporaryFolder folder = StubTemporaryFolder.builder()
                .assureDeletion()
                .build();

        @Test
        public void alwaysPasses() {
        }
    }

    @Test
    public void testStrictVerificationFailure() {
        PrintableResult result = testResult(HasTempFolderWithAssuredDeletion.class);
        assertThat(result, failureCountIs(1));
        assertThat(result.toString(), containsString("Unable to clean up temporary folder"));
    }

    public static class HasTempFolderWithoutAssuredDeletion {
        @Rule public TemporaryFolder folder = StubTemporaryFolder.builder().build();

        @Test
        public void alwaysPasses() {
        }
    }

    @Test
    public void testStrictVerificationSuccess() {
        PrintableResult result = testResult(HasTempFolderWithoutAssuredDeletion.class);
        assertThat(result, isSuccessful());
    }
}

@@ -346,37 +346,8 @@
 l;%0A%0A
-import java.io.IOException;%0A%0A
 impo
@@ -504,22 +504,21 @@
 %7B%0A%0A    p
-rivate
+ublic
  static 
@@ -527,146 +527,64 @@
 ass 
-StubTemporaryFolder extends TemporaryFolder %7B%0A        private StubTemporaryFolder(Builder builder) %7B%0A            super(builder);%0A        %7D
+TestClass %7B%0A        static TemporaryFolder injectedRule;
 %0A%0A  
@@ -581,35 +581,37 @@
 dRule;%0A%0A        
-/**
+@Rule
 %0A         * Don'
@@ -607,74 +607,54 @@
     
- * Don't need to create as we are overriding deletion.%0A         */
+public TemporaryFolder folder = injectedRule;%0A
 %0A   
@@ -655,32 +655,28 @@
 ;%0A%0A        @
-Override
+Test
 %0A        pub
@@ -688,35 +688,42 @@
 oid 
-create() throws IOException
+alwaysPassesButDeletesRootFolder()
  %7B%0A 
@@ -732,152 +732,79 @@
     
- %7D%0A%0A
      
-   /**%0A         * Simulates failure to clean-up temporary folder.%0A         */%0A        @Override%0A        protected boolean tryD
+//we delete the folder in the test so that it cannot be d
 elete
-() %7B
+d by
 %0A   
@@ -816,270 +816,66 @@
     
-return false;%0A        %7D%0A%0A        public static Builder builder() %7B%0A            return new TemporaryFolder.Builder() %7B%0A                @Override%0A                public TemporaryFolder build() %7B%0A                    return new StubTemporaryFolder(this);%0A   
+//the rule%0A            folder.getRoot().delete();%0A
         
     
@@ -874,369 +874,227 @@
     
-    
 %7D%0A    
-        %7D;%0A        %7D%0A    %7D%0A%0A    public static class HasTempFolderWithAssuredDeletion %7B%0A        @Rule public TemporaryFolder folder = StubTemporaryFolder.builder()%0A                .assureDeletion()%0A                .build();%0A%0A        @Test%0A        public void alwaysPasses() %7B%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testStrictVerificationFailure() %7B
+%7D%0A%0A    @Test%0A    public void testFailsWhenCreatedFolderCannotBeDeletedButDeletionIsAssured() %7B%0A        TestClass.injectedRule = TemporaryFolder.builder()%0A                .assureDeletion()%0A                .build();
 %0A   
@@ -1138,40 +1138,17 @@
 ult(
-HasTempFolderWithAssuredDeletion
+TestClass
 .cla
@@ -1308,151 +1308,90 @@
     
-public static class HasTempFolderWithoutAssuredDeletion %7B%0A        @Rule public TemporaryFolder folder = StubTemporaryFolder.builder().build();%0A
+@Test%0A    public void byDefaultTestDoesNotFailWhenCreatedFolderCannotBeDeleted() %7B
 %0A   
@@ -1399,127 +1399,55 @@
     
-@
 Test
-%0A        public void alwaysPasses() %7B%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testStrictVerificationSuccess() %7B
+Class.injectedRule = new TemporaryFolder();
 %0A   
@@ -1491,43 +1491,17 @@
 ult(
-HasTempFolderWithoutAssuredDeletion
+TestClass
 .cla

Use standard TemporaryFolder for test