package org.leguan.refactoring.steps;

import org.junit.Assert;
import org.junit.Test;
import org.leguan.language.java.JavaField;
import org.leguan.language.java.JavaMethod;
import org.leguan.language.java.JavaSource;
import org.leguan.language.java.delegates.JavaContainerItem;
import org.leguan.refactoring.api.RefactoringException;
import org.leguan.refactoring.selection.CompilationUnitSelectionStrategy;
import org.mockito.Mockito;

public class AbstractRefactoringStepTest {

  @Test
  public void setAndGetSelectionStrategy () {
    CompilationUnitSelectionStrategy selectionStrategy = Mockito.mock(CompilationUnitSelectionStrategy.class);
    AbstractJavaRefactoringStep refactoringStep = createStep();
    refactoringStep.setSelectionStrategy(selectionStrategy);
    Assert.assertEquals (selectionStrategy, refactoringStep.getSelectionStrategy());

  }

  @Test(expected = IllegalStateException.class)
  public void getJavaSourceNoContainerItemSet () {
    AbstractJavaRefactoringStep refactoringStep = createStep();
    refactoringStep.getJavaSource();
  }

  @Test(expected = IllegalStateException.class)
  public void getJavaMethodNoContainerItemSet () {
    AbstractJavaRefactoringStep refactoringStep = createStep();
    refactoringStep.getJavaMethod();
  }

  @Test(expected = IllegalStateException.class)
  public void getJavaFieldNoContainerItemSet () {
    AbstractJavaRefactoringStep refactoringStep = createStep();
    refactoringStep.getJavaField();
  }

  @Test
  public void getJavaMethodWithJavaField () {
    AbstractJavaRefactoringStep refactoringStep = createStep();
    JavaField javaField = Mockito.mock(JavaField.class);
    refactoringStep.setContainerItem(javaField);
    Assert.assertNull (refactoringStep.getJavaMethod());
  }

  @Test
  public void getJavaMethodWithJavaMethod () {
    AbstractJavaRefactoringStep refactoringStep = createStep();
    JavaMethod javaMethod = Mockito.mock(JavaMethod.class);
    refactoringStep.setContainerItem(javaMethod);
    Assert.assertEquals (javaMethod, refactoringStep.getJavaMethod());
  }

  @Test
  public void getJavaFieldWithJavaField () {
    AbstractJavaRefactoringStep refactoringStep = createStep();
    JavaField javaField = Mockito.mock(JavaField.class);
    refactoringStep.setContainerItem(javaField);

    Assert.assertEquals (javaField, refactoringStep.getJavaField());
  }

  @Test
  public void getJavaFieldWithJavaMethod () {
    AbstractJavaRefactoringStep refactoringStep = createStep();
    JavaMethod javaMethod = Mockito.mock(JavaMethod.class);
    refactoringStep.setContainerItem(javaMethod);
    Assert.assertNull (refactoringStep.getJavaField());

  }

  @Test
  public void getJavaSource () {

    JavaSource javaSource = Mockito.mock(JavaSource.class);
    JavaContainerItem javaContainerItem = Mockito.mock(JavaContainerItem.class);
    Mockito.when(javaContainerItem.getJavaSource()).thenReturn(javaSource);

    AbstractJavaRefactoringStep refactoringStep = createStep();
    refactoringStep.setContainerItem(javaContainerItem);
    Assert.assertEquals (javaSource, refactoringStep.getJavaSource());
  }


  private AbstractJavaRefactoringStep createStep () {
    return new AbstractJavaRefactoringStep() {
      @Override
      public void configure() {

      }

      @Override
      public void rewriteCompilationUnit(JavaSource javaSource) throws RefactoringException {

      }
    };

  }
}
