INtroduce JUNit Support
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 13 Oct 2009 08:17:50 +0000 (08:17 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 13 Oct 2009 08:17:50 +0000 (08:17 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@3017 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

basic/pom.xml
basic/runtime/org.argeo.support.junit/.classpath [new file with mode: 0644]
basic/runtime/org.argeo.support.junit/.project [new file with mode: 0644]
basic/runtime/org.argeo.support.junit/.settings/org.eclipse.jdt.core.prefs [new file with mode: 0644]
basic/runtime/org.argeo.support.junit/.settings/org.maven.ide.eclipse.prefs [new file with mode: 0644]
basic/runtime/org.argeo.support.junit/pom.xml [new file with mode: 0644]
basic/runtime/org.argeo.support.junit/src/main/java/org/argeo/support/junit/AbstractSpringTestCase.java [new file with mode: 0644]
basic/runtime/pom.xml [new file with mode: 0644]
server/runtime/org.argeo.server.jxl/src/main/java/org/argeo/server/jxl/dao/JxlDaoSupport.java
server/runtime/org.argeo.server.jxl/src/test/java/org/argeo/server/jxl/dao/JxlDaoSupportTest.java

index 197b7f74481bdde3133bd9e3808634f6a74b4a40..e9f9d00e690624099b9289604a2dd58655f36284 100644 (file)
@@ -13,5 +13,6 @@
        <packaging>pom</packaging>
        <modules>
                <module>dep</module>
+               <module>runtime</module>
        </modules>
 </project>
\ No newline at end of file
diff --git a/basic/runtime/org.argeo.support.junit/.classpath b/basic/runtime/org.argeo.support.junit/.classpath
new file mode 100644 (file)
index 0000000..16f01e2
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+       <classpathentry kind="src" output="target/classes" path="src/main/java"/>
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+       <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
+       <classpathentry kind="output" path="target/classes"/>
+</classpath>
diff --git a/basic/runtime/org.argeo.support.junit/.project b/basic/runtime/org.argeo.support.junit/.project
new file mode 100644 (file)
index 0000000..9b9565d
--- /dev/null
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>org.argeo.support.junit</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>org.eclipse.jdt.core.javabuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.maven.ide.eclipse.maven2Builder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>org.maven.ide.eclipse.maven2Nature</nature>
+               <nature>org.eclipse.jdt.core.javanature</nature>
+       </natures>
+</projectDescription>
diff --git a/basic/runtime/org.argeo.support.junit/.settings/org.eclipse.jdt.core.prefs b/basic/runtime/org.argeo.support.junit/.settings/org.eclipse.jdt.core.prefs
new file mode 100644 (file)
index 0000000..383a4ec
--- /dev/null
@@ -0,0 +1,5 @@
+#Tue Oct 13 10:02:09 CEST 2009
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+org.eclipse.jdt.core.compiler.compliance=1.5
+org.eclipse.jdt.core.compiler.source=1.5
diff --git a/basic/runtime/org.argeo.support.junit/.settings/org.maven.ide.eclipse.prefs b/basic/runtime/org.argeo.support.junit/.settings/org.maven.ide.eclipse.prefs
new file mode 100644 (file)
index 0000000..721e27e
--- /dev/null
@@ -0,0 +1,9 @@
+#Tue Oct 13 10:01:59 CEST 2009
+activeProfiles=
+eclipse.preferences.version=1
+fullBuildGoals=process-test-resources
+includeModules=false
+resolveWorkspaceProjects=true
+resourceFilterGoals=process-resources resources\:testResources
+skipCompilerPlugin=true
+version=1
diff --git a/basic/runtime/org.argeo.support.junit/pom.xml b/basic/runtime/org.argeo.support.junit/pom.xml
new file mode 100644 (file)
index 0000000..dbf12ce
--- /dev/null
@@ -0,0 +1,60 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+       <modelVersion>4.0.0</modelVersion>
+       <parent>
+               <groupId>org.argeo.commons.basic</groupId>
+               <artifactId>runtime</artifactId>
+               <version>0.1.1-SNAPSHOT</version>
+               <relativePath>..</relativePath>
+       </parent>
+       <artifactId>org.argeo.support.junit</artifactId>
+       <name>Commons Support JUnit</name>
+       <build>
+               <plugins>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-compiler-plugin</artifactId>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-source-plugin</artifactId>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-jar-plugin</artifactId>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.apache.felix</groupId>
+                               <artifactId>maven-bundle-plugin</artifactId>
+                               <version>${version.maven-bundle-plugin}</version>
+                               <configuration>
+                                       <instructions>
+                                               <Export-Package>
+                                                       org.argeo.support.junit.*
+                                               </Export-Package>
+                                       </instructions>
+                               </configuration>
+                       </plugin>
+               </plugins>
+       </build>
+       <dependencies>
+               <dependency>
+                       <groupId>org.junit</groupId>
+                       <artifactId>com.springsource.junit</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.springframework</groupId>
+                       <artifactId>org.springframework.core</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.springframework</groupId>
+                       <artifactId>org.springframework.context</artifactId>
+               </dependency>
+
+               <!-- Logging -->
+               <dependency>
+                       <groupId>org.slf4j</groupId>
+                       <artifactId>com.springsource.slf4j.org.apache.commons.logging</artifactId>
+               </dependency>
+       </dependencies>
+</project>
diff --git a/basic/runtime/org.argeo.support.junit/src/main/java/org/argeo/support/junit/AbstractSpringTestCase.java b/basic/runtime/org.argeo.support.junit/src/main/java/org/argeo/support/junit/AbstractSpringTestCase.java
new file mode 100644 (file)
index 0000000..7971a8d
--- /dev/null
@@ -0,0 +1,93 @@
+package org.argeo.support.junit;
+
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.beans.factory.BeanFactoryUtils;
+import org.springframework.beans.factory.ListableBeanFactory;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+
+/** Helper for tests using a Spring application context. */
+public abstract class AbstractSpringTestCase extends TestCase {
+       protected final Log log = LogFactory.getLog(getClass());
+       private ConfigurableApplicationContext context;
+
+       /**
+        * Gets (and create if necessary) the application context to use. Default
+        * implementation uses a class path xml application context and calls
+        * {@link #getApplicationContextLocation()}.
+        */
+       protected ConfigurableApplicationContext getContext() {
+               if (context == null) {
+                       context = new ClassPathXmlApplicationContext(
+                                       getApplicationContextLocation());
+                       if (getIsStartContext())
+                               context.start();
+               }
+               return context;
+       }
+
+       /** Whether the context should be started after being created. */
+       protected Boolean getIsStartContext() {
+               return false;
+       }
+
+       /** Returns a bean from the underlying context */
+       @SuppressWarnings(value = { "unchecked" })
+       protected <T> T getBean(String beanId) {
+               return (T) getContext().getBean(beanId);
+       }
+
+       protected <T> T getBean(Class<? extends T> clss) {
+               T bean = loadSingleFromContext(getContext(), clss);
+               if (bean == null) {
+                       throw new RuntimeException("Cannot retrieve a unique bean of type "
+                                       + clss);
+               } else {
+                       return bean;
+               }
+       }
+
+       /**
+        * Th location of the application to load. The default implementation
+        * returns <i>applicationContext.xml</i> found in the same package as the
+        * test.
+        */
+       protected String getApplicationContextLocation() {
+               return inPackage("applicationContext.xml");
+       }
+
+       /**
+        * Prefixes the package of the class after converting the '.' to '/' in
+        * order to have a resource path.
+        */
+       protected String inPackage(String suffix) {
+               String prefix = getClass().getPackage().getName().replace('.', '/');
+               return prefix + '/' + suffix;
+       }
+
+       @SuppressWarnings(value = { "unchecked" })
+       protected <T> T loadSingleFromContext(ListableBeanFactory context,
+                       Class<T> clss) {
+               Map<String, T> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
+                               context, clss, false, false);
+               if (beans.size() == 1) {
+                       return beans.values().iterator().next();
+               } else if (beans.size() > 1) {
+                       if (log.isDebugEnabled()) {
+                               log
+                                               .debug(("Found more that on bean for type " + clss
+                                                               + ": " + beans.keySet()));
+                       }
+                       return null;
+               } else {
+                       return null;
+               }
+       }
+
+}
diff --git a/basic/runtime/pom.xml b/basic/runtime/pom.xml
new file mode 100644 (file)
index 0000000..058e0f5
--- /dev/null
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+       <modelVersion>4.0.0</modelVersion>
+       <parent>
+               <groupId>org.argeo.commons</groupId>
+               <version>0.1.1-SNAPSHOT</version>
+               <artifactId>basic</artifactId>
+               <relativePath>..</relativePath>
+       </parent>
+       <groupId>org.argeo.commons.basic</groupId>
+       <artifactId>runtime</artifactId>
+       <name>Commons Basic Runtime</name>
+       <packaging>pom</packaging>
+       <modules>
+               <module>org.argeo.support.junit</module>
+       </modules>
+</project>
\ No newline at end of file
index 0488d519815896da54ccde15f204a2860e12bb02..7b4110d8e487e31428398d37ccc7791f4dca5092 100644 (file)
@@ -45,37 +45,42 @@ public class JxlDaoSupport implements LightDaoSupport, ApplicationContextAware,
        private List<Resource> workbooks = new ArrayList<Resource>();
 
        public void afterPropertiesSet() throws Exception {
+               init();
+       }
+
+       public void init() {
+               // used to resolve inner references
+               Map<String, List<Object>> tempRefs = new HashMap<String, List<Object>>();
+
+               List<Reference> references = new ArrayList<Reference>();
+
                for (Resource res : workbooks) {
                        InputStream in = null;
                        try {
                                in = res.getInputStream();
-                               load(in);
+                               load(in, references, tempRefs);
+                       } catch (Exception e) {
+                               throw new ArgeoServerException("Cannot load stream", e);
                        } finally {
                                IOUtils.closeQuietly(in);
                        }
                }
 
+               // Inject references
+               for (Reference ref : references) {
+                       injectReference(ref, tempRefs);
+               }
+               if (log.isDebugEnabled())
+                       log.debug(references.size() + " references linked");
        }
 
-       public void load(InputStream in) {
+       public void load(InputStream in, List<Reference> references,
+                       Map<String, List<Object>> tempRefs) {
                try {
-                       // used to resolve inner references
-                       Map<String, List<Object>> tempRefs = new HashMap<String, List<Object>>();
-
-                       List<Reference> references = new ArrayList<Reference>();
-
                        Workbook workbook = Workbook.getWorkbook(in);
-
                        for (Sheet sheet : workbook.getSheets()) {
                                loadSheet(sheet, references, tempRefs);
                        }
-
-                       for (Reference ref : references) {
-                               injectReference(ref, tempRefs);
-                       }
-                       if (log.isDebugEnabled())
-                               log.debug(references.size() + " references linked");
-
                } catch (Exception e) {
                        throw new ArgeoServerException("Cannot load workbook", e);
                }
@@ -130,6 +135,10 @@ public class JxlDaoSupport implements LightDaoSupport, ApplicationContextAware,
                if (cell instanceof FormulaCell) {
                        String formula = ((FormulaCell) cell).getFormula();
                        int index = formula.indexOf('!');
+                       if (index < 0)
+                               throw new ArgeoServerException("Cannot interpret formula "
+                                               + formula);
+                       ;
                        String targetSheet = formula.substring(0, index);
                        // assume no double letters!!
                        String targetRowStr = formula.substring(index + 2);
index 5de2b5af1239269f0a739d18b9f67ba38d9fb154..0a003b6b773552ec42a2926fea8e8cf894ca4305 100644 (file)
@@ -1,40 +1,33 @@
 package org.argeo.server.jxl.dao;
 
-import java.io.InputStream;
 import java.util.List;
 
 import junit.framework.TestCase;
 
+import org.springframework.core.io.ClassPathResource;
+
 public class JxlDaoSupportTest extends TestCase {
        public void testBasic() throws Exception {
                JxlDaoSupport jsd = new JxlDaoSupport();
                jsd.getExternalRefs().put("test", new OtherObject());
 
-               InputStream in = null;
-               try {
-                       in = getClass().getResourceAsStream("/dao/simple.xls");
-                       jsd.load(in);
-
-                       SimpleObject soAaa = jsd.getByKey(SimpleObject.class, "aaa");
-                       assertNotNull(soAaa);
-                       assertEquals("aaa", soAaa.getString());
-                       assertEquals(1, soAaa.getInteger().intValue());
-                       assertNotNull(soAaa.getOtherObject());
-                       assertEquals("USD", soAaa.getOtherObject().getKey());
-                       assertEquals("US Dollar", soAaa.getOtherObject().getValue());
-
-                       SimpleObject soBbb = jsd.getByKey(SimpleObject.class, "bbb");
-                       assertNotNull(soBbb.getOtherObject());
-                       assertEquals("bbb", ((SimpleObject) soBbb.getOtherObject()
-                                       .getValue()).getString());
-
-                       List<SimpleObject> simpleObjects = jsd.list(SimpleObject.class,
-                                       null);
-                       assertEquals(2, simpleObjects.size());
-               } finally {
-                       if (in != null)
-                               in.close();
-               }
+               jsd.getWorkbooks().add(new ClassPathResource("/dao/simple.xls"));
+               jsd.init();
+
+               SimpleObject soAaa = jsd.getByKey(SimpleObject.class, "aaa");
+               assertNotNull(soAaa);
+               assertEquals("aaa", soAaa.getString());
+               assertEquals(1, soAaa.getInteger().intValue());
+               assertNotNull(soAaa.getOtherObject());
+               assertEquals("USD", soAaa.getOtherObject().getKey());
+               assertEquals("US Dollar", soAaa.getOtherObject().getValue());
+
+               SimpleObject soBbb = jsd.getByKey(SimpleObject.class, "bbb");
+               assertNotNull(soBbb.getOtherObject());
+               assertEquals("bbb", ((SimpleObject) soBbb.getOtherObject().getValue())
+                               .getString());
 
+               List<SimpleObject> simpleObjects = jsd.list(SimpleObject.class, null);
+               assertEquals(2, simpleObjects.size());
        }
 }