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.

MavenTestUtils.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.api.test;
  21. import org.apache.commons.io.FileUtils;
  22. import org.apache.commons.io.IOUtils;
  23. import org.apache.maven.model.Model;
  24. import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
  25. import org.apache.maven.project.MavenProject;
  26. import org.sonar.api.batch.maven.MavenUtils;
  27. import org.sonar.api.resources.InputFile;
  28. import org.sonar.api.resources.Language;
  29. import org.sonar.api.resources.Project;
  30. import org.sonar.api.resources.ProjectFileSystem;
  31. import org.sonar.api.resources.Resource;
  32. import org.sonar.api.scan.filesystem.PathResolver;
  33. import org.sonar.api.utils.SonarException;
  34. import java.io.File;
  35. import java.io.FileReader;
  36. import java.io.IOException;
  37. import java.nio.charset.Charset;
  38. import java.util.ArrayList;
  39. import java.util.Arrays;
  40. import java.util.List;
  41. import static org.mockito.Mockito.mock;
  42. import static org.mockito.Mockito.when;
  43. public final class MavenTestUtils {
  44. public static MavenProject loadPom(Class clazz, String path) {
  45. String fullpath = "/" + clazz.getName().replaceAll("\\.", "/") + "/" + path;
  46. return loadPom(fullpath);
  47. }
  48. public static MavenProject loadPom(String pomUrlInClasspath) {
  49. FileReader fileReader = null;
  50. try {
  51. File pomFile = new File(MavenTestUtils.class.getResource(pomUrlInClasspath).toURI());
  52. MavenXpp3Reader pomReader = new MavenXpp3Reader();
  53. fileReader = new FileReader(pomFile);
  54. Model model = pomReader.read(fileReader);
  55. MavenProject project = new MavenProject(model);
  56. project.setFile(pomFile);
  57. project.getBuild().setDirectory(pomFile.getParentFile().getPath());
  58. project.addCompileSourceRoot(pomFile.getParentFile().getPath() + "/src/main/java");
  59. project.addTestCompileSourceRoot(pomFile.getParentFile().getPath() + "/src/test/java");
  60. return project;
  61. } catch (Exception e) {
  62. throw new SonarException("Failed to read Maven project file : " + pomUrlInClasspath, e);
  63. } finally {
  64. IOUtils.closeQuietly(fileReader);
  65. }
  66. }
  67. public static Project loadProjectFromPom(Class clazz, String path) {
  68. MavenProject pom = loadPom(clazz, path);
  69. Project project = new Project(pom.getGroupId() + ":" + pom.getArtifactId())
  70. .setPom(pom);
  71. // configuration.setProperty("sonar.java.source", MavenUtils.getJavaSourceVersion(pom));
  72. // configuration.setProperty("sonar.java.target", MavenUtils.getJavaVersion(pom));
  73. // configuration.setProperty(CoreProperties.ENCODING_PROPERTY, MavenUtils.getSourceEncoding(pom));
  74. project.setFileSystem(new MavenModuleFileSystem(pom));
  75. return project;
  76. }
  77. public static MavenProject mockPom(String packaging) {
  78. MavenProject mavenProject = mock(MavenProject.class);
  79. when(mavenProject.getPackaging()).thenReturn(packaging);
  80. return mavenProject;
  81. }
  82. static class MavenModuleFileSystem implements ProjectFileSystem {
  83. private MavenProject pom;
  84. MavenModuleFileSystem(MavenProject pom) {
  85. this.pom = pom;
  86. }
  87. public Charset getSourceCharset() {
  88. return Charset.forName(MavenUtils.getSourceEncoding(pom));
  89. }
  90. public File getBasedir() {
  91. return pom.getBasedir();
  92. }
  93. public File getBuildDir() {
  94. return new File(pom.getBuild().getDirectory());
  95. }
  96. public File getBuildOutputDir() {
  97. return new File(pom.getBuild().getOutputDirectory());
  98. }
  99. public List<File> getSourceDirs() {
  100. return Arrays.asList(new File(pom.getBuild().getSourceDirectory()));
  101. }
  102. public ProjectFileSystem addSourceDir(File dir) {
  103. throw new UnsupportedOperationException();
  104. }
  105. public List<File> getTestDirs() {
  106. return null;
  107. }
  108. public ProjectFileSystem addTestDir(File dir) {
  109. throw new UnsupportedOperationException();
  110. }
  111. public File getReportOutputDir() {
  112. return null;
  113. }
  114. public File getSonarWorkingDirectory() {
  115. File dir = new File(getBuildDir(), "sonar");
  116. try {
  117. FileUtils.forceMkdir(dir);
  118. } catch (IOException e) {
  119. throw new IllegalStateException(e);
  120. }
  121. return dir;
  122. }
  123. public File resolvePath(String path) {
  124. return new PathResolver().relativeFile(getBasedir(), path);
  125. }
  126. public List<File> getSourceFiles(Language... langs) {
  127. return new ArrayList(FileUtils.listFiles(getSourceDirs().get(0), new String[] {"java"}, true));
  128. }
  129. public List<File> getJavaSourceFiles() {
  130. return getSourceFiles();
  131. }
  132. public boolean hasJavaSourceFiles() {
  133. return !getJavaSourceFiles().isEmpty();
  134. }
  135. public List<File> getTestFiles(Language... langs) {
  136. return new ArrayList(FileUtils.listFiles(getTestDirs().get(0), new String[] {"java"}, true));
  137. }
  138. public boolean hasTestFiles(Language lang) {
  139. return !getTestFiles(lang).isEmpty();
  140. }
  141. public File writeToWorkingDirectory(String content, String fileName) throws IOException {
  142. throw new UnsupportedOperationException();
  143. }
  144. public File getFileFromBuildDirectory(String filename) {
  145. throw new UnsupportedOperationException();
  146. }
  147. public Resource toResource(File file) {
  148. throw new UnsupportedOperationException();
  149. }
  150. public List<InputFile> mainFiles(String... langs) {
  151. throw new UnsupportedOperationException();
  152. }
  153. public List<InputFile> testFiles(String... langs) {
  154. throw new UnsupportedOperationException();
  155. }
  156. }
  157. }