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

Commit

Permalink
refactor entity delete logic in test
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Sep 9, 2014
1 parent 57d3731 commit b8db681
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 50 deletions.
96 changes: 96 additions & 0 deletions zanata-model/src/main/java/org/zanata/util/ZanataEntities.java
@@ -0,0 +1,96 @@
/*
* 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.util;

import java.util.List;

import org.zanata.model.Activity;
import org.zanata.model.HAccount;
import org.zanata.model.HAccountActivationKey;
import org.zanata.model.HDocument;
import org.zanata.model.HDocumentHistory;
import org.zanata.model.HGlossaryEntry;
import org.zanata.model.HGlossaryTerm;
import org.zanata.model.HIterationGroup;
import org.zanata.model.HLocale;
import org.zanata.model.HLocaleMember;
import org.zanata.model.HPerson;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.model.HTermComment;
import org.zanata.model.HTextFlow;
import org.zanata.model.HTextFlowHistory;
import org.zanata.model.HTextFlowTarget;
import org.zanata.model.HTextFlowTargetHistory;
import org.zanata.model.HTextFlowTargetReviewComment;
import org.zanata.model.po.HPoTargetHeader;
import org.zanata.model.security.HCredentials;
import org.zanata.model.tm.TransMemory;
import org.zanata.model.tm.TransMemoryUnit;
import org.zanata.model.tm.TransMemoryUnitVariant;

import com.google.common.collect.ImmutableList;

/**
* @author Patrick Huang <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
public class ZanataEntities {
private static List<Class> entitiesForDelete;

/**
* @return a list of entity classes that is in order for deletion i.e. won't
* violate referential constraint.
*/
public static List<Class> entitiesInOrderForDelete() {
if (entitiesForDelete != null) {
return entitiesForDelete;
}
ImmutableList.Builder<Class> builder = ImmutableList.builder();
// TMX
builder.add(TransMemoryUnitVariant.class, TransMemoryUnit.class,
TransMemory.class);
builder.add(Activity.class);
// glossary
builder.add(HTermComment.class, HGlossaryTerm.class,
HGlossaryEntry.class);
// text flows and targets
builder.add(HPoTargetHeader.class, HTextFlowTargetHistory.class,
HTextFlowTargetReviewComment.class,
HTextFlowTarget.class,
HTextFlowHistory.class,
HTextFlow.class);
// documents
builder.add(HDocumentHistory.class, HDocument.class);
// locales
builder.add(HLocaleMember.class, HLocale.class);
builder.add(HIterationGroup.class);
// project
builder.add(HProjectIteration.class, HProject.class);
// account
builder.add(HAccountActivationKey.class, HCredentials.class,
HPerson.class,
HAccount.class);

entitiesForDelete = builder.build();
return entitiesForDelete;
}
}
26 changes: 4 additions & 22 deletions zanata-war/src/test/java/org/zanata/ZanataJpaTest.java
Expand Up @@ -45,9 +45,12 @@
import org.zanata.model.tm.TransMemoryUnit;
import org.zanata.model.tm.TransMemoryUnitVariant;
import org.zanata.testng.TestMethodListener;
import org.zanata.util.ZanataEntities;
import com.github.huangp.entityunit.entity.EntityCleaner;
import com.google.common.collect.Lists;

import static com.github.huangp.entityunit.entity.EntityCleaner.deleteAll;

@Listeners(TestMethodListener.class)
// single threaded because of ehcache (perhaps other reasons too)
@Test(singleThreaded = true)
Expand Down Expand Up @@ -152,27 +155,6 @@ protected void clearHibernateSecondLevelCache() {
}

protected void deleteAllTables() {
EntityCleaner.deleteAll(getEm(), Lists.<Class>newArrayList(
TransMemoryUnitVariant.class, TransMemoryUnit.class,
TransMemory.class,
Activity.class,
// glossary
HTermComment.class, HGlossaryTerm.class,
HGlossaryEntry.class,
// tex flows and targets
HPoTargetHeader.class, HTextFlowTargetHistory.class,
HTextFlowTargetReviewComment.class,
HTextFlowTarget.class, HTextFlow.class,
// documents
HDocumentHistory.class, HDocument.class,
// locales
HLocaleMember.class, HLocale.class,
// version group
HIterationGroup.class,
// project
HProjectIteration.class, HProject.class,
// account
HAccountActivationKey.class, HCredentials.class, HPerson.class,
HAccount.class));
deleteAll(getEm(), ZanataEntities.entitiesInOrderForDelete());
}
}
@@ -1,5 +1,6 @@
package org.zanata.service.impl;

import static com.github.huangp.entityunit.entity.EntityCleaner.deleteAll;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -40,6 +41,7 @@
import org.junit.Test;
import org.junit.rules.ExternalResource;
import org.junit.rules.TestRule;
import org.zanata.PerformanceProfiling;
import org.zanata.SlowTest;
import org.zanata.common.ContentState;
import org.zanata.common.LocaleId;
Expand Down Expand Up @@ -72,6 +74,7 @@
import org.zanata.seam.SeamAutowire;
import org.zanata.service.CopyTransService;
import org.zanata.service.SearchIndexManager;
import org.zanata.util.ZanataEntities;

import com.github.huangp.entityunit.entity.EntityCleaner;
import com.github.huangp.entityunit.entity.EntityMakerBuilder;
Expand Down Expand Up @@ -195,31 +198,6 @@ protected Session getSession() {
return (Session) em.getDelegate();
}

// TODO dup in FilterConstraintToQueryJpaTest. Should move to a common place
private void deleteData() {
EntityCleaner.deleteAll(getEm(), Lists.<Class> newArrayList(
TransMemoryUnitVariant.class, TransMemoryUnit.class,
TransMemory.class,
Activity.class,
// glossary
HTermComment.class, HGlossaryTerm.class,
HGlossaryEntry.class,
// tex flows and targets
HPoTargetHeader.class, HTextFlowTargetHistory.class,
HTextFlowTarget.class, HTextFlow.class,
// documents
HDocumentHistory.class, HDocument.class,
// locales
HLocaleMember.class, HLocale.class,
// version group
HIterationGroup.class,
// project
HProjectIteration.class, HProject.class,
// account
HAccountActivationKey.class, HCredentials.class, HPerson.class,
HAccount.class));
}

@Before
public void setUp() throws Exception {
// runLiquibase();
Expand All @@ -241,7 +219,7 @@ public void setUp() throws Exception {

copyTransService = seam.autowire(CopyTransServiceImpl.class);

deleteData();
deleteAll(getEm(), ZanataEntities.entitiesInOrderForDelete());

HLocale enUS = makeLocale(LocaleId.EN_US);
HLocale de = makeLocale(LocaleId.DE);
Expand Down Expand Up @@ -354,8 +332,9 @@ private static EntityMakerBuilder getEntityMakerBuilder() {
}

@Test
@Ignore()
@Ignore
@SlowTest
@PerformanceProfiling
public void testCopyTransForDocument() throws Exception {
HCopyTransOptions options =
new HCopyTransOptions(
Expand Down
Expand Up @@ -40,7 +40,7 @@
import com.jamonapi.MonitorFactory;

@Slf4j
@Test(groups = { "jpa-tests" })
//@Test(groups = { "jpa-tests" })
public class TranslationServiceImplJpaTest extends ZanataJpaTest {

static SeamAutowire seam = SeamAutowire.instance();
Expand Down

0 comments on commit b8db681

Please sign in to comment.