Author: sletellier Date: 2011-11-24 14:31:55 +0100 (Thu, 24 Nov 2011) New Revision: 114 Url: http://forge.codelutin.com/repositories/revision/echobase/114 Log: - Add topia-service-replication - Add service test structure (AbstractEchoBaseServiceTest) - Add tests for embeddedApplication service - Creating visitor to create mocked entities (MokeEntityVisitorCreator) - Copy start files (.sh and .bat) in embedded application - Replicate referenciel and voyage selected creating embedded application Added: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/EntitiesUtil.java trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/data/ trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/data/DataUtil.java trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/references/ trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/references/ReferencesUtil.java trunk/echobase-entities/src/test/java/fr/ifremer/echobase/TestHelper.java trunk/echobase-services/src/main/resources/embedded/startEchobase.bat trunk/echobase-services/src/main/resources/embedded/startEchobase.sh trunk/echobase-services/src/test/java/fr/ifremer/echobase/entities/ trunk/echobase-services/src/test/java/fr/ifremer/echobase/entities/MockEntityVisitorCreator.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AbstractEchoBaseServiceTest.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EmbeddedApplicationServiceTest.java trunk/echobase-services/src/test/resources/echobase.properties Removed: trunk/echobase-entities/src/test/java/fr/ifremer/echobase/entities/ Modified: trunk/echobase-entities/pom.xml trunk/echobase-entities/src/main/java/fr/ifremer/echobase/EchoBaseTopiaRootContextSupplierFactory.java trunk/echobase-entities/src/test/resources/echobase.properties trunk/echobase-services/pom.xml trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java trunk/echobase-ui/src/main/resources/echobase.properties trunk/pom.xml Modified: trunk/echobase-entities/pom.xml =================================================================== --- trunk/echobase-entities/pom.xml 2011-11-23 19:08:01 UTC (rev 113) +++ trunk/echobase-entities/pom.xml 2011-11-24 13:31:55 UTC (rev 114) @@ -74,6 +74,11 @@ </dependency> <dependency> + <groupId>org.nuiton.topia</groupId> + <artifactId>topia-service-replication</artifactId> + </dependency> + + <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> @@ -127,6 +132,17 @@ <build> <plugins> + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <executions> + <execution> + <id>attach-test</id> + <goals> + <goal>test-jar</goal> + </goals> + </execution> + </executions> + </plugin> <plugin> <groupId>org.nuiton.eugene</groupId> @@ -196,6 +212,13 @@ </plugins> + <testResources> + <testResource> + <directory>${maven.src.dir}/test/resources</directory> + <filtering>true</filtering> + </testResource> + </testResources> + </build> </project> Modified: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/EchoBaseTopiaRootContextSupplierFactory.java =================================================================== --- trunk/echobase-entities/src/main/java/fr/ifremer/echobase/EchoBaseTopiaRootContextSupplierFactory.java 2011-11-23 19:08:01 UTC (rev 113) +++ trunk/echobase-entities/src/main/java/fr/ifremer/echobase/EchoBaseTopiaRootContextSupplierFactory.java 2011-11-24 13:31:55 UTC (rev 114) @@ -33,6 +33,8 @@ import org.nuiton.topia.TopiaContextFactory; import org.nuiton.topia.TopiaNotFoundException; import org.nuiton.topia.TopiaRuntimeException; +import org.nuiton.topia.replication.TopiaReplicationService; +import org.nuiton.topia.replication.TopiaReplicationServiceImpl; import java.io.File; import java.io.IOException; @@ -90,6 +92,10 @@ TopiaContextFactory.CONFIG_PERSISTENCE_CLASSES, EchoBaseDAOHelper.getImplementationClassesAsString()); + // add topiaReplicationService in the context + properties.setProperty(TopiaReplicationService.TOPIA_SERVICE_NAME, + TopiaReplicationServiceImpl.class.getName()); + TopiaContext rootContext; try { rootContext = TopiaContextFactory.getContext(properties); Added: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/EntitiesUtil.java =================================================================== --- trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/EntitiesUtil.java (rev 0) +++ trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/EntitiesUtil.java 2011-11-24 13:31:55 UTC (rev 114) @@ -0,0 +1,58 @@ +/* + * #%L + * EchoBase :: Entities + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.entities; + +import org.nuiton.topia.persistence.TopiaEntity; + +import java.util.ArrayList; +import java.util.List; + +/** + * Utility method for enities + * + * @author sletellier <letellier@codelutin.com> + * @since 0.2 + */ +public class EntitiesUtil { + + /** + * Used to get all contract of a package + * + * @param entitiesPackage package contening desired entities + * @return contracts contained in entitiesPackage + */ + public static EchoBaseEntityEnum[] getContractsOf(Package entitiesPackage) { + EchoBaseEntityEnum[] echoBaseEntityEnums = EchoBaseEntityEnum.values(); + List<EchoBaseEntityEnum> refClasses = new ArrayList<EchoBaseEntityEnum>(); + for (EchoBaseEntityEnum echoBaseEntityEnum : echoBaseEntityEnums) { + + // Get all entities in package fr.ifremer.echobase.entities.references + Class<? extends TopiaEntity> contract = echoBaseEntityEnum.getContract(); + if (contract.getPackage().equals(entitiesPackage)) { + refClasses.add(echoBaseEntityEnum); + } + } + return refClasses.toArray(new EchoBaseEntityEnum[refClasses.size()]); + } +} Added: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/data/DataUtil.java =================================================================== --- trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/data/DataUtil.java (rev 0) +++ trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/data/DataUtil.java 2011-11-24 13:31:55 UTC (rev 114) @@ -0,0 +1,45 @@ +/* + * #%L + * EchoBase :: Entities + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.entities.data; + +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; +import fr.ifremer.echobase.entities.EntitiesUtil; + +/** + * Data method utility + * + * @author sletellier <letellier@codelutin.com> + * @since 0.2 + */ +public class DataUtil { + + /** + * Used to get all contract of a data package + * + * @return contracts in package data + */ + public static EchoBaseEntityEnum[] getDataContracts() { + return EntitiesUtil.getContractsOf(DataUtil.class.getPackage()); + } +} Added: trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/references/ReferencesUtil.java =================================================================== --- trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/references/ReferencesUtil.java (rev 0) +++ trunk/echobase-entities/src/main/java/fr/ifremer/echobase/entities/references/ReferencesUtil.java 2011-11-24 13:31:55 UTC (rev 114) @@ -0,0 +1,45 @@ +/* + * #%L + * EchoBase :: Entities + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.entities.references; + +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; +import fr.ifremer.echobase.entities.EntitiesUtil; + +/** + * References method utility + * + * @author sletellier <letellier@codelutin.com> + * @since 0.2 + */ +public class ReferencesUtil { + + /** + * Used to get all contract of a referenciel package + * + * @return contracts in package referenciel + */ + public static EchoBaseEntityEnum[] getReferencielContracts() { + return EntitiesUtil.getContractsOf(ReferencesUtil.class.getPackage()); + } +} Added: trunk/echobase-entities/src/test/java/fr/ifremer/echobase/TestHelper.java =================================================================== --- trunk/echobase-entities/src/test/java/fr/ifremer/echobase/TestHelper.java (rev 0) +++ trunk/echobase-entities/src/test/java/fr/ifremer/echobase/TestHelper.java 2011-11-24 13:31:55 UTC (rev 114) @@ -0,0 +1,54 @@ +/* + * #%L + * EchoBase :: Entities + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase; + +import org.apache.commons.io.FileUtils; +import org.junit.Ignore; + +import java.io.File; + +/** + * Utility methods for tests + * + * @author sletellier <letellier@codelutin.com> + * @since 0.2 + */ +@Ignore +public class TestHelper { + + protected static File basedir; + + public static File getBasedir() { + if (basedir == null) { +// String tmp = System.getProperty("basedir"); +// if (tmp == null) { +// tmp = new File("").getAbsolutePath(); +// } +// basedir = new File(tmp); + basedir = FileUtils.getTempDirectory(); + } + return basedir; + } + +} Modified: trunk/echobase-entities/src/test/resources/echobase.properties =================================================================== --- trunk/echobase-entities/src/test/resources/echobase.properties 2011-11-23 19:08:01 UTC (rev 113) +++ trunk/echobase-entities/src/test/resources/echobase.properties 2011-11-24 13:31:55 UTC (rev 114) @@ -29,3 +29,6 @@ hibernate.connection.url=jdbc:h2:file:${data.directory}/echobase/h2data hibernate.connection.username=sa hibernate.connection.password=sa + +war.directory=${data.directory}/war +war.location=${war.directory}/echobase-ui-${project.version}.war \ No newline at end of file Modified: trunk/echobase-services/pom.xml =================================================================== --- trunk/echobase-services/pom.xml 2011-11-23 19:08:01 UTC (rev 113) +++ trunk/echobase-services/pom.xml 2011-11-24 13:31:55 UTC (rev 114) @@ -24,6 +24,14 @@ <version>${project.version}</version> </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>echobase-entities</artifactId> + <version>${project.version}</version> + <classifier>tests</classifier> + <scope>test</scope> + </dependency> + <!--dependency> <groupId>fr.ird</groupId> <artifactId>msaccess-importer</artifactId> @@ -97,6 +105,11 @@ <artifactId>topia-service-migration</artifactId> </dependency--> + <dependency> + <groupId>org.nuiton.topia</groupId> + <artifactId>topia-service-replication</artifactId> + </dependency> + <!-- test dependencies --> <dependency> @@ -153,6 +166,7 @@ </includes> <excludes> <exclude>echobase-config</exclude> + <exclude>embedded/*</exclude> </excludes> </resource> <resource> @@ -162,6 +176,11 @@ </includes> <filtering>true</filtering> </resource> + <resource> + <directory>src/main/resources/embedded</directory> + <targetPath>embedded</targetPath> + <filtering>true</filtering> + </resource> </resources> <plugins> Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java 2011-11-23 19:08:01 UTC (rev 113) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java 2011-11-24 13:31:55 UTC (rev 114) @@ -24,18 +24,21 @@ package fr.ifremer.echobase.services; import com.google.common.base.Supplier; -import com.google.common.collect.Lists; import fr.ifremer.echobase.EchoBaseTechnicalException; import fr.ifremer.echobase.EchoBaseTopiaRootContextSupplierFactory; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; import fr.ifremer.echobase.entities.EchoBaseUser; import fr.ifremer.echobase.entities.ExportQuery; -import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.entities.data.DataUtil; +import fr.ifremer.echobase.entities.references.ReferencesUtil; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; +import org.nuiton.topia.replication.TopiaReplicationService; +import org.nuiton.topia.replication.model.ReplicationModel; import org.nuiton.util.FileUtil; import org.nuiton.util.ZipUtil; @@ -44,12 +47,14 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.URISyntaxException; import java.util.List; /** * To create embedded application. * * @author tchemit <chemit@codelutin.com> + * @author sletellier <letellier@codelutin.com> * @since 0.1 */ public class EmbeddedApplicationService extends AbstractEchoBaseService { @@ -58,7 +63,27 @@ private static final Log log = LogFactory.getLog(EmbeddedApplicationService.class); + public static final String EMBEDDED_PATH = "/embedded/"; + public enum StartFiles { + BAT("startEchobase.sh"), + SH("startEchobase.bat"); + + protected String fileName; + + StartFiles(String fileName) { + this.fileName = fileName; + } + + public String getFileName() { + return fileName; + } + + public String getFilePath() { + return EMBEDDED_PATH + fileName; + } + } + public File createEmbeddedApplication(String fileName, String... voyageIds) { File warLocation = getConfiguration().getWarLocation(); @@ -82,6 +107,9 @@ // copy configuration file to / copyConfigurationFile(zipDirectory); + // copy start scripts + copyStartFiles(zipDirectory); + // copy war to / FileUtil.copy(warLocation, new File(zipDirectory, warLocation.getName())); @@ -107,6 +135,24 @@ return zipFile; } + protected void copyStartFiles(File zipDirectory) throws IOException, URISyntaxException { + + for (StartFiles startFile : StartFiles.values()) { + + InputStream inputStream = + getClass().getResourceAsStream(startFile.getFilePath()); + + File startFileDest = new File(zipDirectory, startFile.getFileName()); + OutputStream outputStream = new FileOutputStream(startFileDest); + try { + IOUtils.copy(inputStream, outputStream); + } finally { + IOUtils.closeQuietly(inputStream); + IOUtils.closeQuietly(outputStream); + } + } + } + protected void copyConfigurationFile(File zipDirectory) throws IOException { File configurationfile = new File(zipDirectory, "echobase.properties"); @@ -114,7 +160,8 @@ log.info("Copy configuration to " + configurationfile); } InputStream configInputStream = - getClass().getResourceAsStream("/embedded/echobase-embedded.properties"); + getClass().getResourceAsStream(EMBEDDED_PATH + "echobase-embedded.properties"); + OutputStream configOutputStream = new FileOutputStream(configurationfile); try { IOUtils.copy(configInputStream, configOutputStream); @@ -124,9 +171,17 @@ } } - protected void createH2Batabase(File zipDirectory, + protected TopiaContext createH2Batabase(File zipDirectory, String... voyageIds) throws IOException, TopiaException { + return createH2Batabase(zipDirectory, true, voyageIds); + } + + + // Use it only for tests + protected TopiaContext createH2Batabase(File zipDirectory, boolean closeRootContext, + String... voyageIds) throws IOException, TopiaException { + // create new h2 db Supplier<TopiaContext> topiaContextSupplier = new EchoBaseTopiaRootContextSupplierFactory().newEmbeddedDatabase(zipDirectory); @@ -180,22 +235,43 @@ } // replicate referentiel to new h2 db - //TODO + replicateReferentiel(topiaContext); if (voyageIds != null) { // replicate selected voyage to new h2 db - VoyageService voyageService = newService(VoyageService.class); - List<Voyage> voyages = Lists.newArrayList(); - for (String voyageId : voyageIds) { - Voyage voyage = voyageService.getVoyageById(voyageId); - voyages.add(voyage); - } - getTransaction().replicateEntities(topiaContext, voyages); + replicateVoyages(topiaContext, voyageIds); } } finally { - rootContext.closeContext(); + if (closeRootContext) { + rootContext.closeContext(); + } } + return rootContext; + } + protected void replicateVoyages(TopiaContext topiaContextDest, String... voyagesIds) throws TopiaException { + TopiaReplicationService service = getTransaction().getService(TopiaReplicationService.class); + ReplicationModel replicationModel = service.prepare(DataUtil.getDataContracts(), true, voyagesIds); + try { + service.doReplicate(replicationModel, topiaContextDest); + } catch (Exception eee) { + throw new EchoBaseTechnicalException( + "Failed to replicate referentiel : ", eee); + } } + protected void replicateReferentiel(TopiaContext topiaContextDest) throws TopiaException { + + // Get all referenciel contracts + EchoBaseEntityEnum[] referencielClass = ReferencesUtil.getReferencielContracts(); + TopiaReplicationService service = getTransaction().getService(TopiaReplicationService.class); + ReplicationModel replicationModel = service.prepareForAll(referencielClass); + try { + service.doReplicate(replicationModel, topiaContextDest); + } catch (Exception eee) { + throw new EchoBaseTechnicalException( + "Failed to replicate referentiel : ", eee); + } + } + } Added: trunk/echobase-services/src/main/resources/embedded/startEchobase.bat =================================================================== --- trunk/echobase-services/src/main/resources/embedded/startEchobase.bat (rev 0) +++ trunk/echobase-services/src/main/resources/embedded/startEchobase.bat 2011-11-24 13:31:55 UTC (rev 114) @@ -0,0 +1 @@ +java -jar ${project.build.finalName}.war \ No newline at end of file Added: trunk/echobase-services/src/main/resources/embedded/startEchobase.sh =================================================================== --- trunk/echobase-services/src/main/resources/embedded/startEchobase.sh (rev 0) +++ trunk/echobase-services/src/main/resources/embedded/startEchobase.sh 2011-11-24 13:31:55 UTC (rev 114) @@ -0,0 +1,11 @@ +#!/bin/sh +MX=512M +cd `dirname $0` +if [ -n "$JAVA_HOME" ]; then + $JAVA_HOME/bin/java -Xmx$MX -Xms$MX -jar ${project.build.finalName}.war $* +else + java -Xmx$MX -Xms$MX -jar ${project.build.finalName}.war $* +fi +cd "$OLDPWD" + + Added: trunk/echobase-services/src/test/java/fr/ifremer/echobase/entities/MockEntityVisitorCreator.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/entities/MockEntityVisitorCreator.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/entities/MockEntityVisitorCreator.java 2011-11-24 13:31:55 UTC (rev 114) @@ -0,0 +1,229 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.entities; + +import fr.ifremer.echobase.EchoBaseTechnicalException; +import fr.ifremer.echobase.entities.data.Cell; +import org.apache.commons.beanutils.BeanUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.persistence.EntityVisitor; +import org.nuiton.topia.persistence.TopiaDAO; +import org.nuiton.topia.persistence.TopiaEntity; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; + +/** + * Visitor to create mocked entities + * + * @author sletellier <letellier@codelutin.com> + * @since 0.2 + */ +public class MockEntityVisitorCreator implements EntityVisitor { + + /** Logger. */ + private static final Log log = LogFactory.getLog(MockEntityVisitorCreator.class); + + protected TopiaContext transaction; + + private static final int NB_ASSOCIATIONS = 2; + + public MockEntityVisitorCreator(TopiaContext transaction) { + this.transaction = transaction; + } + + public enum DefaultSimpleValues { + DATE(Date.class, new Date()), + STRING(String.class, "string"), + INTEGER(int.class, 1), + FLOAT(float.class, 1f), + LONG(long.class, 1l); + + protected Class<?> type; + protected Object defaultValue; + + DefaultSimpleValues(Class<?> type, Object defaultValue) { + this.type = type; + this.defaultValue = defaultValue; + } + + protected Class<?> getType() { + return type; + } + + public Object getDefaultValue() { + return defaultValue; + } + + public static <E> E getDefaultValue(Class<E> type) { + DefaultSimpleValues defaultSimpleValues = getDefaultSimpleValues(type); + return (E) defaultSimpleValues.getDefaultValue(); + } + + protected static <E> DefaultSimpleValues getDefaultSimpleValues(Class<E> type) { + for (DefaultSimpleValues values : values()) { + if (type.isAssignableFrom(values.getType())) { + return values; + } + } + throw new IllegalArgumentException("No default value found for type : " + type); + } + } + + @Override + public void start(TopiaEntity topiaEntity) { + // Do nothing + } + + @Override + public void end(TopiaEntity topiaEntity) { + // Do nothing + } + + @Override + public final void visit(TopiaEntity entity, + String propertyName, + Class<?> type, + Object value) { + + if (TopiaEntity.class.isAssignableFrom(type)) { + + // chargement d'une composition + visitComposition(entity, propertyName, (Class<? extends TopiaEntity>) type); + + } else { + + // visite d'une propriété simple + visitSimpleProperty(entity, propertyName, type); + } + } + + @Override + public void visit(TopiaEntity entity, + String propertyName, + Class<?> collectionType, + Class<?> type, + Object value) { + + if (!TopiaEntity.class.isAssignableFrom(type)) { + + // ce n'est pas une association (ce cas ne devrait jamais arrivé) + return; + } + + visitAssociation(entity, propertyName, (Class<? extends TopiaEntity>) type); + } + + @Override + public final void visit(TopiaEntity entity, + String propertyName, + Class<?> collectionType, + Class<?> type, + int index, + Object value) { + // Never append + } + + @Override + public void clear() { + // Do nothing + } + + protected void visitSimpleProperty(TopiaEntity entity, + String propertyName, + Class<?> type) { + + Object defaultValue = DefaultSimpleValues.getDefaultValue(type); + setProperty(entity, propertyName, defaultValue); + } + + protected void visitComposition(TopiaEntity entity, + String propertyName, + Class<? extends TopiaEntity> type) { + + // Do not continue for parent of Cell entities + if (Cell.class.isAssignableFrom(entity.getClass()) && Cell.class.equals(type)) { + return; + } + + TopiaEntity value = createEntity(type); + setProperty(entity, propertyName, value); + } + + protected void visitAssociation(TopiaEntity entity, + String propertyName, + Class<? extends TopiaEntity> type) { + + Collection<TopiaEntity> values = new ArrayList<TopiaEntity>(); + for (int i=0;i<NB_ASSOCIATIONS;i++) { + TopiaEntity value = createEntity(type); + values.add(value); + } + setProperty(entity, propertyName, values); + } + + protected void setProperty(TopiaEntity entity, + String propertyName, + Object value) { + try { + BeanUtils.setProperty(entity, propertyName, value); + } catch (Exception eee) { + throw new RuntimeException("Failed to set property '" + propertyName + + "' with value '" + value + + "' for entity : " + entity, eee); + } + } + + public <T extends TopiaEntity> T createEntity(Class<T> type) { + + TopiaDAO<T> dao = getDAO(type); + try { + + // Creating new entity + T created = dao.create(); + try { + // Visit created entity + created.accept(this); + } catch (TopiaException eee) { + throw new EchoBaseTechnicalException("Failed to visit composition", eee); + } + return created; + } catch (TopiaException eee) { + throw new RuntimeException("Failed to create entity type '" + type.getName() + "'", eee); + } + } + + protected <T extends TopiaEntity, D extends TopiaDAO<? super T>> D getDAO(Class<T> type) { + + try { + return EchoBaseDAOHelper.getDAO(transaction, type); + } catch (TopiaException eee) { + throw new RuntimeException("Failed to get dao for type '" + type.getName() + "'", eee); + } + } +} Added: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AbstractEchoBaseServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AbstractEchoBaseServiceTest.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AbstractEchoBaseServiceTest.java 2011-11-24 13:31:55 UTC (rev 114) @@ -0,0 +1,116 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services; + +import com.google.common.base.Supplier; +import com.google.common.collect.Lists; +import fr.ifremer.echobase.EchoBaseConfiguration; +import fr.ifremer.echobase.EchoBaseTopiaRootContextSupplierFactory; +import fr.ifremer.echobase.TestHelper; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; +import fr.ifremer.echobase.entities.MockEntityVisitorCreator; +import fr.ifremer.echobase.entities.meta.DbMeta; +import junit.framework.Assert; +import org.junit.After; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.persistence.TopiaEntity; + +import java.io.File; +import java.util.List; +import java.util.Locale; + +/** + * Common methods (manage transaction, services...) to have on EchoBase tests. + * + * @author sletellier <letellier@codelutin.com> + * @since 0.2 + */ +public abstract class AbstractEchoBaseServiceTest extends Assert { + + protected TopiaContext transaction; + protected EchoBaseConfiguration config; + protected EchoBaseServiceFactory serviceFactory; + protected DbMeta dbMeta; + + @After + public void after() throws TopiaException { + if (transaction != null && !transaction.isClosed()) { + transaction.rollbackTransaction(); + transaction.closeContext(); + } + } + + public TopiaContext getTransaction() throws Exception { + if (transaction == null || transaction.isClosed()) { + EchoBaseTopiaRootContextSupplierFactory factory = + new EchoBaseTopiaRootContextSupplierFactory(); + File basedir = TestHelper.getBasedir(); + File target = new File(basedir, "/target"); + Supplier<TopiaContext> topiaContextSupplier = factory.newEmbeddedDatabase(target); + TopiaContext rootContext = topiaContextSupplier.get(); + transaction = rootContext.beginTransaction(); + } + return transaction; + } + + public EchoBaseServiceFactory getServiceFactory() { + if (serviceFactory == null) { + serviceFactory = new EchoBaseServiceFactory(); + } + return serviceFactory; + } + + public EchoBaseConfiguration getConfig() { + if (config == null) { + config = new EchoBaseConfiguration(); + } + return config; + } + + public DbMeta getDbMeta() { + if (dbMeta == null) { + List<EchoBaseEntityEnum> entityEnums = + Lists.newArrayList(EchoBaseEntityEnum.values()); + dbMeta = new DbMeta(entityEnums.toArray(new EchoBaseEntityEnum[entityEnums.size()])); + } + return dbMeta; + } + + protected <E extends EchoBaseService> E getService(Class<E> clazz) throws Exception { + EchoBaseServiceContext serviceContext = EchoBaseServiceContextImpl.newContext( + Locale.getDefault(), + getTransaction(), + getConfig(), + getDbMeta(), + new EchoBaseServiceFactory() + ); + return getServiceFactory().newService(clazz, serviceContext); + } + + public <T extends TopiaEntity> T createMockedEntity(Class<T> type) throws Exception { + MockEntityVisitorCreator visitor = new MockEntityVisitorCreator(getTransaction()); + return visitor.createEntity(type); + } +} Added: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EmbeddedApplicationServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EmbeddedApplicationServiceTest.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EmbeddedApplicationServiceTest.java 2011-11-24 13:31:55 UTC (rev 114) @@ -0,0 +1,97 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services; + +import fr.ifremer.echobase.TestHelper; +import fr.ifremer.echobase.entities.EchoBaseDAOHelper; +import fr.ifremer.echobase.entities.MockEntityVisitorCreator; +import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.entities.data.VoyageDAO; +import org.junit.Test; +import org.nuiton.topia.TopiaContext; + +import java.io.File; + +/** + * Test all fuctionnality of {@link EmbeddedApplicationService} + * + * @author sletellier <letellier@codelutin.com> + * @since 0.2 + */ +public class EmbeddedApplicationServiceTest extends AbstractEchoBaseServiceTest { + + public static final String TEST_ECHOBASE_EMBEDDED = "test-echobase-embedded"; + + public EmbeddedApplicationService getEmbeddedApplicationService() throws Exception { + return getService(EmbeddedApplicationService.class); + } + + @Test + public void testCreateEmbeddedApplication() throws Exception { + File embeddedApplication = getEmbeddedApplicationService().createEmbeddedApplication(TEST_ECHOBASE_EMBEDDED); + assertTrue(embeddedApplication.exists()); + } + + @Test + public void testCreateVoyageMock() throws Exception { + Voyage mockedVoyage = createMockedEntity(Voyage.class); + assertNotNull(mockedVoyage); + } + + @Test + public void testReplicateVoyageEntity() throws Exception { + + // creating mocked voyage + Voyage voyageMocked = createMockedEntity(Voyage.class); + + // commit + getTransaction().commitTransaction(); + + // create new H2 dataBase in target/voyageId + String voyageId = voyageMocked.getTopiaId(); + File testBaseDir = new File(TestHelper.getBasedir(), voyageId); + TopiaContext rootContextDest = getEmbeddedApplicationService().createH2Batabase(testBaseDir, false, voyageId); + + // verify that replicated voyage is in new base + TopiaContext transactionDest = null; + try { + transactionDest = rootContextDest.beginTransaction(); + VoyageDAO voyageDAO = EchoBaseDAOHelper.getVoyageDAO(transactionDest); + Object stringDefaultValue = MockEntityVisitorCreator.DefaultSimpleValues.STRING.getDefaultValue(); + Voyage voyageReplicated = voyageDAO.findByVoyageName((String) stringDefaultValue); + + // assert voyage is in new base + assertNotNull(voyageReplicated); + + // and same ids + assertEquals(voyageMocked, voyageReplicated); + + // TODO sletellier 20111124 : try to test deph equality between entities + } finally { + if (transactionDest != null) { + transactionDest.closeContext(); + } + } + } +} Added: trunk/echobase-services/src/test/resources/echobase.properties =================================================================== --- trunk/echobase-services/src/test/resources/echobase.properties (rev 0) +++ trunk/echobase-services/src/test/resources/echobase.properties 2011-11-24 13:31:55 UTC (rev 114) @@ -0,0 +1,24 @@ +### +# #%L +# EchoBase :: Services +# +# $Id$ +# $HeadURL$ +# %% +# Copyright (C) 2011 Ifremer, Codelutin +# %% +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# #L% +### +data.directory=${java.io.tmpdir} \ No newline at end of file Modified: trunk/echobase-ui/src/main/resources/echobase.properties =================================================================== --- trunk/echobase-ui/src/main/resources/echobase.properties 2011-11-23 19:08:01 UTC (rev 113) +++ trunk/echobase-ui/src/main/resources/echobase.properties 2011-11-24 13:31:55 UTC (rev 114) @@ -24,6 +24,7 @@ project.version=${project.version} project.site.url=${project.url} +topia.service.replication=org.nuiton.topia.replication.TopiaReplicationServiceImpl hibernate.hbm2ddl.auto=none hibernate.show_sql=false Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2011-11-23 19:08:01 UTC (rev 113) +++ trunk/pom.xml 2011-11-24 13:31:55 UTC (rev 114) @@ -226,6 +226,13 @@ </dependency> <dependency> + <groupId>org.nuiton.topia</groupId> + <artifactId>topia-service-replication</artifactId> + <version>${topiaVersion}</version> + <scope>compile</scope> + </dependency> + + <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernateVersion}</version>