22
33import javax .annotation .Nonnull ;
44
5+ import java .io .BufferedWriter ;
56import java .io .File ;
67import java .io .IOException ;
78import 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 ;
914
15+ import org .apache .commons .io .FileUtils ;
1016import org .codehaus .plexus .archiver .Archiver ;
1117import org .codehaus .plexus .archiver .TestSupport ;
1218import org .codehaus .plexus .archiver .util .ArchiveEntryUtils ;
1521import org .codehaus .plexus .components .io .resources .PlexusIoResource ;
1622import org .junit .jupiter .api .Test ;
1723
18- import static org .junit . jupiter .api .Assertions .assertTrue ;
24+ import static org .assertj . core .api .Assertions .assertThat ;
1925
2026class DirectoryArchiverUnpackJarTest extends TestSupport {
2127
@@ -36,25 +42,94 @@ public InputStream transform(@Nonnull PlexusIoResource resource, @Nonnull InputS
3642 @ Test
3743 void test_dependency_sets_depSet_unpacked_rdonly () throws Exception {
3844 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 ();
40114 DefaultArchivedFileSet afs = DefaultArchivedFileSet .archivedFileSet (src );
41115 afs .setIncludes (DEFAULT_INCLUDES_ARRAY );
42116 afs .setExcludes (null );
43117 afs .setPrefix ("child-1/" );
44118 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+ }
52124
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 );
59134 }
60135}
0 commit comments