@Test
  public void getAutoCompletionHintsForScriptServiceProperty() throws Exception {
    MockitoComponentMockingRule cm = mocker.getInstance(ComponentManager.class);
    AutoCompletionMethodFinder scriptServiceFinder =
        cm.registerMockComponent(AutoCompletionMethodFinder.class, "services");

    ScriptServiceManager scriptServiceManager = mock(ScriptServiceManager.class);
    setupMocks("$services.t", createTestVelocityContext("services", scriptServiceManager));
    when(scriptServiceFinder.findMethods(scriptServiceManager.getClass(), "t"))
        .thenReturn(new Hints().withHints(new HintData("test", "test")));

    String velocity = "{{velocity}}$services.t";
    Hints hints =
        mocker
            .getComponentUnderTest()
            .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity);

    assertEquals(1, hints.getHints().size());
    assertTrue(hints.getHints().contains(new HintData("test", "test")));
  }
  @Test
  public void getAutoCompletionHintsForScriptServicePropertyWhenNestedCalls() throws Exception {
    MockitoComponentMockingRule cm = mocker.getInstance(ComponentManager.class);
    AutoCompletionMethodFinder scriptServiceFinder =
        cm.registerMockComponent(AutoCompletionMethodFinder.class, "services");

    ScriptServiceManager scriptServiceManager = mock(ScriptServiceManager.class);
    setupMocks("$services.query.m", createTestVelocityContext("services", scriptServiceManager));
    when(scriptServiceFinder.findMethodReturnTypes(scriptServiceManager.getClass(), "query"))
        .thenReturn(Arrays.asList((Class) TestClass.class));
    AutoCompletionMethodFinder defaultMethodFinder =
        mocker.getInstance(AutoCompletionMethodFinder.class);
    when(defaultMethodFinder.findMethods(TestClass.class, "m"))
        .thenReturn(new Hints().withHints(new HintData("method", "method")));

    String velocity = "{{velocity}}$services.query.m";
    Hints hints =
        this.mocker
            .getComponentUnderTest()
            .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity);

    assertEquals(1, hints.getHints().size());
    assertTrue(hints.getHints().contains(new HintData("method", "method")));
  }
Exemplo n.º 3
0
 /**
  * Get a sub script service related to wiki. (Note that we're voluntarily using an API name of
  * "get" to make it extra easy to access Script Services from Velocity (since in Velocity writing
  * <code>$services.wiki.name</code> is equivalent to writing <code>$services.wiki.get("name")
  * </code>). It also makes it a short and easy API name for other scripting languages.
  *
  * @param serviceName id of the script service
  * @return the service asked or null if none could be found
  */
 public ScriptService get(String serviceName) {
   return scriptServiceManager.get(ROLEHINT + '.' + serviceName);
 }