package ro.isdc.wro.maven.plugin.support;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;

import org.apache.maven.plugin.logging.Log;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import ro.isdc.wro.config.Context;
import ro.isdc.wro.manager.factory.BaseWroManagerFactory;
import ro.isdc.wro.manager.factory.WroManagerFactory;
import ro.isdc.wro.model.resource.Resource;
import ro.isdc.wro.model.resource.ResourceType;
import ro.isdc.wro.model.resource.locator.ClasspathUriLocator;


/**
 * @author Alex Objelean
 */
public class TestResourceChangeHandler {
  @Mock
  private BuildContextHolder buildContextHolder;
  @Mock
  private Log log;
  private WroManagerFactory managerFactory;
  private ResourceChangeHandler victim;

  @Before
  public void setUp() {
    Context.set(Context.standaloneContext());
    MockitoAnnotations.initMocks(this);
    managerFactory = new BaseWroManagerFactory();
    victim = ResourceChangeHandler.create(managerFactory, log);
  }

  @Test(expected = NullPointerException.class)
  public void cannotAcceptInvalidManagerFactory() {
    victim = ResourceChangeHandler.create(null, log);
  }

  @Test(expected = NullPointerException.class)
  public void cannotAcceptInvalidLog() {
    victim = ResourceChangeHandler.create(managerFactory, null);
  }

  @Test
  public void shouldNotBeIncrementalBuildByDefault() {
    assertEquals(false, victim.isIncrementalBuild());
  }

  @Test(expected = NullPointerException.class)
  public void cannotCheckNullResourceForChange() {
    assertEquals(false, victim.isResourceChanged(null));
  }

  @Test
  public void shouldConsiderInvalidResourceAsUnchanged() {
    assertEquals(false, victim.isResourceChanged(Resource.create("/1.js")));
  }

  @Test
  public void shoulDestroyBuildContextHolder() {
    victim.setBuildContextHolder(buildContextHolder);
    victim.destroy();
    verify(buildContextHolder).destroy();
  }

  @Test
  public void shouldIdentifyChangeAfterDestroyOrForgetIsInvoked() {
    final String resourceUri = ClasspathUriLocator.createUri(getClass().getName().replace(".", "/") + ".class");
    final Resource resource = Resource.create(resourceUri, ResourceType.JS);
    assertEquals(true, victim.isResourceChanged(resource));
    victim.remember(resource);
    assertEquals(false, victim.isResourceChanged(resource));
    victim.forget(resource);
    assertEquals(true, victim.isResourceChanged(resource));
    victim.remember(resource);
    assertEquals(false, victim.isResourceChanged(resource));
    victim.destroy();
    assertEquals(true, victim.isResourceChanged(resource));
  }

  @After
  public void tearDown() {
    Context.unset();
  }
}

@@ -122,16 +122,102 @@
 .verify;
+%0Aimport static org.mockito.MockitoAnnotations.initMocks;%0A%0Aimport java.util.Properties;
 %0A%0Aimport
@@ -354,47 +354,8 @@
 ock;
-%0Aimport org.mockito.MockitoAnnotations;
 %0A%0Aim
@@ -422,37 +422,44 @@
 factory.
-BaseWroManagerFactory
+standalone.StandaloneContext
 ;%0Aimport
@@ -463,32 +463,45 @@
 ort ro.isdc.wro.
+maven.plugin.
 manager.factory.
@@ -500,16 +500,28 @@
 factory.
+Configurable
 WroManag
@@ -856,16 +856,28 @@
 private 
+Configurable
 WroManag
@@ -973,24 +973,46 @@
 d setUp() %7B%0A
+    initMocks(this);%0A%0A
     Context.
@@ -1049,98 +1049,237 @@
 ));%0A
+%0A
     
-MockitoAnnotations.initMocks(this);%0A    managerFactory = new BaseWroManagerFactory(
+managerFactory = new ConfigurableWroManagerFactory() %7B%0A      @Override%0A      protected Properties createProperties() %7B%0A        return new Properties();%0A      %7D%0A    %7D;%0A    managerFactory.initialize(new StandaloneContext()
 );%0A
+%0A
     

fix for issue #985