Added: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimistic.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimistic.java?view=auto&rev=160090 ============================================================================== --- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimistic.java (added) +++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimistic.java Mon Apr 4 12:41:23 2005 @@ -0,0 +1,90 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.tck.transactions; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.tck.JDO_Test; +import org.apache.jdo.tck.util.BatchTestRunner; + + +/** + *<B>Title:</B> Set Optimistic + *<BR> + *<B>Keywords:</B> transactions + *<BR> + *<B>Assertion ID:</B> A13.4.2-14. + *<BR> + *<B>Assertion Description: </B> + A call to Transaction.setOptimistic causes the optimistic setting passed to replace the optimistic setting + currently active, if the Optimistic optional feature is supported. + + */ + + +/* + * Revision History + * ================ + * Author : Date : Version + * Azita Kamangar 10/09/01 1.0 + */ +public class SetOptimistic extends JDO_Test { + + /** */ + private static final String ASSERTION_FAILED = + "Assertion A13.4.2-14 (SetOptimistic) failed: "; + + /** + * The <code>main</code> is called when the class + * is directly executed from the command line. + * @param args The arguments passed to the program. + */ + public static void main(String[] args) { + BatchTestRunner.run(SetOptimistic.class); + } + + /** */ + public void test() { + pm = getPM(); + + runTestSetOptimistic(pm); + + pm.close(); + pm = null; + } + + /** */ + void runTestSetOptimistic(PersistenceManager pm) { + if (!isOptimisticSupported()) { + if (debug) logger.debug("Optimistic not supported."); + return; + } + + Transaction tx = pm.currentTransaction(); + boolean orig = tx.getOptimistic(); + tx.setOptimistic(!orig); + if (tx.getOptimistic() == orig) { + fail(ASSERTION_FAILED, + "changing the optimistic flag by calling tx.setOptimistic does not have a effect."); + } + if ((tx != null) && tx.isActive()) { + tx.rollback(); + } + } +} +
Propchange: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimistic.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticCalledDuringTxCompletion.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticCalledDuringTxCompletion.java?view=auto&rev=160090 ============================================================================== --- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticCalledDuringTxCompletion.java (added) +++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticCalledDuringTxCompletion.java Mon Apr 4 12:41:23 2005 @@ -0,0 +1,134 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.tck.transactions; + +import javax.jdo.JDOUserException; +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; +import javax.transaction.Synchronization; + +import org.apache.jdo.tck.JDO_Test; +import org.apache.jdo.tck.util.BatchTestRunner; + + +/** + *<B>Title:</B> Set Optimistic Called During TX Completion + *<BR> + *<B>Keywords:</B> transactions + *<BR> + *<B>Assertion ID:</B> A13.4.2-3. + *<BR> + *<B>Assertion Description: </B> + If the setOptimistic method of the Transaction interface is called during commit or rollback processing (within the + beforeCompletion and afterCompletion synchronization methods), a JDOUserException is thrown. + + + */ + + +/* + * Revision History + * ================ + * Author : Date : Version + * Azita Kamangar 10/20/01 1.0 + * + */ +public class SetOptimisticCalledDuringTxCompletion + extends JDO_Test + implements Synchronization { + + private Transaction tx; + + private boolean optimisticFlag; + + /** */ + private static final String ASSERTION_FAILED = + "Assertion A13.4.2-3 (SetOptimisticCalledDuringTxCompletion) failed: "; + + /** + * The <code>main</code> is called when the class + * is directly executed from the command line. + * @param args The arguments passed to the program. + */ + public static void main(String[] args) { + BatchTestRunner.run(SetOptimisticCalledDuringTxCompletion.class); + } + + /** */ + public void beforeCompletion(){ + if (debug) logger.debug ("beforeCompletion"); + try { + tx.setOptimistic(optimisticFlag); + fail(ASSERTION_FAILED, + "tx.setOptimistic called in beforeCompletion should throw JDOUserException."); + } + catch (JDOUserException ex) { + // expected exception + if (debug) logger.debug("caught expected exception " + ex); + } + catch (Exception ex) { + fail(ASSERTION_FAILED, + "tx.setOptimistic called in beforeCompletion throws unexpected exception: " + ex); + } + } + + /** */ + public void afterCompletion(int status) { + if (debug) logger.debug("afterCompletion"); + try { + tx.setOptimistic(optimisticFlag); + } + catch (Exception ex) { + fail(ASSERTION_FAILED, + "tx.setOptimistic called in afterCompletion throws unexpected exception: " + ex); + } + } + + /** */ + public void test() { + pm = getPM(); + + runTestSetOptimisticCalledDuringTxCompletion(pm); + + pm.close(); + pm = null; + } + + /** test transactions.setOptimistic() */ + void runTestSetOptimisticCalledDuringTxCompletion(PersistenceManager pm) { + tx = pm.currentTransaction(); + try { + tx.setSynchronization(this); + + optimisticFlag = false; + tx.begin(); + tx.commit(); + + if (isOptimisticSupported()) { + optimisticFlag = true; + tx.begin(); + tx.commit(); + } + + tx = null; + } + finally { + if ((tx != null) && tx.isActive()) + tx.rollback(); + } + } +} Propchange: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticCalledDuringTxCompletion.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticDuringTransaction.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticDuringTransaction.java?view=auto&rev=160090 ============================================================================== --- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticDuringTransaction.java (added) +++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticDuringTransaction.java Mon Apr 4 12:41:23 2005 @@ -0,0 +1,107 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.tck.transactions; + +import javax.jdo.JDOUserException; +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.tck.JDO_Test; +import org.apache.jdo.tck.util.BatchTestRunner; + + +/** + *<B>Title:</B> Set Optimistic During Transaction + *<BR> + *<B>Keywords:</B> transactions + *<BR> + *<B>Assertion ID:</B> A13.4.2-15. + *<BR> + *<B>Assertion Description: </B> + If Transaction.setOptimistic is called while there is an active transaction, a JDOUserException is thrown. + + + */ + + +/* + * Revision History + * ================ + * Author : Date : Version + * Azita Kamangar 10/09/01 1.0 + */ +public class SetOptimisticDuringTransaction extends JDO_Test { + + /** */ + private static final String ASSERTION_FAILED = + "Assertion A13.4.2-15 (SetOptimisticDuringTransaction) failed: "; + + /** + * The <code>main</code> is called when the class + * is directly executed from the command line. + * @param args The arguments passed to the program. + */ + public static void main(String[] args) { + BatchTestRunner.run(SetOptimisticDuringTransaction.class); + } + + /** */ + public void test() { + pm = getPM(); + + runTestSetOptimisticDuringTransaction(pm); + + pm.close(); + pm = null; + } + + /** */ + void runTestSetOptimisticDuringTransaction(PersistenceManager pm) { + Transaction tx = pm.currentTransaction(); + try { + tx.begin(); + // try to call tx.setOptimistic(true) + if (isOptimisticSupported()) { + try { + tx.setOptimistic(true); + fail(ASSERTION_FAILED, + "calling tx.setOptimistic in the context of an active transaction should throw a JDOUserException."); + } + catch (JDOUserException ex) { + // expected exception + if (debug) logger.debug("caught expected exception " + ex); + } + } + // try to call tx.setOptimistic(false) + try { + tx.setOptimistic(false); + fail(ASSERTION_FAILED, + "calling tx.setOptimistic in the context of an active transaction should throw a JDOUserException."); + } + catch (JDOUserException ex) { + // expected exception + if (debug) logger.debug("caught expected exception " + ex); + } + tx.commit(); + tx = null; + } + finally { + if ((tx != null) && tx.isActive()) + tx.rollback(); + } + } +} Propchange: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticDuringTransaction.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticTrueWhenNotSupported.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticTrueWhenNotSupported.java?view=auto&rev=160090 ============================================================================== --- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticTrueWhenNotSupported.java (added) +++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticTrueWhenNotSupported.java Mon Apr 4 12:41:23 2005 @@ -0,0 +1,90 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.tck.transactions; + +import javax.jdo.JDOUnsupportedOptionException; +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.tck.JDO_Test; +import org.apache.jdo.tck.util.BatchTestRunner; + + +/** + *<B>Title:</B> Set Optimistic True When Not Supported + *<BR> + *<B>Keywords:</B> transactions + *<BR> + *<B>Assertion ID:</B> A13.4.2-7. + *<BR> + *<B>Assertion Description: </B> + If the optional feature Optimistic is not supported, then a call to Transaction.setOptimistic with a value of true will + throw a JDOUnsupportedOptionException. + + */ + + +/* + * Revision History + * ================ + * Author : Date : Version + * Azita Kamangar 10/18/01 1.0 + */ +public class SetOptimisticTrueWhenNotSupported extends JDO_Test { + + /** */ + private static final String ASSERTION_FAILED = + "Assertion A13.4.2-7 (SetOptimisticTrueWhenNotSupported) failed: "; + + /** + * The <code>main</code> is called when the class + * is directly executed from the command line. + * @param args The arguments passed to the program. + */ + public static void main(String[] args) { + BatchTestRunner.run(SetOptimisticTrueWhenNotSupported.class); + } + + /** */ + public void test() { + pm = getPM(); + + runTestSetOptimisticTrueWhenNotSupported(pm); + + pm.close(); + pm = null; + } + + /** */ + void runTestSetOptimisticTrueWhenNotSupported(PersistenceManager pm) { + if (isOptimisticSupported()) { + if (debug) logger.debug("Optimistic supported."); + return; + } + + Transaction tx = pm.currentTransaction(); + try { + tx.setOptimistic(true); + fail(ASSERTION_FAILED, + "tx.setOptimistic(true) should throw JDOUnsupportedOptionException, if the implementation does not support optimistic transactions."); + } + catch (JDOUnsupportedOptionException ex) { + // expected excepted + if (debug) logger.debug("caught expected exception " + ex); + } + } +} Propchange: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetOptimisticTrueWhenNotSupported.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValues.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValues.java?view=auto&rev=160090 ============================================================================== --- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValues.java (added) +++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValues.java Mon Apr 4 12:41:23 2005 @@ -0,0 +1,89 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.tck.transactions; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.tck.JDO_Test; +import org.apache.jdo.tck.util.BatchTestRunner; + + +/** + *<B>Title:</B> Set Retain Values + *<BR> + *<B>Keywords:</B> transactions + *<BR> + *<B>Assertion ID:</B> A13.4.2-18. + *<BR> + *<B>Assertion Description: </B> + The retainValues setting passed to setRetainValues replaces + the retainValues setting currently active. + */ + + +/* + * Revision History + * ================ + * Author : Date : Version + * Azita Kamangar 10/09/01 1.0 + */ +public class SetRetainValues extends JDO_Test { + + /** */ + private static final String ASSERTION_FAILED = + "Assertion A13.4.2-18 (SetRetainValues) failed: "; + + /** + * The <code>main</code> is called when the class + * is directly executed from the command line. + * @param args The arguments passed to the program. + */ + public static void main(String[] args) { + BatchTestRunner.run(SetRetainValues.class); + } + + /** */ + public void test() { + pm = getPM(); + + runTestSetRetainValues(pm); + + pm.close(); + pm = null; + } + + /** */ + void runTestSetRetainValues(PersistenceManager pm) { + if (!isRetainValuesSupported()) { + if (debug) logger.debug("RetainValues not supported."); + return; + } + + Transaction tx = pm.currentTransaction(); + boolean orig = tx.getRetainValues(); + tx.setRetainValues(!orig); + if (tx.getRetainValues() == orig) { + fail(ASSERTION_FAILED, + "changing the retainValues flag by calling tx.setRetainValues does not have a effect."); + } + if ((tx != null) && tx.isActive()) { + tx.rollback(); + } + } +} + Propchange: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValues.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValuesCalledDuringTxCompletion.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValuesCalledDuringTxCompletion.java?view=auto&rev=160090 ============================================================================== --- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValuesCalledDuringTxCompletion.java (added) +++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValuesCalledDuringTxCompletion.java Mon Apr 4 12:41:23 2005 @@ -0,0 +1,137 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.tck.transactions; + +import javax.jdo.JDOUserException; +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; +import javax.transaction.Synchronization; + +import org.apache.jdo.tck.JDO_Test; +import org.apache.jdo.tck.util.BatchTestRunner; + + +/** + *<B>Title:</B> Set Optimistic Called During TX Completion + *<BR> + *<B>Keywords:</B> transactions + *<BR> + *<B>Assertion ID:</B> A13.4.2-4. + *<BR> + *<B>Assertion Description: </B> + If the setOptimistic method of the Transaction interface is called during + * commit or rollback processing (within the beforeCompletion and + * afterCompletion synchronization methods), a JDOUserException is thrown. + */ + + +/* + * Revision History + * ================ + * Author : Date : Version + * Azita Kamangar 10/20/01 1.0 + */ +public class SetRetainValuesCalledDuringTxCompletion + extends JDO_Test + implements Synchronization { + + private Transaction tx; + + private boolean retainValuesFlag; + + /** */ + private static final String ASSERTION_FAILED = + "Assertion A13.4.2-4 (SetRetainValuesCalledDuringTxCompletion) failed: "; + + /** + * The <code>main</code> is called when the class + * is directly executed from the command line. + * @param args The arguments passed to the program. + */ + public static void main(String[] args) { + BatchTestRunner.run(SetRetainValuesCalledDuringTxCompletion.class); + } + + /** */ + public void beforeCompletion(){ + if (debug) logger.debug ("beforeCompletion"); + try { + tx.setRetainValues(retainValuesFlag); + fail(ASSERTION_FAILED, + "tx.setRetainValues called in beforeCompletion should throw JDOUserException."); + } + catch (JDOUserException ex) { + // expected exception + if (debug) logger.debug("caught expected exception " + ex); + } + catch (Exception ex) { + fail(ASSERTION_FAILED, + "tx.setRetainValues called in beforeCompletion throws unexpected exception: " + ex); + } + } + + /** */ + public void afterCompletion(int status) { + if (debug) logger.debug ("afterCompletion"); + try { + tx.setRetainValues(retainValuesFlag); + } + catch (JDOUserException ex) { + // TBD: need to remove this catch block as soon as the JDORI is + // fixed see 'Issue 61: Transaction.isActive issues' + if (debug) logger.debug("caught exception " + ex); + } + catch (Exception ex) { + fail(ASSERTION_FAILED, + "tx.setRetainValues called in afterCompletion throws unexpected exception: " + ex); + } + } + + /** */ + public void test() { + pm = getPM(); + + runTestSetRetainValuesCalledDuringTxCompletion(pm); + + pm.close(); + pm = null; + } + + /** */ + void runTestSetRetainValuesCalledDuringTxCompletion(PersistenceManager pm) { + tx = pm.currentTransaction(); + try { + tx.setSynchronization(this); + + retainValuesFlag = false; + tx.begin(); + tx.commit(); + + if (isRetainValuesSupported()) { + retainValuesFlag = true; + tx.begin(); + tx.commit(); + } + + tx = null; + } + finally { + if ((tx != null) && tx.isActive()) + tx.rollback(); + } + } +} Propchange: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValuesCalledDuringTxCompletion.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValuesTrueWhenNotSupported.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValuesTrueWhenNotSupported.java?view=auto&rev=160090 ============================================================================== --- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValuesTrueWhenNotSupported.java (added) +++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValuesTrueWhenNotSupported.java Mon Apr 4 12:41:23 2005 @@ -0,0 +1,90 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.tck.transactions; + +import javax.jdo.JDOUnsupportedOptionException; +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.tck.JDO_Test; +import org.apache.jdo.tck.util.BatchTestRunner; + + +/** + *<B>Title:</B> Set Retain Values True When Not Supported + *<BR> + *<B>Keywords:</B> transactions + *<BR> + *<B>Assertion ID:</B> A13.4.2-8. + *<BR> + *<B>Assertion Description: </B> + If the optional feature RetainValues is not supported, then a call to Transaction.setRetainValues with a + value of true will cause a JDOUnsupportedOptionException to be thrown. + + */ + + +/* + * Revision History + * ================ + * Author : Date : Version + * Azita Kamangar 10/22/01 1.0 + */ +public class SetRetainValuesTrueWhenNotSupported extends JDO_Test { + + /** */ + private static final String ASSERTION_FAILED = + "Assertion A13.4.2-8 (SetRetainValuesTrueWhenNotSupported) failed: "; + + /** + * The <code>main</code> is called when the class + * is directly executed from the command line. + * @param args The arguments passed to the program. + */ + public static void main(String[] args) { + BatchTestRunner.run(SetRetainValuesTrueWhenNotSupported.class); + } + + /** */ + public void test() { + pm = getPM(); + + runTestSetRetainValuesTrueWhenNotSupported(pm); + + pm.close(); + pm = null; + } + + /** test transactions.setRetainValues() */ + void runTestSetRetainValuesTrueWhenNotSupported(PersistenceManager pm) { + if (isRetainValuesSupported()) { + if (debug) logger.debug("RetainValues supported."); + return; + } + + Transaction tx = pm.currentTransaction(); + try { + tx.setRetainValues(true); + fail(ASSERTION_FAILED, + "tx.setRetainValues(true) should throw JDOUnsupportedOptionException, if the implementation does not support retainValues."); + } + catch (JDOUnsupportedOptionException ex) { + // expected excepted + if (debug) logger.debug("caught expected exception " + ex); + } + } +} Propchange: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetRetainValuesTrueWhenNotSupported.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetSynchronization.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetSynchronization.java?view=auto&rev=160090 ============================================================================== --- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetSynchronization.java (added) +++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetSynchronization.java Mon Apr 4 12:41:23 2005 @@ -0,0 +1,125 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.tck.transactions; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; +import javax.transaction.Synchronization; + +import org.apache.jdo.tck.JDO_Test; +import org.apache.jdo.tck.util.BatchTestRunner; + + +/** + *<B>Title:</B> Set Synchronization + *<BR> + *<B>Keywords:</B> transactions + *<BR> + *<B>Assertion ID:</B> A13.4.3-1. + *<BR> + *<B>Assertion Description: </B> + A call to Transaction.setSynchronization registers a Synchronization instance with the + Transaction for transaction completion notifications. Any Synchronization instance already registered will be replaced. + */ + + +/* + * Revision History + * ================ + * Author : Date : Version + * Azita Kamangar 10/11/01 1.0 + */ +public class SetSynchronization extends JDO_Test implements Synchronization { + + private Transaction tx; + + /** */ + private static final String ASSERTION_FAILED = + "Assertion A13.4.3-1 (SetSynchronization) failed: "; + + /** + * The <code>main</code> is called when the class + * is directly executed from the command line. + * @param args The arguments passed to the program. + */ + public static void main(String[] args) { + BatchTestRunner.run(SetSynchronization.class); + } + + /** */ + public void beforeCompletion(){ + try { + if (debug) + logger.debug("before Complition isActive returns :" + + tx.isActive()); + } + catch (Exception ex) { + fail(ASSERTION_FAILED, + "tx.isActive called in beforeCompletion throws unexpected exception: " + ex); + } + + } + + /** */ + public void afterCompletion(int status) { + try { + if (debug) + logger.debug("after Complition isActive returns :" + + tx.isActive()); + } + catch (Exception ex) { + fail(ASSERTION_FAILED, + "tx.isActive called in afterCompletion throws unexpected exception: " + ex); + } + } + + /** */ + public void test() { + pm = getPM(); + + runTestSetSynchronization(pm); + + pm.close(); + pm = null; + } + + /** */ + void runTestSetSynchronization(PersistenceManager pm) { + tx = pm.currentTransaction(); + try { + tx.begin(); + Synchronization orig = tx.getSynchronization(); + tx.setSynchronization(this); + Synchronization current = tx.getSynchronization(); + if (current == orig) { + fail(ASSERTION_FAILED, + "tx.setSynchronization does not replace Synchronization instances registered with the transaction."); + } + if (current != this) { + fail(ASSERTION_FAILED, + "unexpected sxnchronization isntance, expected " + this + ", got " + current); + } + + tx.commit(); + tx = null; + } + finally { + if ((tx != null) && tx.isActive()) + tx.rollback(); + } + } +} Propchange: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetSynchronization.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetSynchronizationToNull.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetSynchronizationToNull.java?view=auto&rev=160090 ============================================================================== --- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetSynchronizationToNull.java (added) +++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetSynchronizationToNull.java Mon Apr 4 12:41:23 2005 @@ -0,0 +1,104 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.tck.transactions; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.tck.JDO_Test; +import org.apache.jdo.tck.util.BatchTestRunner; + + +/** + *<B>Title:</B> Set Synchronization To Null + *<BR> + *<B>Keywords:</B> transactions + *<BR> + *<B>Assertion ID:</B> A13.4.3-2. + *<BR> + *<B>Assertion Description: </B> + If the parameter to Transaction.setSynchronization is null, then no instance + will be notified. + */ + + +/* + * Revision History + * ================ + * Author : Date : Version + * Azita Kamangar 10/11/01 1.0 + */ +public class SetSynchronizationToNull + extends JDO_Test + implements javax.transaction.Synchronization { + + /** */ + private static final String ASSERTION_FAILED = + "Assertion A13.4.3-2 (SetSynchronizationToNull) failed: "; + + /** + * The <code>main</code> is called when the class + * is directly executed from the command line. + * @param args The arguments passed to the program. + */ + public static void main(String[] args) { + BatchTestRunner.run(SetSynchronizationToNull.class); + } + + /** */ + public void beforeCompletion(){ + fail(ASSERTION_FAILED, + "Instance should not be registered, thus this beforeCompletion methgod should not be called."); + } + + /** */ + public void afterCompletion(int status) { + fail(ASSERTION_FAILED, + "Instance should not be registered, thus this afterCompletion methgod should not be called."); + } + + /** */ + public void test() { + pm = getPM(); + + runTestSetSynchronizationToNull(pm); + + pm.close(); + pm = null; + } + + /** */ + void runTestSetSynchronizationToNull(PersistenceManager pm) { + Transaction tx = pm.currentTransaction(); + try { + tx.begin(); + tx.setSynchronization(this); + tx.setSynchronization(null); + if (tx.getSynchronization() != null) { + fail(ASSERTION_FAILED, + "tx.setSynchronization(null) should overwrite previous registered synchronization instance."); + } + + tx.commit(); + tx = null; + } + finally { + if ((tx != null) && tx.isActive()) + tx.rollback(); + } + } +} Propchange: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/SetSynchronizationToNull.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/WhenNontransactionalReadIsFalse.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/WhenNontransactionalReadIsFalse.java?view=auto&rev=160090 ============================================================================== --- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/WhenNontransactionalReadIsFalse.java (added) +++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/WhenNontransactionalReadIsFalse.java Mon Apr 4 12:41:23 2005 @@ -0,0 +1,140 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.tck.transactions; + +import java.util.Collection; +import java.util.Date; + +import javax.jdo.PersistenceManager; +import javax.jdo.Query; +import javax.jdo.Transaction; +import javax.jdo.JDOUserException; + +import org.apache.jdo.tck.JDO_Test; +import org.apache.jdo.tck.pc.company.Company; +import org.apache.jdo.tck.pc.company.Department; +import org.apache.jdo.tck.util.BatchTestRunner; + + +/** + *<B>Title:</B> When Nontransactional Read Is False + *<BR> + *<B>Keywords:</B> transactions + *<BR> + *<B>Assertion ID:</B> A13.4.2-10. + *<BR> + *<B>Assertion Description: </B> + If this flag is set to false, then queries and field read access + (including navigation) outside an active transaction + throw a JDOUserException. + */ + + +/* + * Revision History + * ================ + * Author : Date : Version + * Michelle Caisse 11/11/04 1.0 + */ +public class WhenNontransactionalReadIsFalse extends JDO_Test { + + /** */ + private static final String ASSERTION_FAILED = + "Assertion A13.4.2-10 (WhenNontransactionalReadIsFalse) failed: "; + + /** + * The <code>main</code> is called when the class + * is directly executed from the command line. + * @param args The arguments passed to the program. + */ + public static void main(String[] args) { + BatchTestRunner.run(WhenNontransactionalReadIsFalse.class); + } + + /** */ + public void test() { + pm = getPM(); + + runTestWhenNontransactionalReadIsFalse(pm); + + pm.close(); + pm = null; + } + + /** test transactions.setNontransactionalReadIsFalse() */ + public void runTestWhenNontransactionalReadIsFalse(PersistenceManager pm) { + Transaction tx = pm.currentTransaction(); + try { + tx.setNontransactionalRead(false); + tx.begin(); + Company c = new Company(1L, "MyCompany", new Date(), null); + Department d = new Department(999, "MyDepartment", c); + pm.makePersistent(c); + pm.makePersistent(d); + if (tx.getNontransactionalRead()) { + fail(ASSERTION_FAILED, + "tx.getNontransactionalRead before commit returns true after setting the flag to false."); + } + tx.commit(); + if (tx.getNontransactionalRead()) { + fail(ASSERTION_FAILED, + "tx.getNontransactionalRead after commit returns true after setting the flag to false."); + } + + // make sure transaction is not active + if (tx.isActive()) { + fail(ASSERTION_FAILED, + "transaction still active after tx.commit."); + } + tx = null; + + try { + // read department name + String name = d.getName(); + fail(ASSERTION_FAILED, + "Field read permitted outside an active transaction when NontransactionalRead is false."); + } catch (JDOUserException juex) { + if (debug) + logger.debug(" Caught expected JDOUserException " + juex); + } + try { + // navigate from department to company + c = d.getCompany(); + fail(ASSERTION_FAILED, + "Navigation permitted outside an active transaction when NontransactionalRead is false."); + } catch (JDOUserException juex) { + if (debug) + logger.debug(" Caught expected JDOUserException " + juex); + } + try { + // run query + Query q = pm.newQuery(Department.class); + q.setFilter("name == \"MyDepartment\""); + Collection result = (Collection)q.execute(); + fail(ASSERTION_FAILED, + "Query permitted outside an active transaction when NontransactionalRead is false."); + } catch (JDOUserException juex) { + if (debug) + logger.debug(" Caught expected JDOUserException " + juex); + } + } + finally { + if ((tx != null) && tx.isActive()) + tx.rollback(); + } + } +} Propchange: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/transactions/WhenNontransactionalReadIsFalse.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/BatchResultPrinter.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/BatchResultPrinter.java?view=auto&rev=160090 ============================================================================== --- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/BatchResultPrinter.java (added) +++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/BatchResultPrinter.java Mon Apr 4 12:41:23 2005 @@ -0,0 +1,103 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.tck.util; + +import java.io.PrintStream; + +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestResult; +import junit.textui.ResultPrinter; + +/** + * Default result printer implementation for running tests in batch mode. + * + * @author Michael Bouschen + */ +public class BatchResultPrinter + extends ResultPrinter +{ + /** */ + public BatchResultPrinter(PrintStream writer) { + super(writer); + } + + /** Called in case of a test error. */ + public void addError(Test test, Throwable t) { + getWriter().print(" ERROR"); + } + + /** Called in case of a test failure. */ + public void addFailure(Test test, AssertionFailedError t) { + getWriter().print(" FAILURE"); + } + + /** Called when a test case is finished. */ + public void endTest(Test test) { + getWriter().println(); + } + + /** Called when a test case is started. */ + public void startTest(Test test) { + String testName; + if (test instanceof TestCase) { + testName = getClassBaseName(test) + "." + ((TestCase)test).getName(); + } + else { + testName = test.toString(); + } + getWriter().print("RUN " + testName); + } + + /** */ + protected void printHeader(long runTime) { + getWriter().println("Time: "+elapsedTimeAsString(runTime)); + } + + /** */ + protected void printFooter(TestResult result) { + if (result.wasSuccessful()) { + getWriter().print("OK"); + getWriter().println (" (" + result.runCount() + " test" + (result.runCount() == 1 ? "": "s") + ")"); + + } else { + getWriter().println("FAILURES!!!"); + getWriter().println("Tests run: "+result.runCount()+ + ", Failures: "+result.failureCount()+ + ", Errors: "+result.errorCount()); + } + } + + // helper method + + /** + * @return Name of the class of the given object without package prefix + */ + private String getClassBaseName(Object obj) { + if (obj == null) return null; + String className = obj.getClass().getName(); + int index = className.lastIndexOf('.'); + if (index != -1) { + className = className.substring(index + 1); + } + return className; + } + +} + + Propchange: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/BatchResultPrinter.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/BatchTestRunner.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/BatchTestRunner.java?view=auto&rev=160090 ============================================================================== --- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/BatchTestRunner.java (added) +++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/BatchTestRunner.java Mon Apr 4 12:41:23 2005 @@ -0,0 +1,175 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.tck.util; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.io.PrintStream; +import java.util.Arrays; + +import junit.framework.Test; +import junit.framework.TestResult; +import junit.framework.TestSuite; +import junit.textui.ResultPrinter; +import junit.textui.TestRunner; + +/** + * TestRunner class for running a single test or a test suite in batch + * mode. The format of the test output is specified by the result printer + * class. The main method sets an exit code according to the test result: + * <ul> + * <li><code>0</code>: success + * <li><code>1</code>: failure, the test shows an unexpected behavior + * <li><code>2</code>: exception, the test throws an unhandled excption + * </ul> + * + * @author Michael Bouschen + */ +public class BatchTestRunner + extends TestRunner +{ + /** Name of the system property to specify the result printer class. */ + public static final String RESULTPRINTER_PROPERTY = "ResultPrinterClass"; + + /** Default of the system property ResultPrinterClass. */ + public static final String RESULTPRINTER_DEFAULT = BatchResultPrinter.class.getName(); + + /** + * Constructor. + * It creates a result printer instance based on the system property + * and delegates to the constructor taking a result printer argument. + */ + public BatchTestRunner() { + super(); + setPrinter(getResultPrinter()); + } + + /** + * Constructor. Uses the specified resultPrinter to format the test result. + */ + public BatchTestRunner(ResultPrinter resultPrinter) { + super(resultPrinter); + } + + /** Runs all test methods from the specified class. */ + public static void run(Class clazz) { + run(new TestSuite(clazz)); + } + + /** Runs the specified test. */ + public static TestResult run(Test test) { + return new BatchTestRunner().doRun(test); + } + + /** Runs the specified test and waits until the user types RETURN. */ + public static void runAndWait(Test suite) { + new BatchTestRunner().doRun(suite, true); + } + + /** + * Runs in batch mode and sets an exit code. If the specified String + * array includes a single fully qualified class name, this test class + * is executed. If it is empty it runs the TestListSuite. + */ + public static void main(String args[]) { + try { + TestResult r = new BatchTestRunner().start(args); + if (!r.wasSuccessful()) + System.exit(FAILURE_EXIT); + System.exit(SUCCESS_EXIT); + } catch(Exception e) { + System.err.println(e.getMessage()); + System.exit(EXCEPTION_EXIT); + } + } + + /** */ + public TestResult start(String[] args) { + Test suite = null; + if ((args == null) || args.length == 0) { + suite = getTest(TestListSuite.class.getName()); + } + else if (args.length == 1) { + suite = getTest(args[0]); + } + else { + suite = new TestListSuite("JDO TCK", Arrays.asList(args)); + } + return doRun(suite); + } + + /** + * Returns a result printer instance. The system property + * ResultPrinterClass specifies the class of the returned instanec. The + * class must extend junit.textui.ResultPrinter. + */ + protected ResultPrinter getResultPrinter() { + String className = System.getProperty(RESULTPRINTER_PROPERTY); + if (className != null) { + className = className.trim(); + if (className.length() != 0) { + String msg = null; + try { + // get class instance + Class clazz = Class.forName(className); + // constructor taking PrintStream arg + Constructor ctor = clazz.getConstructor( + new Class[] { PrintStream.class } ); + // create instance + return (ResultPrinter)ctor.newInstance( + new Object[] { System.out }); + } + catch (ClassNotFoundException ex) { + // specified ResultPrinter class not + msg = "Cannot find specified result printer class " + + className + "."; + } + catch (NoSuchMethodException ex) { + msg = "Class " + className + + " does not provide constructor taking a PrintStream."; + } + catch (InstantiationException ex) { + msg = "Class " + className + " is abstract."; + } + catch (IllegalAccessException ex) { + msg = "Constructor taking a PrintStream of class " + + className + " is not accessible."; + } + catch (InvocationTargetException ex) { + msg = "Constructor call results in exception " + ex + "."; + } + + // ResultPrinter class specified, but not avaiable + System.out.println(msg); + ResultPrinter printer = getDefaultResultPrinter(); + System.out.println("Using default result printer of class " + + printer.getClass().getName()); + } + } + + // ResultPrinter class not specified => use default + return getDefaultResultPrinter(); + } + + /** + * Returns an instance of the default result printer class + * BatchResultPrinter. + */ + protected ResultPrinter getDefaultResultPrinter() { + return new BatchResultPrinter(System.out); + } +} Propchange: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/BatchTestRunner.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/ClassGenerator.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/ClassGenerator.java?view=auto&rev=160090 ============================================================================== --- incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/ClassGenerator.java (added) +++ incubator/jdo/trunk/tck20/test/java/org/apache/jdo/tck/util/ClassGenerator.java Mon Apr 4 12:41:23 2005 @@ -0,0 +1,916 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +//Title: Your Product Name +//Version: +//Copyright: Copyright (c) 1998 +//Author: +//Company: Your Company +//Description: Your description + + +package org.apache.jdo.tck.util; + +import java.io.*; + +public class ClassGenerator { + private final String [] fieldTypes = {"boolean", "byte", "short", "int", "long", + "char", "float", "double", + "Boolean", "Character", "Byte", "Short", "Integer", + "Long", "Float", "Double", "String", + "Locale", "Date", "BigDecimal", "BigInteger", + "Object", "SimpleClass", "SimpleInterface" + }; + //includes a place holder for default package access + private final String [] accessSpecifiers = {"private ", "public ", "protected ", "" }; + + private final String [] fieldModifiers = {"", "static ", "transient ", "final ", "volatile ", + "static transient ", "static final ", "static volatile ", + "transient final ", "transient volatile ", + "static transient final ", "static transient volatile " }; + + private final String [] xmlPersistenceModifiers = {"", "persistence-modifier=\"none\"", + "persistence-modifier=\"persistent\"", + "persistence-modifier=\"transactional\""}; + + private final String [] xmlEmbeddedModifiers = {"", "embedded=\"true\"", "embedded=\"false\""}; + + private final String [] collectionTypes = {"Collection", "Map", "Set", "List", + "HashSet", "ArrayList", "HashMap", "Hashtable", + "LinkedList", "TreeMap", "TreeSet", "Vector", "Array"}; + + //Note: Any updates to elementtypes should include an update to elementsWithPackageInfo + private final String [] elementTypes = {"Object", "SimpleClass", "SimpleInterface", "String", + "Date", "Locale", "BigDecimal", "BigInteger", + "Byte", "Double", "Float", "Integer", "Long", + "Short"}; + + private final String [] elementsWithPackageInfo = {"java.lang.Object", + "org.apache.jdo.tck.pc.fieldtypes.SimpleClass", + "org.apache.jdo.tck.pc.fieldtypes.SimpleInterface", + "java.lang.String", "java.util.Date", + "java.util.Locale", "java.math.BigDecimal", + "java.math.BigInteger", "java.lang.Byte", + "java.lang.Double", "java.lang.Float", + "java.lang.Integer","java.lang.Long", + "java.lang.Short"}; + + private StringBuffer fieldSpecs = new StringBuffer(2000); + private StringBuffer isPersistent = new StringBuffer(200); + private StringBuffer isStatic = new StringBuffer(200); + private StringBuffer isFinalArray = new StringBuffer(2000); + + // can accomodate only 4000 fields, have to increase the capacity if the fields exceed 4000 + private static boolean [] isFinal = new boolean[4000]; + + void generate() throws Exception + { + final String twoSpaces = " "; + final String space = " "; + + for (int i=0; i < fieldTypes.length; i++) + { + String classFile = (isPrimitive(fieldTypes[i]) ? "FieldsOfPrimitive" : "FieldsOf") + fieldTypes[i]; + + FileOutputStream xmlFout = new FileOutputStream(classFile + ".jdo.n"); + PrintWriter xmlPw = new PrintWriter(xmlFout); + FileOutputStream xmlFout1 = new FileOutputStream(classFile + ".jdo.a"); + PrintWriter xmlPw1 = new PrintWriter(xmlFout1); + FileOutputStream xmlFout2 = new FileOutputStream(classFile + ".jdo.d"); + PrintWriter xmlPw2 = new PrintWriter(xmlFout2); + startXmlMetaData(xmlPw); + startXmlMetaData(xmlPw1); + startXmlMetaData(xmlPw2); + + startXmlClass(xmlPw, classFile, 0); + startXmlClass(xmlPw1, classFile, 1); + startXmlClass(xmlPw2, classFile, 2); + + FileOutputStream fout = new FileOutputStream(classFile + ".java"); + PrintWriter pw = new PrintWriter(fout); + startClass(pw, classFile); + int fieldCounter = 0; + for(int j=0; j < accessSpecifiers.length; j++) + { + for (int k=0; k < fieldModifiers.length; k++) + { + for(int l = 0; l < xmlPersistenceModifiers.length; l++) + { + // do not generate persistence modifiers (persistent or transactional or none) + // for fields that cannot be persisted + + + if( (fieldModifiers[k].indexOf("static") >= 0 || fieldModifiers[k].indexOf("final") >= 0 ) && + !xmlPersistenceModifiers[l].equals("")) + continue; + +/* original code + if(!isPersistenceCapable(fieldModifiers[k]) + && !xmlPersistenceModifiers[l].equals("")) + continue; +*/ + for(int m=0; m < xmlEmbeddedModifiers.length; m++) { + // generate persistence modifiers (persistent or transactional or none) + // only for fields that can be persisted + // generate embedded modifiers only for persistent fields + + boolean fieldIsPersistent = !( fieldModifiers[k].indexOf("static") >= 0 || + fieldModifiers[k].indexOf("final") >= 0 || + xmlPersistenceModifiers[l].indexOf("none") >= 0 || + xmlPersistenceModifiers[l].indexOf("transactional") >= 0 || + (fieldModifiers[k].indexOf("transient") >= 0 && xmlPersistenceModifiers[l].indexOf("persistent") == -1) + ); + + if(!xmlEmbeddedModifiers[m].equals("") && !fieldIsPersistent ) + continue; + + StringBuffer sb = new StringBuffer(); + sb.append(twoSpaces); + sb.append(accessSpecifiers[j]); + sb.append(fieldModifiers[k]); + sb.append(fieldTypes[i]); + sb.append(space); + String fieldName = (fieldTypes[i] + fieldCounter++); +//temporary fix to get around the bug in the enhancer code +if(!(xmlEmbeddedModifiers[m].equals("") && xmlPersistenceModifiers[l].equals(""))) +{ + printXmlField(xmlPw, "name=\"" + fieldName + "\" " + + xmlPersistenceModifiers[l]+ " " + + xmlEmbeddedModifiers[m]); + printXmlField(xmlPw1, "name=\"" + fieldName + "\" " + + xmlPersistenceModifiers[l]+ " " + + xmlEmbeddedModifiers[m]); + printXmlField(xmlPw2, "name=\"" + fieldName + "\" " + + xmlPersistenceModifiers[l]+ " " + + xmlEmbeddedModifiers[m]); +}//end temporary fix + sb.append(fieldName); + buildisPersistentArray(fieldIsPersistent); // add to isPersistentArray + buildisStaticArray(isStatic(fieldModifiers[k])); // add to isStaticArray + buildFieldSpecs(xmlPersistenceModifiers[l].replace('"',' ') + " " + + xmlEmbeddedModifiers[m].replace('"',' ') + + sb.toString()); // add to the field specs array + isFinal[fieldCounter-1] = fieldModifiers[k].indexOf("final") >= 0; + if(isFinal[fieldCounter-1]) + sb.append(getInitializerForFinalTypes(fieldTypes[i])); + buildisFinalArray(isFinal[fieldCounter-1]); + sb.append(";"); + pw.println(sb.toString()); + } + } + } + } + writeisPersistentArray(pw); + writeisStaticArray(pw); + writeisFinalArray(pw); + writeFieldSpecs(pw); + writeMethodGetLength(pw); + writeMethodGet(pw, fieldTypes[i], fieldCounter); + writeMethodSet(pw, fieldTypes[i], fieldCounter); + endClass(pw); + pw.close(); + fout.close(); + + endXmlClass(xmlPw); + endXmlClass(xmlPw1); + endXmlClass(xmlPw2); + endXmlMetaDeta(xmlPw); + endXmlMetaDeta(xmlPw1); + endXmlMetaDeta(xmlPw2); + xmlPw.close(); + xmlFout.close(); + xmlPw1.close(); + xmlFout1.close(); + xmlPw2.close(); + xmlFout2.close(); + } + } + + + private void startClass(PrintWriter pw, String className) + { + pw.println("package org.apache.jdo.tck.pc.fieldtypes;"); + pw.println(""); + pw.println(getImportStatements(className)); + pw.println("public class " + className + " { " ); + pw.println(" public int identifier;"); + } + + private void endClass(PrintWriter pw) + { + pw.println(""); + pw.println("}"); + } + + private void startXmlMetaData(PrintWriter pw) + { + pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); + pw.println("<!DOCTYPE jdo SYSTEM \"jdo.dtd\">"); + pw.println("<jdo>"); + pw.println("<package name=\"org.apache.jdo.tck.pc.fieldtypes\">"); + } + + private void endXmlMetaDeta(PrintWriter pw) + { + pw.println("</package>"); + pw.println("</jdo>"); + } + + private void startXmlClass(PrintWriter pw, String className, int type) + { + switch(type) + { + case(0): + pw.println("<class name=\"" + className + "\">"); + break; + case(1): + pw.println("<class name=\"" + className + "\" identity-type=\"application\">"); + break; + case(2): + pw.println("<class name=\"" + className + "\" identity-type=\"datastore\">"); + break; + default: + System.out.println("Unsupported Type"); + } + } + + private void endXmlClass(PrintWriter pw) + { + pw.println("</class>"); + } + + // need to check if we should support extents + // fieldName includes modifiers + private void printXmlField(PrintWriter pw, String fieldName) + { + pw.println("<field " + fieldName + ">"); + pw.println("</field>"); + } + + // fieldname does not include any modifiers. + // element type includes package informaiton. + private void printXmlCollectionFieldWithEmbeddedElement(PrintWriter pw, String fieldName, + String elementType, String embeddedValue) + { + pw.println("<field name=\"" + fieldName + "\" " + ">"); + pw.print("<collection element-type=" + "\"" + elementType + "\""); + if(!embeddedValue.equals("")) + pw.print(" embedded-element=" + "\"" + embeddedValue + "\""); + pw.println(">"); + pw.println("</collection>"); + pw.println("</field>"); + } + + private void printXmlArrayFieldWithEmbeddedElement(PrintWriter pw, String fieldName, + String embeddedValue) + { + pw.println("<field name=\"" + fieldName + "\" " + ">"); + pw.println("<array embedded-element=" + "\"" + embeddedValue + "\"" + ">"); + pw.println("</array>"); + pw.println("</field>"); + } + + private void printXmlMapField(PrintWriter pw, String fieldName, String keyType, + String embeddedKey, String valueType, String embeddedValue) + { + pw.println("<field name=\"" + fieldName + "\" " + ">"); + pw.print("<map "); + if(!keyType.equals("")) + pw.print(" key-type=" + "\"" + keyType + "\""); + if(!embeddedKey.equals("")) + pw.print(" embedded-key=" + "\"" + embeddedKey + "\""); + if(!valueType.equals("")) + pw.print(" value-type=" + "\"" + valueType + "\""); + if(!embeddedValue.equals("")) + pw.print(" embedded-value=" + "\"" + embeddedValue + "\""); + pw.println(">"); + pw.println("</map>"); + pw.println("</field>"); + } + + private String getInitializerForFinalTypes(String fieldType) throws Exception + { + if (fieldType.equals("char")) + return " = 'a'"; //primitive character, return 'a'; + else if (fieldType.equals("boolean")) + return " = false"; //primitive boolean, return 'a'; + else if (Character.isLowerCase(fieldType.charAt(0))) + return " = 5"; // return 0 for all other primitive types + else if (fieldType.equals("Byte")) + return " = new Byte((byte)5)"; + else if (fieldType.equals("Boolean")) + return " = new Boolean(false)"; + else if (fieldType.equals("Character")) + return " = new Character('a')"; + else if (fieldType.equals("Short")) + return " = new Short((short)5)"; + else if (fieldType.equals("Integer")) + return " = new Integer((int)5)"; + else if (fieldType.equals("Long")) + return " = new Long((long)5)"; + else if (fieldType.equals("Float")) + return " = new Float((float)5)"; + else if (fieldType.equals("Double")) + return " = new Double((double)5)"; + else if (fieldType.equals("String")) + return " = new String(\"JDO TCK\")"; + else if (fieldType.equals("Locale")) + return " = Locale.US"; + else if (fieldType.equals("BigDecimal")) + return " = new BigDecimal(100.15)"; + else if (fieldType.equals("BigInteger")) + return " = new BigInteger(\"100\")"; + else if (fieldType.equals("Collection")) + return " = new HashSet()"; + else if (fieldType.equals("Set")) + return " = new HashSet()"; + else if (fieldType.equals("HashSet")) + return " = new HashSet()"; + else if (fieldType.equals("Object")) + return " = new Object()"; + else if (fieldType.equals("SimpleClass")) + return " = new SimpleClass()"; + else if (fieldType.equals("SimpleInterface")) + return " = new SimpleClass()"; + else if (fieldType.equals("Date")) + return " = new Date()"; + else + throw new Exception("Unsupported FieldType " + fieldType); + } + + private String getImportStatements(String fieldType) + { + if (fieldType.equals("FieldsOfLocale")) + return "import java.util.*;"; + if (fieldType.equals("FieldsOfDate")) + return "import java.util.*;"; + else if (fieldType.equals("FieldsOfBigDecimal")) + return "import java.math.*;"; + else if (fieldType.equals("FieldsOfBigInteger")) + return "import java.math.*;"; + else if (fieldType.equals("FieldsOfCollection")) + return "import java.util.*;"; + else if (fieldType.equals("FieldsOfSet")) + return "import java.util.*;"; + else if (fieldType.equals("FieldsOfHashSet")) + return "import java.util.*;"; + else if (fieldType.indexOf("Collections") >=0) + return "import java.util.*;\r\nimport java.math.*;"; + else + return ""; + } + + private boolean isPrimitive(String fieldType) + { + if (Character.isUpperCase(fieldType.charAt(0))) + return false; + else + return true; + } + + private boolean isPersistenceCapable(String fieldModifier) + { + if (fieldModifier.indexOf("static") >= 0 + || fieldModifier.indexOf("final") >= 0 + || fieldModifier.indexOf("transient") >= 0) + return false; + else + return true; + } + + private boolean isStatic(String fieldModifier) + { + if (fieldModifier.indexOf("static") >= 0) + return true; + else + return false; + } + private boolean isFinal(String fieldModifier) + { + return fieldModifier.indexOf("final") >= 0; + } + private void buildisPersistentArray(boolean value) + { + if(isPersistent.length() != 0) + isPersistent.append(","); + if(value) + isPersistent.append("true"); + else + isPersistent.append("false"); + } + + private void buildisStaticArray(boolean value) + { + if(isStatic.length() != 0) + isStatic.append(","); + if(value) + isStatic.append("true"); + else + isStatic.append("false"); + } + + private void buildisFinalArray(boolean value) + { + if(isFinalArray.length() != 0) + isFinalArray.append(","); + if(value) + isFinalArray.append("true"); + else + isFinalArray.append("false"); + } + + + private void writeisPersistentArray(PrintWriter pw) + { + // have to go through this hoopla because pw.println(fieldSpecs.toString()); outputs only + // 1024 characters + char [] charArray = new char[isPersistent.length()]; + isPersistent.getChars(0,isPersistent.length(),charArray,0); + pw.println(""); + pw.println("public static final boolean [] isPersistent = { "); + + int fieldCounter=0; + for(int i = 0; i < charArray.length; i++) + { + pw.print(charArray[i]); + if(charArray[i] == ',') + { + fieldCounter++; + if(fieldCounter == 10) + { + pw.println(""); + pw.flush(); + fieldCounter = 0; + } + } + } + pw.println(""); + pw.println(" };"); + isPersistent = new StringBuffer(2000); + } + + + private void writeisStaticArray(PrintWriter pw) + { + // have to go through this hoopla because pw.println(fieldSpecs.toString()); outputs only + // 1024 characters + char [] charArray = new char[isStatic.length()]; + isStatic.getChars(0,isStatic.length(),charArray,0); + pw.println(""); + pw.println("public static final boolean [] isStatic = { "); + + int fieldCounter=0; + for(int i = 0; i < charArray.length; i++) + { + pw.print(charArray[i]); + if(charArray[i] == ',') + { + fieldCounter++; + if(fieldCounter == 10) + { + pw.println(""); + pw.flush(); + fieldCounter = 0; + } + } + } + pw.println(""); + pw.println(" };"); + isStatic = new StringBuffer(2000); + } + private void writeisFinalArray(PrintWriter pw) + { + // have to go through this hoopla because pw.println(fieldSpecs.toString()); outputs only + // 1024 characters + char [] charArray = new char[isFinalArray.length()]; + isFinalArray.getChars(0,isFinalArray.length(),charArray,0); + pw.println(""); + pw.println("public static final boolean [] isFinal = { "); + + int fieldCounter=0; + for(int i = 0; i < charArray.length; i++) + { + pw.print(charArray[i]); + if(charArray[i] == ',') + { + fieldCounter++; + if(fieldCounter == 10) + { + pw.println(""); + pw.flush(); + fieldCounter = 0; + } + } + } + pw.println(""); + pw.println(" };"); + isFinalArray = new StringBuffer(2000); + } + + private void buildFieldSpecs(String field) + { + if(fieldSpecs.length() != 0) + fieldSpecs.append(","); + + fieldSpecs.append("\""); + fieldSpecs.append(field.trim()); + fieldSpecs.append("\""); + } + + + private void writeFieldSpecs(PrintWriter pw) + { + // have to go through this hoopla because pw.println(fieldSpecs.toString()); outputs only + // 1024 characters + char [] charArray = new char[fieldSpecs.length()]; + fieldSpecs.getChars(0,fieldSpecs.length(),charArray,0); + pw.println(""); + pw.println(" public static final String [] fieldSpecs = { "); + + pw.print(" "); + for(int i = 0; i < charArray.length; i++) + { + pw.print(charArray[i]); + if(charArray[i] == ',') + { + pw.println(""); + pw.print(" "); + pw.flush(); + } + } + pw.println(""); + pw.println(" };"); + fieldSpecs = new StringBuffer(2000); + } + + private void writeMethodGetLength(PrintWriter pw) + { + pw.println(" public int getLength()"); + pw.println(" {"); + pw.println(" return fieldSpecs.length;"); + pw.println(" }"); + } + + private void writeMethodGet(PrintWriter pw, String fieldType, int numberOfFields) + { + pw.println(" public " +fieldType+ " get(int index)"); + pw.println(" {"); + + pw.println(" switch (index)"); + pw.println(" {"); + for(int i = 0; i < numberOfFields; i++) + { + pw.println(" case(" + i + "):"); + pw.println(" return " + fieldType + i +";"); + } + pw.println(" default:"); + pw.println(" throw new IndexOutOfBoundsException();"); + pw.println(" }"); + pw.println(" }"); + } + + private void writeMethodGet(PrintWriter pw, String fieldType, String[] fieldNames, int numFields) + { + pw.println(" public " +fieldType+ " get(int index)"); + pw.println(" {"); + + pw.println(" switch (index)"); + pw.println(" {"); + for(int i = 0; i < numFields; i++) + { + pw.println(" case(" + i + "):"); + pw.println(" return " + fieldNames[i] +";"); + } + pw.println(" default:"); + pw.println(" throw new IndexOutOfBoundsException();"); + pw.println(" }"); + pw.println(" }"); + } + private void writeMethodSet(PrintWriter pw, String fieldType, int numberOfFields) + { + pw.println(" public boolean set(int index," + fieldType + " value"+ ")"); + pw.println(" {"); + pw.println(" if(fieldSpecs[index].indexOf(\"final\") != -1)"); + pw.println(" return false;"); + + pw.println(" switch (index)"); + pw.println(" {"); + for(int i = 0; i < numberOfFields; i++) + { + if(!isFinal[i]) + { + pw.println(" case(" + i + "):"); + pw.println(" " + fieldType + i + "= value" + ";"); + pw.println(" break;" ); + } + } + pw.println(" default:"); + pw.println(" throw new IndexOutOfBoundsException();"); + pw.println(" }"); + pw.println(" return true;"); + pw.println(" }"); + } + + private void writeMethodSet(PrintWriter pw, String fieldType, String [] fieldNames, int numFields) + { + pw.println(" public boolean set(int index," + fieldType + " value"+ ")"); + pw.println(" {"); + pw.println(" if(fieldSpecs[index].indexOf(\"final\") != -1)"); + pw.println(" return false;"); + + pw.println(" switch (index)"); + pw.println(" {"); + for(int i = 0; i < numFields; i++) + { +// if(!isFinal[i]) { + pw.println(" case(" + i + "):"); + pw.println(" " + fieldNames[i] + "= value" + ";"); + pw.println(" break;" ); +// } + } + pw.println(" default:"); + pw.println(" throw new IndexOutOfBoundsException();"); + pw.println(" }"); + pw.println(" return true;"); + pw.println(" }"); + } + + + private void writeMethodSetForArray(PrintWriter pw, String fieldType, String [] fieldNames, int numFields) + { + pw.println(" public boolean set(int index," + fieldType + " value"+ ")"); + pw.println(" {"); + pw.println(" if(fieldSpecs[index].indexOf(\"final\") != -1)"); + pw.println(" return false;"); + + pw.println(" switch (index)"); + pw.println(" {"); + for(int i = 0; i < numFields; i++) + { +// if(!isFinal[i]) { + String fieldName = fieldNames[i]; + String valueType; + pw.println(" case(" + i + "):"); + int indexOfValueType = fieldName.indexOf("Of") + 2; + String valueTypeWithNumber = fieldName.substring(indexOfValueType); + int lastIndexOfValueType = 0; + for (int j=valueTypeWithNumber.length() -1; j>=0; j--) + { + if (Character.isDigit(valueTypeWithNumber.charAt(j))) + { + continue; + }else { + lastIndexOfValueType = j; + break; + } + } + valueType = valueTypeWithNumber.substring(0, lastIndexOfValueType+1); + pw.println(" " + fieldNames[i] + "= (" +valueType + " []) value ;"); + pw.println(" break;" ); +// } + } + pw.println(" default:"); + pw.println(" throw new IndexOutOfBoundsException();"); + pw.println(" }"); + pw.println(" return true;"); + pw.println(" }"); + } + + // generates Collection files for the different collectionTypes + // also, updates the corresponding xml files + // called by generate, after it is done dealing with the generic field types + void generateCollections() throws Exception + { + final String [] embeddedElements = {"", "true", "false"}; + final String [] embeddedElementsForFieldSpec = {"", "embedded-element=true", "embedded-element=false"}; + for(int i=0; i < collectionTypes.length; i++) + { + // Map has a lot of combinations, generate it separately + if(collectionTypes[i].indexOf("Map") >= 0 || collectionTypes[i].equals("Hashtable")) + { + generateMapCollection(collectionTypes[i]); + } + else // Array and the other collections + { + String classFile = collectionTypes[i] + "Collections"; + FileOutputStream fout = new FileOutputStream(classFile + ".java"); + PrintWriter pw = new PrintWriter(fout); + FileOutputStream xmlFout = new FileOutputStream(classFile + ".jdo.n"); + PrintWriter xmlPw = new PrintWriter(xmlFout); + FileOutputStream xmlFout1 = new FileOutputStream(classFile + ".jdo.a"); + PrintWriter xmlPw1 = new PrintWriter(xmlFout1); + FileOutputStream xmlFout2 = new FileOutputStream(classFile + ".jdo.d"); + PrintWriter xmlPw2 = new PrintWriter(xmlFout2); + + startClass(pw, classFile); + startXmlMetaData(xmlPw); + startXmlMetaData(xmlPw1); + startXmlMetaData(xmlPw2); + + startXmlClass(xmlPw, classFile, 0); + startXmlClass(xmlPw1, classFile, 1); + startXmlClass(xmlPw2, classFile, 2); + + int fieldCounter=0; + String fieldNames [] = new String [elementTypes.length * embeddedElements.length]; + for(int j=0; j < elementTypes.length; j++) + { + if( elementTypes[j].equals("Locale") && collectionTypes[i].equals("TreeSet") ) + continue; + for(int k=0; k < embeddedElements.length; k++) + { + if(collectionTypes[i].equals("Array")) + { + if(!embeddedElements[k].equals("")) + { + fieldNames[fieldCounter] = collectionTypes[i] + "Of" + elementTypes[j] + fieldCounter; +// pw.println(" public " + "Object [] "+ fieldNames[fieldCounter] +";"); + pw.println(" public " + elementTypes[j] +" [] " + fieldNames[fieldCounter] +";"); + printXmlArrayFieldWithEmbeddedElement(xmlPw, fieldNames[fieldCounter], embeddedElements[k]); + printXmlArrayFieldWithEmbeddedElement(xmlPw1, fieldNames[fieldCounter], embeddedElements[k]); + printXmlArrayFieldWithEmbeddedElement(xmlPw2, fieldNames[fieldCounter], embeddedElements[k]); + buildFieldSpecs(embeddedElementsForFieldSpec[k] + " " + + "public " + elementTypes[j] +" [] " + fieldNames[fieldCounter]); // add to the field specs array*/ + + fieldCounter++; + } + } + else // Collection + { + fieldNames[fieldCounter] = collectionTypes[i] + "Of" + elementTypes[j] + fieldCounter; + pw.println(" public " + collectionTypes[i] +" "+ fieldNames[fieldCounter] +";"); + printXmlCollectionFieldWithEmbeddedElement(xmlPw, fieldNames[fieldCounter], elementsWithPackageInfo[j], + embeddedElements[k]); + printXmlCollectionFieldWithEmbeddedElement(xmlPw1, fieldNames[fieldCounter], elementsWithPackageInfo[j], + embeddedElements[k]); + printXmlCollectionFieldWithEmbeddedElement(xmlPw2, fieldNames[fieldCounter], elementsWithPackageInfo[j], + embeddedElements[k]); + buildFieldSpecs(embeddedElementsForFieldSpec[k] + " " + + "public " + collectionTypes[i] +" "+ fieldNames[fieldCounter]); // add to the field specs array*/ + fieldCounter++; + + } + } + } + writeFieldSpecs(pw); + writeMethodGetLength(pw); + writeMethodGet(pw, collectionTypes[i].equals("Array")? "Object [] " : collectionTypes[i], fieldNames, fieldCounter); + if(collectionTypes[i].equals("Array")) + writeMethodSetForArray(pw, "Object [] ", fieldNames, fieldCounter); + else + writeMethodSet(pw, collectionTypes[i], fieldNames, fieldCounter); + + endClass(pw); + pw.close(); + fout.close(); + endXmlClass(xmlPw); + endXmlClass(xmlPw1); + endXmlClass(xmlPw2); + endXmlMetaDeta(xmlPw); + endXmlMetaDeta(xmlPw1); + endXmlMetaDeta(xmlPw2); + xmlPw.close(); + xmlFout.close(); + xmlPw1.close(); + xmlFout1.close(); + xmlPw2.close(); + xmlFout2.close(); + } + } + } + + private void generateMapCollection(String mapName) throws Exception + { + final String [] keyTypes1 = {"String"}; + final String [] valueTypes1 = {"Object", "SimpleClass", "SimpleInterface", "String", + "Date", "Locale", "BigDecimal", "BigInteger", + "Byte", "Double", "Float", "Integer", "Long", + "Short"}; + final String [] keyTypes2 = {"Object", "SimpleClass", "SimpleInterface", "String", + "Date", "BigDecimal", "BigInteger", + "Byte", "Double", "Float", "Integer", "Long", + "Short"}; + final String [] valueTypes2 = {"String"}; + + final String [][] keyTypes = {keyTypes1, keyTypes2}; + final String [][] valueTypes = {valueTypes1, valueTypes2}; + + String [] classNameArray = {mapName + "StringKey" + "Collections", + mapName + "StringValue" + "Collections"}; + + for(int i=0; i < classNameArray.length; i++) + { + String classFile = classNameArray[i]; + FileOutputStream fout = new FileOutputStream(classFile + ".java"); + PrintWriter pw = new PrintWriter(fout); + FileOutputStream xmlFout = new FileOutputStream(classFile + ".jdo.n"); + PrintWriter xmlPw = new PrintWriter(xmlFout); + FileOutputStream xmlFout1 = new FileOutputStream(classFile + ".jdo.a"); + PrintWriter xmlPw1 = new PrintWriter(xmlFout1); + FileOutputStream xmlFout2 = new FileOutputStream(classFile + ".jdo.d"); + PrintWriter xmlPw2 = new PrintWriter(xmlFout2); + + startClass(pw, classFile); + startXmlMetaData(xmlPw); + startXmlMetaData(xmlPw1); + startXmlMetaData(xmlPw2); + + startXmlClass(xmlPw, classFile, 0); + startXmlClass(xmlPw1, classFile, 1); + startXmlClass(xmlPw2, classFile, 2); + + fillMapCollection(keyTypes[i], valueTypes[i],mapName, pw, xmlPw, xmlPw1, xmlPw2); + + endClass(pw); + pw.close(); + fout.close(); + endXmlClass(xmlPw); + endXmlClass(xmlPw1); + endXmlClass(xmlPw2); + endXmlMetaDeta(xmlPw); + endXmlMetaDeta(xmlPw1); + endXmlMetaDeta(xmlPw2); + xmlPw.close(); + xmlFout.close(); + xmlPw1.close(); + xmlFout1.close(); + xmlPw2.close(); + xmlFout2.close(); + } + } + + private void fillMapCollection(String [] keyTypes, String [] valueTypes, + String mapName, PrintWriter pw, PrintWriter xmlPw, + PrintWriter xmlPw1, PrintWriter xmlPw2) + { + final String [] embeddedKeys = {"", "true", "false"}; + final String [] embeddedValues = embeddedKeys; + final String [] embeddedKeyForFieldSpec = {"", "embedded-key=true", "embedded-key=false"}; + final String [] embeddedValueForFieldSpec = {"", "embedded-value=true", "embedded-value=false"}; + + int fieldCounter=0; + String fieldNames [] = new String [keyTypes.length * embeddedKeys.length + * valueTypes.length * embeddedValues.length]; + for(int i = 0; i < keyTypes.length; i++) + { + for(int j = 0; j < embeddedKeys.length; j++) + { + for(int k = 0; k < valueTypes.length; k++) + { + for(int l = 0; l < embeddedValues.length; l++) + { + if( keyTypes[i].equals("") && embeddedKeys[j].equals("") + && valueTypes[k].equals("") && embeddedValues[l].equals("")) + continue; + fieldNames[fieldCounter] = mapName+ "Of" + keyTypes[i] +"_"+valueTypes[k] + fieldCounter; + pw.println(" public " + mapName +" " + fieldNames[fieldCounter] +";"); + printXmlMapField(xmlPw, fieldNames[fieldCounter], keyTypes[i], embeddedKeys[j], + valueTypes[k], embeddedValues[l]); + printXmlMapField(xmlPw1, fieldNames[fieldCounter], keyTypes[i], embeddedKeys[j], + valueTypes[k], embeddedValues[l]); + printXmlMapField(xmlPw2, fieldNames[fieldCounter], keyTypes[i], embeddedKeys[j], + valueTypes[k], embeddedValues[l]); + buildFieldSpecs(embeddedKeyForFieldSpec[j] + " " + + embeddedValueForFieldSpec[l] + " " + + "public " + mapName +" "+ fieldNames[fieldCounter]); // add to the field specs array*/ + fieldCounter++; + + } + } + } + } + writeFieldSpecs(pw); + writeMethodGetLength(pw); + writeMethodGet(pw, mapName, fieldNames, fieldCounter); + writeMethodSet(pw, mapName, fieldNames, fieldCounter); + } + + public static void main(String[] args) + { + ClassGenerator classGenerator = new ClassGenerator(); + try + { + classGenerator.generate(); + classGenerator.generateCollections(); + } + catch (Exception e) + { + System.out.println(e); + e.printStackTrace(); + } + } +}