This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/ToStringPlugin in repository https://gitbox.apache.org/repos/asf/cxf-xjc-utils.git
commit 649e751e488ba59f1aa64b241938f4d2747db453 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Tue Oct 27 12:42:07 2026 +0000 Fix error handling in ToStringPlugin argument parsing and deleteDirs --- .../cxf/maven_plugin/AbstractXSDToJavaMojo.java | 12 +++-- .../org/apache/cxf/maven_plugin/DeleteDirTest.java | 51 ++++++++++++++++++++ ts/pom.xml | 5 ++ .../java/org/apache/cxf/xjc/ts/ToStringPlugin.java | 4 ++ .../org/apache/cxf/xjc/ts/ToStringPluginTest.java | 55 ++++++++++++++++++++++ 5 files changed, 123 insertions(+), 4 deletions(-) diff --git a/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractXSDToJavaMojo.java b/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractXSDToJavaMojo.java index 44f5453..f1c64d2 100644 --- a/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractXSDToJavaMojo.java +++ b/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/AbstractXSDToJavaMojo.java @@ -280,7 +280,8 @@ public abstract class AbstractXSDToJavaMojo extends AbstractMojo { File[] dirs = xsdOptions[x].getDeleteDirs(); if (dirs != null) { for (File dir : dirs) { - result = result && deleteDir(dir); + // delete every dir, even after one could not be deleted + result = deleteDir(dir) && result; } } buildContext.refresh(outputDirFile); @@ -464,11 +465,14 @@ public abstract class AbstractXSDToJavaMojo extends AbstractMojo { } - private boolean deleteDir(File f) { + static boolean deleteDir(File f) { if (f.isDirectory()) { + // null if the directory could not be read, in which case deleting it fails below File[] files = f.listFiles(); - for (File file : files) { - deleteDir(file); + if (files != null) { + for (File file : files) { + deleteDir(file); + } } } diff --git a/cxf-xjc-plugin/src/test/java/org/apache/cxf/maven_plugin/DeleteDirTest.java b/cxf-xjc-plugin/src/test/java/org/apache/cxf/maven_plugin/DeleteDirTest.java new file mode 100644 index 0000000..b853b6f --- /dev/null +++ b/cxf-xjc-plugin/src/test/java/org/apache/cxf/maven_plugin/DeleteDirTest.java @@ -0,0 +1,51 @@ +/** + * 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.cxf.maven_plugin; + +import java.io.File; +import java.nio.file.Files; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public class DeleteDirTest { + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + + @Test + public void testDeletesNestedDirectories() throws Exception { + File dir = tmp.newFolder("a"); + File nested = new File(dir, "b/c"); + Assert.assertTrue(nested.mkdirs()); + Files.write(new File(nested, "X.java").toPath(), new byte[] {1}); + Files.write(new File(dir, "Y.java").toPath(), new byte[] {1}); + + Assert.assertTrue(AbstractXSDToJavaMojo.deleteDir(dir)); + Assert.assertFalse(dir.exists()); + } + + @Test + public void testMissingDirectory() throws Exception { + Assert.assertTrue(AbstractXSDToJavaMojo.deleteDir(new File(tmp.getRoot(), "missing"))); + } +} diff --git a/ts/pom.xml b/ts/pom.xml index a78fe74..b8d94e6 100644 --- a/ts/pom.xml +++ b/ts/pom.xml @@ -41,6 +41,11 @@ <artifactId>jaxb-runtime</artifactId> <scope>provided</scope> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/ts/src/main/java/org/apache/cxf/xjc/ts/ToStringPlugin.java b/ts/src/main/java/org/apache/cxf/xjc/ts/ToStringPlugin.java index bbf1200..1556bea 100644 --- a/ts/src/main/java/org/apache/cxf/xjc/ts/ToStringPlugin.java +++ b/ts/src/main/java/org/apache/cxf/xjc/ts/ToStringPlugin.java @@ -88,6 +88,10 @@ public class ToStringPlugin { styleFieldName = "SIMPLE_STYLE"; } else { int idx = v.lastIndexOf('.'); + if (idx <= 0 || idx == v.length() - 1) { + throw new BadCommandLineException("Invalid -Xts:style value \"" + v + + "\": expected multiline, simple or the full class+field name of a ToStringStyle"); + } styleFieldName = v.substring(idx + 1); styleClassName = v.substring(0, idx); } diff --git a/ts/src/test/java/org/apache/cxf/xjc/ts/ToStringPluginTest.java b/ts/src/test/java/org/apache/cxf/xjc/ts/ToStringPluginTest.java new file mode 100644 index 0000000..fd9294a --- /dev/null +++ b/ts/src/test/java/org/apache/cxf/xjc/ts/ToStringPluginTest.java @@ -0,0 +1,55 @@ +/** + * 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.cxf.xjc.ts; + +import com.sun.tools.xjc.BadCommandLineException; +import com.sun.tools.xjc.Options; + +import org.junit.Assert; +import org.junit.Test; + +public class ToStringPluginTest { + + private static int parse(String arg) throws Exception { + com.sun.tools.xjc.addon.apache_cxf.ts.ToStringPlugin plugin = + new com.sun.tools.xjc.addon.apache_cxf.ts.ToStringPlugin(); + return plugin.parseArgument(new Options(), new String[] {arg}, 0); + } + + @Test + public void testValidStyles() throws Exception { + Assert.assertEquals(1, parse("-Xts")); + Assert.assertEquals(1, parse("-Xts:style:multiline")); + Assert.assertEquals(1, parse("-Xts:style:simple")); + Assert.assertEquals(1, parse("-Xts:style:org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE")); + } + + @Test + public void testStyleWithoutClassName() throws Exception { + for (String style : new String[] {"FIELD", ".FIELD", "org.example.Style."}) { + try { + parse("-Xts:style:" + style); + Assert.fail("Expected BadCommandLineException for " + style); + } catch (BadCommandLineException e) { + Assert.assertTrue(e.getMessage(), e.getMessage().contains(style)); + } + } + } +}
