|
1 | 1 | package org.codehaus.mojo.mrm.impl.maven;
|
2 | 2 |
|
3 |
| -import static org.junit.Assert.assertEquals; |
4 |
| -import static org.junit.Assert.assertNotNull; |
5 |
| -import static org.junit.Assert.assertTrue; |
6 |
| - |
7 | 3 | import java.io.File;
|
8 | 4 | import java.io.FileInputStream;
|
| 5 | +import java.io.InputStream; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.StandardCopyOption; |
9 | 8 | import java.util.ArrayList;
|
| 9 | +import java.util.Enumeration; |
10 | 10 | import java.util.List;
|
11 | 11 | import java.util.jar.JarEntry;
|
12 |
| -import java.util.jar.JarInputStream; |
| 12 | +import java.util.jar.JarFile; |
| 13 | +import java.util.jar.Manifest; |
13 | 14 |
|
14 | 15 | import org.apache.commons.io.IOUtils;
|
15 | 16 | import org.apache.maven.archetype.catalog.Archetype;
|
16 | 17 | import org.apache.maven.archetype.catalog.ArchetypeCatalog;
|
17 | 18 | import org.codehaus.mojo.mrm.api.maven.Artifact;
|
| 19 | +import org.junit.Rule; |
18 | 20 | import org.junit.Test;
|
| 21 | +import org.junit.rules.TemporaryFolder; |
| 22 | + |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | +import static org.junit.Assert.assertNotNull; |
| 25 | +import static org.junit.Assert.assertTrue; |
19 | 26 |
|
20 | 27 | public class MockArtifactStoreTest
|
21 | 28 | {
|
| 29 | + @Rule |
| 30 | + public TemporaryFolder temporaryFolder= new TemporaryFolder(); |
22 | 31 |
|
23 | 32 | // MMOCKRM-3
|
24 | 33 | @Test
|
@@ -70,22 +79,99 @@ public void testDirectoryContent() throws Exception
|
70 | 79 | assertTrue( "Content equals", IOUtils.contentEquals( new FileInputStream( "target/test-classes/mrm-15/mrm-15-1.0.pom" ), artifactStore.get( pomArtifact ) ) );
|
71 | 80 |
|
72 | 81 | Artifact mainArtifact = new Artifact( "localhost", "mrm-15", "1.0", "jar" );
|
73 |
| - assertNotNull( artifactStore.get( mainArtifact ) ); |
| 82 | + InputStream inputStreamJar = artifactStore.get( mainArtifact ); |
| 83 | + assertNotNull( inputStreamJar ); |
74 | 84 |
|
75 | 85 | List<String> names = new ArrayList<>();
|
76 | 86 |
|
77 |
| - try (JarInputStream jis = new JarInputStream( artifactStore.get( mainArtifact ) ) ) |
| 87 | + File jarFile = temporaryFolder.newFile(); |
| 88 | + Files.copy( inputStreamJar, jarFile.toPath(), StandardCopyOption.REPLACE_EXISTING ); |
| 89 | + |
| 90 | + try (JarFile jar = new JarFile( jarFile ) ) |
78 | 91 | {
|
79 |
| - JarEntry jarEntry; |
80 |
| - while ( ( jarEntry = jis.getNextJarEntry() ) != null ) |
| 92 | + Enumeration<JarEntry> entries = jar.entries(); |
| 93 | + while ( entries.hasMoreElements() ) |
81 | 94 | {
|
82 |
| - names.add( jarEntry.getName() ); |
| 95 | + JarEntry entry = entries.nextElement(); |
| 96 | + names.add( entry.getName() ); |
83 | 97 | }
|
| 98 | + Manifest manifest = jar.getManifest(); |
| 99 | + assertNotNull( manifest ); |
| 100 | + assertEquals( 2, manifest.getMainAttributes().size()); |
84 | 101 | }
|
85 | 102 |
|
86 | 103 | assertTrue( names.contains( "README.txt" ) );
|
87 | 104 | }
|
88 |
| - |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testEmptyJarContent() throws Exception |
| 108 | + { |
| 109 | + MockArtifactStore artifactStore = new MockArtifactStore( new File( "target/test-classes/empty-jar" ) ); |
| 110 | + |
| 111 | + Artifact pomArtifact = new Artifact( "localhost", "mrm-empty-jar", "1.0", "pom" ); |
| 112 | + InputStream inputStreamPom = artifactStore.get( pomArtifact ); |
| 113 | + assertNotNull( inputStreamPom ); |
| 114 | + assertTrue( "Content equals", IOUtils.contentEquals( new FileInputStream( "target/test-classes/empty-jar/mrm-empty-jar-1.0.pom" ), |
| 115 | + inputStreamPom ) ); |
| 116 | + |
| 117 | + Artifact mainArtifact = new Artifact( "localhost", "mrm-empty-jar", "1.0", "jar" ); |
| 118 | + InputStream inputStreamJar = artifactStore.get( mainArtifact ); |
| 119 | + assertNotNull( inputStreamJar ); |
| 120 | + |
| 121 | + File jarFile = temporaryFolder.newFile(); |
| 122 | + Files.copy( inputStreamJar, jarFile.toPath(), StandardCopyOption.REPLACE_EXISTING ); |
| 123 | + |
| 124 | + List<String> names = new ArrayList<>(); |
| 125 | + try (JarFile jar = new JarFile( jarFile ) ) |
| 126 | + { |
| 127 | + Enumeration<JarEntry> entries = jar.entries(); |
| 128 | + while ( entries.hasMoreElements() ) |
| 129 | + { |
| 130 | + JarEntry entry = entries.nextElement(); |
| 131 | + names.add( entry.getName() ); |
| 132 | + } |
| 133 | + Manifest manifest = jar.getManifest(); |
| 134 | + assertNotNull( manifest ); |
| 135 | + assertEquals( 3, manifest.getMainAttributes().size()); |
| 136 | + } |
| 137 | + assertTrue( names.contains( "META-INF/MANIFEST.MF" ) ); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testEmptyPluginJarContent() throws Exception |
| 142 | + { |
| 143 | + MockArtifactStore artifactStore = new MockArtifactStore( new File( "target/test-classes/empty-plugin-jar" ) ); |
| 144 | + |
| 145 | + Artifact pomArtifact = new Artifact( "localhost", "mrm-empty-plugin-jar", "1.0", "pom" ); |
| 146 | + InputStream inputStreamPom = artifactStore.get( pomArtifact ); |
| 147 | + assertNotNull( inputStreamPom ); |
| 148 | + assertTrue( "Content equals", IOUtils.contentEquals( new FileInputStream( "target/test-classes/empty-plugin-jar/mrm-empty-plugin-jar-1.0.pom" ), |
| 149 | + inputStreamPom ) ); |
| 150 | + |
| 151 | + Artifact mainArtifact = new Artifact( "localhost", "mrm-empty-plugin-jar", "1.0", "jar" ); |
| 152 | + InputStream inputStreamJar = artifactStore.get( mainArtifact ); |
| 153 | + assertNotNull( inputStreamJar ); |
| 154 | + |
| 155 | + File jarFile = temporaryFolder.newFile(); |
| 156 | + Files.copy( inputStreamJar, jarFile.toPath(), StandardCopyOption.REPLACE_EXISTING ); |
| 157 | + |
| 158 | + List<String> names = new ArrayList<>(); |
| 159 | + try (JarFile jar = new JarFile( jarFile ) ) |
| 160 | + { |
| 161 | + Enumeration<JarEntry> entries = jar.entries(); |
| 162 | + while ( entries.hasMoreElements() ) |
| 163 | + { |
| 164 | + JarEntry entry = entries.nextElement(); |
| 165 | + names.add( entry.getName() ); |
| 166 | + } |
| 167 | + Manifest manifest = jar.getManifest(); |
| 168 | + assertNotNull( manifest ); |
| 169 | + assertEquals( 3, manifest.getMainAttributes().size()); |
| 170 | + } |
| 171 | + assertTrue( names.contains( "META-INF/MANIFEST.MF" ) ); |
| 172 | + assertTrue( names.contains( "META-INF/maven/plugin.xml" ) ); |
| 173 | + } |
| 174 | + |
89 | 175 | @Test
|
90 | 176 | public void testDirectoryWithClassifierContent() throws Exception
|
91 | 177 | {
|
|
0 commit comments