Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Add test cache container to unit tests.
Browse files Browse the repository at this point in the history
This is a memory-only Infinispan cache container.
  • Loading branch information
Carlos A. Munoz committed Sep 9, 2014
1 parent 7f0908d commit 11fdd52
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 48 deletions.
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;

import com.google.common.annotations.VisibleForTesting;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down Expand Up @@ -95,7 +96,13 @@ public class TranslationStateCacheImpl implements TranslationStateCache {
private CacheContainer cacheContainer;

@In
private ServiceLocator serviceLocator;
private TextFlowDAO textFlowDAO;

@In
private TextFlowTargetDAO textFlowTargetDAO;

@In
private DocumentDAO documentDAO;

// constructor for Seam
public TranslationStateCacheImpl() {
Expand All @@ -104,6 +111,7 @@ public TranslationStateCacheImpl() {
}

// Constructor for testing
@VisibleForTesting
public TranslationStateCacheImpl(
CacheLoader<DocumentLocaleKey, WordStatistic> documentStatisticLoader,
CacheLoader<DocumentLocaleKey, DocumentStatus> docStatsLoader,
Expand Down Expand Up @@ -177,7 +185,7 @@ public void textFlowStateUpdated(TextFlowTargetStateEvent event) {
WordStatistic stats = documentStatisticCache.get(key);

if (stats != null) {
HTextFlow textFlow = getTextFlowDAO().findById(
HTextFlow textFlow = textFlowDAO.findById(
event.getTextFlowId());

stats.decrement(event.getPreviousState(),
Expand All @@ -199,16 +207,16 @@ private void updateDocStatusCache(DocumentLocaleKey key,
DocumentStatus documentStatus = docStatusCache.get(key);
if(documentStatus != null) {
HTextFlowTarget target =
getTextFlowTargetDAO().findById(updatedTargetId, false);
updateDocumentStatus(getDocumentDAO(), documentStatus,
textFlowTargetDAO.findById(updatedTargetId, false);
updateDocumentStatus(documentDAO, documentStatus,
key.getDocumentId(), target);
}
}

private Boolean loadTargetValidation(Long textFlowTargetId,
ValidationId validationId) {
HTextFlowTarget tft =
getTextFlowTargetDAO().findById(textFlowTargetId, false);
textFlowTargetDAO.findById(textFlowTargetId, false);
if (tft != null) {
ValidationAction action =
ValidationFactoryProvider.getFactoryInstance()
Expand All @@ -221,18 +229,6 @@ private Boolean loadTargetValidation(Long textFlowTargetId,
return null;
}

DocumentDAO getDocumentDAO() {
return serviceLocator.getInstance(DocumentDAO.class);
}

TextFlowTargetDAO getTextFlowTargetDAO() {
return serviceLocator.getInstance(TextFlowTargetDAO.class);
}

TextFlowDAO getTextFlowDAO() {
return serviceLocator.getInstance(TextFlowDAO.class);
}

private static class DocumentStatisticLoader extends
CacheLoader<DocumentLocaleKey, WordStatistic> {

Expand Down
@@ -0,0 +1,54 @@
/*
* Copyright 2014, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.cache;

import lombok.Delegate;
import org.infinispan.lifecycle.Lifecycle;
import org.infinispan.manager.CacheContainer;
import org.infinispan.manager.DefaultCacheManager;

/**
* A cache container to be used in tests. Everything is kept in memory in a
* real Infinispan cache which can be restarted / reset at will.
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
public class InfinispanTestCacheContainer implements CacheContainer {

@Delegate(types = CacheContainer.class, excludes = Lifecycle.class)
private DefaultCacheManager delegate;

public InfinispanTestCacheContainer() {
}

@Override
public void start() {
stop();
this.delegate = new DefaultCacheManager();
this.delegate.start();
}

@Override
public void stop() {
if( delegate != null) {
delegate.stop();
}
}
}
Expand Up @@ -5,6 +5,7 @@

import javax.ws.rs.core.Response.Status;

import org.infinispan.manager.CacheContainer;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.seam.security.management.JpaIdentityStore;
import org.mockito.Mock;
Expand All @@ -13,6 +14,7 @@
import org.slf4j.LoggerFactory;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.zanata.cache.InfinispanTestCacheContainer;
import org.zanata.model.HAccount;
import org.zanata.model.HPerson;
import org.zanata.rest.StringSet;
Expand All @@ -33,13 +35,16 @@ public class ResourceServiceRestTest extends ResourceTranslationServiceRestTest
@Mock
private ZanataIdentity mockIdentity;

private CacheContainer cacheContainer = new InfinispanTestCacheContainer();

@Override
protected void prepareResources() {
MockitoAnnotations.initMocks(this);

cacheContainer.start();
SeamAutowire seamAutowire = getSeamAutowire();
seamAutowire.use("session", getSession()).use("entityManager", getEm())
.use("identity", mockIdentity).useImpl(LocaleServiceImpl.class)
.use("cacheContainer", cacheContainer)
.useImpl(CopyTransServiceImpl.class)
.useImpl(DocumentServiceImpl.class)
.useImpl(VersionStateCacheImpl.class);
Expand Down
Expand Up @@ -11,6 +11,7 @@
import org.dbunit.operation.DatabaseOperation;
import org.fedorahosted.tennera.jgettext.HeaderFields;
import org.hamcrest.Matchers;
import org.infinispan.manager.CacheContainer;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.seam.security.Identity;
import org.jboss.seam.security.management.JpaIdentityStore;
Expand All @@ -22,6 +23,7 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.ZanataRestTest;
import org.zanata.cache.InfinispanTestCacheContainer;
import org.zanata.common.ContentState;
import org.zanata.common.ContentType;
import org.zanata.common.LocaleId;
Expand Down Expand Up @@ -110,6 +112,7 @@ public class TranslationResourceRestTest extends ZanataRestTest {
@Mock
private TranslationWorkspaceManager transWorspaceManager;

CacheContainer cacheContainer = new InfinispanTestCacheContainer();
ISourceDocResource sourceDocResource;
ITranslatedDocResource transResource;

Expand Down Expand Up @@ -138,10 +141,12 @@ protected void prepareDBUnitOperations() {
@SuppressWarnings("serial")
@Override
protected void prepareResources() {
cacheContainer.start();
SeamAutowire seamAutowire = getSeamAutowire();
seamAutowire.use("session", getSession()).use("identity", mockIdentity)
.use("translationWorkspaceManager", transWorspaceManager)
.use("entityManager", super.getEm())
.use("cacheContainer", cacheContainer)
.useImpl(CopyTransServiceImpl.class)
.useImpl(TranslationServiceImpl.class)
.useImpl(LocaleServiceImpl.class)
Expand Down
Expand Up @@ -4,13 +4,15 @@
import javax.xml.bind.JAXBException;

import org.hamcrest.Matchers;
import org.infinispan.manager.CacheContainer;
import org.jboss.resteasy.client.ClientResponse;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.zanata.cache.InfinispanTestCacheContainer;
import org.zanata.common.LocaleId;
import org.zanata.rest.StringSet;
import org.zanata.rest.dto.DTOUtil;
Expand All @@ -36,6 +38,7 @@ public class TranslationServiceRestTest extends
new TranslationsResourceTestObjectFactory();
private ResourceTestObjectFactory resourceTestFactory =
new ResourceTestObjectFactory();
private CacheContainer cacheContainer = new InfinispanTestCacheContainer();

@DataProvider(name = "TranslationTestData")
public Object[][] getTestData() {
Expand All @@ -53,9 +56,11 @@ public Object[][] getTestData() {
@Override
protected void prepareResources() {
MockitoAnnotations.initMocks(this);
cacheContainer.start();
SeamAutowire seamAutowire = getSeamAutowire();
seamAutowire.use("entityManager", getEm()).use("session", getSession())
.use("identity", Mockito.mock(ZanataIdentity.class))
.use("cacheContainer", cacheContainer)
.useImpl(LocaleServiceImpl.class)
.useImpl(CopyTransServiceImpl.class)
.useImpl(DocumentServiceImpl.class)
Expand Down
Expand Up @@ -27,13 +27,17 @@
import org.dbunit.operation.DatabaseOperation;
import org.hibernate.search.impl.FullTextSessionImpl;
import org.hibernate.search.jpa.Search;
import org.infinispan.manager.CacheContainer;
import org.infinispan.manager.DefaultCacheManager;
import org.jboss.seam.security.management.JpaIdentityStore;
import org.mockito.Mockito;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.zanata.SlowTest;
import org.zanata.ZanataDbunitJpaTest;
import org.zanata.cache.InfinispanTestCacheContainer;
import org.zanata.common.ContentState;
import org.zanata.common.ContentType;
import org.zanata.common.EntityStatus;
Expand Down Expand Up @@ -78,6 +82,7 @@
@Test(groups = { "business-tests" })
public class CopyTransServiceImplTest extends ZanataDbunitJpaTest {
private SeamAutowire seam = SeamAutowire.instance();
private CacheContainer cacheContainer = new InfinispanTestCacheContainer();

@Override
protected void prepareDBUnitOperations() {
Expand All @@ -97,10 +102,12 @@ protected void prepareDBUnitOperations() {

@BeforeMethod
protected void beforeMethod() throws Exception {
cacheContainer.start();
seam.reset()
.use("entityManager", Search.getFullTextEntityManager(getEm()))
.use("entityManagerFactory", getEmf())
.use("session", new FullTextSessionImpl(getSession()))
.use("cacheContainer", cacheContainer)
.use(JpaIdentityStore.AUTHENTICATED_USER,
seam.autowire(AccountDAO.class).getByUsername("demo"))
.useImpl(LocaleServiceImpl.class)
Expand Down
Expand Up @@ -31,12 +31,14 @@
import java.util.Map;

import org.dbunit.operation.DatabaseOperation;
import org.infinispan.manager.CacheContainer;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.ZanataDbunitJpaTest;
import org.zanata.cache.InfinispanTestCacheContainer;
import org.zanata.common.EntityStatus;
import org.zanata.dao.DocumentDAO;
import org.zanata.dao.LocaleDAO;
Expand Down Expand Up @@ -85,6 +87,8 @@ public class CopyVersionServiceImplTest extends ZanataDbunitJpaTest {

private CopyVersionServiceImpl service;

private CacheContainer cacheContainer = new InfinispanTestCacheContainer();

@Override
protected void prepareDBUnitOperations() {
beforeTestOperations.add(new DataSetOperation(
Expand Down Expand Up @@ -113,6 +117,7 @@ protected void beforeMethod() throws Exception {
textFlowTargetDAO = new TextFlowTargetDAO(getSession());
textFlowDAO = new TextFlowDAO(getSession());
rawDocumentDAO = new RawDocumentDAO((getSession()));
cacheContainer.start();

service = seam.reset()
.use("projectIterationDAO",
Expand All @@ -124,6 +129,7 @@ protected void beforeMethod() throws Exception {
.use("session", getSession())
.use("identity", identity)
.use("filePersistService", fileSystemPersistService)
.use("cacheContainer", cacheContainer)
.useImpl(VersionStateCacheImpl.class)
.useImpl(AsyncTaskManagerServiceImpl.class)
.ignoreNonResolvable()
Expand Down
Expand Up @@ -31,19 +31,18 @@
import java.util.Arrays;

import org.dbunit.operation.DatabaseOperation;
import org.infinispan.manager.CacheContainer;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.ZanataDbunitJpaTest;
import org.zanata.cache.InfinispanTestCacheContainer;
import org.zanata.rest.dto.stats.ContainerTranslationStatistics;
import org.zanata.rest.dto.stats.TranslationStatistics;
import org.zanata.seam.SeamAutowire;
import org.zanata.service.ValidationService;

import net.sf.ehcache.CacheManager;

/**
* @author Carlos Munoz <a
* href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
Expand All @@ -54,7 +53,7 @@ public class StatisticsServiceImplTest extends ZanataDbunitJpaTest {

@Mock
private ValidationService validationServiceImpl;
private CacheManager cacheManager;
private CacheContainer cacheContainer = new InfinispanTestCacheContainer();

@Override
protected void prepareDBUnitOperations() {
Expand All @@ -78,21 +77,16 @@ protected void prepareDBUnitOperations() {
@BeforeMethod
public void initializeSeam() {
MockitoAnnotations.initMocks(this);
cacheContainer.start();
// @formatter:off
seam.reset()
seam.reset()
.use("entityManager", getEm())
.use("session", getSession())
.use("validationServiceImpl", validationServiceImpl)
.use("cacheContainer", cacheContainer)
.useImpl(TranslationStateCacheImpl.class)
.ignoreNonResolvable();
// @formatter:on
cacheManager = CacheManager.create();
cacheManager.removalAll();
}

@AfterMethod
public void after() {
cacheManager.shutdown();
// @formatter:on
}

@Test
Expand Down

0 comments on commit 11fdd52

Please sign in to comment.