Commits

dep4b  committed e680f08

Propertyset unit tests

git-svn-id: http://svn.opensymphony.com/svn/propertyset/trunk@103bf3cbcdd-1c1a-0410-9a68-d6f521e3fa7b

  • Participants
  • Parent commits 1e4babd

Comments (0)

Files changed (9)

File src/test/com/opensymphony/module/propertyset/BasePropertySetTest.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset;
+
+import junit.framework.TestCase;
+
+import java.util.HashMap;
+
+
+/**
+ * This test case is attempts to execise a the property set API.
+ * This is also a good resource for beginners
+ * to PropertySet.  This class is extended to test the various SPI's.
+ *
+ * @author Eric Pugh (epugh@upstate.com)
+ */
+public abstract class BasePropertySetTest extends TestCase {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    protected HashMap args = new HashMap();
+    protected PropertySet propertySet;
+
+    //~ Constructors ///////////////////////////////////////////////////////////
+
+    public BasePropertySetTest(String s) {
+        super(s);
+    }
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void setUp() throws Exception {
+        super.setUp();
+        propertySet = PropertySetManager.getInstance(getType(), args);
+    }
+
+    public void tearDown() throws Exception {
+        super.tearDown();
+        args.clear();
+    }
+
+    public void testPropertySet() throws Exception {
+        propertySet.setString("aString", "aString value");
+        assertEquals("aString value", propertySet.getString("aString"));
+        propertySet.setBoolean("aBoolean", true);
+        assertEquals(true, propertySet.getBoolean("aBoolean"));
+        propertySet.setDouble("aDouble", 10.2342);
+        assertEquals(10.2342, propertySet.getDouble("aDouble"), 0);
+    }
+
+    protected abstract String getType();
+}

File src/test/com/opensymphony/module/propertyset/DatabaseHelper.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset;
+
+import com.mckoi.database.jdbc.MSQLException;
+
+import junit.framework.Assert;
+
+import net.sf.hibernate.SessionFactory;
+import net.sf.hibernate.cfg.Configuration;
+import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import java.sql.Connection;
+import java.sql.Statement;
+
+import javax.naming.InitialContext;
+
+import javax.sql.DataSource;
+
+
+/**
+ * @author Eric Pugh
+ *
+ * This helper class populates a test mckoi database.
+ */
+public class DatabaseHelper {
+    //~ Static fields/initializers /////////////////////////////////////////////
+
+    private static Log log = LogFactory.getLog(DatabaseHelper.class);
+    private static boolean databaseBuilt = false;
+    private static SessionFactory sessionFactory;
+    private static Configuration configuration;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    /**
+     * @return
+     */
+    public static Configuration getConfiguration() {
+        return configuration;
+    }
+
+    /**
+     * @return Hibernate SessionFactory
+     */
+    public static SessionFactory getSessionFactory() {
+        return sessionFactory;
+    }
+
+    /**
+     * Use the default Hibernate *.hbm.xml files.  These build the primary keys
+     * based on an identity or sequence, whatever is native to the database.
+     * @throws Exception
+     */
+    public static void exportSchemaForHibernate() throws Exception {
+        configuration = new Configuration();
+
+        //cfg.addClass(HibernateHistoryStep.class);
+        File fileHibernatePropertySetItem = new File("src/java/com/opensymphony/module/propertyset/hibernate/PropertySetItem.hbm.xml");
+
+        Assert.assertTrue(fileHibernatePropertySetItem.exists());
+        configuration.addFile(fileHibernatePropertySetItem);
+
+		// Use SchemaExport to see what Hibernate would have created!
+        createDatabase("src/etc/deployment/jdbc/mckoi-hibernate.sql");
+
+        //new SchemaExport(configuration).create(true, true);
+        sessionFactory = configuration.buildSessionFactory();
+        System.out.println("done");
+    }
+
+    /**
+     * Build the database using a sql script
+     * @throws Exception
+     */
+    public static void exportSchemaForJDBC() throws Exception {
+        createDatabase("src/etc/deployment/jdbc/mckoi.sql");
+    }
+
+    private static String getDatabaseCreationScript(String scriptFile) throws Exception {
+        File file = new File(scriptFile);
+        Assert.assertTrue(file.exists());
+
+        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
+
+        return readTextStream(bis);
+    }
+
+    private static void createDatabase(String scriptFile) {
+        Connection connection;
+        Statement statement = null;
+        String sqlLine = null;
+
+        try {
+            InitialContext context = new InitialContext();
+            DataSource ds = (DataSource) context.lookup("jdbc/CreateDS");
+            connection = ds.getConnection();
+            statement = connection.createStatement();
+
+            String sql = getDatabaseCreationScript(scriptFile);
+            String[] sqls = StringUtils.split(sql, ";");
+
+            for (int i = 0; i < sqls.length; i++) {
+                sqlLine = StringUtils.stripToEmpty(sqls[i]);
+                sqlLine = StringUtils.replace(sqlLine, "\r", "");
+                sqlLine = StringUtils.replace(sqlLine, "\n", "");
+
+                //String s = sqls[i];
+                if (sqlLine.length() > 0) {
+                    try {
+                        statement.executeQuery(sqlLine);
+                    } catch (MSQLException msqlEx) {
+                        // Eat any drop tables that fail.  The IF EXISTS clause doesn't seem to work.
+                        if (msqlEx.getMessage().indexOf("does not exist") == -1) {
+                            throw msqlEx;
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            log.error("Database creation error.  sqlLine:" + sqlLine, e);
+        } finally {
+            if (statement != null) {
+                try {
+                    statement.close();
+                } catch (Exception ex) {
+                    //not catch
+                }
+            }
+        }
+
+        databaseBuilt = true;
+
+        //  return connection;
+    }
+
+    private static String readTextStream(InputStream is) throws Exception {
+        //System.out.println("InputStream" + is.toString());
+        InputStreamReader reader = new InputStreamReader(is);
+        StringBuffer sb = new StringBuffer(100);
+        int c = 0;
+
+        while (c > -1) {
+            c = reader.read();
+
+            if (c > -1) {
+                sb.append((char) c);
+            }
+        }
+
+        return sb.toString();
+    }
+}

File src/test/com/opensymphony/module/propertyset/database/JDBCPropertySetTest.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.database;
+
+import com.opensymphony.module.propertyset.BasePropertySetTest;
+import com.opensymphony.module.propertyset.DatabaseHelper;
+
+
+/**
+ * This test case attempts to verify the database implementation.  This is also a good resource for beginners
+ * to PropertySet.  This leverages straight JDBC as the persistence mechanism which requires
+ * fewer dependencies then hibernate..
+ *
+ * @author Eric Pugh (epugh@upstate.com)
+ */
+public class JDBCPropertySetTest extends BasePropertySetTest {
+    //~ Constructors ///////////////////////////////////////////////////////////
+
+    public JDBCPropertySetTest(String s) {
+        super(s);
+    }
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public String getType() {
+        return "jdbc";
+    }
+
+    public void setUp() throws Exception {
+        //ok so this code usually goes in the setUp but...
+        DatabaseHelper.exportSchemaForJDBC();
+        args.put("globalKey", "test");
+        super.setUp();
+    }
+}

File src/test/com/opensymphony/module/propertyset/hibernate/HibernatePropertySetTest.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.hibernate;
+
+import com.opensymphony.module.propertyset.BasePropertySetTest;
+import com.opensymphony.module.propertyset.DatabaseHelper;
+
+
+/**
+ * This test case tests a propertyset backed by Hibernate
+ *
+ * @author Eric Pugh (epugh@upstate.com)
+ */
+public class HibernatePropertySetTest extends BasePropertySetTest {
+    //~ Constructors ///////////////////////////////////////////////////////////
+
+    public HibernatePropertySetTest(String s) {
+        super(s);
+    }
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public String getType() {
+        return "hibernate";
+    }
+
+    public void setUp() throws Exception {
+        //DatabaseHelper.exportSchemaForJDBC();
+        DatabaseHelper.exportSchemaForHibernate();
+
+        DefaultHibernateConfigurationProvider configurationProvider = new DefaultHibernateConfigurationProvider();
+        configurationProvider.setConfiguration(DatabaseHelper.getConfiguration());
+        configurationProvider.setSessionFactory(DatabaseHelper.getSessionFactory());
+
+        args.put("entityName", "testHibernate");
+        args.put("entityId", new Long(3));
+
+        //args.put("sessionFactory",DatabaseHelper.getSessionFactory());
+        args.put("configurationProvider", configurationProvider);
+        super.setUp();
+    }
+}

File src/test/com/opensymphony/module/propertyset/memory/MemoryBasePropertySetTest.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.memory;
+
+import com.opensymphony.module.propertyset.BasePropertySetTest;
+
+
+/**
+ * This test case is functional in that it attempts to validate the entire
+ * lifecycle of a workflow.  This is also a good resource for beginners
+ * to OSWorkflow.
+ *
+ * @author Eric Pugh (epugh@upstate.com)
+ */
+public class MemoryBasePropertySetTest extends BasePropertySetTest {
+    //~ Constructors ///////////////////////////////////////////////////////////
+
+    public MemoryBasePropertySetTest(String s) {
+        super(s);
+    }
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public String getType() {
+        return "memory";
+    }
+}

File src/test/hibernate.properties

+######################
+### Query Language ###
+######################
+
+## define query language constants / function names
+
+hibernate.query.substitutions true 1, false 0, yes 'Y', no 'N'
+
+## package imports
+
+hibernate.query.imports net.sf.hibernate.test, net.sf.hibernate.eg
+
+hibernate.dialect net.sf.hibernate.dialect.MckoiDialect
+
+#################
+### Platforms ###
+#################
+
+## JNDI Datasource
+
+hibernate.connection.datasource jdbc/CreateDS
+#hibernate.connection.username db2
+#hibernate.connection.password db2
+
+#################################
+### Hibernate Connection Pool ###
+#################################
+
+hibernate.connection.pool_size 1
+hibernate.statement_cache.size 25
+
+
+
+
+
+##############################
+### Miscellaneous Settings ###
+##############################
+
+## print all generated SQL to the console
+
+hibernate.show_sql false
+
+
+## specify a JDBC isolation level
+
+#hibernate.connection.isolation 4
+
+
+## set the JDBC fetch size
+
+#hibernate.jdbc.fetch_size 25
+
+
+## set the maximum JDBC 2 batch size (a nonzero value enables batching)
+
+hibernate.jdbc.batch_size 0
+
+
+## enable use of JDBC 2 scrollable ResultSets (specifying a Dialect will cause Hibernate to use a sensible default)
+
+#hibernate.jdbc.use_scrollable_resultset true
+
+
+## use streams when writing binary types to / from JDBC
+
+hibernate.jdbc.use_streams_for_binary true
+
+
+## specify a default schema for unqualified tablenames
+
+#hibernate.default_schema test
+
+
+## use a custom stylesheet for XML generation (if not specified, hibernate-default.xslt will be used)
+
+#hibernate.xml.output_stylesheet C:/Hibernate/net/sf/hibernate/hibernate-default.xslt
+
+
+## enable outerjoin fetching (specifying a Dialect will cause Hibernate to use sensible default)
+
+#hibernate.use_outer_join false
+
+
+## enable CGLIB reflection optimizer (enabled by default)
+
+hibernate.cglib.use_reflection_optimizer false
+
+
+
+############
+### JNDI ###
+############
+
+## specify a JNDI name for the SessionFactory
+
+#hibernate.session_factory_name hibernate/session_factory
+
+
+## Hibernate uses JNDI to bind a name to a SessionFactory and to look up the JTA UserTransaction;
+## if hibernate.jndi.* are not specified, Hibernate will use the default InitialContext() which
+## is the best approach in an application server
+
+#file system
+#hibernate.jndi.class com.sun.jndi.fscontext.RefFSContextFactory
+#hibernate.jndi.url file:/
+
+#WebSphere
+#hibernate.jndi.class com.ibm.websphere.naming.WsnInitialContextFactory
+#hibernate.jndi.url iiop://localhost:900/
+

File src/test/jndi.properties

+java.naming.factory.initial=org.osjava.jndi.PropertiesFactory
+org.osjava.jndi.root=classpath://config
+org.osjava.jndi.delimiter=/

File src/test/mckoi.conf

+
+#######################################################
+#
+# Configuration options for the Mckoi SQL Database.
+#
+#   This is a configuration for the simple database
+#   demonstrations.
+#
+#
+# NOTE: Lines starting with '#' are comments.
+#
+#######################################################
+
+#
+# database_path - The path where the database data files
+#   are located.
+#   See the 'root_path' configuration property for the
+#   details of how the engine resolves this to an
+#   absolute path in your file system.
+
+database_path=./data
+storage_system = v1javaheap
+
+#
+# log_path - The path the log files are written.
+#   See the 'root_path' configuration property for the
+#   details of how the engine resolves this to an
+#   absolute path in your file system.
+#   The log path must point to a writable directory.  If
+#   no log files are to be kept, then comment out (or
+#   remove) the 'log_path' variable.
+
+log_path=../../build/mckoi/log
+
+#
+# root_path - If this is set to 'jvm' then the root
+#   path of all database files is the root path of the
+#   JVM (Java virtual machine) running the database
+#   engine.  If this property is set to 'configuration'
+#   or if it is not present then the root path is the
+#   path of this configuration file.
+#   This property is useful if you are deploying a
+#   database and need this configuration file to be the
+#   root of the directory tree of the database files.
+
+root_path=configuration
+#root_path=jvm
+
+#
+# jdbc_server_port - The TCP/IP port on this host where
+#   the database server is mounted.  The default port
+#   of the Mckoi SQL Database server is '9157'
+
+jdbc_server_port=9157
+
+#
+# ignore_case_for_identifiers - If enabled all
+#   identifiers are compared case insensitive.  If
+#   disabled (the default) the case of the identifier
+#   is important.
+#   For example, if a table called 'MyTable' contains
+#   a column called 'my_column' and this property is
+#   enabled, the identifier 'MYTAble.MY_COlumN' will
+#   correctly reference the column of the table.  If
+#   this property is disable a not found error is
+#   generated.
+#   This property is intended for compatibility with
+#   other database managements systems where the case
+#   of identifiers is not important.
+
+ignore_case_for_identifiers=disabled
+
+
+# ----- PLUG-INS -----
+
+#
+# function_factories - Registers one or more FunctionFactory
+#   classes with the database at boot time.  A
+#   FunctionFactory allows user-defined functions to be
+#   incorporated into the SQL language.  Each factory class
+#   is separated by a semi-colon (;) character.
+#
+#function_factories=mypackage.MyFunctionFactory
+
+#
+# The Java regular expression library to use.  Currently
+# the engine supports the Apache Jakarta regular expression
+# library, and the GNU LGPL regular expression library.
+# These two regular expression libraries can be found at the
+# following web sites:
+#
+# GNU Regexp: http://www.cacas.org/~wes/java/
+# Apache Regexp: http://jakarta.apache.org/regexp/
+#
+# The libraries provide similar functionality, however they
+# are released under a different license.  The GNU library
+# is released under the LGPL and is compatible with GPL
+# distributions of the database.  The Apache Jakarta library
+# is released under the Apache Software License and must not
+# be linked into GPL distributions.
+#
+# Use 'regex_library=gnu.regexp' to use the GNU library, or
+# 'regex_library=org.apache.regexp' to use the Apache
+# library.
+#
+# NOTE: To use either library, you must include the
+#  respective .jar package in the Java classpath.
+
+regex_library=gnu.regexp
+
+
+
+
+# ----- PERFORMANCE -----
+
+#
+# data_cache_size - The maximum amount of memory (in bytes)
+#   to allow the memory cache to grow to.  If this is set
+#   to a value < 4096 then the internal cache is disabled.
+#   It is recommended that a database server should provide
+#   a cache of 4 Megabytes (4194304).  A stand alone
+#   database need not have such a large cache.
+
+data_cache_size=2097152
+
+#
+# max_cache_entry_size - The maximum size of an element
+#   in the data cache.  This is available for tuning
+#   reasons and the value here is dependant on the type
+#   of data being stored.  If your data has more larger
+#   fields that would benefit from being stored in the
+#   cache then increase this value from its default of
+#   8192 (8k).
+
+max_cache_entry_size=8192
+
+#
+# max_worker_threads - The maximum number of worker
+#   threads that can be spawned to handle incoming
+#   requests.  The higher this number, the more
+#   'multi-threaded' the database becomes.  The
+#   default setting is '4'.
+
+maximum_worker_threads=2
+
+#
+# dont_synch_filesystem - If this is enabled, the engine
+#   will not synchronize the file handle when a table change
+#   is committed.  This will mean the data is not as
+#   safe but the 'commit' command will work faster.  If this
+#   is enabled, there is a chance that committed changes will
+#   not get a chance to flush to the file system if the
+#   system crashes.
+#
+#   It is recommended this property is left commented out.
+#
+#dont_synch_filesystem=enabled
+
+#
+# transaction_error_on_dirty_select - If this is disabled
+#   the 4th conflict (dirty read on modified table) will
+#   not be detected.  This has transactional consequences
+#   that will cause data modifications to sometimes be
+#   out of syncronization.  For example, one transaction
+#   adds an entry, and another concurrent transaction
+#   deletes all entries.  If this is disabled this
+#   conflict will not be detected.  The table will end up
+#   with the one entry added after commit.
+#
+#   It is recommended this property is left commented out.
+#
+#transaction_error_on_dirty_select=disabled
+
+
+
+
+# ----- SPECIAL -----
+
+#
+# read_only - If this is set to 'enabled' then the database
+#   is readable and not writable.  You may boot a database
+#   in read only mode from multiple VM's.  If the database
+#   data files are stored on a read only medium such as a
+#   CD, then the property must be enabled else it will not
+#   be possible to boot the database.
+#   ( Uncomment the line below for read only mode )
+#read_only=enabled
+
+
+
+
+# ----- DEBUGGING -----
+
+#
+# debug_log_file - The file that is used to log all debug
+#   information.  This file is stored in the 'log_path'
+#   path.
+
+debug_log_file=debug.log
+
+#
+# debug_level - The minimum debug level of messages that
+#   are written to the log file.  Reducing this number
+#   will cause more debug information to be written to
+#   the log.
+#     10 = INFORMATION
+#     20 = WARNINGS
+#     30 = ALERTS
+#     40 = ERRORS
+
+debug_level=10

File src/test/propertyset.xml

+<propertysets>
+    <propertyset name="aggregate" class="com.opensymphony.module.propertyset.aggregate.AggregatePropertySet"/>
+    <propertyset name="cached" class="com.opensymphony.module.propertyset.cached.CachingPropertySet"/>
+    <propertyset name="jdbc" class="com.opensymphony.module.propertyset.database.JDBCPropertySet">
+        <arg name="datasource" value="jdbc/DefaultDS"/>
+        <arg name="table.name" value="OS_PROPERTYENTRY"/>
+        <arg name="col.globalKey" value="GLOBAL_KEY"/>
+        <arg name="col.itemKey" value="ITEM_KEY"/>
+        <arg name="col.itemType" value="ITEM_TYPE"/>
+        <arg name="col.string" value="STRING_VALUE"/>
+        <arg name="col.date" value="DATE_VALUE"/>
+        <arg name="col.data" value="DATA_VALUE"/>
+        <arg name="col.float" value="FLOAT_VALUE"/>
+        <arg name="col.number" value="NUMBER_VALUE"/>
+    </propertyset>
+    <propertyset name="ejb" class="com.opensymphony.module.propertyset.ejb.EJBPropertySet"/>
+    <propertyset name="javabeans" class="com.opensymphony.module.propertyset.javabeans.BeanIntrospectorPropertySet"/>
+    <propertyset name="map" class="com.opensymphony.module.propertyset.map.MapPropertySet"/>
+    <propertyset name="memory" class="com.opensymphony.module.propertyset.memory.MemoryPropertySet"/>
+    <propertyset name="serializable" class="com.opensymphony.module.propertyset.memory.SerializablePropertySet"/>
+    <propertyset name="ofbiz" class="com.opensymphony.module.propertyset.ofbiz.OFBizPropertySet"/>
+    <propertyset name="hibernate" class="com.opensymphony.module.propertyset.hibernate.HibernatePropertySet">
+        <arg name="hibernate.connection.datasource" value="jdbc/DefaultDS" />
+        <arg name="hibernate.dialect" value="net.sf.hibernate.dialect.MckoiDialect" />
+    </propertyset>
+    <propertyset name="xml" class="com.opensymphony.module.propertyset.xml.XMLPropertySet"/>
+</propertysets>