Index: api2/test/java/javax/jdo/EnhancerTest.java
===================================================================
--- api2/test/java/javax/jdo/EnhancerTest.java (revision 0)
+++ api2/test/java/javax/jdo/EnhancerTest.java (revision 0)
@@ -0,0 +1,343 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 javax.jdo;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.nio.CharBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import javax.jdo.util.AbstractTest;
+import javax.jdo.util.BatchTestRunner;
+
+
+/**
+ * Tests class javax.jdo.Enhancer (Enhancer main class).
+ *
+ */
+public class EnhancerTest extends AbstractTest implements Constants {
+
+ /** The path delimiter for constructing classpaths. */
+ private static String pathDelimiter = System.getProperty("path.separator");
+
+ /** The maven basedir identifying the directory of the execution environment. */
+ private static String basedir = System.getProperty("basedir");
+
+ /** */
+ public static void main(String args[]) {
+ BatchTestRunner.run(EnhancerTest.class);
+ }
+
+ public void testUsageOption() {
+ // invoke enhancer with a usage option
+ InvocationResult result = invokeEnhancer("?");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected Usage message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("javax.jdo.Enhancer"));
+ }
+
+ public void testHelpOption() {
+ // invoke enhancer with a usage option
+ InvocationResult result = invokeEnhancer("-help");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected Usage message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("javax.jdo.Enhancer"));
+ }
+
+ public void testHOption() {
+ // invoke enhancer with a usage option
+ InvocationResult result = invokeEnhancer("-h");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected Usage message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("javax.jdo.Enhancer"));
+ }
+
+ public void testInvalidOption() {
+ // invoke enhancer with an invalid option
+ InvocationResult result = invokeEnhancer("-poo");
+ assertEquals("Wrong return value ", ENHANCER_USAGE_ERROR, result.getExitValue());
+ String errorString = result.getErrorString();
+ assertTrue("Expected Usage message from err:\n" + errorString, errorString.contains("javax.jdo.Enhancer"));
+ }
+
+ public void testProperties() {
+ // invoke enhancer with verbose option
+ InvocationResult result = invokeEnhancer("-v");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected MockEnhancer vendor message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains(PROPERTY_ENHANCER_VENDOR_NAME));
+ assertTrue("Expected MockEnhancer version message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains(PROPERTY_ENHANCER_VERSION_NUMBER));
+ assertTrue("Expected MockEnhancer vendor message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("Mock Enhancer"));
+ assertTrue("Expected MockEnhancer vendor message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("2.3.0"));
+ assertTrue("Expected MockEnhancer properties message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("MockKey"));
+ }
+
+ public void testVOption() {
+ // invoke enhancer with verbose option
+ InvocationResult result = invokeEnhancer("-v");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected Enhancer class message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("javax.jdo.MockEnhancer"));
+ }
+
+ public void testVerboseOption() {
+ // invoke enhancer with verbose option
+ InvocationResult result = invokeEnhancer("-verbose");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected Enhancer class message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("javax.jdo.MockEnhancer"));
+ }
+
+ public void testVerboseClasses() {
+ // invoke enhancer with .class parameter
+ InvocationResult result = invokeEnhancer("-v some.class");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected class message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("some.class"));
+ assertTrue("Expected number of classes from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("1"));
+ }
+
+ public void testVerboseJars() {
+ // invoke enhancer with a .jar parameter
+ InvocationResult result = invokeEnhancer("-v some.jar");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected jar message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("some.jar"));
+ assertTrue("Expected number of jars from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("1"));
+ }
+
+ public void testVerboseJDOs() {
+ // invoke enhancer with a .jdo parameter
+ InvocationResult result = invokeEnhancer("-v some.jdo");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected jdo message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("some.jdo"));
+ assertTrue("Expected number of jdos from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("1"));
+ }
+
+ public void testVerboseAll() {
+ // invoke enhancer with multiple parameters
+ InvocationResult result = invokeEnhancer("-v some.class some.jar some.jdo");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected jdo message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("some.jdo"));
+ assertTrue("Expected jar message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("some.jar"));
+ assertTrue("Expected class message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("some.class"));
+ assertTrue("Expected number of elements from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("3"));
+ }
+
+ public void testVerboseCheckonlyAll() {
+ // invoke enhancer with a checkonly option
+ InvocationResult result = invokeEnhancer("-v -checkonly some.class some.jar some.jdo");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected jdo message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("some.jdo"));
+ assertTrue("Expected jar message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("some.jar"));
+ assertTrue("Expected class message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("some.class"));
+ assertTrue("Expected number of elements from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("3"));
+ }
+
+ public void testMissingPU() {
+ // invoke enhancer with missing parameter
+ InvocationResult result = invokeEnhancer("-v -pu");
+ assertEquals("Wrong return value ", 3, result.getExitValue());
+ }
+
+ public void testVerbosePU() {
+ // invoke enhancer with a pu parameter
+ InvocationResult result = invokeEnhancer("-v -pu myPU -pu yourPU");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected pu message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("myPU"));
+ assertTrue("Expected pu message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("yourPU"));
+ assertTrue("Expected number of elements from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("2"));
+ }
+
+ public void testClasspath() {
+ // invoke enhancer with a classpath parameter
+ // JDOHelper must be loadable from this path
+ // the File.toURI should append "/" to the path, so only "target/classes" is needed
+ InvocationResult result = invokeEnhancer("-v -cp " + basedir + "/target/classes");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected classpath message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("target/classes"));
+ }
+
+ public void testBadClasspath() {
+ // invoke enhancer with a bad classpath parameter
+ // JDOHelper is not loadable from this path
+ InvocationResult result = invokeEnhancer("-v -cp target");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 1, result.getExitValue());
+ assertTrue("Expected classpath error message from out:\n" + outputString + " with err:\n" + errorString, errorString.contains("JDOHelper"));
+ }
+
+ public void testClasspathJar() throws IOException, InterruptedException {
+ // invoke enhancer with a classpath parameter
+ // JDOHelper must be loadable from this path
+ // create the jar file from the target/classes directory
+ Process create = Runtime.getRuntime().exec("jar -cf " + basedir + "/target/enhancer-test.jar -C " + basedir + "/target/classes .");
+ int returnCode = create.waitFor();
+ assertEquals("jar command returned wrong return code.", 0, returnCode);
+ // find the jdo.jar in target
+ InvocationResult result = invokeEnhancer("-v -cp " + basedir + "/target/enhancer-test.jar");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected classpath message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("target/enhancer-test.jar"));
+ // remove the jar file if successful
+ Runtime.getRuntime().exec("rm target/enhancer-test.jar").waitFor();
+ }
+
+ public void testOutputDirectory() {
+ // invoke enhancer with an output directory parameter
+ InvocationResult result = invokeEnhancer("-v -d some/output/directory");
+ String outputString = result.getOutputString();
+ String errorString = result.getErrorString();
+ assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
+ assertTrue("Expected directory message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("some/output/directory"));
+ }
+
+ public void testMissingOutputDirectory() {
+ // invoke enhancer with missing parameter
+ InvocationResult result = invokeEnhancer("-v -d");
+ assertEquals("Wrong return value ", 3, result.getExitValue());
+ }
+
+ private InvocationResult invokeEnhancer(String string) {
+ InvocationResult result = new InvocationResult();
+ try {
+ // create the java command to invoke the Enhancer
+ List commands = new ArrayList();
+ // find the java command in the user's path
+ commands.add("java");
+ commands.add("-cp");
+ commands.add("" + basedir + "/target/classes" + pathDelimiter + "" + basedir + "/target/test-classes");
+ commands.add("javax.jdo.Enhancer");
+ // add the test options (from the method parameter) to the java command
+ String[] optionArray = string.split(" ");
+ for (String option: optionArray) {
+ commands.add(option);
+ }
+ String[] cmdarray = commands.toArray(new String[commands.size()]);
+ ProcessBuilder builder = new ProcessBuilder(cmdarray);
+ Process proc = builder.start();
+ InputStream stdout = proc.getInputStream();
+ InputStream stderr = proc.getErrorStream();
+ CharBuffer outBuffer = CharBuffer.allocate(1000000);
+ CharBuffer errBuffer = CharBuffer.allocate(1000000);
+ Thread outputThread = createReaderThread(stdout, outBuffer);
+ Thread errorThread = createReaderThread(stderr, errBuffer);
+ int exitValue = proc.waitFor();
+ result.setExitValue(exitValue);
+ errorThread.join(10000); // wait ten seconds to get stderr after process terminates
+ outputThread.join(10000); // wait ten seconds to get stdout after process terminates
+ result.setErrorString(errBuffer.toString());
+ result.setOutputString(outBuffer.toString());
+ // wait until the Enhancer command finishes
+ } catch (InterruptedException ex) {
+ throw new RuntimeException("InterruptedException", ex);
+ } catch (IOException ex) {
+ throw new RuntimeException("IOException", ex);
+ } catch (JDOException jdoex) {
+ jdoex.printStackTrace();
+ Throwable[] throwables = jdoex.getNestedExceptions();
+ System.out.println("Exception throwables of size: " + throwables.length);
+ for (Throwable throwable: throwables) {
+ throwable.printStackTrace();
+ }
+ }
+ return result;
+ }
+
+ private Thread createReaderThread(final InputStream input, final CharBuffer output) {
+ final Reader reader = new InputStreamReader(input);
+ Thread thread = new Thread(
+ new Runnable() {
+ public void run() {
+ int count = 0;
+ int outputBytesRead = 0;
+ try {
+ while (-1 != (outputBytesRead = reader.read(output))) {
+ count += outputBytesRead;
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ output.flip();
+ }
+ }
+ });
+ thread.start();
+ return thread;
+ }
+
+ class InvocationResult {
+ private int exitValue;
+ private String errorString;
+ private String outputString;
+
+ int getExitValue() {
+ return exitValue;
+ }
+
+ private void setExitValue(int exitValue) {
+ this.exitValue = exitValue;
+ }
+
+ private void setErrorString(String errorString) {
+ this.errorString = errorString;
+ }
+
+ String getErrorString() {
+ return errorString;
+ }
+
+ private void setOutputString(String outputString) {
+ this.outputString = outputString;
+ }
+
+ String getOutputString() {
+ return outputString;
+ }
+
+ }
+
+}
Property changes on: api2/test/java/javax/jdo/EnhancerTest.java
___________________________________________________________________
Name: svn:eol-style
+ LF
Index: api2/test/java/javax/jdo/MockEnhancer.java
===================================================================
--- api2/test/java/javax/jdo/MockEnhancer.java (revision 0)
+++ api2/test/java/javax/jdo/MockEnhancer.java (revision 0)
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 javax.jdo;
+
+
+import java.lang.instrument.IllegalClassFormatException;
+import java.security.ProtectionDomain;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+
+import javax.jdo.metadata.JDOMetadata;
+
+
+/**
+ * Tests class javax.jdo.Enhancer (main class).
+ *
+ */
+public class MockEnhancer implements JDOEnhancer, Constants {
+
+ static Properties props = new Properties();
+ static {
+ props.put(PROPERTY_ENHANCER_VENDOR_NAME, "Mock Enhancer");
+ props.put(PROPERTY_ENHANCER_VERSION_NUMBER, "2.3.0");
+ props.put("MockKey", "MockValue");
+ }
+ @SuppressWarnings("unused")
+ private boolean verbose;
+ private int numberOfElements;
+ private List classNames = new ArrayList();
+ private List jarNames = new ArrayList();
+ private List jdoNames = new ArrayList();
+ private List puNames = new ArrayList();
+ @SuppressWarnings("unused")
+ private String outputDirectory = null;
+
+ public MockEnhancer(){
+ }
+
+ public Properties getProperties() {
+ return props;
+ }
+
+ public JDOEnhancer setVerbose(boolean flag) {
+ this.verbose = flag;
+ return this;
+ }
+
+ public JDOEnhancer setOutputDirectory(String dirName) {
+ outputDirectory = dirName;
+ return this;
+ }
+
+ public JDOEnhancer setClassLoader(ClassLoader loader) {
+ // check to see if JDOHelper is loadable from the loader
+ try {
+ loader.loadClass("javax.jdo.JDOHelper");
+ } catch (ClassNotFoundException ex) {
+ // bad
+ throw new JDOFatalInternalException("Should be able to load JDOHelper from the class loader");
+ }
+ return this;
+ }
+
+ public JDOEnhancer addPersistenceUnit(String persistenceUnit) {
+ numberOfElements++;
+ this.puNames.add(persistenceUnit);
+ return this;
+ }
+
+ public JDOEnhancer addClass(String className, byte[] bytes) {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ public JDOEnhancer addClasses(String... classNames) {
+ numberOfElements += classNames.length;
+ this.classNames.addAll(Arrays.asList(classNames));
+ return this;
+ }
+
+ public JDOEnhancer addFiles(String... metadataFiles) {
+ numberOfElements += metadataFiles.length;
+ this.jdoNames.addAll(Arrays.asList(metadataFiles));
+ return this;
+ }
+
+ public JDOEnhancer addJar(String jarFileName) {
+ numberOfElements++;
+ this.jarNames.add(jarFileName);
+ return this;
+ }
+
+ public int enhance() {
+ return numberOfElements;
+ }
+
+ public int validate() {
+ return numberOfElements;
+ }
+
+ public byte[] getEnhancedBytes(String className) {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ public void registerMetadata(JDOMetadata metadata) {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ public JDOMetadata newMetadata() {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ public byte[] transform(ClassLoader loader, String className, Class> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+}
+
Property changes on: api2/test/java/javax/jdo/MockEnhancer.java
___________________________________________________________________
Name: svn:eol-style
+ LF
Index: api2/test/resources/META-INF/services/javax.jdo.JDOEnhancer
===================================================================
--- api2/test/resources/META-INF/services/javax.jdo.JDOEnhancer (revision 0)
+++ api2/test/resources/META-INF/services/javax.jdo.JDOEnhancer (revision 0)
@@ -0,0 +1 @@
+javax.jdo.MockEnhancer
Index: api2/project.xml
===================================================================
--- api2/project.xml (revision 811080)
+++ api2/project.xml (working copy)
@@ -85,12 +85,18 @@
${basedir}/test/schema
- **/*.jdo
- **/*.jdoquery
- **/*.orm
- **/jdoconfig.xml
+ **/*.jdo
+ **/*.jdoquery
+ **/*.orm
+ **/jdoconfig.xml
+
+ ${basedir}/test/resources
+
+ **/*
+
+ jar:install
Index: api2/src/java/javax/jdo/Constants.java
===================================================================
--- api2/src/java/javax/jdo/Constants.java (revision 811080)
+++ api2/src/java/javax/jdo/Constants.java (working copy)
@@ -565,6 +565,39 @@
= "javax.jdo.PersistenceManagerFactoryClass";
/**
+ * "VendorName"
+ *
+ * @see JDOEnhancer#getProperties()
+ * @since 2.3
+ */
+ static String PROPERTY_ENHANCER_VENDOR_NAME
+ = "VendorName";
+
+ /**
+ * "VersionNumber"
+ *
+ * @see JDOEnhancer#getProperties()
+ * @since 2.3
+ */
+ static String PROPERTY_ENHANCER_VERSION_NUMBER
+ = "VersionNumber";
+
+ /** Exit value for no enhancer found
+ * @since 2.3
+ * */
+ public static int ENHANCER_NO_JDO_ENHANCER_FOUND = 2;
+
+ /** Exit value for usage error
+ * @since 2.3
+ * */
+ public static int ENHANCER_USAGE_ERROR = 3;
+
+ /** Exit value for an exception from the JDOEnhancer
+ * @since 2.3
+ * */
+ public static int ENHANCER_EXCEPTION = 1;
+
+ /**
* "javax.jdo.option.Optimistic"
*
* @see PersistenceManagerFactory#getOptimistic()
Index: api2/src/java/javax/jdo/JDOHelper.java
===================================================================
--- api2/src/java/javax/jdo/JDOHelper.java (revision 811080)
+++ api2/src/java/javax/jdo/JDOHelper.java (working copy)
@@ -89,7 +89,7 @@
/**
* A mapping from jdoconfig.xsd element attributes to PMF properties.
*/
- static final Map ATTRIBUTE_PROPERTY_XREF
+ static final Map ATTRIBUTE_PROPERTY_XREF
= createAttributePropertyXref();
/** The Internationalization message helper.
@@ -102,8 +102,8 @@
* @return An unmodifiable Map of jdoconfig.xsd element attributes to PMF
* properties.
*/
- static Map createAttributePropertyXref() {
- Map xref = new HashMap();
+ static Map createAttributePropertyXref() {
+ Map xref = new HashMap();
xref.put(
PMF_ATTRIBUTE_CLASS,
@@ -396,7 +396,7 @@
*/
public static Collection