18
18
19
19
import java .io .File ;
20
20
import java .io .IOException ;
21
- import java .nio . file . Files ;
21
+ import java .util . Date ;
22
22
23
23
import org .codehaus .plexus .components .io .filemappers .FileMapper ;
24
+ import org .hamcrest .BaseMatcher ;
25
+ import org .hamcrest .Description ;
26
+ import org .hamcrest .core .StringContains ;
24
27
import org .junit .After ;
25
28
import org .junit .Before ;
26
29
import org .junit .Rule ;
27
30
import org .junit .Test ;
28
31
import 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 ;
29
37
30
38
/**
31
39
* Unit test for {@link AbstractUnArchiver}
@@ -37,7 +45,11 @@ public class AbstractUnArchiverTest
37
45
@ Rule
38
46
public ExpectedException thrown = ExpectedException .none ();
39
47
48
+ @ Rule
49
+ public TemporaryFolder temporaryFolder = new TemporaryFolder ();
50
+
40
51
private AbstractUnArchiver abstractUnArchiver ;
52
+ private CapturingLog log = new CapturingLog ( 0 /*debug*/ , "AbstractUnArchiver" );
41
53
42
54
@ Before
43
55
public void setUp ()
@@ -58,6 +70,7 @@ protected void execute()
58
70
// unused
59
71
}
60
72
};
73
+ this .abstractUnArchiver .enableLogging ( log );
61
74
}
62
75
63
76
@ After
@@ -72,7 +85,7 @@ public void shouldThrowExceptionBecauseRewrittenPathIsOutOfDirectory()
72
85
{
73
86
// given
74
87
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 ();
76
89
final FileMapper [] fileMappers = new FileMapper [] { new FileMapper ()
77
90
{
78
91
@ Override
@@ -97,4 +110,105 @@ public String getMappedFileName( String pName )
97
110
// ArchiverException is thrown providing the rewritten path
98
111
}
99
112
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
+ }
100
214
}
0 commit comments