org.sonar.api.SonarRuntime Java Examples

The following examples show how to use org.sonar.api.SonarRuntime. 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: 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 #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 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 #3
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 #4
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 #5
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 #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: 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 #8
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;
}