2
2
3
3
import javax .annotation .Nonnull ;
4
4
5
+ import java .io .BufferedWriter ;
5
6
import java .io .File ;
6
7
import java .io .IOException ;
7
8
import java .io .InputStream ;
8
- import java .nio .charset .Charset ;
9
+ import java .nio .charset .StandardCharsets ;
10
+ import java .nio .file .Files ;
11
+ import java .nio .file .Path ;
12
+ import java .nio .file .StandardOpenOption ;
13
+ import java .nio .file .attribute .FileTime ;
9
14
15
+ import org .apache .commons .io .FileUtils ;
10
16
import org .codehaus .plexus .archiver .Archiver ;
11
17
import org .codehaus .plexus .archiver .TestSupport ;
12
18
import org .codehaus .plexus .archiver .util .ArchiveEntryUtils ;
15
21
import org .codehaus .plexus .components .io .resources .PlexusIoResource ;
16
22
import org .junit .jupiter .api .Test ;
17
23
18
- import static org .junit . jupiter .api .Assertions .assertTrue ;
24
+ import static org .assertj . core .api .Assertions .assertThat ;
19
25
20
26
class DirectoryArchiverUnpackJarTest extends TestSupport {
21
27
@@ -36,25 +42,94 @@ public InputStream transform(@Nonnull PlexusIoResource resource, @Nonnull InputS
36
42
@ Test
37
43
void test_dependency_sets_depSet_unpacked_rdonly () throws Exception {
38
44
File src = new File ("src/test/resources/unpack_issue.jar" );
39
- assertTrue (src .exists ());
45
+ File dest = new File ("target/depset_unpack" );
46
+ FileUtils .deleteDirectory (dest );
47
+ assertThat (src ).isFile ();
48
+
49
+ Archiver archiver = createArchiver (src , dest );
50
+ archiver .setDefaultDirectoryMode (0555 );
51
+ archiver .setDirectoryMode (0555 ); // causes permission denied if bug is not fixed.
52
+ archiver .createArchive ();
53
+ assertThat (new File (dest , "child-1/META-INF/MANIFEST.MF" )).isFile ();
54
+
55
+ // make them writeable or mvn clean will fail
56
+ ArchiveEntryUtils .chmod (new File (dest , "child-1/META-INF" ), 0777 );
57
+ ArchiveEntryUtils .chmod (new File (dest , "child-1/META-INF/maven" ), 0777 );
58
+ ArchiveEntryUtils .chmod (new File (dest , "child-1/META-INF/maven/test" ), 0777 );
59
+ ArchiveEntryUtils .chmod (new File (dest , "child-1/META-INF/maven/test/child1" ), 0777 );
60
+ ArchiveEntryUtils .chmod (new File (dest , "child-1/assembly-resources" ), 0777 );
61
+ }
62
+
63
+ @ Test
64
+ void test_dependency_sets_depSet_unpacked_by_default_dont_override () throws Exception {
65
+
66
+ File src = new File ("src/test/resources/unpack_issue.jar" );
67
+ File dest = new File ("target/depset_unpack_dont_override" );
68
+ FileUtils .deleteDirectory (dest );
69
+
70
+ Archiver archiver = createArchiver (src , dest );
71
+ archiver .createArchive ();
72
+
73
+ File manifestFile = new File (dest , "child-1/META-INF/MANIFEST.MF" );
74
+ assertThat (manifestFile ).content ().hasLineCount (6 );
75
+
76
+ // change content of one file
77
+ overwriteFileContent (manifestFile .toPath ());
78
+ assertThat (manifestFile ).content ().hasLineCount (1 );
79
+
80
+ archiver = createArchiver (src , dest );
81
+ archiver .createArchive ();
82
+
83
+ // content was not changed
84
+ assertThat (manifestFile ).content ().hasLineCount (1 );
85
+ }
86
+
87
+ @ Test
88
+ void test_dependency_sets_depSet_force_unpacked () throws Exception {
89
+
90
+ File src = new File ("src/test/resources/unpack_issue.jar" );
91
+ File dest = new File ("target/depset_unpack_force" );
92
+ FileUtils .deleteDirectory (dest );
93
+
94
+ Archiver archiver = createArchiver (src , dest );
95
+ archiver .createArchive ();
96
+
97
+ File manifestFile = new File (dest , "child-1/META-INF/MANIFEST.MF" );
98
+ assertThat (manifestFile ).content ().hasLineCount (6 );
99
+
100
+ // change content of one file
101
+ overwriteFileContent (manifestFile .toPath ());
102
+ assertThat (manifestFile ).content ().hasLineCount (1 );
103
+
104
+ archiver = createArchiver (src , dest );
105
+ archiver .setForced (true );
106
+ archiver .createArchive ();
107
+
108
+ // content was changed
109
+ assertThat (manifestFile ).content ().hasLineCount (6 );
110
+ }
111
+
112
+ private Archiver createArchiver (File src , File dest ) {
113
+ assertThat (src ).isFile ();
40
114
DefaultArchivedFileSet afs = DefaultArchivedFileSet .archivedFileSet (src );
41
115
afs .setIncludes (DEFAULT_INCLUDES_ARRAY );
42
116
afs .setExcludes (null );
43
117
afs .setPrefix ("child-1/" );
44
118
afs .setStreamTransformer (new IdentityTransformer ());
45
- Archiver archiver = (Archiver ) lookup (Archiver .class , "dir" );
46
- archiver .setDefaultDirectoryMode (0555 );
47
- archiver .setDirectoryMode (0555 ); // causes permission denied if bug is not fixed.
48
- archiver .setDestFile (new File ("target/depset_unpack" ));
49
- archiver .addArchivedFileSet (afs , Charset .forName ("UTF-8" ));
50
- archiver .createArchive ();
51
- assertTrue (new File ("target/depset_unpack/child-1/META-INF/MANIFEST.MF" ).exists ());
119
+ Archiver archiver = lookup (Archiver .class , "dir" );
120
+ archiver .setDestFile (dest );
121
+ archiver .addArchivedFileSet (afs , StandardCharsets .UTF_8 );
122
+ return archiver ;
123
+ }
52
124
53
- // make them writeable or mvn clean will fail
54
- ArchiveEntryUtils .chmod (new File ("target/depset_unpack/child-1/META-INF" ), 0777 );
55
- ArchiveEntryUtils .chmod (new File ("target/depset_unpack/child-1/META-INF/maven" ), 0777 );
56
- ArchiveEntryUtils .chmod (new File ("target/depset_unpack/child-1/META-INF/maven/test" ), 0777 );
57
- ArchiveEntryUtils .chmod (new File ("target/depset_unpack/child-1/META-INF/maven/test/child1" ), 0777 );
58
- ArchiveEntryUtils .chmod (new File ("target/depset_unpack/child-1/assembly-resources" ), 0777 );
125
+ private void overwriteFileContent (Path path ) throws IOException {
126
+ FileTime lastModifiedTime = Files .getLastModifiedTime (path );
127
+
128
+ try (BufferedWriter writer =
129
+ Files .newBufferedWriter (path , StandardCharsets .UTF_8 , StandardOpenOption .TRUNCATE_EXISTING )) {
130
+ writer .write ("TEST123" );
131
+ }
132
+
133
+ Files .setLastModifiedTime (path , lastModifiedTime );
59
134
}
60
135
}
0 commit comments