Uploaded image for project: 'XWiki Commons'
  1. XWiki Commons
  2. XCOMMONS-1869

Cannot write tests for a component having several hints

    XMLWordPrintable

Details

    • Unit
    • Unknown
    • N/A
    • N/A

    Description

      For example:

      @Component(hints = {"hidden", HiddenDocumentFilter.HINT})
      public class HiddenDocumentFilter extends AbstractHiddenFilter
      

      When converting the test to:

      /*
       * See the NOTICE file distributed with this work for additional
       * information regarding copyright ownership.
       *
       * This is free software; you can redistribute it and/or modify it
       * under the terms of the GNU Lesser General Public License as
       * published by the Free Software Foundation; either version 2.1 of
       * the License, or (at your option) any later version.
       *
       * This software is distributed in the hope that it will be useful,
       * but WITHOUT ANY WARRANTY; without even the implied warranty of
       * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
       * Lesser General Public License for more details.
       *
       * You should have received a copy of the GNU Lesser General Public
       * License along with this software; if not, write to the Free
       * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
       * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
       */
      package org.xwiki.query.internal;
      
      import org.junit.jupiter.api.Test;
      import org.xwiki.component.util.ReflectionUtils;
      import org.xwiki.query.Query;
      import org.xwiki.test.annotation.BeforeComponent;
      import org.xwiki.test.junit5.mockito.ComponentTest;
      import org.xwiki.test.junit5.mockito.InjectMockComponents;
      import org.xwiki.test.junit5.mockito.MockComponent;
      import org.xwiki.user.User;
      import org.xwiki.user.UserReference;
      import org.xwiki.user.UserResolver;
      
      import static org.junit.jupiter.api.Assertions.assertEquals;
      import static org.mockito.Mockito.mock;
      import static org.mockito.Mockito.when;
      
      /**
       * Tests for {@link HiddenDocumentFilter}
       *
       * @version $Id: c9d6cbdb205f5bb23170e80ffc49a201f211f4bb $
       */
      @ComponentTest
      public class HiddenDocumentFilterTest
      {
          @InjectMockComponents
          private HiddenDocumentFilter filter;
      
          @MockComponent
          private UserResolver<UserReference> userResolver;
      
          @BeforeComponent
          public void before()
          {
              User user = mock(User.class);
              when(user.displayHiddenDocuments()).thenReturn(true);
              when(this.userResolver.resolve(UserReference.CURRENT_USER_REFERENCE)).thenReturn(user);
          }
      
          @Test
          void filterHQLStatementWithDoNotDisplayHiddenDocumentsInTheUserPreferences()
          {
              assertEquals(
                  "select doc.fullName from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null) and (1=1)",
                  filter.filterStatement("select doc.fullName from XWikiDocument doc where 1=1", Query.HQL));
          }
      
          @Test
          void filterHQLStatementWithDisplayHiddenDocumentsInTheUserPreferences()
          {
              // We need to do it that way since the expectation must be set in #configure() and the expectation sets the
              // isActive property to true
              ReflectionUtils.setFieldValue(this.filter, "isActive", false);
      
              // Insertions of distinct
              assertEquals("select doc.fullName from XWikiDocument doc where 1=1",
                  filter.filterStatement("select doc.fullName from XWikiDocument doc where 1=1", Query.HQL));
          }
      
          @Test
          void filterIncorrectHQLStatement()
          {
              // Insertions of distinct
              assertEquals("select doc.fullName from XWikiDocument mydoc where 1=1",
                  filter.filterStatement("select doc.fullName from XWikiDocument mydoc where 1=1", Query.HQL));
          }
      
          @Test
          void filterXWQLStatement()
          {
              assertEquals("select doc.fullName from XWikiDocument doc where 1=1",
                  filter.filterStatement("select doc.fullName from XWikiDocument doc where 1=1", Query.XWQL));
          }
      
          @Test
          void filterHQLStatementWithWhereAndOrderBy()
          {
              // Insertions of distinct
              assertEquals("select doc.name from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null) and "
                  + "(1=1) order by doc.name",
                  filter.filterStatement("select doc.name from XWikiDocument doc where 1=1 order by doc.name",
                      Query.HQL));
          }
      
          @Test
          void filterHQLStatementWithWhereAndGroupBy()
          {
              // Insertions of distinct
              assertEquals("select doc.name from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null) and "
                  + "(1=1) group by doc.name",
                  filter.filterStatement("select doc.name from XWikiDocument doc where 1=1 group by doc.name",
                      Query.HQL));
          }
      
          @Test
          void filterHQLStatementWithWhereAndOrderByAndGroupBy()
          {
              // Insertions of distinct
              assertEquals("select doc.name from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null) and "
                  + "(1=1) order by doc.name group by doc.name",
                  filter.filterStatement("select doc.name from XWikiDocument doc where 1=1 order by doc.name group by "
                      + "doc.name", Query.HQL));
          }
      
          @Test
          void filterHQLStatementWithoutWhere()
          {
              // Insertions of distinct
              assertEquals("select doc.name from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null)",
                  filter.filterStatement("select doc.name from XWikiDocument doc", Query.HQL));
          }
      
          @Test
          void filterHQLStatementWithoutWhereWithOrderBy()
          {
              // Insertions of distinct
              assertEquals("select doc.name from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null) order by "
                  + "doc.name asc",
                  filter.filterStatement("select doc.name from XWikiDocument doc order by doc.name asc", Query.HQL));
          }
      
          @Test
          void filterHQLStatementWithoutWhereWithGroupBy()
          {
              // Insertions of distinct
              assertEquals(
                  "select doc.web, doc.name from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null) " +
                      "group by doc.web",
                  filter.filterStatement("select doc.web, doc.name from XWikiDocument doc group by doc.web", Query.HQL));
          }
      }
      

      When we execute it, we get:

      java.lang.Exception: The component under field [filter] is implementing several roles. Please disambiguate by using the "role" parameter of the @InjectMockComponents annotation.
      

      Attachments

        Activity

          People

            vmassol Vincent Massol
            vmassol Vincent Massol
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: