Przeglądaj źródła

FOP-2903: Do not delete files on syntax errors using command line by Dave Roxburgh

tags/2_9
Simon Steiner 9 miesięcy temu
rodzic
commit
a731bf3ab2

+ 13
- 8
fop-core/src/main/java/org/apache/fop/cli/CommandLineOptions.java Wyświetl plik

private File iffile; private File iffile;
/* area tree input file */ /* area tree input file */
private File imagefile; private File imagefile;
/* output filename */
private String outfilename;
/* output file */ /* output file */
private File outfile; private File outfile;
/* input mode */ /* input mode */
System.exit(1); System.exit(1);
} }
} }
if (outfilename != null) {
outfile = new File(outfilename);
}
return true; return true;
} // end parseOptions } // end parseOptions


if (isSystemInOutFile(filename)) { if (isSystemInOutFile(filename)) {
this.useStdOut = true; this.useStdOut = true;
} else { } else {
outfile = new File(filename);
outfilename = filename;
} }
} }


log.info("not set"); log.info("not set");
} else if (MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputmode)) { } else if (MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputmode)) {
log.info("awt on screen"); log.info("awt on screen");
if (outfile != null) {
if (outfilename != null) {
log.error("awt mode, but outfile is set:"); log.error("awt mode, but outfile is set:");
log.error("out file: " + outfile.toString());
log.error("out file: " + outfilename);
} }
} else if (MimeConstants.MIME_FOP_PRINT.equals(outputmode)) { } else if (MimeConstants.MIME_FOP_PRINT.equals(outputmode)) {
log.info("print directly"); log.info("print directly");
if (outfile != null) {
if (outfilename != null) {
log.error("print mode, but outfile is set:"); log.error("print mode, but outfile is set:");
log.error("out file: " + outfile.toString());
log.error("out file: " + outfilename);
} }
} else if (MimeConstants.MIME_FOP_AREA_TREE.equals(outputmode)) { } else if (MimeConstants.MIME_FOP_AREA_TREE.equals(outputmode)) {
log.info("area tree"); log.info("area tree");
if (isOutputToStdOut()) { if (isOutputToStdOut()) {
log.info("output file: to stdout"); log.info("output file: to stdout");
} else { } else {
log.info("output file: " + outfile.toString());
log.info("output file: " + outfilename);
} }
} else if (MimeConstants.MIME_FOP_IF.equals(outputmode)) { } else if (MimeConstants.MIME_FOP_IF.equals(outputmode)) {
log.info("intermediate format"); log.info("intermediate format");
log.info("output file: " + outfile.toString());
log.info("output file: " + outfilename);
} else { } else {
log.info(outputmode); log.info(outputmode);
if (isOutputToStdOut()) { if (isOutputToStdOut()) {
log.info("output file: to stdout"); log.info("output file: to stdout");
} else { } else {
log.info("output file: " + outfile.toString());
log.info("output file: " + outfilename);
} }
} }



+ 17
- 4
fop-core/src/main/java/org/apache/fop/cli/Main.java Wyświetl plik

} }
} }


public static void startFOP(String[] args) {
startFOP(args, new SystemWrapper());
}

/** /**
* Executes FOP with the given arguments. If no argument is provided, returns its * Executes FOP with the given arguments. If no argument is provided, returns its
* version number as well as a short usage statement; if '-v' is provided, returns its * version number as well as a short usage statement; if '-v' is provided, returns its
* version number alone; if '-h' is provided, returns its short help message. * version number alone; if '-h' is provided, returns its short help message.
* *
* @param args command-line arguments * @param args command-line arguments
* @param systemWrapper Object on which exit() is to be called.
*/ */
public static void startFOP(String[] args) {
public static void startFOP(String[] args, SystemWrapper systemWrapper) {
//System.out.println("static CCL: " //System.out.println("static CCL: "
// + Thread.currentThread().getContextClassLoader().toString()); // + Thread.currentThread().getContextClassLoader().toString());
//System.out.println("static CL: " + Fop.class.getClassLoader().toString()); //System.out.println("static CL: " + Fop.class.getClassLoader().toString());
options = new CommandLineOptions(); options = new CommandLineOptions();
if (!options.parse(args)) { if (!options.parse(args)) {
// @SuppressFBWarnings("DM_EXIT") // @SuppressFBWarnings("DM_EXIT")
System.exit(0);
systemWrapper.exit(0);
} }


foUserAgent = options.getFOUserAgent(); foUserAgent = options.getFOUserAgent();
// AWTRenderer closes with window shutdown, so exit() should not // AWTRenderer closes with window shutdown, so exit() should not
// be called here // be called here
if (!MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputFormat)) { if (!MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputFormat)) {
System.exit(0);
systemWrapper.exit(0);
} }
} catch (Exception e) { } catch (Exception e) {
if (options != null) { if (options != null) {
options.getOutputFile().delete(); options.getOutputFile().delete();
} }
} }
System.exit(1);
systemWrapper.exit(1);
} }
} }


} }
} }


/**
* Wrapper to support dependency injection.
*/
public static class SystemWrapper {
public void exit(int status) {
System.exit(status);
}
}
} }

+ 74
- 0
fop-core/src/test/java/org/apache/fop/cli/CommandLineOptions2TestCase.java Wyświetl plik

/*
* 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.fop.cli;

import java.io.File;
import java.io.IOException;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.apache.fop.apps.FOPException;

public class CommandLineOptions2TestCase {

@Rule
public ExpectedException exceptionRule = ExpectedException.none();

/**
* Check that the parser detects the case where the -xsl switch has been omitted.
* This detection prevents the XSL file being deleted.
* @throws FOPException
* @throws IOException
*/
@Test
public void testXslSwitchMissing() throws FOPException, IOException {
final String xslFilename = "../fop/examples/embedding/xml/xslt/projectteam2fo.xsl";
final String outputFilename = "out.pdf";
exceptionRule.expect(FOPException.class);
exceptionRule.expectMessage("Don't know what to do with " + outputFilename);
// -xsl switch omitted.
String cmdLine = "-xml ../fop/examples/embedding/xml/xml/projectteam.xml " + xslFilename + " " + outputFilename;
String[] args = cmdLine.split(" ");
CommandLineOptions clo = new CommandLineOptions();
clo.parse(args);
}

/**
* Check that the XSL file is not deleted in the case where the -xsl switch has been omitted.
* @throws FOPException
* @throws IOException
*/
@Test
public void testXslFileNotDeleted() {
Main.SystemWrapper mockSystemWrapper = mock(Main.SystemWrapper.class);
final String xslFilename = "../fop/examples/embedding/xml/xslt/projectteam2fo.xsl";
// -xsl switch omitted.
String cmdLine = "-xml ../fop/examples/embedding/xml/xml/projectteam.xml " + xslFilename + " out.pdf";
String[] args = cmdLine.split(" ");
Main.startFOP(args, mockSystemWrapper);
verify(mockSystemWrapper).exit(1);
File file = new File(xslFilename);
assertTrue(file.exists());
}
}

Ładowanie…
Anuluj
Zapisz