You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CePluginJarExploderTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.container;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.TemporaryFolder;
  26. import org.sonar.core.platform.ExplodedPlugin;
  27. import org.sonar.core.platform.PluginInfo;
  28. import org.sonar.server.platform.ServerFileSystem;
  29. import static org.apache.commons.io.FileUtils.sizeOfDirectory;
  30. import static org.assertj.core.api.Assertions.assertThat;
  31. public class CePluginJarExploderTest {
  32. @Rule
  33. public TemporaryFolder temp = new TemporaryFolder();
  34. DumbFileSystem fs = new DumbFileSystem(temp);
  35. CePluginJarExploder underTest = new CePluginJarExploder(fs);
  36. @Test
  37. public void explode_jar_to_temp_directory() {
  38. PluginInfo info = PluginInfo.create(plugin1Jar());
  39. ExplodedPlugin exploded = underTest.explode(info);
  40. // all the files loaded by classloaders (JAR + META-INF/libs/*.jar) are copied to a dedicated temp directory
  41. File copiedJar = exploded.getMain();
  42. assertThat(exploded.getKey()).isEqualTo("test");
  43. assertThat(copiedJar).isFile().exists();
  44. assertThat(copiedJar.getParentFile()).isDirectory().hasName("test");
  45. assertThat(copiedJar.getParentFile().getParentFile()).isDirectory().hasName("ce-exploded-plugins");
  46. }
  47. @Test
  48. public void plugins_do_not_overlap() {
  49. PluginInfo info1 = PluginInfo.create(plugin1Jar());
  50. PluginInfo info2 = PluginInfo.create(plugin2Jar());
  51. ExplodedPlugin exploded1 = underTest.explode(info1);
  52. ExplodedPlugin exploded2 = underTest.explode(info2);
  53. assertThat(exploded1.getKey()).isEqualTo("test");
  54. assertThat(exploded1.getMain()).isFile().exists().hasName("sonar-test-plugin-0.1-SNAPSHOT.jar");
  55. assertThat(exploded2.getKey()).isEqualTo("test2");
  56. assertThat(exploded2.getMain()).isFile().exists().hasName("sonar-test2-plugin-0.1-SNAPSHOT.jar");
  57. }
  58. @Test
  59. public void explode_is_reentrant() throws Exception {
  60. PluginInfo info = PluginInfo.create(plugin1Jar());
  61. ExplodedPlugin exploded1 = underTest.explode(info);
  62. long dirSize1 = sizeOfDirectory(exploded1.getMain().getParentFile());
  63. ExplodedPlugin exploded2 = underTest.explode(info);
  64. long dirSize2 = sizeOfDirectory(exploded2.getMain().getParentFile());
  65. assertThat(exploded2.getMain().getCanonicalPath()).isEqualTo(exploded1.getMain().getCanonicalPath());
  66. assertThat(dirSize1).isEqualTo(dirSize2);
  67. }
  68. private File plugin1Jar() {
  69. return new File("src/test/plugins/sonar-test-plugin/target/sonar-test-plugin-0.1-SNAPSHOT.jar");
  70. }
  71. private File plugin2Jar() {
  72. return new File("src/test/plugins/sonar-test2-plugin/target/sonar-test2-plugin-0.1-SNAPSHOT.jar");
  73. }
  74. private static class DumbFileSystem implements ServerFileSystem {
  75. private final TemporaryFolder temp;
  76. private File tempDir;
  77. public DumbFileSystem(TemporaryFolder temp) {
  78. this.temp = temp;
  79. }
  80. @Override
  81. public File getHomeDir() {
  82. throw new UnsupportedOperationException();
  83. }
  84. @Override
  85. public File getTempDir() {
  86. if (tempDir == null) {
  87. try {
  88. this.tempDir = temp.newFolder();
  89. } catch (IOException e) {
  90. throw new IllegalStateException(e);
  91. }
  92. }
  93. return tempDir;
  94. }
  95. @Override
  96. public File getDeployedPluginsDir() {
  97. throw new UnsupportedOperationException();
  98. }
  99. @Override
  100. public File getDownloadedPluginsDir() {
  101. throw new UnsupportedOperationException();
  102. }
  103. @Override
  104. public File getInstalledPluginsDir() {
  105. throw new UnsupportedOperationException();
  106. }
  107. @Override
  108. public File getPluginIndex() {
  109. throw new UnsupportedOperationException();
  110. }
  111. @Override
  112. public File getUninstalledPluginsDir() {
  113. throw new UnsupportedOperationException();
  114. }
  115. }
  116. }