<commit_before>/*
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.jboss.forge.ui.test.impl;

import java.util.LinkedList;
import java.util.List;

import org.jboss.forge.addon.convert.ConverterFactory;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.result.NavigationResult;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.util.InputComponents;
import org.jboss.forge.addon.ui.wizard.UIWizard;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.ui.test.WizardListener;
import org.jboss.forge.ui.test.WizardState;
import org.jboss.forge.ui.test.WizardTester;

/**
 * This class eases the testing of Wizards
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * 
 */
public class WizardTesterImpl<W extends UIWizard> implements WizardTester<W>
{
   private final AddonRegistry addonRegistry;

   private final LinkedList<UIBuilderImpl> pages = new LinkedList<UIBuilderImpl>();

   private final UIContextImpl context;

   private WizardState state = WizardState.FIRST;

   WizardTesterImpl(Class<W> wizardClass, AddonRegistry addonRegistry, UIContextImpl contextImpl)
            throws Exception
   {
      this.addonRegistry = addonRegistry;
      this.context = contextImpl;
      pages.add(createBuilder(wizardClass));
   }

   @SuppressWarnings("unchecked")
   @Override
   public String next() throws Exception
   {
      if (!canFlipToNextPage())
      {
         throw new IllegalStateException("Wizard is already on the last page");
      }
      UIBuilderImpl currentBuilder = getCurrentBuilder();
      NavigationResult result = currentBuilder.getWizard().next(context);
      UIBuilderImpl nextBuilder = createBuilder((Class<W>) result.getNext());
      pages.add(nextBuilder);
      return result.getMessage();
   }

   @Override
   public void previous() throws Exception
   {
      if (!canFlipToPreviousPage())
      {
         throw new IllegalStateException("Wizard is already on the first page");
      }
      pages.poll();
   }

   @Override
   public boolean canFlipToNextPage()
   {
      UIBuilderImpl currentBuilder = getCurrentBuilder();
      try
      {
         return currentBuilder.getWizard().next(context) != null;
      }
      catch (Exception e)
      {
         throw new IllegalStateException(e);
      }
   }

   @Override
   public boolean canFlipToPreviousPage()
   {
      return pages.size() > 1;
   }

   @Override
   public boolean canFinish()
   {
      return getValidationErrors().isEmpty() && !canFlipToNextPage();
   }

   @Override
   public boolean isValid()
   {<commit_msg>Fixed minor bug in WizardTester<commit_after>/*
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.jboss.forge.ui.test.impl;

import java.util.LinkedList;
import java.util.List;

import org.jboss.forge.addon.convert.ConverterFactory;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.result.NavigationResult;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.util.InputComponents;
import org.jboss.forge.addon.ui.wizard.UIWizard;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.ui.test.WizardListener;
import org.jboss.forge.ui.test.WizardState;
import org.jboss.forge.ui.test.WizardTester;

/**
 * This class eases the testing of Wizards
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * 
 */
public class WizardTesterImpl<W extends UIWizard> implements WizardTester<W>
{
   private final AddonRegistry addonRegistry;

   private final LinkedList<UIBuilderImpl> pages = new LinkedList<UIBuilderImpl>();

   private final UIContextImpl context;

   private WizardState state = WizardState.FIRST;

   WizardTesterImpl(Class<W> wizardClass, AddonRegistry addonRegistry, UIContextImpl contextImpl)
            throws Exception
   {
      this.addonRegistry = addonRegistry;
      this.context = contextImpl;
      pages.add(createBuilder(wizardClass));
   }

   @SuppressWarnings("unchecked")
   @Override
   public String next() throws Exception
   {
      if (!canFlipToNextPage())
      {
         throw new IllegalStateException("Wizard is already on the last page");
      }
      UIBuilderImpl currentBuilder = getCurrentBuilder();
      NavigationResult result = currentBuilder.getWizard().next(context);
      UIBuilderImpl nextBuilder = createBuilder((Class<W>) result.getNext());
      pages.add(nextBuilder);
      return result.getMessage();
   }

   @Override
   public void previous() throws Exception
   {
      if (!canFlipToPreviousPage())
      {
         throw new IllegalStateException("Wizard is already on the first page");
      }
      pages.removeLast();
   }

   @Override
   public boolean canFlipToNextPage()
   {
      UIBuilderImpl currentBuilder = getCurrentBuilder();
      try
      {
         return currentBuilder.getWizard().next(context) != null;
      }
      catch (Exception e)
      {
         throw new IllegalStateException(e);
      }
   }

   @Override
   public boolean canFlipToPreviousPage()
   {
      return pages.size() > 1;
   }

   @Override
   public boolean canFinish()
   {
      return getValidationErrors().isEmpty() && !canFlipToNextPage();
   }

   @Override
   public boolean isValid()
   {
{'fraction_non_alphanumeric': 0.10638297872340426, 'fraction_numerical': 0.0032455824017309774, 'mean_word_length': 3.4058776806989672, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5714933', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1665, 'n_words': 360}