Class CockpitTestUtil


  • public final class CockpitTestUtil
    extends java.lang.Object
    Utility methods for testing.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      protected static org.mockito.stubbing.Answer<java.lang.Object> createAttributesAnswer​(java.util.Set<java.lang.String> keys, java.util.function.BiFunction<java.lang.String,​java.lang.Object,​java.lang.Object> mock)  
      static AuResponseProcessor<org.zkoss.zk.au.out.AuEcho> createEchoProcessor()
      Creates a processor for AuEcho requests.
      static void executeWithLabels​(java.util.Map<java.lang.String,​java.lang.String> labels, java.lang.Runnable runnable)
      Allows to mock ZK labels that are fetched by static methods like Labels.getLabel(String).
      static <C extends org.zkoss.zk.ui.Component>
      java.util.Optional<C>
      find​(org.zkoss.zk.ui.Component parent, java.lang.Class<? extends C> childClass)
      Looks for a component of provided type (incl.
      static <C extends org.zkoss.zk.ui.Component>
      java.util.Optional<C>
      find​(org.zkoss.zk.ui.Component parent, java.util.function.Predicate<org.zkoss.zk.ui.Component> test)
      Looks for a component that meets of provided requirement (incl.
      static <C extends org.zkoss.zk.ui.Component>
      java.util.stream.Stream<C>
      findAll​(org.zkoss.zk.ui.Component parent, java.lang.Class<? extends C> childClass)
      Looks for a all components of provided type (incl.
      static <C extends org.zkoss.zk.ui.Component>
      java.util.stream.Stream<C>
      findAll​(org.zkoss.zk.ui.Component parent, java.util.function.Predicate<org.zkoss.zk.ui.Component> test)
      Looks for a all components that meets of provided requirement (incl.
      static <C extends org.zkoss.zk.ui.Component>
      java.util.stream.Stream<C>
      findAllChildren​(org.zkoss.zk.ui.Component parent, java.lang.Class<? extends C> childClass)
      Looks for all children of provided type.
      static <C extends org.zkoss.zk.ui.Component>
      java.util.stream.Stream<C>
      findAllChildren​(org.zkoss.zk.ui.Component parent, java.util.function.Predicate<org.zkoss.zk.ui.Component> test)
      Looks for all children that meets of provided requirement.
      static <C extends org.zkoss.zk.ui.Component>
      java.util.Optional<C>
      findChild​(org.zkoss.zk.ui.Component parent, java.lang.Class<? extends C> childClass)
      Looks for a child of provided type.
      static <C extends org.zkoss.zk.ui.Component>
      java.util.Optional<C>
      findChild​(org.zkoss.zk.ui.Component parent, java.util.function.Predicate<org.zkoss.zk.ui.Component> test)
      Looks for a child that meets of provided requirement.
      static java.lang.String[] getSClasses​(java.lang.String sclass)
      Retrieves all CSS classes from provided sclass attribute of component
      static java.lang.String[] getSClasses​(org.zkoss.zk.ui.HtmlBasedComponent component)
      Retrieves all CSS classes currently set on provided component
      static boolean isSClassSet​(org.zkoss.zk.ui.HtmlBasedComponent component, java.lang.String... expectedClasses)
      Checks if provided component has all specified CSS classes set
      static boolean isSClassUnset​(org.zkoss.zk.ui.HtmlBasedComponent component, java.lang.String... unexpectedClasses)
      Checks if provided component none of specified CSS classes set.
      protected static org.mockito.stubbing.Stubber mockAttributes​(java.util.function.BiFunction<java.lang.String,​java.lang.Object,​java.lang.Object> mock, java.util.function.Supplier<java.util.Enumeration<java.lang.String>> keysSupplier)  
      static void mockAuResponseProcessing​(AuResponseProcessorRegistry processors)
      Informs zk Execution on how to deal with AuResponse
      static void mockBeanLookup​(BeanLookup lookup)
      Mocks beans that are accessible by SpringUtil.
      static void mockBeanLookup​(BeanLookupFactory lookup)
      Mocks beans that are accessible by SpringUtil.
      static void mockHttpRequestAttributes()
      Mocks http request, response and session.
      static void mockLabelLookup​(LabelLookup lookup)
      Mocks labels that are accessible by Labels.
      static void mockLabelLookup​(LabelLookupFactory lookup)
      Mocks labels that are accessible by Labels.
      static WidgetInstanceManager mockWidgetInstanceManager()  
      static WidgetInstanceManager mockWidgetInstanceManager​(TypedSettingsMap widgetSettings)  
      static DefaultWidgetModel mockWidgetModel()  
      static void mockZkEnvironment()
      Mocks all necessary ThreadLocal variables necessary to deal with zk components, e.g.
      static void simulateEvent​(org.zkoss.zk.ui.Component component, java.lang.String eventName, java.lang.Object eventData)
      static void simulateEvent​(org.zkoss.zk.ui.Component component, org.zkoss.zk.ui.event.Event event)
      Simulates an Event at the specified Component.
      static void testSClassSet​(org.zkoss.zk.ui.HtmlBasedComponent component, java.lang.String... expectedClasses)
      Checks if provided component has all specified CSS classes set.
      static void testSClassUnset​(org.zkoss.zk.ui.HtmlBasedComponent component, java.lang.String... unexpectedClasses)
      Checks if provided component none of specified CSS classes set.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • mockLabelLookup

        public static void mockLabelLookup​(LabelLookupFactory lookup)
        Mocks labels that are accessible by Labels.
        Parameters:
        lookup - labels mock factory
      • mockLabelLookup

        public static void mockLabelLookup​(LabelLookup lookup)
        Mocks labels that are accessible by Labels.
        Parameters:
        lookup - labels mock
      • executeWithLabels

        public static void executeWithLabels​(java.util.Map<java.lang.String,​java.lang.String> labels,
                                             java.lang.Runnable runnable)
        Allows to mock ZK labels that are fetched by static methods like Labels.getLabel(String). It sets the labels for the time of test, then reverts them after finishing.
        Parameters:
        labels - map of labels to use
        runnable - code to run that will see the labels
      • mockBeanLookup

        public static void mockBeanLookup​(BeanLookupFactory lookup)
        Mocks beans that are accessible by SpringUtil.

        Notice that only one bean mock may be done for single mockZkEnvironment() call

        Parameters:
        lookup - beans mock factory
      • mockBeanLookup

        public static void mockBeanLookup​(BeanLookup lookup)
        Mocks beans that are accessible by SpringUtil.

        Notice that only one bean mock may be done for single mockZkEnvironment() call

        Parameters:
        lookup - beans mock
      • mockHttpRequestAttributes

        public static void mockHttpRequestAttributes()
        Mocks http request, response and session. Then it is stored in RequestContextHolder.
      • mockAttributes

        protected static org.mockito.stubbing.Stubber mockAttributes​(java.util.function.BiFunction<java.lang.String,​java.lang.Object,​java.lang.Object> mock,
                                                                     java.util.function.Supplier<java.util.Enumeration<java.lang.String>> keysSupplier)
      • createAttributesAnswer

        protected static org.mockito.stubbing.Answer<java.lang.Object> createAttributesAnswer​(java.util.Set<java.lang.String> keys,
                                                                                              java.util.function.BiFunction<java.lang.String,​java.lang.Object,​java.lang.Object> mock)
      • mockZkEnvironment

        public static void mockZkEnvironment()
        Mocks all necessary ThreadLocal variables necessary to deal with zk components, e.g. Execution, Desktop, WebApp, WebApplicationContext, etc.
      • mockAuResponseProcessing

        public static void mockAuResponseProcessing​(AuResponseProcessorRegistry processors)
        Informs zk Execution on how to deal with AuResponse
        Parameters:
        processors - registry of processors for AuResponse
      • createEchoProcessor

        public static AuResponseProcessor<org.zkoss.zk.au.out.AuEcho> createEchoProcessor()
        Creates a processor for AuEcho requests. This processor notifies all listeners for this event type immediately.
        Returns:
        processor to be registered in AuResponseProcessorRegistry
      • find

        public static <C extends org.zkoss.zk.ui.Component> java.util.Optional<C> find​(org.zkoss.zk.ui.Component parent,
                                                                                       java.lang.Class<? extends C> childClass)
        Looks for a component of provided type (incl. parent). Method performs a deep-search in whole DOM tree of provided parent.
        Type Parameters:
        C - type of component
        Parameters:
        parent - component which child is to be found
        childClass - class of a child to be found
        Returns:
        result of search
      • find

        public static <C extends org.zkoss.zk.ui.Component> java.util.Optional<C> find​(org.zkoss.zk.ui.Component parent,
                                                                                       java.util.function.Predicate<org.zkoss.zk.ui.Component> test)
        Looks for a component that meets of provided requirement (incl. parent). Method performs a deep-search in whole DOM tree of provided parent.
        Type Parameters:
        C - expected type of component
        Parameters:
        parent - component which child is to be found
        test - requirement for component
        Returns:
        result of search
      • findAll

        public static <C extends org.zkoss.zk.ui.Component> java.util.stream.Stream<C> findAll​(org.zkoss.zk.ui.Component parent,
                                                                                               java.lang.Class<? extends C> childClass)
        Looks for a all components of provided type (incl. parent). Method performs a deep-search in whole DOM tree of provided parent.
        Type Parameters:
        C - type of component
        Parameters:
        parent - component which child is to be found
        childClass - class of a child to be found
        Returns:
        result of search
      • findAll

        public static <C extends org.zkoss.zk.ui.Component> java.util.stream.Stream<C> findAll​(org.zkoss.zk.ui.Component parent,
                                                                                               java.util.function.Predicate<org.zkoss.zk.ui.Component> test)
        Looks for a all components that meets of provided requirement (incl. parent). Method performs a deep-search in whole DOM tree of provided parent.
        Type Parameters:
        C - expected type of component
        Parameters:
        parent - component which child is to be found
        test - requirement for component
        Returns:
        result of search
      • findChild

        public static <C extends org.zkoss.zk.ui.Component> java.util.Optional<C> findChild​(org.zkoss.zk.ui.Component parent,
                                                                                            java.lang.Class<? extends C> childClass)
        Looks for a child of provided type. Method performs a deep-search in whole DOM tree of provided parent.
        Type Parameters:
        C - type of component
        Parameters:
        parent - component which child is to be found
        childClass - class of a child to be found
        Returns:
        result of search
      • findChild

        public static <C extends org.zkoss.zk.ui.Component> java.util.Optional<C> findChild​(org.zkoss.zk.ui.Component parent,
                                                                                            java.util.function.Predicate<org.zkoss.zk.ui.Component> test)
        Looks for a child that meets of provided requirement. Method performs a deep-search in whole DOM tree of provided parent.
        Type Parameters:
        C - expected type of child
        Parameters:
        parent - component which child is to be found
        test - requirement for child
        Returns:
        result of search
      • findAllChildren

        public static <C extends org.zkoss.zk.ui.Component> java.util.stream.Stream<C> findAllChildren​(org.zkoss.zk.ui.Component parent,
                                                                                                       java.lang.Class<? extends C> childClass)
        Looks for all children of provided type. Method performs a deep-search in whole DOM tree of provided parent.
        Type Parameters:
        C - type of component
        Parameters:
        parent - component which child is to be found
        childClass - class of a child to be found
        Returns:
        result of search
      • findAllChildren

        public static <C extends org.zkoss.zk.ui.Component> java.util.stream.Stream<C> findAllChildren​(org.zkoss.zk.ui.Component parent,
                                                                                                       java.util.function.Predicate<org.zkoss.zk.ui.Component> test)
        Looks for all children that meets of provided requirement. Method performs a deep-search in whole DOM tree of provided parent.
        Type Parameters:
        C - type of component
        Parameters:
        parent - component which child is to be found
        test - requirement for child
        Returns:
        result of search
      • getSClasses

        public static java.lang.String[] getSClasses​(org.zkoss.zk.ui.HtmlBasedComponent component)
        Retrieves all CSS classes currently set on provided component
        Parameters:
        component - component, which CSS classes are to be retrieved
        Returns:
        true if all classes are set
      • getSClasses

        public static java.lang.String[] getSClasses​(java.lang.String sclass)
        Retrieves all CSS classes from provided sclass attribute of component
        Parameters:
        sclass - component's sclass attribute value
        Returns:
        arrays of separate CSS classes included in provieded sclass attribute
      • isSClassSet

        public static boolean isSClassSet​(org.zkoss.zk.ui.HtmlBasedComponent component,
                                          java.lang.String... expectedClasses)
        Checks if provided component has all specified CSS classes set
        Parameters:
        component - component, which CSS classes are to be tested
        expectedClasses - names of required classes
        Returns:
        true if all classes are set
      • testSClassSet

        public static void testSClassSet​(org.zkoss.zk.ui.HtmlBasedComponent component,
                                         java.lang.String... expectedClasses)
        Checks if provided component has all specified CSS classes set. If not - assertion fails.
        Parameters:
        component - component, which CSS classes are to be tested
        expectedClasses - names of required classes
      • testSClassUnset

        public static void testSClassUnset​(org.zkoss.zk.ui.HtmlBasedComponent component,
                                           java.lang.String... unexpectedClasses)
        Checks if provided component none of specified CSS classes set. If not - assertion fails.
        Parameters:
        component - component, which CSS classes are to be tested
        unexpectedClasses - names of classes that should not appear
      • isSClassUnset

        public static boolean isSClassUnset​(org.zkoss.zk.ui.HtmlBasedComponent component,
                                            java.lang.String... unexpectedClasses)
        Checks if provided component none of specified CSS classes set.
        Parameters:
        component - component, which CSS classes are to be tested
        unexpectedClasses - names of classes that should not appear
        Returns:
        true if none of provided classes are set
      • simulateEvent

        public static void simulateEvent​(org.zkoss.zk.ui.Component component,
                                         java.lang.String eventName,
                                         java.lang.Object eventData)
      • simulateEvent

        public static void simulateEvent​(org.zkoss.zk.ui.Component component,
                                         org.zkoss.zk.ui.event.Event event)
        Simulates an Event at the specified Component. Note that it only calls the EventListeners registered for the given event name and does NOT do zk internal state changes. That means, if you want to test the correct execution of an EventListener, you have do make these state changes manually before calling this method, e.g. if you simulate an onSelect event on a combobox, its selectedItem field is NOT being updated.