Skip to content

Commit

Permalink
Add basic tests for the launcher package
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Sep 20, 2024
1 parent 3837d17 commit c19c812
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class VmsCommandLauncher extends Java13CommandLauncher {
/**
* Writes the command into a temporary DCL script and returns the corresponding File object. The script will be deleted on exit.
*/
private File createCommandFile(final CommandLine cmd, final Map<String, String> env) throws IOException {
File createCommandFile(final CommandLine cmd, final Map<String, String> env) throws IOException {
final Path path = Files.createTempFile("EXEC", ".TMP");
final File script = path.toFile();
script.deleteOnExit();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 org.apache.commons.exec.launcher;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

abstract class AbstractCommandLauncherTest<T extends CommandLauncher> {

abstract T createCommandLauncher();

@Test
public void testIsFailure() throws Exception {
final T commandLauncher = createCommandLauncher();
assertTrue(commandLauncher.isFailure(2));
assertTrue(commandLauncher.isFailure(1));
}

@Test
public void testIsFailureZero() throws Exception {
assertFalse(createCommandLauncher().isFailure(0));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 org.apache.commons.exec.launcher;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;

/**
* Tests {@link CommandLauncherFactory}.
*/
public class CommandLauncherFactoryTest {

@Test
public void testCreateVMLauncher() throws Exception {
assertNotNull(CommandLauncherFactory.createVMLauncher());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 org.apache.commons.exec.launcher;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.apache.commons.exec.CommandLine;

public class CommandLauncherImplTest extends AbstractCommandLauncherTest<CommandLauncherImpl> {

static class CommandLauncherImplFixture extends CommandLauncherImpl {

@Override
public Process exec(final CommandLine cmd, final Map<String, String> env, final File workingDir) throws IOException {
return null;
}

}

@Override
CommandLauncherImpl createCommandLauncher() {
return new CommandLauncherImplFixture();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 org.apache.commons.exec.launcher;

public class Java13CommandLauncherTest extends AbstractCommandLauncherTest<Java13CommandLauncher> {

@Override
Java13CommandLauncher createCommandLauncher() {
return new Java13CommandLauncher();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 org.apache.commons.exec.launcher;

public class OS2CommandLauncherTest extends AbstractCommandLauncherTest<OS2CommandLauncher> {

@Override
OS2CommandLauncher createCommandLauncher() {
return new OS2CommandLauncher(CommandLauncherFactory.createVMLauncher());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 org.apache.commons.exec.launcher;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.util.HashMap;

import org.apache.commons.exec.CommandLine;
import org.junit.jupiter.api.Test;

public class VmsCommandLauncherTest extends AbstractCommandLauncherTest<VmsCommandLauncher> {

@Override
VmsCommandLauncher createCommandLauncher() {
return new VmsCommandLauncher();
}

@Test
public void testCreateCommandFile() throws IOException {
final VmsCommandLauncher commandLauncher = createCommandLauncher();
final CommandLine cl = CommandLine.parse("a b \"c d\"");
assertNotNull(commandLauncher.createCommandFile(cl, null));
final HashMap<String, String> env = new HashMap<>();
assertNotNull(commandLauncher.createCommandFile(cl, env));
env.put("EnvKey", "EnvValue");
assertNotNull(commandLauncher.createCommandFile(cl, env));

}

@Override
@Test
public void testIsFailure() throws Exception {
final CommandLauncher commandLauncher = createCommandLauncher();
assertTrue(commandLauncher.isFailure(2));
assertFalse(commandLauncher.isFailure(1));
}

@Override
@Test
public void testIsFailureZero() throws Exception {
assertTrue(createCommandLauncher().isFailure(0));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 org.apache.commons.exec.launcher;

public class WinNTCommandLauncherTest extends AbstractCommandLauncherTest<WinNTCommandLauncher> {

@Override
WinNTCommandLauncher createCommandLauncher() {
return new WinNTCommandLauncher(CommandLauncherFactory.createVMLauncher());
}

}

0 comments on commit c19c812

Please sign in to comment.