|
From: <tre...@us...> - 2007-08-28 02:51:03
|
Revision: 277
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=277&view=rev
Author: trevorolio
Date: 2007-08-27 19:50:58 -0700 (Mon, 27 Aug 2007)
Log Message:
-----------
Added fetal mail tests
Modified Paths:
--------------
maven/trunk/ogoglio-server/pom.xml
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/mail/MailTemplateFactory.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/AccountsForTesting.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/DBVersionPersistTasks.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/DBVersionRecord.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/DBZapTest.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/Migration.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/MigrationSupport.java
Added Paths:
-----------
maven/trunk/ogoglio-server/src/main/resources/mail/
maven/trunk/ogoglio-server/src/main/resources/mail/EmailValidationTemplate.txt
maven/trunk/ogoglio-server/src/test/java/com/ogoglio/mail/
maven/trunk/ogoglio-server/src/test/java/com/ogoglio/mail/test/
maven/trunk/ogoglio-server/src/test/java/com/ogoglio/mail/test/MailTest.java
maven/trunk/ogoglio-server/src/test/resources/mail/
maven/trunk/ogoglio-server/src/test/resources/mail/TestTemplate1.txt
maven/trunk/ogoglio-server/src/test/resources/test-config.properties
Removed Paths:
-------------
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/mail/MailTests.java
maven/trunk/ogoglio-server/src/test/resources/hibernate/test-config.properties
Property Changed:
----------------
maven/trunk/ogoglio-server/
Property changes on: maven/trunk/ogoglio-server
___________________________________________________________________
Name: svn:ignore
- target
+
target
mailTestFiles
Modified: maven/trunk/ogoglio-server/pom.xml
===================================================================
--- maven/trunk/ogoglio-server/pom.xml 2007-08-27 23:53:13 UTC (rev 276)
+++ maven/trunk/ogoglio-server/pom.xml 2007-08-28 02:50:58 UTC (rev 277)
@@ -61,10 +61,11 @@
<!-- -->
<testResources>
<testResource>
- <directory>src/test/resources/hibernate</directory>
+ <directory>src/test/resources</directory>
<filtering>true</filtering>
<includes>
<include>test-config.properties</include>
+ <include>mail/*</include>
</includes>
</testResource>
</testResources>
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/mail/MailTemplateFactory.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/mail/MailTemplateFactory.java 2007-08-27 23:53:13 UTC (rev 276)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/mail/MailTemplateFactory.java 2007-08-28 02:50:58 UTC (rev 277)
@@ -8,7 +8,7 @@
public class MailTemplateFactory {
- private static final String TEMPLATE_RESOURCE_PATH = "com/ogoglio/mail/";
+ private static final String TEMPLATE_RESOURCE_PATH = "mail/";
public static final String TEST_TEMPLATE_1 = "TestTemplate1.txt";
Deleted: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/mail/MailTests.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/mail/MailTests.java 2007-08-27 23:53:13 UTC (rev 276)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/mail/MailTests.java 2007-08-28 02:50:58 UTC (rev 277)
@@ -1,100 +0,0 @@
-package com.ogoglio.mail;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.util.HashMap;
-
-import junit.framework.TestCase;
-
-import com.ogoglio.util.ArgumentUtils;
-import com.ogoglio.util.StreamUtils;
-
-public class MailTests extends TestCase {
-
- private static final String TEST_DIR_PATH = "mailTestFiles";
-
- private File mailOutputDir = null;
-
- private MailClient mailClient1 = null;
-
- private MailFormatter mailFormatter1 = null;
-
- private String testTemplate1 = null;
-
- private HashMap testMap1 = null;
-
- public void setUp() throws IOException {
- mailOutputDir = new File(TEST_DIR_PATH);
- if (mailOutputDir.exists()) {
- delete(mailOutputDir);
- }
- assertTrue(mailOutputDir.mkdir());
-
- mailClient1 = new MailClient(mailOutputDir);
-
- mailFormatter1 = new MailFormatter();
-
- testTemplate1 = MailTemplateFactory.getTemplate(MailTemplateFactory.TEST_TEMPLATE_1);
- assertNotNull(testTemplate1);
-
- testMap1 = new HashMap();
- testMap1.put("username", "susan");
- testMap1.put("url", "http://example.com/og/");
- testMap1.put("moon.unit", "zappa");
- testMap1.put("Kurt", "Vonnegut.");
- }
-
- private void delete(File dir) {
- File[] children = dir.listFiles();
- for (int i = 0; i < children.length; i++) {
- assertTrue(children[i].delete());
- }
- assertTrue(dir.delete());
- }
-
- public void tearDown() {
- if (mailOutputDir != null && mailOutputDir.exists()) {
- delete(mailOutputDir);
- }
- }
-
- public void testClientFileOutput() throws MailSendException, FileNotFoundException, IOException {
- String body = "This is the body.";
- String to = "to...@ex...";
- String from = "ha...@ex...";
- String subject = "OWLs 'n Stuff";
- mailClient1.sendEmail(to, from, subject, body);
- File[] files = mailClient1.getOutputDir().listFiles();
- assertNotNull(files);
- assertEquals(1, files.length);
- assertFalse(files[0].length() == 0);
- String fileContents = StreamUtils.readInput(new FileInputStream(files[0]));
- assertTrue(fileContents.endsWith(body));
- }
-
- public void testFormat() {
- String result = mailFormatter1.format(testMap1, testTemplate1);
- assertNotNull(result);
- assertTrue(result.length() > 1);
- assertExactlyOne(result, (String) testMap1.get("username"));
- assertMultiple(result, (String) testMap1.get("url"));
- assertExactlyOne(result, (String) testMap1.get("moon.unit"));
- assertExactlyOne(result, (String) testMap1.get("Kurt"));
- }
-
- private void assertExactlyOne(String data, String target) {
- ArgumentUtils.assertNotEmpty(data);
- ArgumentUtils.assertNotEmpty(target);
- assertFalse(data.indexOf(target) == -1);
- assertTrue(data.indexOf(target) == data.lastIndexOf(target));
- }
-
- private void assertMultiple(String data, String target) {
- ArgumentUtils.assertNotEmpty(data);
- ArgumentUtils.assertNotEmpty(target);
- assertFalse(data.indexOf(target) == -1);
- assertFalse(data.indexOf(target) == data.lastIndexOf(target));
- }
-}
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/AccountsForTesting.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/AccountsForTesting.java 2007-08-27 23:53:13 UTC (rev 276)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/AccountsForTesting.java 2007-08-28 02:50:58 UTC (rev 277)
@@ -13,46 +13,41 @@
public class AccountsForTesting implements Migration {
- private static final String BOOTSTRAP_ACCT_USER_KEY = "ogoglio/bootstrapUser";
- private static final String BOOTSTRAP_ACCT_PW_KEY = "ogoglio/bootstrapUserPW";
+ private static final String BOOTSTRAP_ACCT_USER_KEY = "ogoglio/bootstrapUser";
- public boolean migrate(SessionFactory sessionFactory,
- ServletConfig NOTUSED, Context ctx, Properties testConfig, int from, int to) {
+ private static final String BOOTSTRAP_ACCT_PW_KEY = "ogoglio/bootstrapUserPW";
- if ((from != 0) || (to != 1)) {
- System.out
- .println("Migration called in the wrong place! Expected 0->1 but was:"
- + from + "->" + to + "!");
- System.out
- .println("Migration called in the wrong place! Check the ordering of migration array!");
- return false;
- }
- try {
- String uriString, user, pw;
-
- //if null, we are test mode
- if (ctx==null) {
- uriString=testConfig.getProperty(AbstractResourceServlet.OGOGLIO_BASE_URL_KEY.replace('/', '.'));
- user=testConfig.getProperty(BOOTSTRAP_ACCT_USER_KEY.replace('/', '.'));
- pw=testConfig.getProperty(BOOTSTRAP_ACCT_PW_KEY.replace('/', '.'));
- } else {
- uriString=(String) ctx.lookup(AbstractResourceServlet.OGOGLIO_BASE_URL_KEY);
- user=(String) ctx.lookup(BOOTSTRAP_ACCT_USER_KEY);
- pw=(String) ctx.lookup(BOOTSTRAP_ACCT_PW_KEY);
- }
- URI uri = new URI(uriString);
- //actual work
- ServiceInitializationPersistTasks.initializeLocalSim(uri,sessionFactory);
- ServiceInitializationPersistTasks.initializeBootstrapAccount(sessionFactory, uri.getHost(), user, pw);
+ public boolean migrate(SessionFactory sessionFactory, ServletConfig NOTUSED, Context ctx, Properties testConfig, int from, int to) {
+
+ if ((from != 0) || (to != 1)) {
+ System.out.println("Migration called in the wrong place! Expected 0->1 but was:" + from + "->" + to + "!");
+ System.out.println("Migration called in the wrong place! Check the ordering of migration array!");
+ return false;
+ }
+ try {
+ String uriString, user, pw;
+
+ //if null, we are test mode
+ if (ctx == null) {
+ uriString = testConfig.getProperty(AbstractResourceServlet.OGOGLIO_BASE_URL_KEY.replace('/', '.'));
+ user = testConfig.getProperty(BOOTSTRAP_ACCT_USER_KEY.replace('/', '.'));
+ pw = testConfig.getProperty(BOOTSTRAP_ACCT_PW_KEY.replace('/', '.'));
+ } else {
+ uriString = (String) ctx.lookup(AbstractResourceServlet.OGOGLIO_BASE_URL_KEY);
+ user = (String) ctx.lookup(BOOTSTRAP_ACCT_USER_KEY);
+ pw = (String) ctx.lookup(BOOTSTRAP_ACCT_PW_KEY);
+ }
+ URI uri = new URI(uriString);
+ //actual work
+ ServiceInitializationPersistTasks.initializeLocalSim(uri, sessionFactory);
+ ServiceInitializationPersistTasks.initializeBootstrapAccount(sessionFactory, uri.getHost(), user, pw);
ServiceInitializationPersistTasks.initializeServiceState(sessionFactory);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- System.err
- .println("Exception during attempt to init DB in migration from 0->1:\n"
- + e.getMessage());
- return false;
- }
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.err.println("Exception during attempt to init DB in migration from 0->1:\n" + e.getMessage());
+ return false;
+ }
- }
+ }
}
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/DBVersionPersistTasks.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/DBVersionPersistTasks.java 2007-08-27 23:53:13 UTC (rev 276)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/DBVersionPersistTasks.java 2007-08-28 02:50:58 UTC (rev 277)
@@ -8,48 +8,42 @@
import com.ogoglio.persist.PersistException;
public class DBVersionPersistTasks {
- public final static String FIND_VERSION = "com.ogoglio.migrate.dbversions";
+ public final static String FIND_VERSION = "com.ogoglio.migrate.dbversions";
- public static int findVersion(SessionFactory sessionFactory)
- throws PersistException {
- HibernateTask task = new HibernateTask() {
- public Object run(Session session) throws PersistException {
- Query query = session.getNamedQuery(FIND_VERSION);
- return query.uniqueResult();
- }
- };
- task.setSessionFactory(sessionFactory);
- return ((DBVersionRecord) task.execute()).getVersion();
- }
+ public static int findVersion(SessionFactory sessionFactory) throws PersistException {
+ HibernateTask task = new HibernateTask() {
+ public Object run(Session session) throws PersistException {
+ Query query = session.getNamedQuery(FIND_VERSION);
+ return query.uniqueResult();
+ }
+ };
+ task.setSessionFactory(sessionFactory);
+ return ((DBVersionRecord) task.execute()).getVersion();
+ }
- public static boolean updateVersion(final int newVersion,
- final int expectedSize/* one or zero */,
- SessionFactory sessionFactory) throws PersistException {
- HibernateTask task = new HibernateTask() {
- public Object run(Session session) throws PersistException {
- Query query = session.getNamedQuery(FIND_VERSION);
- int size = query.list().size();
- DBVersionRecord rec;
- if (expectedSize != size) {
- System.out.println(
- "Whoa! Size of returned result for db version record was ("
- + size + ") but should have been "
- + expectedSize + "!");
- return Boolean.FALSE;
- }
- if (size == 0) {
- rec = new DBVersionRecord();
- } else {
- rec = (DBVersionRecord) query.uniqueResult();
- }
- rec.setVersion(newVersion);
- session.saveOrUpdate(rec);
- System.out.println("******* Wrote new version FART:"+newVersion);
- return Boolean.TRUE;
- }
- };
- task.setSessionFactory(sessionFactory);
- return (task.execute().equals(Boolean.TRUE));
- }
+ public static boolean updateVersion(final int newVersion, final int expectedSize/* one or zero */, SessionFactory sessionFactory) throws PersistException {
+ HibernateTask task = new HibernateTask() {
+ public Object run(Session session) throws PersistException {
+ Query query = session.getNamedQuery(FIND_VERSION);
+ int size = query.list().size();
+ DBVersionRecord rec;
+ if (expectedSize != size) {
+ System.out.println("Whoa! Size of returned result for db version record was (" + size + ") but should have been " + expectedSize + "!");
+ return Boolean.FALSE;
+ }
+ if (size == 0) {
+ rec = new DBVersionRecord();
+ } else {
+ rec = (DBVersionRecord) query.uniqueResult();
+ }
+ rec.setVersion(newVersion);
+ session.saveOrUpdate(rec);
+ System.out.println("******* Wrote new version FART:" + newVersion);
+ return Boolean.TRUE;
+ }
+ };
+ task.setSessionFactory(sessionFactory);
+ return (task.execute().equals(Boolean.TRUE));
+ }
}
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/DBVersionRecord.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/DBVersionRecord.java 2007-08-27 23:53:13 UTC (rev 276)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/DBVersionRecord.java 2007-08-28 02:50:58 UTC (rev 277)
@@ -1,25 +1,28 @@
package com.ogoglio.migrate;
public class DBVersionRecord {
- public static int INTERNAL_DEV_STATE=-8288;
-
- private int version=0;
- private long id;
-
- public DBVersionRecord() {
-
- }
- public long getVersionId() {
- return id;
- }
- public void setVersionId(long id) {
- this.id=id;
- }
- public int getVersion() {
- return version;
- }
-
- public void setVersion(int version) {
- this.version=version;
- }
+ public static int INTERNAL_DEV_STATE = -8288;
+
+ private int version = 0;
+
+ private long id;
+
+ public DBVersionRecord() {
+ }
+
+ public long getVersionId() {
+ return id;
+ }
+
+ public void setVersionId(long id) {
+ this.id = id;
+ }
+
+ public int getVersion() {
+ return version;
+ }
+
+ public void setVersion(int version) {
+ this.version = version;
+ }
}
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/DBZapTest.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/DBZapTest.java 2007-08-27 23:53:13 UTC (rev 276)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/DBZapTest.java 2007-08-28 02:50:58 UTC (rev 277)
@@ -8,30 +8,29 @@
protected SessionFactory sessionFactory;
- public void setUp() {
+ public void setUp() {
try {
MigrationSupport support = new MigrationSupport();
if (!("true".equals(support.getTestConfig().getProperty("ogoglio.okToZapDB")))) {
- fail("Whoa! Shouldn't be running tests without setting the ogoglio.okToZapDB property!");
+ fail("Whoa! Shouldn't be running tests without setting the ogoglio.okToZapDB property!");
}
- if (support.initialize_version(null, null, false)==false) {
- fail("can't get DB set up: trying to initialize it in the test code");
+ if (support.initialize_version(null, null, false) == false) {
+ fail("can't get DB set up: trying to initialize it in the test code");
}
-
+
//if we are here, db stuff worked, but we still need to avoid all the jndi stuff
- sessionFactory= support.createConfigurationForHibernate(MigrationSupport.DB_VERSION_NUMBER,
- null, false).buildSessionFactory();
+ sessionFactory = support.createConfigurationForHibernate(MigrationSupport.DB_VERSION_NUMBER, null, false).buildSessionFactory();
} catch (Exception e) {
System.out.println("-------");
e.printStackTrace(System.out);
System.out.println("---------");
fail("Could not setup:" + e.getMessage());
}
- }
-
- public void tearDown() {
- if (sessionFactory!=null) {
- sessionFactory.close();
- }
- }
+ }
+
+ public void tearDown() {
+ if (sessionFactory != null) {
+ sessionFactory.close();
+ }
+ }
}
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/Migration.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/Migration.java 2007-08-27 23:53:13 UTC (rev 276)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/Migration.java 2007-08-28 02:50:58 UTC (rev 277)
@@ -9,7 +9,6 @@
public interface Migration {
- public boolean migrate(SessionFactory sessionFactory, ServletConfig servletConfig, Context ctx,
- Properties testConfig, int from, int to);
+ public boolean migrate(SessionFactory sessionFactory, ServletConfig servletConfig, Context ctx, Properties testConfig, int from, int to);
}
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/MigrationSupport.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/MigrationSupport.java 2007-08-27 23:53:13 UTC (rev 276)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/migrate/MigrationSupport.java 2007-08-28 02:50:58 UTC (rev 277)
@@ -15,166 +15,158 @@
public class MigrationSupport {
- // THIS IS THE CRITICAL VERSION NUMBER
- public static final int DB_VERSION_NUMBER = 1;
+ // THIS IS THE CRITICAL VERSION NUMBER
+ public static final int DB_VERSION_NUMBER = 1;
- //check on migration okayness
- public static final String MIGRATION_KEY = "ogoglio/okToMigrateDB";
-
- // this is the set of semantic migrations, in order
- public static final Migration[] migration = {
- new AccountsForTesting()
- };
+ //check on migration okayness
+ public static final String MIGRATION_KEY = "ogoglio/okToMigrateDB";
- public MigrationSupport() {
- }
+ // this is the set of semantic migrations, in order
+ public static final Migration[] migration = { new AccountsForTesting() };
- public boolean verifyVersion(ServletConfig servletConf, Context ctx) {
- try {
- SessionFactory sessionFactory = getCurrentConfiguration().buildSessionFactory();
- int version = DBVersionPersistTasks.findVersion(sessionFactory);
- if (version != DB_VERSION_NUMBER) {
- System.out.println("DB Version Mismatch! Expected ("
- + DB_VERSION_NUMBER + ") but got " + version + "!");
- sessionFactory.close();
- return tryUpgrade(servletConf, ctx, version, DB_VERSION_NUMBER,"update",true);
- }
- sessionFactory.close();
- return true; // we are at the expected version
- } catch (PersistException e) {
- if (e.innerThrowable==null) {
- System.out.println("Serious problem with hibernate:" + e.getMessage());
- return false;
- }
- System.out.println("Unable to figure out DB version number. Likely this is a fresh database....("+e.innerThrowable.getClass().getName()+")");
- return initialize_version(servletConf,ctx, true);
- }
- }
+ public MigrationSupport() {
+ }
- public boolean initialize_version(ServletConfig conf, Context ctx, boolean useJNDI) {
- Configuration configuration = createConfigurationForHibernate(0,"create",useJNDI);
- try {
- SessionFactory factory=configuration.buildSessionFactory();
- if (setVersionNumberOfDbViaQuery(factory,0,0)==false) {
- factory.close();
- return false;
- }
- factory.close();
- return tryUpgrade(conf,ctx,0, DB_VERSION_NUMBER, "create",useJNDI);
- } catch (Throwable e) {
- System.out.println("Error trying initialized DB:" + e.getMessage());
- return false;
- }
-
- }
+ public boolean verifyVersion(ServletConfig servletConf, Context ctx) {
+ try {
+ SessionFactory sessionFactory = getCurrentConfiguration().buildSessionFactory();
+ int version = DBVersionPersistTasks.findVersion(sessionFactory);
+ if (version != DB_VERSION_NUMBER) {
+ System.err.println("DB Version Mismatch! Expected (" + DB_VERSION_NUMBER + ") but got " + version + "!");
+ sessionFactory.close();
+ return tryUpgrade(servletConf, ctx, version, DB_VERSION_NUMBER, "update", true);
+ }
+ sessionFactory.close();
+ return true; // we are at the expected version
+ } catch (PersistException e) {
+ if (e.innerThrowable == null) {
+ System.out.println("Serious problem with hibernate:" + e.getMessage());
+ return false;
+ }
+ System.err.println("Unable to figure out DB version number. Likely this is a fresh database....(" + e.innerThrowable.getClass().getName() + ")");
+ return initialize_version(servletConf, ctx, true);
+ }
+ }
- public Configuration getCurrentConfiguration() {
- return createConfigurationForHibernate(DB_VERSION_NUMBER, null, true);
- }
-
- public Configuration createConfigurationForHibernate(int num, String autoSetting, boolean useJNDI) {
- Configuration configuration = new Configuration();
- if (autoSetting!=null) {
- configuration.setProperty("hibernate.hbm2ddl.auto", autoSetting);
- }
- if (useJNDI) {
- configuration.setProperty("hibernate.connection.datasource","java:comp/env/jdbc/space");
- } else {
- Properties p=new Properties();
- try {
- p.load(getClass().getResourceAsStream("/test-config.properties"));
- configuration.addProperties(p);
- } catch (IOException e){
- System.out.println("Unable to find the test configuration file!"+e.getMessage());
- return configuration.configure();
- }
- }
- configuration.addResource("com/ogoglio/migrate/migration-"+num+".xml");
- return configuration.configure();
- }
+ public boolean initialize_version(ServletConfig conf, Context ctx, boolean useJNDI) {
+ Configuration configuration = createConfigurationForHibernate(0, "create", useJNDI);
+ try {
+ SessionFactory factory = configuration.buildSessionFactory();
+ if (setVersionNumberOfDbViaQuery(factory, 0, 0) == false) {
+ factory.close();
+ return false;
+ }
+ factory.close();
+ return tryUpgrade(conf, ctx, 0, DB_VERSION_NUMBER, "create", useJNDI);
+ } catch (Throwable e) {
+ System.err.println("Error trying initialized DB:" + e.getMessage());
+ return false;
+ }
- private boolean setVersionNumberOfDbViaQuery(SessionFactory sessionFactory,int targetVersion,int expectedSize) {
- try {
- return DBVersionPersistTasks.updateVersion(targetVersion, expectedSize, sessionFactory);
- } catch (PersistException e) {
- System.out.println("Problem updating the version of the database to version "+targetVersion+":"+e.getMessage());
- return false;
- }
- }
+ }
- public boolean tryUpgrade(ServletConfig servletConfig, Context ctx, int db_is, int db_wants_to_be,
- String ddlMode, boolean useJNDI) {
- if (migration.length != DB_VERSION_NUMBER) {
- System.out
- .println("Internal error! Migration list length should be "
- + DB_VERSION_NUMBER + " but is " + migration.length
- + "!");
- return false;
- }
- Properties testConfig=new Properties();
- boolean canMigrate=false;
- if (ctx==null) {
- //in test mode
- testConfig = getTestConfig();
- if ("true".equals(testConfig.getProperty("ogoglio.okToMigrateDB"))) {
- canMigrate=true;
- }
- } else {
- //not testing, but might be integration testing
- try {
- String migrate=(String)ctx.lookup(MIGRATION_KEY);
- if ("true".equals(migrate)) {
- canMigrate=true;
- }
- } catch (NamingException e) {
- System.out.println("Naming exception trying to access "+MIGRATION_KEY+" from naming context!");
- }
- }
-
- if (!canMigrate) {
- System.out.println("Cannot migrate data! Property ogoglio.okToMigrateDB is false or non-existent!");
- return false;
- }
+ public Configuration getCurrentConfiguration() {
+ return createConfigurationForHibernate(DB_VERSION_NUMBER, null, true);
+ }
- for (int i = db_is; i < db_wants_to_be; ++i) {
- Migration current = migration[i];
- System.out.println("------------------------------------------------\n");
- System.out.println("DB: Attempting migration from " + i + " to "
- + (i + 1) + "...");
-
- //try to get hibernate to do the work
- Configuration config = createConfigurationForHibernate(i+1, ddlMode ,useJNDI);
- SessionFactory factory = config.buildSessionFactory();
-
- //subtle: if the ddlMode is create, hibernate blows away all the data (including our version number record)
- int expectedRecords=1;
- if ("create".equals(ddlMode)) {
- expectedRecords=0;
- }
- setVersionNumberOfDbViaQuery(factory,i+1, expectedRecords);
-
- if (!current.migrate(factory,servletConfig, ctx, testConfig, i, i + 1)) {
- factory.close();
- return false;
- }
- factory.close();
- System.out.println("------------------------------------------------\n");
- }
- return true;
- }
+ public Configuration createConfigurationForHibernate(int num, String autoSetting, boolean useJNDI) {
+ Configuration configuration = new Configuration();
+ if (autoSetting != null) {
+ configuration.setProperty("hibernate.hbm2ddl.auto", autoSetting);
+ }
+ if (useJNDI) {
+ configuration.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/space");
+ } else {
+ Properties p = new Properties();
+ try {
+ p.load(getClass().getResourceAsStream("/test-config.properties"));
+ configuration.addProperties(p);
+ } catch (IOException e) {
+ System.err.println("Unable to find the test configuration file!" + e.getMessage());
+ return configuration.configure();
+ }
+ }
+ configuration.addResource("com/ogoglio/migrate/migration-" + num + ".xml");
+ return configuration.configure();
+ }
- public Properties getTestConfig() {
- Properties testConfig = new Properties();
- InputStream is=getClass().getResourceAsStream("/test-config.properties");
- if (is==null) {
- System.out.println("Warning! Unable to find test-config.properties! Probably something is broken!");
- } else {
- try {
- testConfig.load(is);
- } catch (IOException e) {
- System.out.println("Warning! Error reading test-config.properties! "+e.getMessage());
- }
- }
- return testConfig;
- }
+ private boolean setVersionNumberOfDbViaQuery(SessionFactory sessionFactory, int targetVersion, int expectedSize) {
+ try {
+ return DBVersionPersistTasks.updateVersion(targetVersion, expectedSize, sessionFactory);
+ } catch (PersistException e) {
+ System.err.println("Problem updating the version of the database to version " + targetVersion + ":" + e.getMessage());
+ return false;
+ }
+ }
+
+ public boolean tryUpgrade(ServletConfig servletConfig, Context ctx, int db_is, int db_wants_to_be, String ddlMode, boolean useJNDI) {
+ if (migration.length != DB_VERSION_NUMBER) {
+ System.out.println("Internal error! Migration list length should be " + DB_VERSION_NUMBER + " but is " + migration.length + "!");
+ return false;
+ }
+ Properties testConfig = new Properties();
+ boolean canMigrate = false;
+ if (ctx == null) {
+ //in test mode
+ testConfig = getTestConfig();
+ if ("true".equals(testConfig.getProperty("ogoglio.okToMigrateDB"))) {
+ canMigrate = true;
+ }
+ } else {
+ //not testing, but might be integration testing
+ try {
+ String migrate = (String) ctx.lookup(MIGRATION_KEY);
+ if ("true".equals(migrate)) {
+ canMigrate = true;
+ }
+ } catch (NamingException e) {
+ System.err.println("Naming exception trying to access " + MIGRATION_KEY + " from naming context!");
+ }
+ }
+
+ if (!canMigrate) {
+ System.err.println("Cannot migrate data! Property ogoglio.okToMigrateDB is false or non-existent!");
+ return false;
+ }
+
+ for (int i = db_is; i < db_wants_to_be; ++i) {
+ Migration current = migration[i];
+ System.out.println("------------------------------------------------\n");
+ System.out.println("DB: Attempting migration from " + i + " to " + (i + 1) + "...");
+
+ //try to get hibernate to do the work
+ Configuration config = createConfigurationForHibernate(i + 1, ddlMode, useJNDI);
+ SessionFactory factory = config.buildSessionFactory();
+
+ //subtle: if the ddlMode is create, hibernate blows away all the data (including our version number record)
+ int expectedRecords = 1;
+ if ("create".equals(ddlMode)) {
+ expectedRecords = 0;
+ }
+ setVersionNumberOfDbViaQuery(factory, i + 1, expectedRecords);
+
+ if (!current.migrate(factory, servletConfig, ctx, testConfig, i, i + 1)) {
+ factory.close();
+ return false;
+ }
+ factory.close();
+ System.out.println("------------------------------------------------\n");
+ }
+ return true;
+ }
+
+ public Properties getTestConfig() {
+ Properties testConfig = new Properties();
+ InputStream is = getClass().getResourceAsStream("/test-config.properties");
+ if (is == null) {
+ System.err.println("Warning! Unable to find test-config.properties! Probably something is broken!");
+ } else {
+ try {
+ testConfig.load(is);
+ } catch (IOException e) {
+ System.err.println("Warning! Error reading test-config.properties! " + e.getMessage());
+ }
+ }
+ return testConfig;
+ }
}
Added: maven/trunk/ogoglio-server/src/main/resources/mail/EmailValidationTemplate.txt
===================================================================
--- maven/trunk/ogoglio-server/src/main/resources/mail/EmailValidationTemplate.txt (rev 0)
+++ maven/trunk/ogoglio-server/src/main/resources/mail/EmailValidationTemplate.txt 2007-08-28 02:50:58 UTC (rev 277)
@@ -0,0 +1,16 @@
+Hello from ${serviceName},
+
+Someone (maybe you) has requested that the address ${emailToValidate} be associated
+with an account at ${serviceName}. If that someone was you and you'd like to make that
+association, just click this link:
+
+${validationURL}
+
+If you did not make this request, don't worry. If you don't click that link then the
+email will not be associated with the account and after a few days the request will be
+deleted.
+
+Keep it real,
+
+ - The folks from ${serviceName} at ${serviceURL}
+
Added: maven/trunk/ogoglio-server/src/test/java/com/ogoglio/mail/test/MailTest.java
===================================================================
--- maven/trunk/ogoglio-server/src/test/java/com/ogoglio/mail/test/MailTest.java (rev 0)
+++ maven/trunk/ogoglio-server/src/test/java/com/ogoglio/mail/test/MailTest.java 2007-08-28 02:50:58 UTC (rev 277)
@@ -0,0 +1,104 @@
+package com.ogoglio.mail.test;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.HashMap;
+
+import junit.framework.TestCase;
+
+import com.ogoglio.mail.MailClient;
+import com.ogoglio.mail.MailFormatter;
+import com.ogoglio.mail.MailSendException;
+import com.ogoglio.mail.MailTemplateFactory;
+import com.ogoglio.util.ArgumentUtils;
+import com.ogoglio.util.StreamUtils;
+
+public class MailTest extends TestCase {
+
+ private static final String TEST_DIR_PATH = "mailTestFiles";
+
+ private File mailOutputDir = null;
+
+ private MailClient mailClient1 = null;
+
+ private MailFormatter mailFormatter1 = null;
+
+ private String testTemplate1 = null;
+
+ private HashMap testMap1 = null;
+
+ public void setUp() throws IOException {
+ mailOutputDir = new File(TEST_DIR_PATH);
+ if (mailOutputDir.exists()) {
+ delete(mailOutputDir);
+ }
+ assertTrue(mailOutputDir.mkdir());
+
+ mailClient1 = new MailClient(mailOutputDir);
+
+ mailFormatter1 = new MailFormatter();
+
+ testTemplate1 = MailTemplateFactory.getTemplate(MailTemplateFactory.TEST_TEMPLATE_1);
+ assertNotNull(testTemplate1);
+
+ testMap1 = new HashMap();
+ testMap1.put("username", "susan");
+ testMap1.put("url", "http://example.com/og/");
+ testMap1.put("moon.unit", "zappa");
+ testMap1.put("Kurt", "Vonnegut.");
+ }
+
+ private void delete(File dir) {
+ File[] children = dir.listFiles();
+ for (int i = 0; i < children.length; i++) {
+ assertTrue(children[i].delete());
+ }
+ assertTrue(dir.delete());
+ }
+
+ public void tearDown() {
+ if (mailOutputDir != null && mailOutputDir.exists()) {
+ delete(mailOutputDir);
+ }
+ }
+
+ public void testClientFileOutput() throws MailSendException, FileNotFoundException, IOException {
+ String body = "This is the body.";
+ String to = "to...@ex...";
+ String from = "ha...@ex...";
+ String subject = "OWLs 'n Stuff";
+ mailClient1.sendEmail(to, from, subject, body);
+ File[] files = mailClient1.getOutputDir().listFiles();
+ assertNotNull(files);
+ assertEquals(1, files.length);
+ assertFalse(files[0].length() == 0);
+ String fileContents = StreamUtils.readInput(new FileInputStream(files[0]));
+ assertTrue(fileContents.endsWith(body));
+ }
+
+ public void testFormat() {
+ String result = mailFormatter1.format(testMap1, testTemplate1);
+ assertNotNull(result);
+ assertTrue(result.length() > 1);
+ assertExactlyOne(result, (String) testMap1.get("username"));
+ assertMultiple(result, (String) testMap1.get("url"));
+ assertExactlyOne(result, (String) testMap1.get("moon.unit"));
+ assertExactlyOne(result, (String) testMap1.get("Kurt"));
+ }
+
+ private void assertExactlyOne(String data, String target) {
+ ArgumentUtils.assertNotEmpty(data);
+ ArgumentUtils.assertNotEmpty(target);
+ assertFalse(data.indexOf(target) == -1);
+ assertTrue(data.indexOf(target) == data.lastIndexOf(target));
+ }
+
+ private void assertMultiple(String data, String target) {
+ ArgumentUtils.assertNotEmpty(data);
+ ArgumentUtils.assertNotEmpty(target);
+ assertFalse(data.indexOf(target) == -1);
+ assertFalse(data.indexOf(target) == data.lastIndexOf(target));
+ }
+}
Deleted: maven/trunk/ogoglio-server/src/test/resources/hibernate/test-config.properties
===================================================================
--- maven/trunk/ogoglio-server/src/test/resources/hibernate/test-config.properties 2007-08-27 23:53:13 UTC (rev 276)
+++ maven/trunk/ogoglio-server/src/test/resources/hibernate/test-config.properties 2007-08-28 02:50:58 UTC (rev 277)
@@ -1,28 +0,0 @@
-# this is needed for running tests because tomcat isn't
-# actually around when these tests get run so we need
-# to use the DB via the simple c3po connection manager
-# (supplied with hibernate)
-
-hibernate.connection.driver_class = com.mysql.jdbc.Driver
-hibernate.connection.url = ${ogoglio.mysql.url}
-hibernate.connection.username = ${ogoglio.mysql.user}
-hibernate.connection.password = ${ogoglio.mysql.password}
-hibernate.show_sql=false
-hibernate.dialect=org.hibernate.dialect.MySQLDialect
-
-#copied from the web
-#hibernate.c3p0.min_size=5
-#hibernate.c3p0.max_size=20
-#hibernate.c3p0.timeout=1800
-#hibernate.c3p0.max_statements=50
-
-#for doing tests that need bootstrap
-ogoglio.baseURL=${ogoglio.baseURL}
-ogoglio.bootstrapUser=${ogoglio.bootstrapUser}
-ogoglio.bootstrapUserPW=${ogoglio.bootstrapUserPW}
-
-#can destroy the db?
-ogoglio.okToZapDB=${ogoglio.okToZapDB}
-#can migrate data?
-ogoglio.okToMigrateDB=${ogoglio.okToMigrateDB}
-
Added: maven/trunk/ogoglio-server/src/test/resources/mail/TestTemplate1.txt
===================================================================
--- maven/trunk/ogoglio-server/src/test/resources/mail/TestTemplate1.txt (rev 0)
+++ maven/trunk/ogoglio-server/src/test/resources/mail/TestTemplate1.txt 2007-08-28 02:50:58 UTC (rev 277)
@@ -0,0 +1,6 @@
+Replace this with username: ${username}
+Replace this with url: ${url}
+Also replace this with url: ${url}
+Replace this with moon.unit: ${moon.unit}
+Don't replace this with it, though: ${moonAunit}
+Replace this with Kurt: ${Kurt}
\ No newline at end of file
Copied: maven/trunk/ogoglio-server/src/test/resources/test-config.properties (from rev 270, maven/trunk/ogoglio-server/src/test/resources/hibernate/test-config.properties)
===================================================================
--- maven/trunk/ogoglio-server/src/test/resources/test-config.properties (rev 0)
+++ maven/trunk/ogoglio-server/src/test/resources/test-config.properties 2007-08-28 02:50:58 UTC (rev 277)
@@ -0,0 +1,28 @@
+# this is needed for running tests because tomcat isn't
+# actually around when these tests get run so we need
+# to use the DB via the simple c3po connection manager
+# (supplied with hibernate)
+
+hibernate.connection.driver_class = com.mysql.jdbc.Driver
+hibernate.connection.url = ${ogoglio.mysql.url}
+hibernate.connection.username = ${ogoglio.mysql.user}
+hibernate.connection.password = ${ogoglio.mysql.password}
+hibernate.show_sql=false
+hibernate.dialect=org.hibernate.dialect.MySQLDialect
+
+#copied from the web
+#hibernate.c3p0.min_size=5
+#hibernate.c3p0.max_size=20
+#hibernate.c3p0.timeout=1800
+#hibernate.c3p0.max_statements=50
+
+#for doing tests that need bootstrap
+ogoglio.baseURL=${ogoglio.baseURL}
+ogoglio.bootstrapUser=${ogoglio.bootstrapUser}
+ogoglio.bootstrapUserPW=${ogoglio.bootstrapUserPW}
+
+#can destroy the db?
+ogoglio.okToZapDB=${ogoglio.okToZapDB}
+#can migrate data?
+ogoglio.okToMigrateDB=${ogoglio.okToMigrateDB}
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|