Java Code Examples for org.sonar.api.Plugin#Context

The following examples show how to use org.sonar.api.Plugin#Context . 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: 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 3
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 4
Source File: TypeScriptPluginTest.java    From SonarTsPlugin with MIT License 5 votes vote down vote up
@Test
public void advertisesAppropriateExtensions() {
    Plugin.Context context = new Plugin.Context(SonarQubeVersion.V5_6);

    this.plugin.define(context);

    List extensions = context.getExtensions();

    assertTrue(extensions.contains(TypeScriptRuleProfile.class));
    assertTrue(extensions.contains(TypeScriptLanguage.class));
    assertTrue(extensions.contains(TsLintSensor.class));
    assertTrue(extensions.contains(CombinedCoverageSensor.class));
    assertTrue(extensions.contains(TsRulesDefinition.class));
}
 
Example 5
Source File: MyGherkinCustomRulesPluginTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void should_get_the_right_version() {
  Plugin.Context context = new Plugin.Context(Version.create(5, 6));
  new MyGherkinCustomRulesPlugin().define(context);
  assertThat(context.getSonarQubeVersion().major()).isEqualTo(5);
  assertThat(context.getSonarQubeVersion().minor()).isEqualTo(6);
}
 
Example 6
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 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: RubyPluginTest.java    From sonar-ruby-plugin with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testDefine() {
    RubyPlugin plugin = new RubyPlugin();

    Plugin.Context context = new Plugin.Context(Version.create(1, 0));

    plugin.define(context);

    assertThat(context.getExtensions().size()).isEqualTo(18);

    assertThat(context.getExtensions().stream().filter(ext -> ext instanceof PropertyDefinition).count()).isEqualTo(7);
    assertThat(context.getExtensions().stream().filter(ext -> ext instanceof Class).count()).isEqualTo(11);
}
 
Example 9
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 10
Source File: GherkinPluginTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void should_get_the_right_version() {
  Plugin.Context context = new Plugin.Context(Version.create(5, 6));
  new GherkinPlugin().define(context);
  assertThat(context.getSonarQubeVersion().major()).isEqualTo(5);
  assertThat(context.getSonarQubeVersion().minor()).isEqualTo(6);
}
 
Example 11
Source File: DefaultElevatedClassLoaderFactoryProviderTest.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void validFactoryReturnedOnNoPropertiesSet() {
    Plugin.Context context = mock(Plugin.Context.class);
    Configuration configuration = mock(Configuration.class);
    when(context.getBootConfiguration()).thenReturn(configuration);
    when(configuration.get(any())).thenReturn(Optional.empty());

    assertTrue(DefaultElevatedClassLoaderFactoryProvider.getInstance()
                       .createFactory(context) instanceof ClassReferenceElevatedClassLoaderFactory);
}
 
Example 12
Source File: ProviderTypeTest.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testClassReferenceTypeMatched() {
    Plugin.Context context = mock(Plugin.Context.class);
    Configuration configuration = mock(Configuration.class);
    when(context.getBootConfiguration()).thenReturn(configuration);

    assertTrue(ProviderType.fromName("CLASS_REFERENCE")
                       .createFactory(context) instanceof ClassReferenceElevatedClassLoaderFactory);
}
 
Example 13
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 14
Source File: GherkinPluginTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void should_get_the_right_number_of_extensions() {
  Plugin.Context context = new Plugin.Context(Version.create(5, 6));
  new GherkinPlugin().define(context);
  assertThat(context.getExtensions()).hasSize(4);
}
 
Example 15
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 16
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 17
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 18
Source File: LuaPluginTest.java    From sonar-lua with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testGetExtensions() throws Exception {
  Plugin.Context context = new Plugin.Context(Version.create(5, 6));
  new LuaPlugin().define(context);
  assertThat(context.getExtensions()).isNotEmpty();
}
 
Example 19
Source File: EsqlPluginTest.java    From sonar-esql-plugin with Apache License 2.0 4 votes vote down vote up
private Plugin.Context setupContext(SonarRuntime runtime) {
  Plugin.Context context = new Plugin.Context(runtime);
  new EsqlPlugin().define(context);
  return context;
}
 
Example 20
Source File: DefaultElevatedClassLoaderFactoryProvider.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ElevatedClassLoaderFactory createFactory(Plugin.Context context) {
    return ProviderType.fromName(
            context.getBootConfiguration().get(ElevatedClassLoaderFactoryProvider.class.getName() + ".providerType")
                    .orElse(ProviderType.CLASS_REFERENCE.name())).createFactory(context);
}