This is an automated email from the ASF dual-hosted git repository. ebourg pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/tomcat-jakartaee-migration.git
commit 44521e8f1f4beb84ce1e7780481eb3ee19e7e2bd Author: Emmanuel Bourg <ebo...@apache.org> AuthorDate: Wed Apr 8 22:46:02 2020 +0200 Ant task for the migration tool --- pom.xml | 12 ++++ .../org/apache/tomcat/jakartaee/AntHandler.java | 67 ++++++++++++++++++ .../org/apache/tomcat/jakartaee/MigrationTask.java | 82 ++++++++++++++++++++++ .../org/apache/tomcat/jakartaee/antlib.xml | 4 ++ .../apache/tomcat/jakartaee/MigrationTaskTest.java | 81 +++++++++++++++++++++ src/test/resources/testbuild.xml | 13 ++++ 6 files changed, 259 insertions(+) diff --git a/pom.xml b/pom.xml index f6bd9d3..c094b75 100644 --- a/pom.xml +++ b/pom.xml @@ -76,6 +76,12 @@ <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> + <dependency> + <groupId>org.apache.ant</groupId> + <artifactId>ant</artifactId> + <version>1.10.7</version> + <scope>provided</scope> + </dependency> <!-- Test dependencies --> <dependency> @@ -95,6 +101,12 @@ </resources> <plugins> <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <reuseForks>false</reuseForks> + </configuration> + </plugin> + <plugin> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> diff --git a/src/main/java/org/apache/tomcat/jakartaee/AntHandler.java b/src/main/java/org/apache/tomcat/jakartaee/AntHandler.java new file mode 100644 index 0000000..e7747b7 --- /dev/null +++ b/src/main/java/org/apache/tomcat/jakartaee/AntHandler.java @@ -0,0 +1,67 @@ +/* + * 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.tomcat.jakartaee; + +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; + +import org.apache.tools.ant.Project; +import org.apache.tools.ant.Task; + +/** + * JUL log handler redirecting the messages logged to Ant. + */ + class AntHandler extends Handler { + + private final Task task; + + public AntHandler(Task task) { + this.task = task; + } + + @Override + public void publish(LogRecord record) { + task.log(record.getMessage(), record.getThrown(), toAntLevel(record.getLevel())); + } + + @Override + public void flush() { + } + + @Override + public void close() throws SecurityException { + } + + /** + * Convert the JUL level to the equivalent Ant one. + */ + private int toAntLevel(Level level) { + if (level.intValue() >= Level.SEVERE.intValue()) { + return Project.MSG_ERR; + } else if (level.intValue() >= Level.WARNING.intValue()) { + return Project.MSG_WARN; + } else if (level.intValue() >= Level.INFO.intValue()) { + return Project.MSG_INFO; + } else if (level.intValue() >= Level.FINE.intValue()) { + return Project.MSG_VERBOSE; + } else { + return Project.MSG_DEBUG; + } + } +} diff --git a/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java b/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java new file mode 100644 index 0000000..4d7d967 --- /dev/null +++ b/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java @@ -0,0 +1,82 @@ +/* + * 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.tomcat.jakartaee; + +import java.io.File; +import java.io.IOException; +import java.util.logging.Handler; +import java.util.logging.Logger; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; + +/** + * Ant task for the Jakarta EE migration tool. + */ +public class MigrationTask extends Task { + + private File src; + private File dest; + private String profile = EESpecProfile.TOMCAT.toString(); + + public void setSrc(File src) { + this.src = src; + } + + public void setDest(File dest) { + this.dest = dest; + } + + public void setProfile(String profile) { + this.profile = profile; + } + + @Override + public void execute() throws BuildException { + // redirect the log messages to Ant + Logger logger = Logger.getLogger(Migration.class.getCanonicalName()); + logger.setUseParentHandlers(false); + for (Handler handler : logger.getHandlers()) { + logger.removeHandler(handler); + } + logger.addHandler(new AntHandler(this)); + + // check the parameters + EESpecProfile profile = null; + try { + profile = EESpecProfile.valueOf(this.profile.toUpperCase()); + } catch (IllegalArgumentException e) { + throw new BuildException("Invalid profile specified: " + this.profile, getLocation()); // todo i18n + } + + Migration migration = new Migration(); + migration.setSource(src); + migration.setDestination(dest); + migration.setEESpecProfile(profile); + + boolean success = false; + try { + success = migration.execute(); + } catch (IOException e) { + throw new BuildException(e, getLocation()); + } + + if (!success) { + throw new BuildException("Migration failed", getLocation()); + } + } +} diff --git a/src/main/resources/org/apache/tomcat/jakartaee/antlib.xml b/src/main/resources/org/apache/tomcat/jakartaee/antlib.xml new file mode 100644 index 0000000..daedd05 --- /dev/null +++ b/src/main/resources/org/apache/tomcat/jakartaee/antlib.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<antlib> + <taskdef name="javax2jakarta" classname="org.apache.tomcat.jakartaee.MigrationTask"/> +</antlib> diff --git a/src/test/java/org/apache/tomcat/jakartaee/MigrationTaskTest.java b/src/test/java/org/apache/tomcat/jakartaee/MigrationTaskTest.java new file mode 100644 index 0000000..1baa1d7 --- /dev/null +++ b/src/test/java/org/apache/tomcat/jakartaee/MigrationTaskTest.java @@ -0,0 +1,81 @@ +/* + * 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.tomcat.jakartaee; + +import java.io.File; +import java.io.OutputStream; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.io.FileUtils; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.DefaultLogger; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.ProjectHelper; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class MigrationTaskTest { + + private Project project; + + @Before + public void setUp() throws Exception { + project = new Project(); + project.setCoreLoader(getClass().getClassLoader()); + project.init(); + + File buildFile = new File("target/test-classes/testbuild.xml"); + project.setBaseDir(buildFile.getParentFile()); + + final ProjectHelper helper = ProjectHelper.getProjectHelper(); + helper.parse(project, buildFile); + + redirectOutput(System.out); + } + + /** + * Redirects the Ant output to the specified stream. + */ + private void redirectOutput(OutputStream out) { + DefaultLogger logger = new DefaultLogger(); + logger.setOutputPrintStream(new PrintStream(out)); + logger.setMessageOutputLevel(Project.MSG_INFO); + project.addBuildListener(logger); + } + + @Test(expected = BuildException.class) + public void testUnsupportedKeyStoreType() { + project.executeTarget("invalid-profile"); + } + + @Test + public void testMigrateSingleSourceFile() throws Exception { + project.executeTarget("migrate-single-source-file"); + + File migratedFile = new File("target/test-classes/HelloServlet.migrated-by-ant.java"); + + assertTrue("Migrated file not found", migratedFile.exists()); + + String migratedSource = FileUtils.readFileToString(migratedFile, StandardCharsets.UTF_8); + assertFalse("Imports not migrated", migratedSource.contains("import javax.servlet")); + assertTrue("Migrated imports not found", migratedSource.contains("import jakarta.servlet")); + } +} diff --git a/src/test/resources/testbuild.xml b/src/test/resources/testbuild.xml new file mode 100644 index 0000000..3b64b59 --- /dev/null +++ b/src/test/resources/testbuild.xml @@ -0,0 +1,13 @@ +<project name="Jsign Ant tests"> + + <taskdef name="javax2jakarta" classname="org.apache.tomcat.jakartaee.MigrationTask"/> + + <target name="migrate-single-source-file"> + <javax2jakarta src="HelloServlet.java" dest="HelloServlet.migrated-by-ant.java" profile="tomcat"/> + </target> + + <target name="invalid-profile"> + <javax2jakarta src="foo" dest="bar" profile="tOmCaT"/> + </target> + +</project> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org