org.sonar.api.SonarQubeSide Java Examples

The following examples show how to use org.sonar.api.SonarQubeSide. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CommunityBranchPluginTest.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testScannerSideDefine() {
    final CommunityBranchPlugin testCase = new CommunityBranchPlugin();

    final Plugin.Context context = spy(mock(Plugin.Context.class, Mockito.RETURNS_DEEP_STUBS));
    when(context.getRuntime().getSonarQubeSide()).thenReturn(SonarQubeSide.SCANNER);

    testCase.define(context);

    final ArgumentCaptor<Object> argumentCaptor = ArgumentCaptor.forClass(Object.class);
    verify(context, times(1))
            .addExtensions(argumentCaptor.capture(), argumentCaptor.capture(), argumentCaptor.capture());


    assertEquals(Arrays.asList(CommunityProjectBranchesLoader.class, CommunityProjectPullRequestsLoader.class,
                               CommunityBranchConfigurationLoader.class, CommunityBranchParamsValidator.class),
                 argumentCaptor.getAllValues().subList(0, 4));
}
 
Example #2
Source File: CommunityBranchPluginTest.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testServerSideLoad() {
    final CommunityBranchPlugin testCase = new CommunityBranchPlugin();

    final CoreExtension.Context context = spy(mock(CoreExtension.Context.class, Mockito.RETURNS_DEEP_STUBS));
    when(context.getRuntime().getSonarQubeSide()).thenReturn(SonarQubeSide.SERVER);

    testCase.load(context);

    final ArgumentCaptor<Object> argumentCaptor = ArgumentCaptor.forClass(Object.class);
    verify(context, times(2)).addExtensions(argumentCaptor.capture(), argumentCaptor.capture());

    assertEquals(23, argumentCaptor.getAllValues().size());

    assertEquals(Arrays.asList(CommunityBranchFeatureExtension.class, CommunityBranchSupportDelegate.class),
                 argumentCaptor.getAllValues().subList(0, 2));
}
 
Example #3
Source File: CommunityBranchPluginBootstrap.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void define(Context context) {
    if (SonarQubeSide.SCANNER != context.getRuntime().getSonarQubeSide()) {
        return;
    }
    try {
        ClassLoader classLoader =
                elevatedClassLoaderFactoryProvider.createFactory(context).createClassLoader(getClass());
        Class<?> targetClass = classLoader.loadClass(getClass().getName().replace("Bootstrap", ""));
        Object instance = targetClass.getDeclaredConstructor().newInstance();
        if (!(instance instanceof Plugin)) {
            throw new IllegalStateException(
                    String.format("Expected loaded class to be instance of '%s' but was '%s'",
                                  Plugin.class.getName(), instance.getClass().getName()));
        }
        ((Plugin) instance).define(context);
    } catch (ReflectiveOperationException ex) {
        throw new IllegalStateException("Could not create CommunityBranchPlugin instance", ex);
    }
}
 
Example #4
Source File: CommunityBranchPluginBootstrapTest.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testDefineInvokedOnSuccessLoad() throws ClassNotFoundException {
    Plugin.Context context = spy(mock(Plugin.Context.class));
    Configuration configuration = mock(Configuration.class);
    when(context.getBootConfiguration()).thenReturn(configuration);
    when(configuration.get(any())).thenReturn(Optional.empty());
    SonarRuntime sonarRuntime = mock(SonarRuntime.class);
    when(context.getRuntime()).thenReturn(sonarRuntime);
    when(sonarRuntime.getSonarQubeSide()).thenReturn(SonarQubeSide.SCANNER);

    ClassLoader classLoader = mock(ClassLoader.class);
    when(classLoader.loadClass(any())).thenReturn((Class) MockPlugin.class);

    ElevatedClassLoaderFactory elevatedClassLoaderFactory = mock(ElevatedClassLoaderFactory.class);
    when(elevatedClassLoaderFactory.createClassLoader(any())).thenReturn(classLoader);

    ElevatedClassLoaderFactoryProvider elevatedClassLoaderFactoryProvider =
            mock(ElevatedClassLoaderFactoryProvider.class);
    when(elevatedClassLoaderFactoryProvider.createFactory(any())).thenReturn(elevatedClassLoaderFactory);

    CommunityBranchPluginBootstrap testCase =
            new CommunityBranchPluginBootstrap(elevatedClassLoaderFactoryProvider);
    testCase.define(context);

    assertEquals(context, MockPlugin.invokedContext);
}
 
Example #5
Source File: CommunityBranchPluginBootstrapTest.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testDefineNotInvokedForNonScanner() throws ClassNotFoundException {
    Plugin.Context context = spy(mock(Plugin.Context.class));
    Configuration configuration = mock(Configuration.class);
    when(context.getBootConfiguration()).thenReturn(configuration);
    when(configuration.get(any())).thenReturn(Optional.empty());
    SonarRuntime sonarRuntime = mock(SonarRuntime.class);
    when(context.getRuntime()).thenReturn(sonarRuntime);
    when(sonarRuntime.getSonarQubeSide()).thenReturn(SonarQubeSide.SERVER);

    ClassLoader classLoader = mock(ClassLoader.class);
    when(classLoader.loadClass(any())).thenReturn((Class) MockPlugin.class);

    ElevatedClassLoaderFactory elevatedClassLoaderFactory = mock(ElevatedClassLoaderFactory.class);
    when(elevatedClassLoaderFactory.createClassLoader(any())).thenReturn(classLoader);

    ElevatedClassLoaderFactoryProvider elevatedClassLoaderFactoryProvider =
            mock(ElevatedClassLoaderFactoryProvider.class);
    when(elevatedClassLoaderFactoryProvider.createFactory(any())).thenReturn(elevatedClassLoaderFactory);

    CommunityBranchPluginBootstrap testCase =
            new CommunityBranchPluginBootstrap(elevatedClassLoaderFactoryProvider);
    testCase.define(context);

    assertNull(MockPlugin.invokedContext);
}
 
Example #6
Source File: ColdfusionSensorTest.java    From sonar-coldfusion with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicCFMAnalysis() {
    DefaultFileSystem fileSystem = new DefaultFileSystem(tmpFolder.getRoot());
    fileSystem.setEncoding(Charsets.UTF_8);
    fileSystem.setWorkDir(tmpFolder.getRoot().toPath());

    context.setFileSystem(fileSystem);
    context.setRuntime(SonarRuntimeImpl.forSonarQube(Version.create(7, 6), SonarQubeSide.SCANNER));

    context.settings().appendProperty("sonar.projectBaseDir", baseDir.getPath());
    addFilesToFs();

    CommandExecutor commandExecutor = CommandExecutor.create();
    String javaHome = System.getProperty("java.home");
    Assert.assertTrue(javaHome!=null && !javaHome.equals(""));

    if(OSValidator.isWindows()) {
        context.settings().appendProperty(ColdFusionPlugin.CFLINT_JAVA, javaHome + "/bin/java.exe");
    } else {
        context.settings().appendProperty(ColdFusionPlugin.CFLINT_JAVA, javaHome + "/bin/java");
    }

    ColdFusionSensor sensor = new ColdFusionSensor(context.fileSystem(), rulesProfile);
    sensor.execute(context);

    Integer nloc = 0;
    Integer comments = 0;
    Integer complexity = 0;
    for (InputFile o : context.fileSystem().inputFiles()) {
        Measure<Integer> measureNloc = context.measure(o.key(),CoreMetrics.NCLOC.key());
        Measure<Integer> measureComment = context.measure(o.key(),CoreMetrics.COMMENT_LINES.key());
        Measure<Integer> measureComplexity = context.measure(o.key(),CoreMetrics.COMPLEXITY.key());
        nloc+=measureNloc.value();
        comments+=measureComment.value();
        complexity+=measureComplexity.value();
    }
    assertThat(nloc).isEqualTo(56);
    assertThat(comments).isEqualTo(9);
    assertThat(complexity).isEqualTo(10);
}
 
Example #7
Source File: ColdfusionPluginTest.java    From sonar-coldfusion with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtensions() {
    ColdFusionPlugin plugin = new ColdFusionPlugin();
    SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(VERSION_7_6, SonarQubeSide.SERVER);
    Plugin.Context context = new Plugin.Context(runtime);
    plugin.define(context);

    Assert.assertEquals(5, context.getExtensions().size());
}
 
Example #8
Source File: FlowPluginTest.java    From sonar-flow-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void debug() {
  SonarRuntime runtime = 
      SonarRuntimeImpl.forSonarQube(Version.create(5, 6), SonarQubeSide.SCANNER);
  Plugin.Context context = new Plugin.Context(runtime);
  new FlowPlugin().define(context);
  logger.debug("Nr of extentions: " + context.getExtensions().size());
  assertTrue(context.getExtensions().size() == 13);
}
 
Example #9
Source File: AemRulesSonarPluginTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Test
public void webPluginTester() {
    Plugin.Context context = new Plugin.Context(SonarRuntimeImpl.forSonarQube(Version.create(6, 7), SonarQubeSide.SERVER, SonarEdition.COMMUNITY));

    new AemRulesSonarPlugin().define(context);
    assertThat(context.getExtensions()).hasSize(7);
}
 
Example #10
Source File: ClojurePluginTest.java    From sonar-clojure with MIT License 5 votes vote down vote up
@Before
public void setUp() {
    SonarRuntime runtime = SonarRuntimeImpl
            .forSonarQube(Version.create(7, 9, 3), SonarQubeSide.SERVER, SonarEdition.COMMUNITY);
    context = new Plugin.Context(runtime);
    new ClojurePlugin().define(context);
}
 
Example #11
Source File: CommunityBranchPluginBootstrapTest.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testFailureOnIncorrectClassTypeReturned() throws ReflectiveOperationException {
    ClassLoader classLoader = mock(ClassLoader.class);
    doReturn(this.getClass()).when(classLoader).loadClass(any());

    ElevatedClassLoaderFactory classLoaderFactory = mock(ElevatedClassLoaderFactory.class);
    when(classLoaderFactory.createClassLoader(any())).thenReturn(classLoader);

    Plugin.Context context = mock(Plugin.Context.class, Mockito.RETURNS_DEEP_STUBS);
    SonarRuntime sonarRuntime = mock(SonarRuntime.class);
    when(context.getRuntime()).thenReturn(sonarRuntime);
    when(sonarRuntime.getSonarQubeSide()).thenReturn(SonarQubeSide.SCANNER);

    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage(IsEqual.equalTo(
            "Expected loaded class to be instance of 'org.sonar.api.Plugin' but was '" + getClass().getName() +
            "'"));

    ElevatedClassLoaderFactory elevatedClassLoaderFactory = mock(ElevatedClassLoaderFactory.class);
    when(elevatedClassLoaderFactory.createClassLoader(any())).thenReturn(classLoader);

    ElevatedClassLoaderFactoryProvider elevatedClassLoaderFactoryProvider =
            mock(ElevatedClassLoaderFactoryProvider.class);
    when(elevatedClassLoaderFactoryProvider.createFactory(any())).thenReturn(elevatedClassLoaderFactory);


    CommunityBranchPluginBootstrap testCase =
            new CommunityBranchPluginBootstrap(elevatedClassLoaderFactoryProvider);
    testCase.define(context);
}
 
Example #12
Source File: CommunityBranchPluginTest.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testLoad() {
    final CommunityBranchPlugin testCase = new CommunityBranchPlugin();

    final CoreExtension.Context context = mock(CoreExtension.Context.class, Mockito.RETURNS_DEEP_STUBS);
    when(context.getRuntime().getSonarQubeSide()).thenReturn(SonarQubeSide.SCANNER);

    testCase.load(context);

    verify(context, never()).addExtensions(any());
}
 
Example #13
Source File: EsqlPluginTest.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
private List<PropertyDefinition> properties() {
  List<PropertyDefinition> propertiesList = new ArrayList<>();
  List extensions = setupContext(SonarRuntimeImpl.forSonarQube(Version.create(7, 9), SonarQubeSide.SERVER, SonarEdition.COMMUNITY)).getExtensions();

  for (Object extension : extensions) {
    if (extension instanceof PropertyDefinition) {
      propertiesList.add((PropertyDefinition) extension);
    }
  }

  return propertiesList;
}
 
Example #14
Source File: CommunityBranchPluginTest.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testNonScannerSideDefine() {
    final CommunityBranchPlugin testCase = new CommunityBranchPlugin();

    final Plugin.Context context = spy(mock(Plugin.Context.class, Mockito.RETURNS_DEEP_STUBS));
    when(context.getRuntime().getSonarQubeSide()).thenReturn(SonarQubeSide.SERVER);

    testCase.define(context);

    verify(context, never()).addExtensions(any());
}
 
Example #15
Source File: CommunityBranchPlugin.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void define(Plugin.Context context) {
    if (SonarQubeSide.SCANNER == context.getRuntime().getSonarQubeSide()) {
        context.addExtensions(CommunityProjectBranchesLoader.class, CommunityProjectPullRequestsLoader.class,
                              CommunityBranchConfigurationLoader.class, CommunityBranchParamsValidator.class,
                              ScannerPullRequestPropertySensor.class);
    }
}
 
Example #16
Source File: CloverPluginTest.java    From sonar-clover with Apache License 2.0 5 votes vote down vote up
@Test
public void test_define() {
  final Plugin.Context context = new Plugin.Context(SonarRuntimeImpl.forSonarQube(
          Version.parse("6.7.4"),
          SonarQubeSide.SCANNER));
  new CloverPlugin().define(context);
  assertThat(context.getExtensions()).hasSize(1);
}
 
Example #17
Source File: EsqlPluginTest.java    From sonar-esql-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void count_extensions() throws Exception {
  Plugin.Context context = setupContext(SonarRuntimeImpl.forSonarQube(Version.create(7, 9), SonarQubeSide.SERVER, SonarEdition.COMMUNITY));

  assertThat(context.getExtensions()).hasSize(10);
}
 
Example #18
Source File: CommunityBranchPluginBootstrapTest.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testFailureOnReflectiveOperationException() throws ReflectiveOperationException {
    ClassLoader classLoader = mock(ClassLoader.class);
    doThrow(new ClassNotFoundException("Whoops")).when(classLoader).loadClass(any());

    Plugin.Context context = mock(Plugin.Context.class, Mockito.RETURNS_DEEP_STUBS);
    SonarRuntime sonarRuntime = mock(SonarRuntime.class);
    when(context.getRuntime()).thenReturn(sonarRuntime);
    when(sonarRuntime.getSonarQubeSide()).thenReturn(SonarQubeSide.SCANNER);

    expectedException.expectCause(new BaseMatcher<ClassNotFoundException>() {

        @Override
        public void describeTo(Description description) {
            description.appendText("Cause matcher");
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof ClassNotFoundException) {
                return "Whoops".equals(((ClassNotFoundException) item).getMessage());
            }
            return false;
        }
    });

    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage(IsEqual.equalTo("Could not create CommunityBranchPlugin instance"));


    ElevatedClassLoaderFactory elevatedClassLoaderFactory = mock(ElevatedClassLoaderFactory.class);
    when(elevatedClassLoaderFactory.createClassLoader(any())).thenReturn(classLoader);

    ElevatedClassLoaderFactoryProvider elevatedClassLoaderFactoryProvider =
            mock(ElevatedClassLoaderFactoryProvider.class);
    when(elevatedClassLoaderFactoryProvider.createFactory(any())).thenReturn(elevatedClassLoaderFactory);


    CommunityBranchPluginBootstrap testCase =
            new CommunityBranchPluginBootstrap(elevatedClassLoaderFactoryProvider);
    testCase.define(context);
}
 
Example #19
Source File: AuthOidcPluginTest.java    From sonar-auth-oidc with Apache License 2.0 4 votes vote down vote up
MockContext() {
  super(SonarRuntimeImpl.forSonarQube(Version.create(7, 6), SonarQubeSide.SERVER));
}
 
Example #20
Source File: AnsiblePluginTest.java    From sonar-ansible with Apache License 2.0 4 votes vote down vote up
public void testExtensionCounts() {
    Plugin.Context context = new Plugin.Context(SonarRuntimeImpl.forSonarQube(Version.create(6, 2), SonarQubeSide.SERVER));
    new AnsiblePlugin().define(context);
    assertEquals(4, context.getExtensions().size());
}
 
Example #21
Source File: AbstractAnsibleExtrasPluginTest.java    From sonar-ansible with Apache License 2.0 4 votes vote down vote up
public void testExtensionCounts() {
    Plugin.Context context = new Plugin.Context(SonarRuntimeImpl.forSonarQube(Version.create(6, 2), SonarQubeSide.SERVER));
    new MyExtrasPlugin().define(context);
    assertEquals(2, context.getExtensions().size());
}
 
Example #22
Source File: CommunityBranchPluginTest.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testComputeEngineSideLoad() {
    final CommunityBranchPlugin testCase = new CommunityBranchPlugin();

    final CoreExtension.Context context = spy(mock(CoreExtension.Context.class, Mockito.RETURNS_DEEP_STUBS));
    when(context.getRuntime().getSonarQubeSide()).thenReturn(SonarQubeSide.COMPUTE_ENGINE);

    testCase.load(context);

    final ArgumentCaptor<Class> argumentCaptor = ArgumentCaptor.forClass(Class.class);
    verify(context, times(2)).addExtensions(argumentCaptor.capture(), argumentCaptor.capture());


    assertEquals(Arrays.asList(CommunityReportAnalysisComponentProvider.class, CommunityBranchEditionProvider.class),
                 argumentCaptor.getAllValues().subList(0, 2));
}
 
Example #23
Source File: CommunityBranchPlugin.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void load(CoreExtension.Context context) {
    if (SonarQubeSide.COMPUTE_ENGINE == context.getRuntime().getSonarQubeSide()) {
        context.addExtensions(CommunityReportAnalysisComponentProvider.class, CommunityBranchEditionProvider.class);
    } else if (SonarQubeSide.SERVER == context.getRuntime().getSonarQubeSide()) {
        context.addExtensions(CommunityBranchFeatureExtension.class, CommunityBranchSupportDelegate.class,

                              AlmSettingsWs.class, CountBindingAction.class, DeleteAction.class,
                              DeleteBindingAction.class, ListAction.class, ListDefinitionsAction.class,
                              GetBindingAction.class,

                              CreateGithubAction.class, SetGithubBindingAction.class, UpdateGithubAction.class,

                              CreateAzureAction.class, SetAzureBindingAction.class, UpdateAzureAction.class,

                              CreateBitbucketAction.class, SetBitbucketBindingAction.class,
                              UpdateBitbucketAction.class,

                              CreateGitlabAction.class, SetGitlabBindingAction.class, UpdateGitlabAction.class,

            /* org.sonar.db.purge.PurgeConfiguration uses the value for the this property if it's configured, so it only
            needs to be specified here, but doesn't need any additional classes to perform the relevant purge/cleanup
            */
                              PropertyDefinition
                                      .builder(PurgeConstants.DAYS_BEFORE_DELETING_INACTIVE_BRANCHES_AND_PRS)
                                      .name("Number of days before purging inactive short living branches")
                                      .description(
                                              "Short living branches are permanently deleted when there are no analysis for the configured number of days.")
                                      .category(CoreProperties.CATEGORY_HOUSEKEEPING)
                                      .subCategory(CoreProperties.SUBCATEGORY_GENERAL).defaultValue("30")
                                      .type(PropertyType.INTEGER).build()


                             );

    }

    if (SonarQubeSide.COMPUTE_ENGINE == context.getRuntime().getSonarQubeSide() ||
        SonarQubeSide.SERVER == context.getRuntime().getSonarQubeSide()) {
        context.addExtensions(PropertyDefinition.builder(IMAGE_URL_BASE)
                                      .category(CoreProperties.CATEGORY_GENERAL)
                                      .subCategory(CoreProperties.SUBCATEGORY_GENERAL)
                                      .onQualifiers(Qualifiers.APP)
                                      .name("Images base URL")
                                      .description("Base URL used to load the images for the PR comments (please use this only if images are not displayed properly).")
                                      .type(PropertyType.STRING)
                                      .build());

    }
}