From d30bd48f095265530edb19da89c8dddb0f9954a1 Mon Sep 17 00:00:00 2001 From: Peter Gergo Barna Date: Mon, 19 Feb 2018 12:20:29 +0100 Subject: [PATCH] FALCON-2322 cast Action.getAny() to org.w3c.dom.Node instead of ElementNSImpl in order to eliminate ClassCastException --- .../org/apache/falcon/util/OozieUtils.java | 8 +- .../oozie/process/ClassCastExceptionTest.java | 99 +++++++++++++++++++ 2 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 oozie/src/test/java/org/apache/falcon/oozie/process/ClassCastExceptionTest.java diff --git a/oozie/src/main/java/org/apache/falcon/util/OozieUtils.java b/oozie/src/main/java/org/apache/falcon/util/OozieUtils.java index fd94d8e7f..1c79b9bb9 100644 --- a/oozie/src/main/java/org/apache/falcon/util/OozieUtils.java +++ b/oozie/src/main/java/org/apache/falcon/util/OozieUtils.java @@ -23,8 +23,8 @@ import org.apache.falcon.oozie.workflow.CONFIGURATION; import org.apache.falcon.oozie.workflow.WORKFLOWAPP; import org.apache.hadoop.conf.Configuration; -import org.apache.xerces.dom.ElementNSImpl; import org.w3c.dom.Document; +import org.w3c.dom.Node; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; @@ -86,7 +86,7 @@ public static JAXBElement unMarshalHiveAction(org.apache.falcon.oozie.wo Unmarshaller unmarshaller = HIVE_ACTION_JAXB_CONTEXT.createUnmarshaller(); unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler()); return (JAXBElement) - unmarshaller.unmarshal((ElementNSImpl) wfAction.getAny()); + unmarshaller.unmarshal((Node) wfAction.getAny()); } catch (JAXBException e) { throw new RuntimeException("Unable to unmarshall hive action.", e); } @@ -111,7 +111,7 @@ public static JAXBElement unMarshalSqoopAc Unmarshaller unmarshaller = SQOOP_ACTION_JAXB_CONTEXT.createUnmarshaller(); unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler()); return (JAXBElement) - unmarshaller.unmarshal((ElementNSImpl) wfAction.getAny()); + unmarshaller.unmarshal((Node) wfAction.getAny()); } catch (JAXBException e) { throw new RuntimeException("Unable to unmarshall sqoop action.", e); } @@ -135,7 +135,7 @@ public static JAXBElement unMarshalSparkAc Unmarshaller unmarshaller = SPARK_ACTION_JAXB_CONTEXT.createUnmarshaller(); unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler()); return (JAXBElement) - unmarshaller.unmarshal((ElementNSImpl) wfAction.getAny()); + unmarshaller.unmarshal((Node) wfAction.getAny()); } catch (JAXBException e) { throw new RuntimeException("Unable to unmarshall spark action.", e); } diff --git a/oozie/src/test/java/org/apache/falcon/oozie/process/ClassCastExceptionTest.java b/oozie/src/test/java/org/apache/falcon/oozie/process/ClassCastExceptionTest.java new file mode 100644 index 000000000..811abc939 --- /dev/null +++ b/oozie/src/test/java/org/apache/falcon/oozie/process/ClassCastExceptionTest.java @@ -0,0 +1,99 @@ +/** + * 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.falcon.oozie.process; + +import org.apache.falcon.util.OozieUtils; +import org.testng.annotations.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.StringReader; +import java.net.URL; + +import static org.testng.Assert.assertFalse; + +/** + * Make sure OozieUtils.unMarshal[*]Action does not throw ClassCastException. + */ +public class ClassCastExceptionTest { + + + public static String getXmlFile(URL url) throws IOException { + BufferedReader rd = new BufferedReader(new InputStreamReader(url.openStream())); + String inputLine = null; + StringBuilder builder = new StringBuilder(); + while((inputLine = rd.readLine()) != null) { + builder.append(inputLine); + } + rd.close(); + return builder.toString(); + } + + public static Document convertStringToDocument(String xmlStr) { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder; + try { + builder = factory.newDocumentBuilder(); + Document doc = builder.parse(new InputSource(new StringReader(xmlStr))); + return doc; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Test + public void testClassCastException() throws IOException { + String filePath = "/action/process/hive-action.xml"; + URL resource = this.getClass().getResource(filePath); + Document doc = convertStringToDocument(getXmlFile(resource)); + NodeList ns = doc.getElementsByTagName("action"); + Node node = ns.item(0); + + org.apache.falcon.oozie.workflow.ACTION wfAction = new org.apache.falcon.oozie.workflow.ACTION(); + wfAction.setAny(node); + + try { + OozieUtils.unMarshalHiveAction(wfAction); + } catch (Exception e){ + //expected: UnmarshalException + assertFalse(e.getClass().equals(ClassCastException.class)); + } + + try { + OozieUtils.unMarshalSqoopAction(wfAction); + } catch (Exception e){ + assertFalse(e.getClass().equals(ClassCastException.class)); + } + + try { + OozieUtils.unMarshalSparkAction(wfAction); + } catch (Exception e){ + assertFalse(e.getClass().equals(ClassCastException.class)); + } + } + +}