this.configuration = configuration;
}
- public void compressJar(String pluginKey, Path jarFile) {
+ public void compressJar(String pluginKey, Path sourceDir, Path targetJarFile) {
if (configuration.getBoolean(PROPERTY_PLUGIN_COMPRESSION_ENABLE).orElse(false)) {
- Path pack200Path = FileUtils.getPack200FilePath(jarFile);
- pack200(jarFile, pack200Path, pluginKey);
- String hash = calculateMd5(pack200Path);
- RemotePluginFile compressedPlugin = new RemotePluginFile(pack200Path.getFileName().toString(), hash);
+ Path targetPack200Path = FileUtils.getPack200FilePath(targetJarFile);
+ Path sourcePack200Path = sourceDir.resolve(targetPack200Path.getFileName());
+
+ // check if packed file was deployed alongside the jar. If that's the case, use it instead of generating it (SONAR-10395).
+ if (Files.isRegularFile(sourcePack200Path)) {
+ try {
+ LOG.debug("Found pack200: " + sourcePack200Path);
+ Files.copy(sourcePack200Path, targetPack200Path);
+ } catch (IOException e) {
+ throw new IllegalStateException("Failed to copy pack200 file from " + sourcePack200Path + " to " + targetPack200Path, e);
+ }
+ } else {
+ pack200(targetJarFile, targetPack200Path, pluginKey);
+ }
+
+ String hash = calculateMd5(targetPack200Path);
+ RemotePluginFile compressedPlugin = new RemotePluginFile(targetPack200Path.getFileName().toString(), hash);
compressedPlugins.put(pluginKey, compressedPlugin);
}
}
public TemporaryFolder temp = new TemporaryFolder();
private MapSettings settings = new MapSettings();
- private Path jarPath;
+ private Path targetJarPath;
+ private Path targetFolder;
+ private Path sourceFolder;
private PluginCompression underTest;
@Before
public void setUp() throws IOException {
- jarPath = temp.newFile("test.jar").toPath();
+ sourceFolder = temp.newFolder("source").toPath();
+ targetFolder = temp.newFolder("target").toPath();
+ targetJarPath = targetFolder.resolve("test.jar");
+ Files.createFile(targetJarPath);
}
@Test
public void disable_if_proparty_not_set() throws IOException {
underTest = new PluginCompression(settings.asConfig());
- underTest.compressJar("key", jarPath);
+ underTest.compressJar("key", sourceFolder, targetJarPath);
- assertThat(Files.list(jarPath.getParent())).containsOnly(jarPath);
+ assertThat(Files.list(targetFolder)).containsOnly(targetJarPath);
assertThat(underTest.getPlugins()).isEmpty();
}
public void should_compress_plugin() throws IOException {
settings.setProperty(PluginCompression.PROPERTY_PLUGIN_COMPRESSION_ENABLE, true);
underTest = new PluginCompression(settings.asConfig());
- underTest.compressJar("key", jarPath);
+ underTest.compressJar("key", targetFolder, targetJarPath);
- assertThat(Files.list(jarPath.getParent())).containsOnly(jarPath, jarPath.getParent().resolve("test.pack.gz"));
+ assertThat(Files.list(targetFolder)).containsOnly(targetJarPath, targetFolder.resolve("test.pack.gz"));
assertThat(underTest.getPlugins()).hasSize(1);
assertThat(underTest.getPlugins().get("key").getFilename()).isEqualTo("test.pack.gz");
}
+
+ @Test
+ public void should_use_deployed_packed_file() throws IOException {
+ Path packedPath = sourceFolder.resolve("test.pack.gz");
+ Files.write(packedPath, new byte[] {1, 2, 3});
+
+ settings.setProperty(PluginCompression.PROPERTY_PLUGIN_COMPRESSION_ENABLE, true);
+ underTest = new PluginCompression(settings.asConfig());
+ underTest.compressJar("key", sourceFolder, targetJarPath);
+
+ assertThat(Files.list(targetFolder)).containsOnly(targetJarPath, targetFolder.resolve("test.pack.gz"));
+ assertThat(underTest.getPlugins()).hasSize(1);
+ assertThat(underTest.getPlugins().get("key").getFilename()).isEqualTo("test.pack.gz");
+
+ // check that the file was copied, not generated
+ assertThat(targetFolder.resolve("test.pack.gz")).hasSameContentAs(packedPath);
+ }
+
}