Guillaume Nodet | 05fac96 | 2009-04-27 10:01:58 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package org.apache.servicemix.jpm; |
| 18 | |
| 19 | import java.io.File; |
| 20 | |
| 21 | import junit.framework.TestCase; |
| 22 | import org.apache.servicemix.jpm.impl.ScriptUtils; |
| 23 | |
| 24 | public class ProcessTest extends TestCase { |
| 25 | |
| 26 | public void testCreate() throws Exception { |
| 27 | String javaPath = new File(System.getProperty("java.home"), ScriptUtils.isWindows() ? "bin\\java.exe" : "bin/java").getCanonicalPath(); |
| 28 | System.err.println(javaPath); |
| 29 | StringBuilder command = new StringBuilder(); |
| 30 | command.append(javaPath); |
| 31 | command.append(" -Dprop=\"key\""); |
| 32 | command.append(" -classpath "); |
| 33 | String clRes = getClass().getName().replace('.', '/') + ".class"; |
| 34 | String str = getClass().getClassLoader().getResource(clRes).toString(); |
| 35 | str = str.substring("file:".length(), str.indexOf(clRes)); |
| 36 | command.append(str); |
| 37 | command.append(" "); |
| 38 | command.append(MainTest.class.getName()); |
| 39 | command.append(" "); |
| 40 | command.append(60000); |
| 41 | System.err.println("Executing: " + command.toString()); |
| 42 | |
| 43 | ProcessBuilder builder = ProcessBuilderFactory.newInstance().newBuilder(); |
| 44 | Process p = builder.command(command.toString()).start(); |
| 45 | assertNotNull(p); |
| 46 | System.err.println("Process: " + p.getPid()); |
| 47 | assertNotNull(p.getPid()); |
| 48 | Thread.currentThread().sleep(1000); |
| 49 | System.err.println("Running: " + p.isRunning()); |
| 50 | assertTrue(p.isRunning()); |
| 51 | System.err.println("Destroying"); |
| 52 | p.destroy(); |
| 53 | Thread.currentThread().sleep(1000); |
| 54 | System.err.println("Running: " + p.isRunning()); |
| 55 | assertFalse(p.isRunning()); |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * When the process creation fails, no error is reported by the script |
| 60 | * |
| 61 | public void testFailure() throws Exception { |
| 62 | ProcessBuilder builder = ProcessBuilderFactory.newInstance().newBuilder(); |
| 63 | Process p = builder.command("ec").start(); |
| 64 | fail("An exception should have been thrown"); |
| 65 | } |
| 66 | */ |
| 67 | } |