@Test
  public void getAutoCompletionHintsWhenOnlyDollarSign() throws Exception {
    // Note that we create nested Velocity Context in order to verify that we support that in
    // getAutoCompletionHints
    Context innerContext = new VelocityContext();
    innerContext.put("key1", "value1");
    VelocityContext vcontext = new VelocityContext(innerContext);
    vcontext.put("key2", "value2");
    setupMocks("$", vcontext);

    String velocity = "{{velocity}}$";

    Hints hints =
        mocker
            .getComponentUnderTest()
            .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity);

    // Verify we can get keys from the Velocity Context and its inner context too. We test this
    // since our
    // Velocity tools are put in an inner Velocity Context.
    assertEquals(5, hints.getHints().size());

    // Also verifies that hints are sorted
    SortedSet<HintData> expected = new TreeSet<HintData>();
    expected.addAll(
        Arrays.asList(
            new HintData("doc", "doc"),
            new HintData("key1", "key1"),
            new HintData("key2", "key2"),
            new HintData("sdoc", "sdoc"),
            new HintData("tdoc", "tdoc")));
    assertEquals(expected, hints.getHints());
  }
  @Test
  public void getAutoCompletionHintsForMethodWhenJustAfterTheDot() throws Exception {
    setupMocks("$key.", createTestVelocityContext("key", new TestClass()));

    Hints expectedMethods =
        new Hints()
            .withHints(
                new HintData("doWork", "doWork(...) AncillaryTestClass"),
                new HintData("something", "something String"),
                new HintData("getSomething", "getSomething(...) String"),
                new HintData("method1", "method1(...)"),
                new HintData("method2", "method2(...) String"));
    setupMethodFinderMock(expectedMethods, "", TestClass.class);

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

    assertEquals(5, hints.getHints().size());

    // Verify methods are returned sorted
    SortedSet<HintData> expected = new TreeSet<HintData>();
    expected.add(new HintData("doWork", "doWork(...) AncillaryTestClass"));
    expected.add(new HintData("getSomething", "getSomething(...) String"));
    expected.add(new HintData("method1", "method1(...)"));
    expected.add(new HintData("method2", "method2(...) String"));
    expected.add(new HintData("something", "something String"));
    assertEquals(expected, hints.getHints());
  }
  @Test
  public void getAutoCompletionHintsWhenNoDollarSign() throws Exception {
    setupMocks("whatever", new VelocityContext());
    String velocity = "{{velocity}}whatever";

    Hints hints =
        mocker
            .getComponentUnderTest()
            .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity);

    assertEquals(0, hints.getHints().size());
  }
  @Test
  public void getAutoCompletionHintsWhenInvalidAutoCompletion() throws Exception {
    setupMocks("$k ", createTestVelocityContext());

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

    assertEquals(0, hints.getHints().size());
  }
  /**
   * See http://jira.xwiki.org/browse/WIKIEDITOR-18
   *
   * @throws Exception
   */
  @Test
  public void getAutoCompletionHintsWhenDollarSignFollowedBySomeNonMatchingLettersThenDot()
      throws Exception {
    setupMocks("$o.", createTestVelocityContext("key", "value"));

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

    assertEquals(0, hints.getHints().size());
  }
  @Test
  public void getAutoCompletionHintsWhenDollarSignFollowedByCurlyBracketSymbol() throws Exception {
    setupMocks("${ke", createTestVelocityContext("key", "value", "otherKey", "otherValue"));

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

    assertEquals(1, hints.getHints().size());
    assertTrue(hints.getHints().contains(new HintData("key", "key")));
  }
  @Test
  public void getAutoCompletionHintsWhenOnlyDollarBangAndCurlyBracketSigns() throws Exception {
    setupMocks("$!{", createTestVelocityContext("key", "value"));

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

    assertEquals(4, hints.getHints().size());
    assertTrue(hints.getHints().contains(new HintData("key", "key")));
  }
  @Test
  @Ignore("Not working yet")
  public void getAutoCompletionHintsWhenSetDoneAbove() throws Exception {
    String velocity = "#set ($mydoc = $key.doWork())\n$mydoc.";
    setupMocks(velocity, createTestVelocityContext("key", new TestClass()));

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

    assertEquals(2, hints.getHints().size());
  }
  @Test
  public void getAutoCompletionHintsForMethodsWhenGetter() throws Exception {
    setupMocks("$key.s", createTestVelocityContext("key", new TestClass()));

    Hints expectedMethods = new Hints().withHints(new HintData("something", "something String"));
    setupMethodFinderMock(expectedMethods, "s", TestClass.class);

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

    assertEquals(1, hints.getHints().size());
  }
  @Test
  public void getAutoCompletionHintsWhenSeveralDollar() throws Exception {
    setupMocks("$var\n$key.", createTestVelocityContext("key", new TestClass()));

    Hints expectedMethods = new Hints().withHints(new HintData("method1", "method1"));
    setupMethodFinderMock(expectedMethods, "", TestClass.class);

    String velocity = "{{velocity}}$var\n$key.";
    Hints hints =
        mocker
            .getComponentUnderTest()
            .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity);

    assertEquals(1, hints.getHints().size());
    assertEquals(new HintData("method1", "method1"), hints.getHints().first());
  }
  @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 getAutoCompletionHintsForDeepChainedMethod() throws Exception {
    setupMocks("$key.doWork().method1().spl", createTestVelocityContext("key", new TestClass()));

    Hints expectedMethods = new Hints().withHints(new HintData("split", "split"));
    AutoCompletionMethodFinder methodFinder = mocker.getInstance(AutoCompletionMethodFinder.class);
    when(methodFinder.findMethodReturnTypes(TestClass.class, "doWork"))
        .thenReturn(Arrays.asList((Class) AncillaryTestClass.class));
    when(methodFinder.findMethodReturnTypes(AncillaryTestClass.class, "method1"))
        .thenReturn(Arrays.asList((Class) String.class));
    when(methodFinder.findMethods(String.class, "spl")).thenReturn(expectedMethods);

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

    assertEquals(1, hints.getHints().size());
    assertEquals(new HintData("split", "split"), hints.getHints().first());
    assertEquals(velocity.length() - "spl".length(), hints.getStartOffset());
  }
  @Test
  public void getAutoCompletionHintsForMethodWhenThereIsContentAfterCursorWithCapitalLetters()
      throws Exception {
    setupMocks("$key.doW", createTestVelocityContext("key", new TestClass()));

    Hints expectedMethods =
        new Hints().withHints(new HintData("doWork", "doWork(...) AncillaryTestClass"));
    setupMethodFinderMock(expectedMethods, "doW", TestClass.class);

    String velocity = "{{velocity}}$key.doWork";
    Hints hints =
        mocker
            .getComponentUnderTest()
            .getAutoCompletionHints(velocity.length() - 3, "xwiki/2.0", velocity);

    assertEquals(1, hints.getHints().size());

    // Verify methods are returned sorted
    SortedSet<HintData> expected = new TreeSet<HintData>();
    expected.add(new HintData("doWork", "doWork(...) AncillaryTestClass"));
    assertEquals(expected, hints.getHints());
  }
  @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")));
  }
Example #15
0
 /**
  * Build an instance of {@link Hints}.
  *
  * @param otherHints another instance to populate this instance with
  * @return the {@link Hints} containing also the hints from the passed instance
  */
 public Hints withHints(Hints otherHints) {
   this.withHints(otherHints.getHints());
   return this;
 }