Author: fdesbois Date: 2011-04-17 00:10:21 +0200 (Sun, 17 Apr 2011) New Revision: 2268 Url: http://nuiton.org/repositories/revision/topia/2268 Log: - Add test with service - Add svn:keywords and license header Added: trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/LibraryException.java trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/service/ trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/service/BookService.java trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/service/BookServiceImpl.java trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/service/ trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/service/BookServiceImplTest.java Modified: trunk/topia-tutorial/LICENSE.txt trunk/topia-tutorial/README.txt trunk/topia-tutorial/changelog.txt trunk/topia-tutorial/pom.xml trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/LibraryConfig.java trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/model/AuthorImpl.java trunk/topia-tutorial/src/main/resources/library-config.properties trunk/topia-tutorial/src/main/resources/log4j.properties trunk/topia-tutorial/src/main/xmi/library.properties trunk/topia-tutorial/src/main/xmi/library.zargo trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/StartTest.java Property changes on: trunk/topia-tutorial/LICENSE.txt ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Property changes on: trunk/topia-tutorial/README.txt ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Property changes on: trunk/topia-tutorial/changelog.txt ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Property changes on: trunk/topia-tutorial/pom.xml ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/LibraryConfig.java =================================================================== --- trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/LibraryConfig.java 2011-04-16 22:05:35 UTC (rev 2267) +++ trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/LibraryConfig.java 2011-04-16 22:10:21 UTC (rev 2268) @@ -1,5 +1,28 @@ +/* + * #%L + * ToPIA :: Tutorial Library + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program 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 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ package org.nuiton.topia.tutorial.library; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.nuiton.i18n.I18n; import org.nuiton.i18n.init.DefaultI18nInitializer; import org.nuiton.i18n.init.I18nInitializer; @@ -19,6 +42,8 @@ */ public class LibraryConfig { + private static final Log log = LogFactory.getLog(LibraryConfig.class); + protected static final String CONFIG_FILENAME = "library-config.properties"; protected ApplicationConfig config; @@ -43,6 +68,10 @@ // Initialize I18n to display Topia log messages I18nInitializer i18nInitializer = new DefaultI18nInitializer("library-bundle"); I18n.init(i18nInitializer, getLocale()); + + if (log.isDebugEnabled()) { + config.printConfig(); + } } /** Property changes on: trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/LibraryConfig.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/LibraryException.java =================================================================== --- trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/LibraryException.java (rev 0) +++ trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/LibraryException.java 2011-04-16 22:10:21 UTC (rev 2268) @@ -0,0 +1,48 @@ +/* + * #%L + * ToPIA :: Tutorial Library + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program 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 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.topia.tutorial.library; + +/** + * Created: 16/04/11 + * + * @author fdesbois <desbois@codelutin.com> + * $Id$ + */ +public class LibraryException extends RuntimeException { + + public LibraryException() { + super(); + } + + public LibraryException(String message) { + super(message); + } + + public LibraryException(String message, Throwable cause) { + super(message, cause); + } + + public LibraryException(Throwable cause) { + super(cause); + } + +} Property changes on: trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/LibraryException.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/model/AuthorImpl.java =================================================================== --- trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/model/AuthorImpl.java 2011-04-16 22:05:35 UTC (rev 2267) +++ trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/model/AuthorImpl.java 2011-04-16 22:10:21 UTC (rev 2268) @@ -1,3 +1,24 @@ +/* + * #%L + * ToPIA :: Tutorial Library + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program 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 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ package org.nuiton.topia.tutorial.library.model; /** Property changes on: trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/model/AuthorImpl.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/service/BookService.java =================================================================== --- trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/service/BookService.java (rev 0) +++ trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/service/BookService.java 2011-04-16 22:10:21 UTC (rev 2268) @@ -0,0 +1,36 @@ +/* + * #%L + * ToPIA :: Tutorial Library + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program 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 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.topia.tutorial.library.service; + +import org.nuiton.topia.tutorial.library.model.Book; + +/** + * Created: 16/04/11 + * + * @author fdesbois <desbois@codelutin.com> + * $Id$ + */ +public interface BookService { + + Book saveBook(Book book); + +} Property changes on: trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/service/BookService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/service/BookServiceImpl.java =================================================================== --- trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/service/BookServiceImpl.java (rev 0) +++ trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/service/BookServiceImpl.java 2011-04-16 22:10:21 UTC (rev 2268) @@ -0,0 +1,168 @@ +/* + * #%L + * ToPIA :: Tutorial Library + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program 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 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.topia.tutorial.library.service; + +import org.apache.commons.lang.StringUtils; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaContextFactory; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.TopiaNotFoundException; +import org.nuiton.topia.tutorial.library.LibraryConfig; +import org.nuiton.topia.tutorial.library.LibraryException; +import org.nuiton.topia.tutorial.library.model.Book; +import org.nuiton.topia.tutorial.library.model.BookDAO; +import org.nuiton.topia.tutorial.library.model.LibraryDAOHelper; + +/** + * Created: 16/04/11 + * + * @author fdesbois <desbois@codelutin.com> + * $Id$ + */ +public class BookServiceImpl implements BookService { + + protected LibraryConfig config; + + protected TopiaContext rootContext; + + /** + * New BookServiceImpl with configuration injection. + * + * @param config Configuration that contains application properties needed + * to retrieve the TopiaContext root. + */ + public BookServiceImpl(LibraryConfig config) { + this.config = config; + } + + /** + * Get root TopiaContext using config properties. + * + * @return the TopiaContext root + * @throws TopiaNotFoundException if no TopiaContext can't be found + */ + protected TopiaContext getRootContext() throws TopiaNotFoundException { + if (rootContext == null) { + rootContext = TopiaContextFactory.getContext(config.getProperties()); + } + return rootContext; + } + + /** + * Begin a new transaction with application exception encapsulation on + * errors. + * + * @return a new TopiaContext + * @throws LibraryException for any errors during beginTransaction + * @see TopiaContext#beginTransaction() + */ + protected TopiaContext beginTransaction() throws LibraryException { + TopiaContext transaction; + try { + transaction = getRootContext().beginTransaction(); + } catch (TopiaException ex) { + throw new LibraryException("Error on start", ex); + } + return transaction; + } + + + /** + * Close the {@code transaction} with application exception encapsulation + * on errors. + * + * @param transaction TopiaContext to close + * @throws LibraryException for any errors during closeContext + * @see TopiaContext#closeContext() + */ + protected void closeTransaction(TopiaContext transaction) throws LibraryException { + if (transaction != null) { + try { + transaction.closeContext(); + } catch (TopiaException ex) { + throw new LibraryException("Error on close", ex); + } + } + } + + /** + * Treate {@code caught} exception and rollback {@code transaction} if + * necessary. + * + * @param caught Exception to treate + * @param transaction TopiaContext to rollback if necessary + * @throws LibraryException encapsulate all errors + */ + protected void onException(Throwable caught, TopiaContext transaction) throws LibraryException { + + if (transaction != null) { + + // Rollback exception + try { + transaction.rollbackTransaction(); + } catch (TopiaException ex) { + throw new LibraryException("Error on rollback", ex); + } + } + + // Escape LibraryException + if (caught instanceof LibraryException) { + throw (LibraryException) caught; + + } else { + // Encapsulate caught exception + throw new LibraryException("Unexpected error", caught); + } + } + + @Override + public Book saveBook(Book book) { + + Book result = null; + + // Start transaction + TopiaContext transaction = beginTransaction(); + try { + BookDAO dao = LibraryDAOHelper.getBookDAO(transaction); + + // Business, create or update the book entity + if (StringUtils.isBlank(book.getTopiaId())) { + result = dao.create(book); + } else { + result = dao.update(book); + } + + transaction.commitTransaction(); + + } catch (Exception ex) { + + // Treate all errors + onException(ex, transaction); + + // Always close the transaction + } finally { + closeTransaction(transaction); + } + return result; + } + +} Property changes on: trunk/topia-tutorial/src/main/java/org/nuiton/topia/tutorial/library/service/BookServiceImpl.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/topia-tutorial/src/main/resources/library-config.properties =================================================================== --- trunk/topia-tutorial/src/main/resources/library-config.properties 2011-04-16 22:05:35 UTC (rev 2267) +++ trunk/topia-tutorial/src/main/resources/library-config.properties 2011-04-16 22:10:21 UTC (rev 2268) @@ -1,3 +1,24 @@ +### +# #%L +# ToPIA :: Tutorial Library +# %% +# Copyright (C) 2011 CodeLutin +# %% +# This program 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 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 Lesser Public License for more details. +# +# You should have received a copy of the GNU General Lesser Public +# License along with this program. If not, see +# <http://www.gnu.org/licenses/lgpl-3.0.html>. +# #L% +### # Default url to database, this path will delete the database on mvn clean # You can override this configuration using an other properties file in user directory # For linux system : ~/.local/library/library-config.properties Property changes on: trunk/topia-tutorial/src/main/resources/library-config.properties ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/topia-tutorial/src/main/resources/log4j.properties =================================================================== --- trunk/topia-tutorial/src/main/resources/log4j.properties 2011-04-16 22:05:35 UTC (rev 2267) +++ trunk/topia-tutorial/src/main/resources/log4j.properties 2011-04-16 22:10:21 UTC (rev 2268) @@ -2,8 +2,8 @@ # #%L # ToPIA :: Persistence # -# $Id: log4j.properties 2245 2011-04-14 12:47:09Z tchemit $ -# $HeadURL: http://svn.nuiton.org/svn/topia/trunk/topia-persistence/src/test/resources/l... $ +# $Id$ +# $HeadURL$ # %% # Copyright (C) 2004 - 2010 CodeLutin # %% @@ -31,4 +31,6 @@ log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) %M - %m%n # package level +log4j.logger.org.hibernate=INFO log4j.logger.org.nuiton.topia=INFO +log4j.logger.org.nuiton.topia.tutorial=DEBUG Property changes on: trunk/topia-tutorial/src/main/resources/log4j.properties ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/topia-tutorial/src/main/xmi/library.properties =================================================================== --- trunk/topia-tutorial/src/main/xmi/library.properties 2011-04-16 22:05:35 UTC (rev 2267) +++ trunk/topia-tutorial/src/main/xmi/library.properties 2011-04-16 22:10:21 UTC (rev 2268) @@ -1,3 +1,24 @@ +### +# #%L +# ToPIA :: Tutorial Library +# %% +# Copyright (C) 2011 CodeLutin +# %% +# This program 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 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 Lesser Public License for more details. +# +# You should have received a copy of the GNU General Lesser Public +# License along with this program. If not, see +# <http://www.gnu.org/licenses/lgpl-3.0.html>. +# #L% +### # Properties file used by EUGene generator. # # It will contains tagvalue and stereotype definitions. Property changes on: trunk/topia-tutorial/src/main/xmi/library.properties ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Property changes on: trunk/topia-tutorial/src/main/xmi/library.zargo ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/StartTest.java =================================================================== --- trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/StartTest.java 2011-04-16 22:05:35 UTC (rev 2267) +++ trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/StartTest.java 2011-04-16 22:10:21 UTC (rev 2268) @@ -1,3 +1,24 @@ +/* + * #%L + * ToPIA :: Tutorial Library + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program 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 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ package org.nuiton.topia.tutorial.library; import org.junit.After; Property changes on: trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/StartTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/service/BookServiceImplTest.java =================================================================== --- trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/service/BookServiceImplTest.java (rev 0) +++ trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/service/BookServiceImplTest.java 2011-04-16 22:10:21 UTC (rev 2268) @@ -0,0 +1,87 @@ +/* + * #%L + * ToPIA :: Tutorial Library + * %% + * Copyright (C) 2011 CodeLutin + * %% + * This program 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 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.topia.tutorial.library.service; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.tutorial.library.LibraryConfig; +import org.nuiton.topia.tutorial.library.model.Book; +import org.nuiton.topia.tutorial.library.model.BookImpl; +import org.nuiton.util.ArgumentsParserException; + +/** + * Created: 16/04/11 + * + * @author fdesbois <desbois@codelutin.com> + * $Id$ + */ +public class BookServiceImplTest { + + private static final Log log = LogFactory.getLog(BookServiceImplTest.class); + + protected BookServiceImpl bookService; + + @Before + public void setUp() throws ArgumentsParserException { + + log.debug("setUp configuration for test"); + + LibraryConfig config = new LibraryConfig(); + + // Instantiate new service + bookService = new BookServiceImpl(config); + } + + @After + public void tearDown() throws TopiaException { + + // Close rootContext and drop the database (Work only with H2 database) + TopiaContext rootContext = bookService.getRootContext(); + rootContext.clear(true); + } + + @Test + public void testSaveBook() { + + // ---- PREPARE DATA ---- // + + Book book = new BookImpl(); + book.setName("Topia for dummies"); + book.setIsbn("T12"); + + // ---- EXECUTE ---- // + + Book bookSaved = bookService.saveBook(book); + + // ---- CHECK DATA ---- // + + Assert.assertNotNull(bookSaved); + Assert.assertNotNull(bookSaved.getTopiaId()); + } + +} Property changes on: trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/service/BookServiceImplTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL