19
19
import java .io .File ;
20
20
import java .io .IOException ;
21
21
import java .nio .file .Files ;
22
+ import java .util .Date ;
22
23
23
24
import org .codehaus .plexus .components .io .filemappers .FileMapper ;
25
+ import org .hamcrest .BaseMatcher ;
26
+ import org .hamcrest .CoreMatchers ;
27
+ import org .hamcrest .Description ;
28
+ import org .hamcrest .core .StringContains ;
24
29
import org .junit .After ;
25
30
import org .junit .Before ;
26
31
import org .junit .Rule ;
27
32
import org .junit .Test ;
28
33
import org .junit .rules .ExpectedException ;
34
+ import org .junit .rules .TemporaryFolder ;
35
+
36
+ import static org .hamcrest .CoreMatchers .hasItem ;
37
+ import static org .hamcrest .CoreMatchers .is ;
38
+ import static org .junit .Assert .assertThat ;
29
39
30
40
/**
31
41
* Unit test for {@link AbstractUnArchiver}
@@ -37,7 +47,11 @@ public class AbstractUnArchiverTest
37
47
@ Rule
38
48
public ExpectedException thrown = ExpectedException .none ();
39
49
50
+ @ Rule
51
+ public TemporaryFolder temporaryFolder = new TemporaryFolder ();
52
+
40
53
private AbstractUnArchiver abstractUnArchiver ;
54
+ private CapturingLog log = new CapturingLog ( 0 /*debug*/ , "AbstractUnArchiver" );
41
55
42
56
@ Before
43
57
public void setUp ()
@@ -58,6 +72,7 @@ protected void execute()
58
72
// unused
59
73
}
60
74
};
75
+ this .abstractUnArchiver .enableLogging ( log );
61
76
}
62
77
63
78
@ After
@@ -72,7 +87,7 @@ public void shouldThrowExceptionBecauseRewrittenPathIsOutOfDirectory()
72
87
{
73
88
// given
74
89
this .thrown .expectMessage ( "Entry is outside of the target directory (../PREFIX/ENTRYNAME.SUFFIX)" );
75
- final File targetFolder = Files . createTempDirectory ( null ). toFile ();
90
+ final File targetFolder = temporaryFolder . newFolder ();
76
91
final FileMapper [] fileMappers = new FileMapper [] { new FileMapper ()
77
92
{
78
93
@ Override
@@ -97,4 +112,93 @@ public String getMappedFileName( String pName )
97
112
// ArchiverException is thrown providing the rewritten path
98
113
}
99
114
115
+ @ Test
116
+ public void shouldNotExtractWhenFileOnDiskIsNewerThanEntryInArchive () throws IOException
117
+ {
118
+ // given
119
+ File file = temporaryFolder .newFile ( "readme.txt" );
120
+ file .setLastModified ( System .currentTimeMillis () );
121
+ String entryname = "readme.txt" ;
122
+ Date entryDate = new Date ( 0 );
123
+
124
+ // when & then
125
+ assertThat ( this .abstractUnArchiver .shouldExtractEntry ( file , entryname , entryDate ), is ( false ) );
126
+ }
127
+
128
+ @ Test
129
+ public void shouldNotExtractWhenFileOnDiskIsNewerThanEntryInArchive_andWarnAboutDifferentCasing () throws IOException
130
+ {
131
+ // given
132
+ File file = temporaryFolder .newFile ( "readme.txt" );
133
+ file .setLastModified ( System .currentTimeMillis () );
134
+ String entryname = "README.txt" ;
135
+ Date entryDate = new Date ( 0 );
136
+
137
+ // when & then
138
+ assertThat ( this .abstractUnArchiver .shouldExtractEntry ( file , entryname , entryDate ), is ( false ) );
139
+ assertThat ( this .log .getWarns (), hasItem ( new LogMessageMatcher ( "names differ only by case" ) ) );
140
+ }
141
+
142
+ @ Test
143
+ public void shouldExtractWhenEntryInArchiveIsNewerThanFileOnDisk () throws IOException
144
+ {
145
+ // given
146
+ File file = temporaryFolder .newFile ( "readme.txt" );
147
+ file .setLastModified ( 0 );
148
+ String entryname = "readme.txt" ;
149
+ Date entryDate = new Date ( System .currentTimeMillis () );
150
+
151
+ // when & then
152
+ this .abstractUnArchiver .setOverwrite ( true );
153
+ assertThat ( this .abstractUnArchiver .shouldExtractEntry ( file , entryname , entryDate ), is ( true ) );
154
+
155
+ // when & then
156
+ this .abstractUnArchiver .setOverwrite ( false );
157
+ assertThat ( this .abstractUnArchiver .shouldExtractEntry ( file , entryname , entryDate ), is ( false ) );
158
+ }
159
+
160
+ @ Test
161
+ public void shouldExtractWhenEntryInArchiveIsNewerThanFileOnDiskAndWarnAboutDifferentCasing () throws IOException
162
+ {
163
+ // given
164
+ File file = temporaryFolder .newFile ( "readme.txt" );
165
+ file .setLastModified ( 0 );
166
+ String entryname = "README.txt" ;
167
+ Date entryDate = new Date ( System .currentTimeMillis () );
168
+
169
+ // when & then
170
+ this .abstractUnArchiver .setOverwrite ( true );
171
+ assertThat ( this .abstractUnArchiver .shouldExtractEntry ( file , entryname , entryDate ), is ( true ) );
172
+ this .abstractUnArchiver .setOverwrite ( false );
173
+ assertThat ( this .abstractUnArchiver .shouldExtractEntry ( file , entryname , entryDate ), is ( false ) );
174
+ assertThat ( this .log .getWarns (), hasItem ( new LogMessageMatcher ( "names differ only by case" ) ) );
175
+ }
176
+
177
+ static class LogMessageMatcher extends BaseMatcher <CapturingLog .Message > {
178
+ private final StringContains delegateMatcher ;
179
+
180
+ LogMessageMatcher ( String needle )
181
+ {
182
+ this .delegateMatcher = new StringContains ( needle );
183
+ }
184
+
185
+ @ Override
186
+ public void describeTo ( Description description )
187
+ {
188
+ description .appendText ( "a log message with " );
189
+ delegateMatcher .describeTo ( description );
190
+ }
191
+
192
+ @ Override
193
+ public boolean matches ( Object item )
194
+ {
195
+ if ( item instanceof CapturingLog .Message )
196
+ {
197
+ CapturingLog .Message message = (CapturingLog .Message ) item ;
198
+ String haystack = message .message ;
199
+ return delegateMatcher .matches ( haystack );
200
+ }
201
+ return false ;
202
+ }
203
+ }
100
204
}
0 commit comments