1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/*
* 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.File;
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.UUID;
import javax.jdo.util.AbstractTest;
import javax.jdo.util.BatchTestRunner;
/**
* Tests class javax.jdo.Enhancer (Enhancer main class).
* <p>
*/
public class EnhancerTest extends AbstractTest {
/** 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
String uuid = UUID.randomUUID().toString();
File uuidDir = new File(basedir + "/target/" + uuid);
uuidDir.mkdirs();
String enhancerJar = "target/" + uuid + "/enhancer-test.jar";
String enhancerJarPathname = basedir + "/" + enhancerJar;
Process create = Runtime.getRuntime().exec("jar -cf " + enhancerJarPathname + " -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 " + enhancerJar);
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(enhancerJar));
}
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());
}
public void testDir() {
// invoke enhancer with directory and not recurse
InvocationResult result = invokeEnhancer("-v " + basedir + "/target/test-classes/enhancer-test-dir");
String outputString = result.getOutputString();
String errorString = result.getErrorString();
assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
assertTrue("Expected directory enhancer-test-dir in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("enhancer-test-dir"));
assertTrue("Expected file file1.jdo in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("file1.jdo"));
assertTrue("Expected file file2.class in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("file2.class"));
assertTrue("Expected file file3.jar in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("file3.jar"));
assertFalse("Expected no directory enhancer-test-subdir in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("enhancer-test-subdir"));
assertTrue("Expected 3 files to be enhanced in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("3"));
}
public void testDirRecurse() {
// invoke enhancer with directory and recurse
InvocationResult result = invokeEnhancer("-v -r " + basedir + "/target/test-classes/enhancer-test-dir");
String outputString = result.getOutputString();
String errorString = result.getErrorString();
assertEquals("Wrong exit code from Enhancer with stderr:\n" + errorString, 0, result.getExitValue());
assertTrue("Expected directory enhancer-test-dir in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("enhancer-test-dir"));
assertTrue("Expected directory enhancer-test-subdir in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("enhancer-test-subdir"));
assertTrue("Expected file file1.jdo in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("file1.jdo"));
assertTrue("Expected file file2.class in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("file2.class"));
assertTrue("Expected file file3.jar in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("file3.jar"));
assertTrue("Expected file file4.jdo in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("file4.jdo"));
assertTrue("Expected file file5.class in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("file5.class"));
assertTrue("Expected file file6.jar in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("file6.jar"));
assertTrue("Expected 6 files to be enhanced in message from out:\n" + outputString + " with err:\n" + errorString, outputString.contains("6"));
}
private InvocationResult invokeEnhancer(String string) {
InvocationResult result = new InvocationResult();
try {
// create the java command to invoke the Enhancer
List<String> commands = new ArrayList<String>();
// 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;
}
}
}
|