| 
     
      
      
      From: Juergen H. <jho...@us...> - 2008-11-27 18:04:42
      
     
   | 
Update of /cvsroot/springframework/spring/mock/org/springframework/test In directory fdv4jf1.ch3.sourceforge.com:/tmp/cvs-serv4919/mock/org/springframework/test Modified Files: AbstractSpringContextTests.java AbstractTransactionalSpringContextTests.java Log Message: polishing Index: AbstractSpringContextTests.java =================================================================== RCS file: /cvsroot/springframework/spring/mock/org/springframework/test/AbstractSpringContextTests.java,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** AbstractSpringContextTests.java 13 Sep 2007 09:29:18 -0000 1.18 --- AbstractSpringContextTests.java 27 Nov 2008 18:04:35 -0000 1.19 *************** *** 1,4 **** /* ! * Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); --- 1,4 ---- /* ! * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); *************** *** 81,90 **** } /** * Explicitly add an ApplicationContext instance under a given key. ! * <p> ! * This is not meant to be used by subclasses. It is rather exposed for * special test suite environments. - * * @param key the context key * @param context the ApplicationContext instance --- 81,89 ---- } + /** * Explicitly add an ApplicationContext instance under a given key. ! * <p>This is not meant to be used by subclasses. It is rather exposed for * special test suite environments. * @param key the context key * @param context the ApplicationContext instance *************** *** 97,101 **** /** * Return whether there is a cached context for the given key. - * * @param key the context key */ --- 96,99 ---- *************** *** 105,138 **** /** ! * <p> ! * Determines if the supplied context <code>key</code> is <em>empty</em>. ! * </p> ! * <p> ! * By default, <code>null</code> values, empty strings, and zero-length * arrays are considered <em>empty</em>. - * </p> - * * @param key the context key to check ! * @return <code>true</code> if the supplied context key is empty. */ protected boolean isContextKeyEmpty(Object key) { ! return (key == null) || ((key instanceof String) && !StringUtils.hasText((String) key)) ! || ((key instanceof Object[]) && ObjectUtils.isEmpty((Object[]) key)); } /** * Obtain an ApplicationContext for the given key, potentially cached. - * * @param key the context key; may be <code>null</code>. ! * @return the corresponding ApplicationContext instance (potentially ! * cached), or <code>null</code> if the provided <code>key</code> ! * is <em>empty</em>. */ protected final ConfigurableApplicationContext getContext(Object key) throws Exception { - if (isContextKeyEmpty(key)) { return null; } - String keyString = contextKeyString(key); ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) contextKeyToContextMap.get(keyString); --- 103,127 ---- /** ! * Determine if the supplied context <code>key</code> is <em>empty</em>. ! * <p>By default, <code>null</code> values, empty strings, and zero-length * arrays are considered <em>empty</em>. * @param key the context key to check ! * @return <code>true</code> if the supplied context key is empty */ protected boolean isContextKeyEmpty(Object key) { ! return (key == null) || ((key instanceof String) && !StringUtils.hasText((String) key)) || ! ((key instanceof Object[]) && ObjectUtils.isEmpty((Object[]) key)); } /** * Obtain an ApplicationContext for the given key, potentially cached. * @param key the context key; may be <code>null</code>. ! * @return the corresponding ApplicationContext instance (potentially cached), ! * or <code>null</code> if the provided <code>key</code> is <em>empty</em> */ protected final ConfigurableApplicationContext getContext(Object key) throws Exception { if (isContextKeyEmpty(key)) { return null; } String keyString = contextKeyString(key); ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) contextKeyToContextMap.get(keyString); *************** *** 148,158 **** * Mark the context with the given key as dirty. This will cause the cached * context to be reloaded before the next test case is executed. ! * <p> ! * Call this method only if you change the state of a singleton bean, * potentially affecting future tests. */ protected final void setDirty(Object contextKey) { String keyString = contextKeyString(contextKey); ! ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) contextKeyToContextMap.remove(keyString); if (ctx != null) { ctx.close(); --- 137,147 ---- * Mark the context with the given key as dirty. This will cause the cached * context to be reloaded before the next test case is executed. ! * <p>Call this method only if you change the state of a singleton bean, * potentially affecting future tests. */ protected final void setDirty(Object contextKey) { String keyString = contextKeyString(contextKey); ! ConfigurableApplicationContext ctx = ! (ConfigurableApplicationContext) contextKeyToContextMap.remove(keyString); if (ctx != null) { ctx.close(); *************** *** 163,167 **** * Subclasses can override this to return a String representation of their * context key for use in caching and logging. - * * @param contextKey the context key */ --- 152,155 ---- *************** *** 172,178 **** /** * Load a new ApplicationContext for the given key. ! * <p> ! * To be implemented by subclasses. ! * * @param key the context key * @return the corresponding ApplicationContext instance (new) --- 160,164 ---- /** * Load a new ApplicationContext for the given key. ! * <p>To be implemented by subclasses. * @param key the context key * @return the corresponding ApplicationContext instance (new) Index: AbstractTransactionalSpringContextTests.java =================================================================== RCS file: /cvsroot/springframework/spring/mock/org/springframework/test/AbstractTransactionalSpringContextTests.java,v retrieving revision 1.27 retrieving revision 1.28 diff -C2 -d -r1.27 -r1.28 *** AbstractTransactionalSpringContextTests.java 13 Sep 2007 09:07:28 -0000 1.27 --- AbstractTransactionalSpringContextTests.java 27 Nov 2008 18:04:35 -0000 1.28 *************** *** 1,4 **** /* ! * Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); --- 1,4 ---- /* ! * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); *************** *** 27,32 **** * transaction, but normally will roll the transaction back on the completion of * each test. ! * <p> ! * This is useful in a range of circumstances, allowing the following benefits: * <ul> * <li>Ability to delete or insert any data in the database, without affecting --- 27,32 ---- * transaction, but normally will roll the transaction back on the completion of * each test. ! * ! * <p>This is useful in a range of circumstances, allowing the following benefits: * <ul> * <li>Ability to delete or insert any data in the database, without affecting *************** *** 35,54 **** * <li>Ability to write anything to the database without any need to clean up. * </ul> ! * <p> ! * This class is typically very fast, compared to traditional setup/teardown * scripts. ! * <p> ! * If data should be left in the database, call the {@link #setComplete()} * method in each test. The {@link #setDefaultRollback "defaultRollback"} * property, which defaults to "true", determines whether transactions will * complete by default. ! * <p> ! * It is even possible to end the transaction early; for example, to verify lazy * loading behavior of an O/R mapping tool. (This is a valuable away to avoid * unexpected errors when testing a web UI, for example.) Simply call the * {@link #endTransaction()} method. Execution will then occur without a * transactional context. ! * <p> ! * The {@link #startNewTransaction()} method may be called after a call to * {@link #endTransaction()} if you wish to create a new transaction, quite * independent of the old transaction. The new transaction's default fate will --- 35,54 ---- * <li>Ability to write anything to the database without any need to clean up. * </ul> ! * ! * <p>This class is typically very fast, compared to traditional setup/teardown * scripts. ! * ! * <p>If data should be left in the database, call the {@link #setComplete()} * method in each test. The {@link #setDefaultRollback "defaultRollback"} * property, which defaults to "true", determines whether transactions will * complete by default. ! * ! * <p>It is even possible to end the transaction early; for example, to verify lazy * loading behavior of an O/R mapping tool. (This is a valuable away to avoid * unexpected errors when testing a web UI, for example.) Simply call the * {@link #endTransaction()} method. Execution will then occur without a * transactional context. ! * ! * <p>The {@link #startNewTransaction()} method may be called after a call to * {@link #endTransaction()} if you wish to create a new transaction, quite * independent of the old transaction. The new transaction's default fate will *************** *** 57,62 **** * ended in this way. The final transaction will automatically be rolled back * when the test case is torn down. ! * <p> ! * Transactional behavior requires a single bean in the context implementing the * {@link PlatformTransactionManager} interface. This will be set by the * superclass's Dependency Injection mechanism. If using the superclass's Field --- 57,62 ---- * ended in this way. The final transaction will automatically be rolled back * when the test case is torn down. ! * ! * <p>Transactional behavior requires a single bean in the context implementing the * {@link PlatformTransactionManager} interface. This will be set by the * superclass's Dependency Injection mechanism. If using the superclass's Field *************** *** 65,70 **** * {@link AbstractDependencyInjectionSpringContextTests} superclass even when * there is more than one transaction manager in the context. ! * <p> ! * <b>This base class can also be used without transaction management, if no * PlatformTransactionManager bean is found in the context provided.</b> Be * careful about using this mode, as it allows the potential to permanently --- 65,70 ---- * {@link AbstractDependencyInjectionSpringContextTests} superclass even when * there is more than one transaction manager in the context. ! * ! * <p><b>This base class can also be used without transaction management, if no * PlatformTransactionManager bean is found in the context provided.</b> Be * careful about using this mode, as it allows the potential to permanently *************** *** 82,95 **** /** The transaction manager to use */ ! protected PlatformTransactionManager transactionManager; /** Should we roll back by default? */ ! private boolean defaultRollback = true; /** Should we commit the current transaction? */ ! private boolean complete = false; /** Number of transactions started */ ! private int transactionsStarted = 0; /** --- 82,95 ---- /** The transaction manager to use */ ! protected PlatformTransactionManager transactionManager; /** Should we roll back by default? */ ! private boolean defaultRollback = true; /** Should we commit the current transaction? */ ! private boolean complete = false; /** Number of transactions started */ ! private int transactionsStarted = 0; /** *************** *** 98,107 **** * different behavior. */ ! protected TransactionDefinition transactionDefinition = new DefaultTransactionDefinition(); /** * TransactionStatus for this test. Typical subclasses won't need to use it. */ ! protected TransactionStatus transactionStatus; /** --- 98,108 ---- * different behavior. */ ! protected TransactionDefinition transactionDefinition= new DefaultTransactionDefinition(); /** * TransactionStatus for this test. Typical subclasses won't need to use it. */ ! protected TransactionStatus transactionStatus; ! /** *************** *** 109,124 **** */ public AbstractTransactionalSpringContextTests() { - } /** ! * Constructor for AbstractTransactionalSpringContextTests with a JUnit ! * name. */ ! public AbstractTransactionalSpringContextTests(final String name) { ! super(name); } /** * Specify the transaction manager to use. No transaction management will be --- 110,123 ---- */ public AbstractTransactionalSpringContextTests() { } /** ! * Constructor for AbstractTransactionalSpringContextTests with a JUnit name. */ ! public AbstractTransactionalSpringContextTests(String name) { super(name); } + /** * Specify the transaction manager to use. No transaction management will be *************** *** 129,170 **** * {@link AbstractDependencyInjectionSpringContextTests} superclass. */ ! public void setTransactionManager(final PlatformTransactionManager transactionManager) { ! this.transactionManager = transactionManager; } /** * Get the <em>default rollback</em> flag for this test. - * * @see #setDefaultRollback(boolean) * @return The <em>default rollback</em> flag. */ protected boolean isDefaultRollback() { - return this.defaultRollback; } /** - * Subclasses can set this value in their constructor to change the default, - * which is always to roll the transaction back. - */ - public void setDefaultRollback(final boolean defaultRollback) { - - this.defaultRollback = defaultRollback; - } - - /** - * <p> * Determines whether or not to rollback transactions for the current test. ! * </p> ! * <p> ! * The default implementation delegates to {@link #isDefaultRollback()}. * Subclasses can override as necessary. - * </p> - * - * @return The <em>rollback</em> flag for the current test. */ protected boolean isRollback() { - return isDefaultRollback(); } --- 128,157 ---- * {@link AbstractDependencyInjectionSpringContextTests} superclass. */ ! public void setTransactionManager(PlatformTransactionManager transactionManager) { this.transactionManager = transactionManager; } /** + * Subclasses can set this value in their constructor to change the default, + * which is always to roll the transaction back. + */ + public void setDefaultRollback(final boolean defaultRollback) { + this.defaultRollback = defaultRollback; + } + /** * Get the <em>default rollback</em> flag for this test. * @see #setDefaultRollback(boolean) * @return The <em>default rollback</em> flag. */ protected boolean isDefaultRollback() { return this.defaultRollback; } /** * Determines whether or not to rollback transactions for the current test. ! * <p>The default implementation delegates to {@link #isDefaultRollback()}. * Subclasses can override as necessary. */ protected boolean isRollback() { return isDefaultRollback(); } *************** *** 175,179 **** */ protected void preventTransaction() { - this.transactionDefinition = null; } --- 162,165 ---- *************** *** 183,191 **** * the transaction attributes that will be used, so that {@link #setUp()} * and {@link #tearDown()} behavior is modified. - * * @param customDefinition the custom transaction definition */ ! protected void setTransactionDefinition(final TransactionDefinition customDefinition) { ! this.transactionDefinition = customDefinition; } --- 169,175 ---- * the transaction attributes that will be used, so that {@link #setUp()} * and {@link #tearDown()} behavior is modified. * @param customDefinition the custom transaction definition */ ! protected void setTransactionDefinition(TransactionDefinition customDefinition) { this.transactionDefinition = customDefinition; } *************** *** 193,208 **** /** * This implementation creates a transaction before test execution. ! * <p> ! * Override {@link #onSetUpBeforeTransaction()} and/or * {@link #onSetUpInTransaction()} to add custom set-up behavior for * transactional execution. Alternatively, override this method for general * set-up behavior, calling <code>super.onSetUp()</code> as part of your * method implementation. - * * @throws Exception simply let any exception propagate * @see #onTearDown() */ protected void onSetUp() throws Exception { - this.complete = !this.isRollback(); --- 177,189 ---- /** * This implementation creates a transaction before test execution. ! * <p>Override {@link #onSetUpBeforeTransaction()} and/or * {@link #onSetUpInTransaction()} to add custom set-up behavior for * transactional execution. Alternatively, override this method for general * set-up behavior, calling <code>super.onSetUp()</code> as part of your * method implementation. * @throws Exception simply let any exception propagate * @see #onTearDown() */ protected void onSetUp() throws Exception { this.complete = !this.isRollback(); *************** *** 232,240 **** * {@link #preventTransaction()} has not been invoked in an overridden * {@link #runTest()} method. - * * @throws Exception simply let any exception propagate */ protected void onSetUpBeforeTransaction() throws Exception { - } --- 213,219 ---- *************** *** 243,281 **** * as populating a database table, <i>within</i> the transaction created by * this class. ! * <p> ! * <b>NB:</b> Not called if there is no transaction management, due to no * transaction manager being provided in the context. ! * <p> ! * If any {@link Throwable} is thrown, the transaction that has been started * prior to the execution of this method will be * {@link #endTransaction() ended} (or rather an attempt will be made to * {@link #endTransaction() end it gracefully}); The offending * {@link Throwable} will then be rethrown. - * * @throws Exception simply let any exception propagate */ protected void onSetUpInTransaction() throws Exception { - } /** * This implementation ends the transaction after test execution. ! * <p> ! * Override {@link #onTearDownInTransaction()} and/or * {@link #onTearDownAfterTransaction()} to add custom tear-down behavior * for transactional execution. Alternatively, override this method for * general tear-down behavior, calling <code>super.onTearDown()</code> as * part of your method implementation. ! * <p> ! * Note that {@link #onTearDownInTransaction()} will only be called if a * transaction is still active at the time of the test shutdown. In * particular, it will <i>not</i> be called if the transaction has been * completed with an explicit {@link #endTransaction()} call before. - * * @throws Exception simply let any exception propagate * @see #onSetUp() */ protected void onTearDown() throws Exception { - // Call onTearDownInTransaction and end transaction if the transaction // is still active. --- 222,252 ---- * as populating a database table, <i>within</i> the transaction created by * this class. ! * <p><b>NB:</b> Not called if there is no transaction management, due to no * transaction manager being provided in the context. ! * <p>If any {@link Throwable} is thrown, the transaction that has been started * prior to the execution of this method will be * {@link #endTransaction() ended} (or rather an attempt will be made to * {@link #endTransaction() end it gracefully}); The offending * {@link Throwable} will then be rethrown. * @throws Exception simply let any exception propagate */ protected void onSetUpInTransaction() throws Exception { } /** * This implementation ends the transaction after test execution. ! * <p>Override {@link #onTearDownInTransaction()} and/or * {@link #onTearDownAfterTransaction()} to add custom tear-down behavior * for transactional execution. Alternatively, override this method for * general tear-down behavior, calling <code>super.onTearDown()</code> as * part of your method implementation. ! * <p>Note that {@link #onTearDownInTransaction()} will only be called if a * transaction is still active at the time of the test shutdown. In * particular, it will <i>not</i> be called if the transaction has been * completed with an explicit {@link #endTransaction()} call before. * @throws Exception simply let any exception propagate * @see #onSetUp() */ protected void onTearDown() throws Exception { // Call onTearDownInTransaction and end transaction if the transaction // is still active. *************** *** 288,291 **** --- 259,263 ---- } } + // Call onTearDownAfterTransaction if there was at least one // transaction, even if it has been completed early through an *************** *** 301,312 **** * the transaction will still be visible. However, there is no need to clean * up the database, as a rollback will follow automatically. ! * <p> ! * <b>NB:</b> Not called if there is no actual transaction, for example due * to no transaction manager being provided in the application context. - * * @throws Exception simply let any exception propagate */ protected void onTearDownInTransaction() throws Exception { - } --- 273,281 ---- * the transaction will still be visible. However, there is no need to clean * up the database, as a rollback will follow automatically. ! * <p><b>NB:</b> Not called if there is no actual transaction, for example due * to no transaction manager being provided in the application context. * @throws Exception simply let any exception propagate */ protected void onTearDownInTransaction() throws Exception { } *************** *** 314,322 **** * Subclasses can override this method to perform cleanup after a * transaction here. At this point, the transaction is <i>not active anymore</i>. - * * @throws Exception simply let any exception propagate */ protected void onTearDownAfterTransaction() throws Exception { - } --- 283,289 ---- *************** *** 324,333 **** * Cause the transaction to commit for this test method, even if the test * method is configured to {@link #isRollback() rollback}. - * * @throws IllegalStateException if the operation cannot be set to complete ! * as no transaction manager was provided */ protected void setComplete() { - if (this.transactionManager == null) { throw new IllegalStateException("No transaction manager set"); --- 291,298 ---- * Cause the transaction to commit for this test method, even if the test * method is configured to {@link #isRollback() rollback}. * @throws IllegalStateException if the operation cannot be set to complete ! * as no transaction manager was provided */ protected void setComplete() { if (this.transactionManager == null) { throw new IllegalStateException("No transaction manager set"); *************** *** 339,353 **** * Immediately force a commit or rollback of the transaction, according to * the <code>complete</code> and {@link #isRollback() rollback} flags. ! * <p> ! * Can be used to explicitly let the transaction end early, for example to * check whether lazy associations of persistent objects work outside of a * transaction (that is, have been initialized properly). - * * @see #setComplete() */ protected void endTransaction() { - final boolean commit = this.complete || !isRollback(); - if (this.transactionStatus != null) { try { --- 304,314 ---- * Immediately force a commit or rollback of the transaction, according to * the <code>complete</code> and {@link #isRollback() rollback} flags. ! * <p>Can be used to explicitly let the transaction end early, for example to * check whether lazy associations of persistent objects work outside of a * transaction (that is, have been initialized properly). * @see #setComplete() */ protected void endTransaction() { final boolean commit = this.complete || !isRollback(); if (this.transactionStatus != null) { try { *************** *** 372,380 **** * used again in the new transaction. The fate of the new transaction, by * default, will be the usual rollback. - * * @throws TransactionException if starting the transaction failed */ protected void startNewTransaction() throws TransactionException { - if (this.transactionStatus != null) { throw new IllegalStateException("Cannot start new transaction without ending existing transaction: " --- 333,339 ----  |