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.

ProjectBuilderTest.java 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.api.batch.bootstrap;
  21. import java.io.File;
  22. import org.junit.Test;
  23. import org.sonar.api.batch.bootstrap.internal.ProjectBuilderContext;
  24. import org.sonar.api.config.Configuration;
  25. import org.sonar.api.config.internal.MapSettings;
  26. import static org.assertj.core.api.Assertions.assertThat;
  27. import static org.hamcrest.core.Is.is;
  28. import static org.junit.Assert.assertThat;
  29. public class ProjectBuilderTest {
  30. @Test
  31. public void shouldChangeProject() {
  32. // this reactor is created and provided by Sonar
  33. final ProjectReactor projectReactor = new ProjectReactor(ProjectDefinition.create());
  34. ProjectBuilder builder = new ProjectBuilderSample();
  35. final MapSettings settings = new MapSettings();
  36. settings.setProperty("foo", "bar");
  37. final Configuration configuration = settings.asConfig();
  38. builder.build(new ProjectBuilderContext(projectReactor, configuration));
  39. assertThat(projectReactor.getProjects().size(), is(2));
  40. ProjectDefinition root = projectReactor.getRoot();
  41. assertThat(root.getName(), is("Name changed by plugin"));
  42. assertThat(root.getSubProjects().size(), is(1));
  43. assertThat(root.getSubProjects().get(0).sources()).contains("src");
  44. }
  45. final static class ProjectBuilderSample extends ProjectBuilder {
  46. @Override
  47. public void build(Context context) {
  48. assertThat(context.config().get("foo")).contains("bar");
  49. // change name of root project
  50. ProjectDefinition root = context.projectReactor().getRoot();
  51. root.setName("Name changed by plugin");
  52. // add sub-project
  53. File baseDir = new File(root.getBaseDir(), "path/to/subproject");
  54. ProjectDefinition subProject = ProjectDefinition.create();
  55. subProject.setBaseDir(baseDir);
  56. subProject.setWorkDir(new File(baseDir, "target/.sonar"));
  57. subProject.setKey("groupId:parentProjectId");
  58. subProject.setProjectVersion(root.getOriginalVersion());
  59. subProject.setName("Sub Project");
  60. subProject.setSources("src");
  61. root.addSubProject(subProject);
  62. }
  63. }
  64. }