1818
1919import java .io .File ;
2020import java .io .IOException ;
21- import java .nio . file . Files ;
21+ import java .util . Date ;
2222
2323import org .codehaus .plexus .components .io .filemappers .FileMapper ;
24+ import org .hamcrest .BaseMatcher ;
25+ import org .hamcrest .Description ;
26+ import org .hamcrest .core .StringContains ;
2427import org .junit .After ;
2528import org .junit .Before ;
2629import org .junit .Rule ;
2730import org .junit .Test ;
2831import org .junit .rules .ExpectedException ;
32+ import org .junit .rules .TemporaryFolder ;
33+
34+ import static org .hamcrest .CoreMatchers .hasItem ;
35+ import static org .hamcrest .CoreMatchers .is ;
36+ import static org .junit .Assert .assertThat ;
2937
3038/**
3139 * Unit test for {@link AbstractUnArchiver}
@@ -37,7 +45,11 @@ public class AbstractUnArchiverTest
3745 @ Rule
3846 public ExpectedException thrown = ExpectedException .none ();
3947
48+ @ Rule
49+ public TemporaryFolder temporaryFolder = new TemporaryFolder ();
50+
4051 private AbstractUnArchiver abstractUnArchiver ;
52+ private CapturingLog log = new CapturingLog ( 0 /*debug*/ , "AbstractUnArchiver" );
4153
4254 @ Before
4355 public void setUp ()
@@ -58,6 +70,7 @@ protected void execute()
5870 // unused
5971 }
6072 };
73+ this .abstractUnArchiver .enableLogging ( log );
6174 }
6275
6376 @ After
@@ -72,7 +85,7 @@ public void shouldThrowExceptionBecauseRewrittenPathIsOutOfDirectory()
7285 {
7386 // given
7487 this .thrown .expectMessage ( "Entry is outside of the target directory (../PREFIX/ENTRYNAME.SUFFIX)" );
75- final File targetFolder = Files . createTempDirectory ( null ). toFile ();
88+ final File targetFolder = temporaryFolder . newFolder ();
7689 final FileMapper [] fileMappers = new FileMapper [] { new FileMapper ()
7790 {
7891 @ Override
@@ -97,4 +110,105 @@ public String getMappedFileName( String pName )
97110 // ArchiverException is thrown providing the rewritten path
98111 }
99112
113+ @ Test
114+ public void shouldExtractWhenFileOnDiskDoesNotExist () throws IOException
115+ {
116+ // given
117+ File file = new File ( temporaryFolder .getRoot (), "whatever.txt" ); // does not create the file!
118+ String entryname = file .getName ();
119+ Date entryDate = new Date ();
120+
121+ // when & then
122+ assertThat ( this .abstractUnArchiver .shouldExtractEntry ( temporaryFolder .getRoot (), file , entryname , entryDate ), is ( true ) );
123+ }
124+
125+ @ Test
126+ public void shouldNotExtractWhenFileOnDiskIsNewerThanEntryInArchive () throws IOException
127+ {
128+ // given
129+ File file = temporaryFolder .newFile ();
130+ file .setLastModified ( System .currentTimeMillis () );
131+ String entryname = file .getName ();
132+ Date entryDate = new Date ( 0 );
133+
134+ // when & then
135+ assertThat ( this .abstractUnArchiver .shouldExtractEntry ( temporaryFolder .getRoot (), file , entryname , entryDate ), is ( false ) );
136+ }
137+
138+ @ Test
139+ public void shouldNotExtractWhenFileOnDiskIsNewerThanEntryInArchive_andWarnAboutDifferentCasing () throws IOException
140+ {
141+ // given
142+ File file = temporaryFolder .newFile ();
143+ file .setLastModified ( System .currentTimeMillis () );
144+ String entryname = file .getName ().toUpperCase ();
145+ Date entryDate = new Date ( 0 );
146+
147+ // when & then
148+ assertThat ( this .abstractUnArchiver .shouldExtractEntry ( temporaryFolder .getRoot (), file , entryname , entryDate ), is ( false ) );
149+ assertThat ( this .log .getWarns (), hasItem ( new LogMessageMatcher ( "names differ only by case" ) ) );
150+ }
151+
152+ @ Test
153+ public void shouldExtractWhenEntryInArchiveIsNewerThanFileOnDisk () throws IOException
154+ {
155+ // given
156+ File file = temporaryFolder .newFile ();
157+ file .setLastModified ( 0 );
158+ String entryname = file .getName ().toUpperCase ();
159+ Date entryDate = new Date ( System .currentTimeMillis () );
160+
161+ // when & then
162+ this .abstractUnArchiver .setOverwrite ( true );
163+ assertThat ( this .abstractUnArchiver .shouldExtractEntry ( temporaryFolder .getRoot (), file , entryname , entryDate ), is ( true ) );
164+
165+ // when & then
166+ this .abstractUnArchiver .setOverwrite ( false );
167+ assertThat ( this .abstractUnArchiver .shouldExtractEntry ( temporaryFolder .getRoot (), file , entryname , entryDate ), is ( false ) );
168+ }
169+
170+ @ Test
171+ public void shouldExtractWhenEntryInArchiveIsNewerThanFileOnDiskAndWarnAboutDifferentCasing () throws IOException
172+ {
173+ // given
174+ File file = temporaryFolder .newFile ();
175+ file .setLastModified ( 0 );
176+ String entryname = file .getName ().toUpperCase ();
177+ Date entryDate = new Date ( System .currentTimeMillis () );
178+
179+ // when & then
180+ this .abstractUnArchiver .setOverwrite ( true );
181+ assertThat ( this .abstractUnArchiver .shouldExtractEntry ( temporaryFolder .getRoot (), file , entryname , entryDate ), is ( true ) );
182+ this .abstractUnArchiver .setOverwrite ( false );
183+ assertThat ( this .abstractUnArchiver .shouldExtractEntry ( temporaryFolder .getRoot (), file , entryname , entryDate ), is ( false ) );
184+ assertThat ( this .log .getWarns (), hasItem ( new LogMessageMatcher ( "names differ only by case" ) ) );
185+ }
186+
187+ static class LogMessageMatcher extends BaseMatcher <CapturingLog .Message > {
188+ private final StringContains delegateMatcher ;
189+
190+ LogMessageMatcher ( String needle )
191+ {
192+ this .delegateMatcher = new StringContains ( needle );
193+ }
194+
195+ @ Override
196+ public void describeTo ( Description description )
197+ {
198+ description .appendText ( "a log message with " );
199+ delegateMatcher .describeTo ( description );
200+ }
201+
202+ @ Override
203+ public boolean matches ( Object item )
204+ {
205+ if ( item instanceof CapturingLog .Message )
206+ {
207+ CapturingLog .Message message = (CapturingLog .Message ) item ;
208+ String haystack = message .message ;
209+ return delegateMatcher .matches ( haystack );
210+ }
211+ return false ;
212+ }
213+ }
100214}
0 commit comments