-
-
Save antonio/1476074 to your computer and use it in GitHub Desktop.
Problem spying cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Test Code: | |
package es.juntadeandalucia.icms.server.cache; | |
import static org.mockito.Matchers.any; | |
import static org.mockito.Mockito.spy; | |
import static org.mockito.Mockito.verify; | |
import static org.mockito.internal.verification.VerificationModeFactory.atLeastOnce; | |
import static org.mockito.internal.verification.VerificationModeFactory.times; | |
import javax.servlet.FilterChain; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import junit.framework.Assert; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.mockito.ArgumentMatcher; | |
import es.juntadeandalucia.icms.ICMSClientFactory; | |
import es.juntadeandalucia.icms.ICMSCollectionProvider; | |
import es.juntadeandalucia.icms.ICMSQueryClient; | |
import es.juntadeandalucia.icms.Util; | |
import es.juntadeandalucia.icms.exceptions.ICMSException; | |
import es.juntadeandalucia.icms.server.plugin.PluginHandler; | |
public class TestCache { | |
private ICMSCollectionProvider icmsProvider; | |
@Before | |
public final void setUp() { | |
Util.initServer(); | |
try { | |
icmsProvider = PluginHandler.getProviderFromIdCollection(Util.ID_COLECCION_EJEMPLO); | |
} catch (final ICMSException e) { | |
try { | |
PluginHandler.refresh(); | |
} catch (final ICMSException e1) { | |
e1.printStackTrace(); | |
} | |
Assert.fail(); | |
} | |
} | |
/** | |
* Primer test para ver si es Cacheable el otro se encuentra en el paquete | |
* de la caché. | |
*/ | |
@Test | |
public final void testIsCacheable() { | |
Assert.assertTrue(icmsProvider.getProviderMetadata().isCacheable()); | |
} | |
/** | |
* Segundo test para ver si es cacheable (colección) | |
* Se configura para no ser cacheable (en tiempo de ejecución) | |
* Se espía que no se llame a ningún método de la caché (al menos al que | |
* recupera objetos | |
*/ | |
@Test | |
public final void testCollectionNotCacheable() throws Exception { | |
// TODO | |
// Probamos que no se llama al método blockingCache.isKeyInCache() --> | |
// (no tira de caché) | |
icmsProvider.getProviderMetadata().setCacheable(false); | |
CacheFilter spy = spy(CacheFilter.getCurrentInstance()); | |
CacheFilter.setCurrentInstance(spy); // Get the spy instance to work | |
final ICMSQueryClient client = ICMSClientFactory.INSTANCE.getICMSQueryClient(Util.URL_TEST_ICMS); | |
client.executeQuery(Util.ID_COLECCION_EJEMPLO, null); | |
verify(spy, times(0)).isKeyInCache(any(String.class)); | |
// Probamos que si cambiamos la colección para que tire de caché, --> se | |
// llama al menos una vez. | |
icmsProvider.getProviderMetadata().setCacheable(true); | |
client.executeQuery(Util.ID_COLECCION_EJEMPLO, null); | |
verify(spy, atLeastOnce()).isKeyInCache(any(String.class)); | |
} | |
/** | |
* Mockito matcher implementation for HttpServletRequest objects | |
* | |
* @author Antonio Santos <[email protected]> | |
*/ | |
class AnyHttpServletRequest extends ArgumentMatcher<HttpServletRequest> { | |
@Override | |
public boolean matches(final Object list) { | |
return true; | |
} | |
} | |
/** | |
* Mockito matcher implementation for HttpServletResponse objects | |
* | |
* @author Antonio Santos <[email protected]> | |
*/ | |
class AnyHttpServletResponse extends ArgumentMatcher<HttpServletResponse> { | |
@Override | |
public boolean matches(final Object list) { | |
return true; | |
} | |
} | |
/** | |
* Mockito matcher implementation for FilterChain objects | |
* | |
* @author Antonio Santos <[email protected]> | |
*/ | |
class AnyFilterChain extends ArgumentMatcher<FilterChain> { | |
@Override | |
public boolean matches(final Object list) { | |
return true; | |
} | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see junit.framework.TestCase#tearDown() | |
*/ | |
@After | |
public void tearDown() throws Exception { | |
Util.reset(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment