Return-Path: X-Original-To: apmail-olingo-commits-archive@minotaur.apache.org Delivered-To: apmail-olingo-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CE37D1117B for ; Tue, 6 May 2014 14:31:34 +0000 (UTC) Received: (qmail 38995 invoked by uid 500); 6 May 2014 14:16:58 -0000 Delivered-To: apmail-olingo-commits-archive@olingo.apache.org Received: (qmail 38940 invoked by uid 500); 6 May 2014 14:16:51 -0000 Mailing-List: contact commits-help@olingo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@olingo.apache.org Delivered-To: mailing list commits@olingo.apache.org Received: (qmail 38626 invoked by uid 99); 6 May 2014 14:16:34 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 06 May 2014 14:16:34 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 25ADA93548B; Tue, 6 May 2014 14:16:34 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: mibo@apache.org To: commits@olingo.apache.org Message-Id: <670c86c323b44b6ca270df472fc8528a@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: git commit: [OLINGO-231] Added tests Date: Tue, 6 May 2014 14:16:34 +0000 (UTC) Repository: olingo-odata2 Updated Branches: refs/heads/OLINGO-231_PocForAndroid 39beefb49 -> c321fb745 [OLINGO-231] Added tests Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/c321fb74 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/c321fb74 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/c321fb74 Branch: refs/heads/OLINGO-231_PocForAndroid Commit: c321fb745c8c1d20d9e3340e982d9311eb2527ca Parents: 39beefb Author: Michael Bolz Authored: Mon May 5 14:59:36 2014 +0200 Committer: Michael Bolz Committed: Tue May 6 16:16:17 2014 +0200 ---------------------------------------------------------------------- odata2-android/pom.xml | 23 ++ .../odata2/android/xml/AndroidXmlFactory.java | 19 +- .../odata2/android/xml/AndroidXmlReader.java | 60 +++-- .../odata2/android/xml/AndroidXmlWriter.java | 264 ++++++++++++++----- .../android/xml/AndroidXmlFactoryTest.java | 114 ++++++++ .../android/xml/AndroidXmlReaderTest.java | 164 ++++++++++++ .../android/xml/AndroidXmlWriterTest.java | 158 +++++++++++ .../odata2/api/xml/XMLStreamConstants.java | 2 +- .../olingo/odata2/api/xml/XMLStreamReader.java | 2 +- .../odata2/core/xml/JavaxStaxReaderWrapper.java | 4 +- .../core/xml/JavaxStaxReaderWrapperTest.java | 164 ++++++++++++ .../core/xml/JavaxStaxStreamFactoryTest.java | 131 +++++++++ .../core/xml/JavaxStaxWriterWrapperTest.java | 194 ++++++++++++++ .../odata2/testutil/helper/StringHelper.java | 10 + 14 files changed, 1222 insertions(+), 87 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-android/pom.xml ---------------------------------------------------------------------- diff --git a/odata2-android/pom.xml b/odata2-android/pom.xml index 4448fd8..8a8e1c3 100644 --- a/odata2-android/pom.xml +++ b/odata2-android/pom.xml @@ -34,6 +34,7 @@ 4.1.1.4 + 2.2 @@ -95,5 +96,27 @@ olingo-odata2-core ${project.version} + + org.apache.olingo + olingo-odata2-testutil + ${project.version} + test + + + junit + junit + test + + + org.robolectric + robolectric + ${version.robolectric} + test + + + xmlunit + xmlunit + test + http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlFactory.java ---------------------------------------------------------------------- diff --git a/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlFactory.java b/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlFactory.java index 1660390..f2888a9 100644 --- a/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlFactory.java +++ b/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlFactory.java @@ -1,6 +1,23 @@ +/******************************************************************************* + * 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.olingo.odata2.android.xml; -import org.apache.olingo.odata2.api.ep.EntityProviderException; import org.apache.olingo.odata2.api.xml.*; import org.apache.olingo.odata2.core.xml.AbstractXmlStreamFactory; http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlReader.java ---------------------------------------------------------------------- diff --git a/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlReader.java b/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlReader.java index cb4bacc..d793fe6 100644 --- a/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlReader.java +++ b/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlReader.java @@ -1,3 +1,21 @@ +/******************************************************************************* + * 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.olingo.odata2.android.xml; import java.io.IOException; @@ -14,6 +32,9 @@ import org.xmlpull.v1.XmlPullParserException; import android.util.Xml; +/** + * Wrapper for XmlPullParser in Android SDK + */ public class AndroidXmlReader implements XMLStreamReader { private final XmlPullParser parser; @@ -79,8 +100,7 @@ public class AndroidXmlReader implements XMLStreamReader { @Override public String getAttributeValue(String namespaceURI, String localName) { - String attributeValue = parser.getAttributeValue(namespaceURI, localName); - return attributeValue; + return parser.getAttributeValue(namespaceURI, localName); } @Override @@ -126,27 +146,23 @@ public class AndroidXmlReader implements XMLStreamReader { e.printStackTrace(); } final String prefix = tmp; - // TODO Auto-generated method stub - NamespaceContext nctx = new NamespaceContext() { + return new NamespaceContext() { public String getPrefix(String index) { return prefix; } }; - return nctx; } @Override - public int getNamespaceCount() { + public int getNamespaceCount() throws XMLStreamException { // FIXME try { int depth = parser.getDepth(); int nsStart = parser.getNamespaceCount(depth-1); int nsEnd = parser.getNamespaceCount(depth); - int nsCount = nsEnd - nsStart; - return nsCount; + return nsEnd - nsStart; } catch (XmlPullParserException e) { - e.printStackTrace(); - return -1; + throw new XMLStreamException("Got XmlPullParserException with message: " + e.getMessage(), e); } } @@ -239,9 +255,9 @@ public class AndroidXmlReader implements XMLStreamReader { } @Override - public void next() throws XMLStreamException { + public int next() throws XMLStreamException { try { - parser.next(); + return map2XmlStreamEventType(parser.next()); } catch (XmlPullParserException e) { throw new XMLStreamException("Got XmlPullParserException with message: " + e.getMessage(), e); } catch (IOException e) { @@ -252,7 +268,7 @@ public class AndroidXmlReader implements XMLStreamReader { @Override public int nextTag() throws XMLStreamException { try { - return parser.nextTag(); + return map2XmlStreamEventType(parser.nextTag()); } catch (XmlPullParserException e) { throw new XMLStreamException("Got XmlPullParserException with message: " + e.getMessage(), e); } catch (IOException e) { @@ -263,7 +279,7 @@ public class AndroidXmlReader implements XMLStreamReader { @Override public void require(int eventType, String namespace, String tag) throws XMLStreamException { try { - int xmlPullEventType = mapEventType(eventType); + int xmlPullEventType = map2AndroidEventType(eventType); parser.require(xmlPullEventType, namespace, tag); } catch (XmlPullParserException e) { throw new XMLStreamException("Got XmlPullParserException with message: " + e.getMessage(), e); @@ -272,7 +288,7 @@ public class AndroidXmlReader implements XMLStreamReader { } } - private int mapEventType(int eventType) { + private int map2AndroidEventType(int eventType) { switch (eventType) { case XMLStreamConstants.START_ELEMENT: return XmlPullParser.START_TAG; case XMLStreamConstants.END_ELEMENT: return XmlPullParser.END_TAG; @@ -284,4 +300,18 @@ public class AndroidXmlReader implements XMLStreamReader { eventType + "') for mapping."); } } + + private int map2XmlStreamEventType(int eventType) { + switch (eventType) { + case XmlPullParser.TEXT: return XMLStreamConstants.CHARACTERS; + case XmlPullParser.START_TAG: return XMLStreamConstants.START_ELEMENT; + case XmlPullParser.END_TAG: return XMLStreamConstants.END_ELEMENT; + case XmlPullParser.START_DOCUMENT: return XMLStreamConstants.START_DOCUMENT; + case XmlPullParser.END_DOCUMENT: return XMLStreamConstants.END_DOCUMENT; + case XmlPullParser.DOCDECL: return XMLStreamConstants.DTD; + default: + throw new IllegalArgumentException("Unknown event type ('" + + eventType + "') for mapping."); + } + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlWriter.java ---------------------------------------------------------------------- diff --git a/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlWriter.java b/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlWriter.java index 211738a..0e8887d 100644 --- a/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlWriter.java +++ b/odata2-android/src/main/java/org/apache/olingo/odata2/android/xml/AndroidXmlWriter.java @@ -1,30 +1,85 @@ +/******************************************************************************* + * 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.olingo.odata2.android.xml; -import org.apache.olingo.odata2.api.ep.EntityProviderException; +import android.util.Xml; import org.apache.olingo.odata2.api.xml.XMLStreamException; import org.apache.olingo.odata2.api.xml.XMLStreamWriter; -import org.xmlpull.v1.XmlPullParserException; +import org.xmlpull.v1.XmlSerializer; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.util.Map; +import java.util.Stack; /** */ public class AndroidXmlWriter implements XMLStreamWriter { - private Writer writer; + private static final String DEFAULT_ENCODING = "UTF-8"; + private static final String DEFAULT_VERSION = "1.0"; - public AndroidXmlWriter(Object output) { - if (output instanceof OutputStream) { - writer = new PrintWriter((OutputStream) output); - } else if (output instanceof Writer) { - writer = (Writer) output; + private final XmlSerializer serializer; + private final Stack tags; + private final Stack namespaces; + + private String defaultNamespace = null; + + public AndroidXmlWriter(Object output) throws XMLStreamException { + this(output, DEFAULT_ENCODING); + } + + public AndroidXmlWriter(Object output, String encoding) throws XMLStreamException { + serializer = Xml.newSerializer(); + tags = new Stack(); + namespaces = new Stack(); + + try { + if (output instanceof OutputStream) { + serializer.setOutput((OutputStream) output, encoding); + } else if (output instanceof Writer) { + serializer.setOutput((Writer) output); + } else { + throw new XMLStreamException("Unsupported parameter value. " + + "Requires an OutputStream or a Writer instance."); + } + } catch (IllegalStateException e) { + wrapAndReThrow(e); + } catch (IOException e) { + wrapAndReThrow(e); } } + private void wrapAndReThrow(Exception e) throws XMLStreamException { + wrapAndReThrow("Unexpected exception with message: " + e.getMessage(), e); + } + + private void wrapAndReThrow(String message, Exception e) throws XMLStreamException { + throw new XMLStreamException(message, e); + } + + private String nextTagToClose() { + return tags.pop(); + } + public AndroidXmlWriter setProperties(Map properties) throws XMLStreamException { for (Map.Entry entry : properties.entrySet()) { setProperty(entry.getKey(), entry.getValue()); @@ -33,125 +88,200 @@ public class AndroidXmlWriter implements XMLStreamWriter { } public AndroidXmlWriter setProperty(String name, Object value) throws XMLStreamException { + try { + serializer.setProperty(name, value); + } catch (IllegalStateException e) { + wrapAndReThrow(e); + } return this; } @Override public void flush() throws XMLStreamException { - write("flush"); try { - if(writer != null) { - writer.flush(); + if(serializer != null) { + serializer.flush(); } } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + wrapAndReThrow("Exception during flush with message: " + e.getMessage(), e); } } @Override - public void setDefaultNamespace(String arg0) throws XMLStreamException { - write("setDefaultNamespace", arg0); + public void setDefaultNamespace(String uri) throws XMLStreamException { + defaultNamespace = uri; + writeNamespace("", defaultNamespace); } @Override - public void setPrefix(String arg0, String arg1) throws XMLStreamException { - write("setPrefix", arg0, arg1); - + public void setPrefix(String prefix, String uri) throws XMLStreamException { + try { + serializer.setPrefix(prefix, uri); + } catch (Exception e) { + wrapAndReThrow(e); + } } @Override - public void writeAttribute(String arg0, String arg1) throws XMLStreamException { - write("writeAttribute", arg0, arg1); - + public void writeStartDocument() throws XMLStreamException { + try { + writeStartDocument(DEFAULT_ENCODING, DEFAULT_VERSION); + } catch (Exception e) { + wrapAndReThrow(e); + } } @Override - public void writeAttribute(String arg0, String arg1, String arg2, String arg3) throws XMLStreamException { - write("writeAttribute", arg0, arg1, arg2); - + public void writeAttribute(String localName, String value) throws XMLStreamException { + try { + writeAttribute(nextNamespaceUriToWrite(), localName, value); + } catch (Exception e) { + wrapAndReThrow(e); + } } @Override - public void writeCharacters(String arg0) throws XMLStreamException { - write("writeCharacters", arg0); - + public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException { + try { + writeAttribute(null, namespaceURI, localName, value); + } catch (Exception e) { + wrapAndReThrow(e); + } } @Override - public void writeDefaultNamespace(String arg0) throws XMLStreamException { - write("writeDefaultNamespace", arg0); - + public void writeAttribute(String prefix, String namespaceURI, String localName, String value) + throws XMLStreamException { + try { + writeCurrentTag(); + serializer.attribute(namespaceURI, localName, value); + } catch (Exception e) { + wrapAndReThrow(e); + } } @Override - public void writeEndDocument() throws XMLStreamException { - write("writeEndDocument"); - + public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException { + setDefaultNamespace(namespaceURI); } @Override - public void writeEndElement() throws XMLStreamException { - write("writeEndElement"); - + public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException { + try { + serializer.setPrefix(prefix, namespaceURI); + } catch (IOException e) { + wrapAndReThrow(e); + } } @Override - public void writeNamespace(String arg0, String arg1) throws XMLStreamException { - write("writeNamespace", arg0, arg1); - + public void writeStartElement(String localName) throws XMLStreamException { + try { + writeStartElement(nextNamespaceUriToWrite(), localName); + } catch (Exception e) { + wrapAndReThrow(e); + } } - @Override - public void writeStartDocument() throws XMLStreamException { - write("writeStartDocument"); + private String nextNamespaceUriToWrite() { + return nextNamespaceUriToWrite(true); + } + private String nextNamespaceUriToWrite(boolean keep) { + if(namespaces.isEmpty()) { + return defaultNamespace; + } + if(keep) { + return namespaces.peek(); + } + return namespaces.pop(); } @Override - public void writeStartElement(String arg0) throws XMLStreamException { - write("writeStartElement", arg0); - + public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException { + try { + String prefix = serializer.getPrefix(namespaceURI, false); + writeStartElement(prefix, localName, namespaceURI); + } catch (Exception e) { + wrapAndReThrow(e); + } } + TagHandle currentTag = null; + @Override - public void writeStartElement(String arg0, String arg1) throws XMLStreamException { - write("writeStartElement", arg0, arg1); + public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { + try { + writeCurrentTag(); + // + tags.push(localName); + namespaces.push(namespaceURI); + // + currentTag = new TagHandle(localName, namespaceURI, prefix); + } catch (Exception e) { + wrapAndReThrow(e); + } + } + @Override + public void writeStartDocument(String encoding, String version) throws XMLStreamException { + try { + serializer.startDocument(encoding, null); + } catch (Exception e) { + wrapAndReThrow(e); + } } @Override - public void writeStartElement(String arg0, String arg1, String arg2) throws XMLStreamException { - write("writeStartElement", arg0, arg1, arg2); + public void writeEndElement() throws XMLStreamException { + try { + writeCurrentTag(); + serializer.endTag(nextNamespaceUriToWrite(false), nextTagToClose()); + } catch (Exception e) { + wrapAndReThrow(e); + } } - private void write(String message, String... content) { + @Override + public void writeEndDocument() throws XMLStreamException { try { - if (writer == null) { - throw new RuntimeException("Writer not initialized."); -// writer.append("Writer not initialized\n"); - } else { - writer.append(message).append("\n"); - for (String string : content) { - writer.append("->").append(string).append("<-\n"); - } - } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + serializer.endDocument(); + } catch (Exception e) { + wrapAndReThrow(e); } } @Override - public void writeAttribute(String arg0, String arg1, String arg2) throws XMLStreamException { - // TODO Auto-generated method stub + public void writeCharacters(String text) throws XMLStreamException { + try { + writeCurrentTag(); + serializer.text(text); + } catch (Exception e) { + wrapAndReThrow(e); + } + } + private void writeCurrentTag() throws XMLStreamException { + try { + if(currentTag != null) { + serializer.startTag(currentTag.namespaceUri, currentTag.name); + currentTag = null; + } + } catch (Exception e) { + wrapAndReThrow(e); + } } - @Override - public void writeStartDocument(String arg0, String arg1) throws XMLStreamException { - // TODO Auto-generated method stub + private class TagHandle { + final String name; + final String namespaceUri; + final String namespacePrefix; + private TagHandle(String name, String namespaceUri, String namespacePrefix) { + this.name = name; + this.namespaceUri = namespaceUri; + this.namespacePrefix = namespacePrefix; + } } } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-android/src/test/java/org/apache/olingo/odata2/android/xml/AndroidXmlFactoryTest.java ---------------------------------------------------------------------- diff --git a/odata2-android/src/test/java/org/apache/olingo/odata2/android/xml/AndroidXmlFactoryTest.java b/odata2-android/src/test/java/org/apache/olingo/odata2/android/xml/AndroidXmlFactoryTest.java new file mode 100644 index 0000000..e51c345 --- /dev/null +++ b/odata2-android/src/test/java/org/apache/olingo/odata2/android/xml/AndroidXmlFactoryTest.java @@ -0,0 +1,114 @@ +/******************************************************************************* + * 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.olingo.odata2.android.xml; + +import junit.framework.Assert; +import org.apache.olingo.odata2.api.xml.XMLStreamException; +import org.apache.olingo.odata2.api.xml.XMLStreamReader; +import org.apache.olingo.odata2.api.xml.XMLStreamWriter; +import org.apache.olingo.odata2.testutil.helper.StringHelper; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.StringWriter; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static org.apache.olingo.odata2.api.xml.XMLStreamReaderFactory.XML_STREAM_READER_FACTORY_CLASS; +import static org.apache.olingo.odata2.api.xml.XMLStreamWriterFactory.XML_STREAM_WRITER_FACTORY_CLASS; + +/** + */ +@RunWith(RobolectricTestRunner.class) +@Config(manifest=Config.NONE) +public class AndroidXmlFactoryTest { + + private AndroidXmlFactory streamFactory; + + public static final String BASIC_CONTENT = ""; + + // CHECKSTYLE:OFF + @Before + public void init() { + // + System.setProperty(XML_STREAM_READER_FACTORY_CLASS, AndroidXmlFactoryTest.class.getName()); // NOSONAR + System.setProperty(XML_STREAM_WRITER_FACTORY_CLASS, AndroidXmlFactoryTest.class.getName()); // NOSONAR + // + streamFactory = new AndroidXmlFactory(); + } + // CHECKSTYLE:ON + + + @Test + @Ignore("Will work with robolectric version 2.3") + public void createReader() throws Exception { + InputStream stream = StringHelper.encapsulate(BASIC_CONTENT); + XMLStreamReader xmlReader = streamFactory.createXMLStreamReader(stream); + assertNotNull(xmlReader); + Assert.assertTrue(xmlReader.hasNext()); + } + + @Test(expected = IllegalArgumentException.class) + public void createReaderWrongContent() throws Exception { + XMLStreamReader xmlReader = streamFactory.createXMLStreamReader("content"); + assertNotNull(xmlReader); + Assert.assertTrue(xmlReader.hasNext()); + } + + @Test + public void createWriterStream() throws Exception { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + XMLStreamWriter xmlReader = streamFactory.createXMLStreamWriter(stream); + xmlReader.writeStartDocument(); + xmlReader.writeStartElement("start"); + xmlReader.writeEndElement(); + xmlReader.writeEndDocument(); + xmlReader.flush(); + + assertNotNull(xmlReader); + assertEquals(BASIC_CONTENT, new String(stream.toByteArray())); + } + + @Test + public void createWriterPrintWriter() throws Exception { + + StringWriter writer = new StringWriter(); + XMLStreamWriter xmlReader = streamFactory.createXMLStreamWriter(writer); + xmlReader.writeStartDocument(); + xmlReader.writeStartElement("start"); + xmlReader.writeEndElement(); + xmlReader.writeEndDocument(); + xmlReader.flush(); + + assertNotNull(xmlReader); + assertEquals(BASIC_CONTENT, writer.toString()); + } + + @Test(expected = XMLStreamException.class) + public void createWriterWrongContent() throws Exception { + XMLStreamWriter xmlReader = streamFactory.createXMLStreamWriter("fail"); + assertNotNull(xmlReader); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-android/src/test/java/org/apache/olingo/odata2/android/xml/AndroidXmlReaderTest.java ---------------------------------------------------------------------- diff --git a/odata2-android/src/test/java/org/apache/olingo/odata2/android/xml/AndroidXmlReaderTest.java b/odata2-android/src/test/java/org/apache/olingo/odata2/android/xml/AndroidXmlReaderTest.java new file mode 100644 index 0000000..51cebbb --- /dev/null +++ b/odata2-android/src/test/java/org/apache/olingo/odata2/android/xml/AndroidXmlReaderTest.java @@ -0,0 +1,164 @@ +/******************************************************************************* + * 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.olingo.odata2.android.xml; + +import org.apache.olingo.odata2.api.edm.Edm; +import org.apache.olingo.odata2.api.edm.EdmEntitySet; +import org.apache.olingo.odata2.api.ep.EntityProvider; +import org.apache.olingo.odata2.api.ep.EntityProviderReadProperties; +import org.apache.olingo.odata2.api.ep.entry.ODataEntry; +import org.apache.olingo.odata2.api.xml.XMLStreamConstants; +import org.apache.olingo.odata2.api.xml.XMLStreamException; +import org.apache.olingo.odata2.testutil.helper.StringHelper; +import org.apache.olingo.odata2.testutil.mock.MockFacade; +import org.custommonkey.xmlunit.SimpleNamespaceContext; +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; + +import static junit.framework.Assert.assertEquals; +import static org.apache.olingo.odata2.api.xml.XMLStreamReaderFactory.XML_STREAM_READER_FACTORY_CLASS; + +/** + */ +@RunWith(RobolectricTestRunner.class) +@Config(manifest=Config.NONE) +@Ignore("Will work with robolectric version 2.3") +public class AndroidXmlReaderTest { + + private static final String BASIC_RESULT = + "" + + "" + + "" + + ""; + + private static final String EMPLOYEE = "\n" + + "\n" + + "\thttp://rootEmployees('1')\n" + + "\tWalter Winter\n" + + "\t1999-01-01T00:00:00Z\n" + + "\t\n" + + "\t\n" + + "\t\n" + + "\t\n" + + "\t\n" + + "\t\n" + + "\t\n" + + "\t\n" + + "\t\t1\n" + + "\t\tWalter Winter\n" + + "\t\t1\n" + + "\t\t1\n" + + "\t\t42\n" + + "\t\t\n" + + "\t\t\t\n" + + "\t\t\t\t33470\n" + + "\t\t\t\tDuckburg\n" + + "\t\t\t\n" + + "\t\t\tCalisota\n" + + "\t\t\n" + + "\t\t52\n" + + "\t\t1999-01-01T00:00:00\n" + + "\t\t\n" + + "\t\n" + + ""; + + // CHECKSTYLE:OFF + @Before + public void init() { + // + System.setProperty(XML_STREAM_READER_FACTORY_CLASS, AndroidXmlFactory.class.getName()); // NOSONAR + // + Map prefixMap = new HashMap(); + prefixMap.put("", Edm.NAMESPACE_ATOM_2005); + prefixMap.put("d", Edm.NAMESPACE_D_2007_08); + prefixMap.put("m", Edm.NAMESPACE_M_2007_08); + prefixMap.put("xml", Edm.NAMESPACE_XML_1998); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(prefixMap)); + } + // CHECKSTYLE:ON + + @Test + public void basic() throws XMLStreamException { + StringHelper.Stream stream = StringHelper.toStream(BASIC_RESULT); + AndroidXmlReader xmlReader = new AndroidXmlReader(stream.asStream()); + + int[] expected = new int[]{ + XMLStreamConstants.START_DOCUMENT, + XMLStreamConstants.START_ELEMENT, + XMLStreamConstants.START_ELEMENT, + XMLStreamConstants.END_ELEMENT, + XMLStreamConstants.END_ELEMENT, + XMLStreamConstants.END_DOCUMENT}; + int pos = 0; + while(xmlReader.hasNext()) { + int elementId = xmlReader.next(); +// System.out.println("E: " + elementId); + assertEquals(expected[pos++], elementId); + } + } + + @Test + public void entityProvider() throws Exception { + Edm edmMock = MockFacade.getMockEdm(); + + String contentType = "application/xml"; + EdmEntitySet entitySet = edmMock.getDefaultEntityContainer().getEntitySet("Employees"); + EntityProviderReadProperties properties = + EntityProviderReadProperties.init().build(); + + StringHelper.Stream content = StringHelper.toStream(EMPLOYEE); + + ODataEntry entry = EntityProvider.readEntry(contentType, entitySet, content.asStream(), properties); + Map employeeData = entry.getProperties(); + + // validate + assertEquals(9, employeeData.size()); + // + assertEquals("1", employeeData.get("EmployeeId")); + assertEquals("Walter Winter", employeeData.get("EmployeeName")); + assertEquals("1", employeeData.get("ManagerId")); + assertEquals("1", employeeData.get("RoomId")); + assertEquals("42", employeeData.get("TeamId")); + assertEquals(Integer.valueOf(52), employeeData.get("Age")); + assertEquals(915148800000l, ((Calendar)employeeData.get("EntryDate")).getTimeInMillis()); + Map location = (Map) employeeData.get("Location"); + assertEquals("Calisota", location.get("Country")); + Map city = (Map) location.get("City"); + assertEquals("33470", city.get("PostalCode")); + assertEquals("Duckburg", city.get("CityName")); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-android/src/test/java/org/apache/olingo/odata2/android/xml/AndroidXmlWriterTest.java ---------------------------------------------------------------------- diff --git a/odata2-android/src/test/java/org/apache/olingo/odata2/android/xml/AndroidXmlWriterTest.java b/odata2-android/src/test/java/org/apache/olingo/odata2/android/xml/AndroidXmlWriterTest.java new file mode 100644 index 0000000..0eb376c --- /dev/null +++ b/odata2-android/src/test/java/org/apache/olingo/odata2/android/xml/AndroidXmlWriterTest.java @@ -0,0 +1,158 @@ +/******************************************************************************* + * 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.olingo.odata2.android.xml; + +import junit.framework.Assert; +import org.apache.olingo.odata2.api.edm.Edm; +import org.apache.olingo.odata2.api.edm.EdmEntitySet; +import org.apache.olingo.odata2.api.ep.EntityProvider; +import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties; +import org.apache.olingo.odata2.api.processor.ODataResponse; +import org.apache.olingo.odata2.api.xml.XMLStreamException; +import org.apache.olingo.odata2.testutil.helper.StringHelper; +import org.apache.olingo.odata2.testutil.mock.MockFacade; +import org.custommonkey.xmlunit.SimpleNamespaceContext; +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import java.io.StringWriter; +import java.io.Writer; +import java.net.URI; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; +import java.util.TimeZone; + +import static org.apache.olingo.odata2.api.xml.XMLStreamWriterFactory.XML_STREAM_WRITER_FACTORY_CLASS; +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.custommonkey.xmlunit.XMLAssert.assertXpathExists; + +/** + */ +@RunWith(RobolectricTestRunner.class) +@Config(manifest=Config.NONE) +public class AndroidXmlWriterTest { + + private static final String BASIC_RESULT = + "" + + "" + + "" + + ""; + + // CHECKSTYLE:OFF + @Before + public void init() { + // + System.setProperty(XML_STREAM_WRITER_FACTORY_CLASS, AndroidXmlFactory.class.getName()); // NOSONAR + // + Map prefixMap = new HashMap(); + prefixMap.put("", Edm.NAMESPACE_ATOM_2005); + prefixMap.put("d", Edm.NAMESPACE_D_2007_08); + prefixMap.put("m", Edm.NAMESPACE_M_2007_08); + prefixMap.put("xml", Edm.NAMESPACE_XML_1998); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(prefixMap)); + } + // CHECKSTYLE:ON + + @Test + public void basic() throws XMLStreamException { + Writer writer = new StringWriter(); + AndroidXmlWriter xmlWriter = new AndroidXmlWriter(writer); + + xmlWriter.writeStartDocument(); + xmlWriter.setDefaultNamespace("http://defaultNamespace"); + xmlWriter.writeStartElement("test"); + xmlWriter.writeStartElement("namespace", "second"); + xmlWriter.writeAttribute("attName", "attValue"); + xmlWriter.writeEndElement(); + xmlWriter.writeEndElement(); + xmlWriter.writeEndDocument(); + + xmlWriter.flush(); + + // + Assert.assertEquals(BASIC_RESULT, writer.toString()); + } + + @Test + public void entityProvider() throws Exception { + Edm edmMock = MockFacade.getMockEdm(); + + String contentType = "application/xml"; + EdmEntitySet entitySet = edmMock.getDefaultEntityContainer().getEntitySet("Employees"); + Map data = createEmployeeData(); + EntityProviderWriteProperties properties = + EntityProviderWriteProperties.serviceRoot(URI.create("http://root")).build(); + + ODataResponse entry = EntityProvider.writeEntry(contentType, entitySet, data, properties); + StringHelper.Stream content = StringHelper.toStream(entry.getEntity()); + + String xmlString = content.asString(); + System.out.println(xmlString); + + assertXpathEvaluatesTo(Edm.NAMESPACE_ATOM_2005, "/*/namespace::*[name()='']", xmlString); + + assertXpathExists("/:entry", xmlString); + assertXpathExists("/:entry/:content", xmlString); + // verify self link + assertXpathExists("/:entry/:link[@href=\"Employees('1')\"]", xmlString); + // verify content media link + assertXpathExists("/:entry/:link[@href=\"Employees('1')/$value\"]", xmlString); + // verify one navigation link + assertXpathExists("/:entry/:link[@title='ne_Manager']", xmlString); + + // verify content + assertXpathExists("/:entry/:content[@type='application/octet-stream']", xmlString); + // verify properties + assertXpathExists("/:entry/m:properties", xmlString); + assertXpathEvaluatesTo("9", "count(/:entry/m:properties/*)", xmlString); + } + + private Map createEmployeeData() { + Map employeeData = new HashMap(); + + Calendar date = Calendar.getInstance(TimeZone.getTimeZone("GMT")); + date.clear(); + date.set(1999, 0, 1); + + employeeData.put("EmployeeId", "1"); + employeeData.put("ImmageUrl", null); + employeeData.put("ManagerId", "1"); + employeeData.put("Age", new Integer(52)); + employeeData.put("RoomId", "1"); + employeeData.put("EntryDate", date); + employeeData.put("TeamId", "42"); + employeeData.put("EmployeeName", "Walter Winter"); + + Map locationData = new HashMap(); + Map cityData = new HashMap(); + cityData.put("PostalCode", "33470"); + cityData.put("CityName", "Duckburg"); + locationData.put("City", cityData); + locationData.put("Country", "Calisota"); + + employeeData.put("Location", locationData); + + return employeeData; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/xml/XMLStreamConstants.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/xml/XMLStreamConstants.java b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/xml/XMLStreamConstants.java index d6ab4c3..25d0d50 100644 --- a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/xml/XMLStreamConstants.java +++ b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/xml/XMLStreamConstants.java @@ -25,7 +25,7 @@ public interface XMLStreamConstants { int START_ELEMENT = 1; int END_ELEMENT = 2; // int PROCESSING_INSTRUCTION = 3; -// int CHARACTERS = 4; + int CHARACTERS = 4; // int COMMENT = 5; // int SPACE = 6; int START_DOCUMENT = 7; http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/xml/XMLStreamReader.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/xml/XMLStreamReader.java b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/xml/XMLStreamReader.java index 8f91335..0a47390 100644 --- a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/xml/XMLStreamReader.java +++ b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/xml/XMLStreamReader.java @@ -43,7 +43,7 @@ public interface XMLStreamReader { public abstract boolean isStartElement() throws XMLStreamException; - public abstract void next() throws XMLStreamException; + public abstract int next() throws XMLStreamException; public abstract boolean isEndElement() throws XMLStreamException; http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/xml/JavaxStaxReaderWrapper.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/xml/JavaxStaxReaderWrapper.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/xml/JavaxStaxReaderWrapper.java index 186ba71..98cadef 100644 --- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/xml/JavaxStaxReaderWrapper.java +++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/xml/JavaxStaxReaderWrapper.java @@ -90,9 +90,9 @@ public class JavaxStaxReaderWrapper implements XMLStreamReader { return reader.isStartElement(); } - public void next() throws XMLStreamException { + public int next() throws XMLStreamException { try { - reader.next(); + return reader.next(); } catch (javax.xml.stream.XMLStreamException e) { throw new XMLStreamException(e); } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/xml/JavaxStaxReaderWrapperTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/xml/JavaxStaxReaderWrapperTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/xml/JavaxStaxReaderWrapperTest.java new file mode 100644 index 0000000..88d16ff --- /dev/null +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/xml/JavaxStaxReaderWrapperTest.java @@ -0,0 +1,164 @@ +/******************************************************************************* + * 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.olingo.odata2.core.xml; + +import org.apache.olingo.odata2.api.edm.Edm; +import org.apache.olingo.odata2.api.edm.EdmEntitySet; +import org.apache.olingo.odata2.api.ep.EntityProvider; +import org.apache.olingo.odata2.api.ep.EntityProviderReadProperties; +import org.apache.olingo.odata2.api.ep.entry.ODataEntry; +import org.apache.olingo.odata2.api.xml.XMLStreamConstants; +import org.apache.olingo.odata2.api.xml.XMLStreamReader; +import org.apache.olingo.odata2.core.ep.AbstractProviderTest; +import org.apache.olingo.odata2.testutil.helper.StringHelper; +import org.apache.olingo.odata2.testutil.mock.MockFacade; +import org.custommonkey.xmlunit.SimpleNamespaceContext; +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Before; +import org.junit.Test; + +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static org.apache.olingo.odata2.api.xml.XMLStreamReaderFactory.XML_STREAM_READER_FACTORY_CLASS; + +/** + */ +public class JavaxStaxReaderWrapperTest extends AbstractProviderTest { + + private static final String BASIC_RESULT = + "" + + "" + + "TEST" + + ""; + + private static final String EMPLOYEE = "\n" + + "\n" + + "\thttp://rootEmployees('1')\n" + + "\tWalter Winter\n" + + "\t1999-01-01T00:00:00Z\n" + + "\t\n" + + "\t\n" + + "\t\n" + + "\t\n" + + "\t\n" + + "\t\n" + + "\t\n" + + "\t\n" + + "\t\t1\n" + + "\t\tWalter Winter\n" + + "\t\t1\n" + + "\t\t1\n" + + "\t\t42\n" + + "\t\t\n" + + "\t\t\t\n" + + "\t\t\t\t33470\n" + + "\t\t\t\tDuckburg\n" + + "\t\t\t\n" + + "\t\t\tCalisota\n" + + "\t\t\n" + + "\t\t52\n" + + "\t\t1999-01-01T00:00:00\n" + + "\t\t\n" + + "\t\n" + + ""; + + public JavaxStaxReaderWrapperTest(StreamWriterImplType type) { + super(type); + } + + // CHECKSTYLE:OFF + @Before + public void init() { + // + System.setProperty(XML_STREAM_READER_FACTORY_CLASS, JavaxStaxStreamFactory.class.getName()); // NOSONAR + // + Map prefixMap = new HashMap(); + prefixMap.put("", Edm.NAMESPACE_ATOM_2005); + prefixMap.put("d", Edm.NAMESPACE_D_2007_08); + prefixMap.put("m", Edm.NAMESPACE_M_2007_08); + prefixMap.put("xml", Edm.NAMESPACE_XML_1998); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(prefixMap)); + } + // CHECKSTYLE:ON + + @Test + public void basic() throws Exception { + StringHelper.Stream stream = StringHelper.toStream(BASIC_RESULT); + JavaxStaxStreamFactory javaxStaxStreamFactory = new JavaxStaxStreamFactory(); + XMLStreamReader xmlReader = javaxStaxStreamFactory.createXMLStreamReader(stream.asStream()); + + final int[] expected = new int[]{ + XMLStreamConstants.START_ELEMENT, + XMLStreamConstants.START_ELEMENT, + XMLStreamConstants.CHARACTERS, + XMLStreamConstants.END_ELEMENT, + XMLStreamConstants.END_ELEMENT, + XMLStreamConstants.END_DOCUMENT}; + int pos = 0; + while(xmlReader.hasNext()) { + int elementId = xmlReader.next(); + assertEquals("Unexpected type at position: " + pos, + expected[pos++], elementId); + } + } + + @Test + public void entityProvider() throws Exception { + Edm edmMock = MockFacade.getMockEdm(); + + String contentType = "application/xml"; + EdmEntitySet entitySet = edmMock.getDefaultEntityContainer().getEntitySet("Employees"); + EntityProviderReadProperties properties = + EntityProviderReadProperties.init().build(); + + StringHelper.Stream content = StringHelper.toStream(EMPLOYEE); + + ODataEntry entry = EntityProvider.readEntry(contentType, entitySet, content.asStream(), properties); + Map employeeData = entry.getProperties(); + + // validate + assertEquals(9, employeeData.size()); + // + assertEquals("1", employeeData.get("EmployeeId")); + assertEquals("Walter Winter", employeeData.get("EmployeeName")); + assertEquals("1", employeeData.get("ManagerId")); + assertEquals("1", employeeData.get("RoomId")); + assertEquals("42", employeeData.get("TeamId")); + assertEquals(Integer.valueOf(52), employeeData.get("Age")); + assertEquals(915148800000l, ((Calendar)employeeData.get("EntryDate")).getTimeInMillis()); + Map location = (Map) employeeData.get("Location"); + assertEquals("Calisota", location.get("Country")); + Map city = (Map) location.get("City"); + assertEquals("33470", city.get("PostalCode")); + assertEquals("Duckburg", city.get("CityName")); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/xml/JavaxStaxStreamFactoryTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/xml/JavaxStaxStreamFactoryTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/xml/JavaxStaxStreamFactoryTest.java new file mode 100644 index 0000000..ba93731 --- /dev/null +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/xml/JavaxStaxStreamFactoryTest.java @@ -0,0 +1,131 @@ +/******************************************************************************* + * 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.olingo.odata2.core.xml; + +import junit.framework.Assert; +import org.apache.olingo.odata2.api.edm.Edm; +import org.apache.olingo.odata2.api.edm.EdmEntitySet; +import org.apache.olingo.odata2.api.ep.EntityProvider; +import org.apache.olingo.odata2.api.ep.EntityProviderException; +import org.apache.olingo.odata2.api.ep.EntityProviderReadProperties; +import org.apache.olingo.odata2.api.ep.entry.ODataEntry; +import org.apache.olingo.odata2.api.xml.XMLStreamConstants; +import org.apache.olingo.odata2.api.xml.XMLStreamException; +import org.apache.olingo.odata2.api.xml.XMLStreamReader; +import org.apache.olingo.odata2.api.xml.XMLStreamWriter; +import org.apache.olingo.odata2.core.ep.AbstractXmlProducerTestHelper; +import org.apache.olingo.odata2.testutil.helper.StringHelper; +import org.apache.olingo.odata2.testutil.mock.MockFacade; +import org.custommonkey.xmlunit.SimpleNamespaceContext; +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Before; +import org.junit.Test; + +import java.io.*; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static org.apache.olingo.odata2.api.xml.XMLStreamReaderFactory.XML_STREAM_READER_FACTORY_CLASS; +import static org.apache.olingo.odata2.api.xml.XMLStreamWriterFactory.XML_STREAM_WRITER_FACTORY_CLASS; +import static org.custommonkey.xmlunit.XMLAssert.assertXpathExists; + +/** + */ +public class JavaxStaxStreamFactoryTest extends AbstractXmlProducerTestHelper { + + private JavaxStaxStreamFactory javaxStaxStreamFactory; + + public static final String BASIC_CONTENT = ""; + + public JavaxStaxStreamFactoryTest(StreamWriterImplType type) { + super(type); + } + + // CHECKSTYLE:OFF + @Before + public void init() { + // + System.setProperty(XML_STREAM_READER_FACTORY_CLASS, JavaxStaxStreamFactory.class.getName()); // NOSONAR + System.setProperty(XML_STREAM_WRITER_FACTORY_CLASS, JavaxStaxStreamFactory.class.getName()); // NOSONAR + // + Map prefixMap = new HashMap(); + prefixMap.put("", Edm.NAMESPACE_ATOM_2005); + prefixMap.put("d", Edm.NAMESPACE_D_2007_08); + prefixMap.put("m", Edm.NAMESPACE_M_2007_08); + prefixMap.put("xml", Edm.NAMESPACE_XML_1998); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(prefixMap)); + javaxStaxStreamFactory = new JavaxStaxStreamFactory(); + } + // CHECKSTYLE:ON + + + @Test + public void createReader() throws Exception { + InputStream stream = StringHelper.encapsulate(BASIC_CONTENT); + XMLStreamReader xmlReader = javaxStaxStreamFactory.createXMLStreamReader(stream); + assertNotNull(xmlReader); + Assert.assertTrue(xmlReader.hasNext()); + } + + @Test(expected = EntityProviderException.class) + public void createReaderWrongContent() throws Exception { + XMLStreamReader xmlReader = javaxStaxStreamFactory.createXMLStreamReader("content"); + assertNotNull(xmlReader); + Assert.assertTrue(xmlReader.hasNext()); + } + + @Test + public void createWriterStream() throws Exception { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + XMLStreamWriter xmlReader = javaxStaxStreamFactory.createXMLStreamWriter(stream); + xmlReader.writeStartDocument(); + xmlReader.writeStartElement("start"); + xmlReader.writeEndElement(); + xmlReader.writeEndDocument(); + xmlReader.flush(); + + assertNotNull(xmlReader); + String content = new String(stream.toByteArray()); + assertXpathExists("/start", content); + } + + @Test + public void createWriterPrintWriter() throws Exception { + + StringWriter writer = new StringWriter(); + XMLStreamWriter xmlReader = javaxStaxStreamFactory.createXMLStreamWriter(writer); + xmlReader.writeStartDocument(); + xmlReader.writeStartElement("start"); + xmlReader.writeEndElement(); + xmlReader.writeEndDocument(); + xmlReader.flush(); + + assertNotNull(xmlReader); + assertXpathExists("/start", writer.toString()); + } + + @Test(expected = IllegalArgumentException.class) + public void createWriterWrongContent() throws Exception { + XMLStreamWriter xmlReader = javaxStaxStreamFactory.createXMLStreamWriter("fail"); + assertNotNull(xmlReader); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/xml/JavaxStaxWriterWrapperTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/xml/JavaxStaxWriterWrapperTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/xml/JavaxStaxWriterWrapperTest.java new file mode 100644 index 0000000..3920b1b --- /dev/null +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/xml/JavaxStaxWriterWrapperTest.java @@ -0,0 +1,194 @@ +/******************************************************************************* + * 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.olingo.odata2.core.xml; + +import org.apache.olingo.odata2.api.edm.Edm; +import org.apache.olingo.odata2.api.edm.EdmEntitySet; +import org.apache.olingo.odata2.api.ep.EntityProvider; +import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties; +import org.apache.olingo.odata2.api.processor.ODataResponse; +import org.apache.olingo.odata2.core.ep.AbstractXmlProducerTestHelper; +import org.apache.olingo.odata2.testutil.helper.StringHelper; +import org.apache.olingo.odata2.testutil.mock.MockFacade; +import org.custommonkey.xmlunit.SimpleNamespaceContext; +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Before; +import org.junit.Test; + +import java.io.InputStream; +import java.io.StringWriter; +import java.io.Writer; +import java.net.URI; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; +import java.util.TimeZone; + +import static org.apache.olingo.odata2.api.xml.XMLStreamWriterFactory.XML_STREAM_WRITER_FACTORY_CLASS; +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.custommonkey.xmlunit.XMLAssert.assertXpathExists; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + */ +public class JavaxStaxWriterWrapperTest extends AbstractXmlProducerTestHelper { + + private static final String BASIC_RESULT = + "" + + ""; + + public JavaxStaxWriterWrapperTest(StreamWriterImplType type) { + super(type); + } + + // CHECKSTYLE:OFF + @Before + public void init() { + // + System.setProperty(XML_STREAM_WRITER_FACTORY_CLASS, JavaxStaxStreamFactory.class.getName()); // NOSONAR + // + Map prefixMap = new HashMap(); + prefixMap.put("", Edm.NAMESPACE_ATOM_2005); + prefixMap.put("d", Edm.NAMESPACE_D_2007_08); + prefixMap.put("m", Edm.NAMESPACE_M_2007_08); + prefixMap.put("xml", Edm.NAMESPACE_XML_1998); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(prefixMap)); + } + // CHECKSTYLE:ON + + @Test + public void basic() throws Exception { + final String defaultNamespace = "http://defaultNamespace"; + final String namespaceNs1 = "namespace"; + + Map prefixMap = new HashMap(); + prefixMap.put("", defaultNamespace); + prefixMap.put("ns1", namespaceNs1); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(prefixMap)); + + Writer content = new StringWriter(); + JavaxStaxStreamFactory javaxStaxStreamFactory = new JavaxStaxStreamFactory(); + JavaxStaxWriterWrapper xmlWriter = (JavaxStaxWriterWrapper) javaxStaxStreamFactory.createXMLStreamWriter(content); + + String encoding = "UTF-8"; + String version = "1.0"; + + xmlWriter.writeStartDocument(encoding, version); + xmlWriter.setDefaultNamespace(defaultNamespace); + xmlWriter.setPrefix("ns1", namespaceNs1); + xmlWriter.writeStartElement("test"); + xmlWriter.writeDefaultNamespace(defaultNamespace); + xmlWriter.writeNamespace("ns1", namespaceNs1); + xmlWriter.writeStartElement(namespaceNs1, "second"); + xmlWriter.writeAttribute("attName", "attValue"); + xmlWriter.writeEndElement(); + xmlWriter.writeEndElement(); + xmlWriter.writeEndDocument(); + + xmlWriter.flush(); + + // + String xmlString = content.toString(); + assertXpathExists("/:test", xmlString); + assertXpathExists("/:test/ns1:second", xmlString); + assertXpathExists("/:test/ns1:second[@attName=\"attValue\"]", xmlString); + } + + @Test + public void entityProvider() throws Exception { + Edm edmMock = MockFacade.getMockEdm(); + + String contentType = "application/xml"; + EdmEntitySet entitySet = edmMock.getDefaultEntityContainer().getEntitySet("Employees"); + Map data = createEmployeeData(); + EntityProviderWriteProperties properties = + EntityProviderWriteProperties.serviceRoot(URI.create("http://root")).build(); + + ODataResponse entry = EntityProvider.writeEntry(contentType, entitySet, data, properties); + StringHelper.Stream content = StringHelper.toStream(entry.getEntity()); + + String xmlString = content.asString(); + + assertXpathEvaluatesTo(Edm.NAMESPACE_ATOM_2005, "/*/namespace::*[name()='']", xmlString); + + assertXpathExists("/:entry", xmlString); + assertXpathExists("/:entry/:content", xmlString); + // verify self link + assertXpathExists("/:entry/:link[@href=\"Employees('1')\"]", xmlString); + // verify content media link + assertXpathExists("/:entry/:link[@href=\"Employees('1')/$value\"]", xmlString); + // verify one navigation link + assertXpathExists("/:entry/:link[@title='ne_Manager']", xmlString); + + // verify content + assertXpathExists("/:entry/:content[@type='application/octet-stream']", xmlString); + // verify properties + assertXpathExists("/:entry/m:properties", xmlString); + assertXpathEvaluatesTo("9", "count(/:entry/m:properties/*)", xmlString); + } + + + @Test + public void writeMetadata() throws Exception { + Map predefinedNamespaces = new HashMap(); + predefinedNamespaces.put("annoPrefix", "http://annoNamespace"); + predefinedNamespaces.put("foo", "http://foo"); + predefinedNamespaces.put("annoPrefix2", "http://annoNamespace"); + predefinedNamespaces.put("annoPrefix", "http://annoNamespace"); + + ODataResponse response = EntityProvider.writeMetadata(null, predefinedNamespaces); + assertNotNull(response); + assertNotNull(response.getEntity()); + assertNull("BasicProvider should not set content header", response.getContentHeader()); + String metadata = StringHelper.inputStreamToString((InputStream) response.getEntity()); + assertTrue(metadata.contains("xmlns:foo=\"http://foo\"")); + assertTrue(metadata.contains("xmlns:annoPrefix=\"http://annoNamespace\"")); + assertTrue(metadata.contains("xmlns:annoPrefix2=\"http://annoNamespace\"")); + } + + private Map createEmployeeData() { + Map employeeData = new HashMap(); + + Calendar date = Calendar.getInstance(TimeZone.getTimeZone("GMT")); + date.clear(); + date.set(1999, 0, 1); + + employeeData.put("EmployeeId", "1"); + employeeData.put("ImmageUrl", null); + employeeData.put("ManagerId", "1"); + employeeData.put("Age", new Integer(52)); + employeeData.put("RoomId", "1"); + employeeData.put("EntryDate", date); + employeeData.put("TeamId", "42"); + employeeData.put("EmployeeName", "Walter Winter"); + + Map locationData = new HashMap(); + Map cityData = new HashMap(); + cityData.put("PostalCode", "33470"); + cityData.put("CityName", "Duckburg"); + locationData.put("City", cityData); + locationData.put("Country", "Calisota"); + + employeeData.put("Location", locationData); + + return employeeData; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/c321fb74/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/helper/StringHelper.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/helper/StringHelper.java b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/helper/StringHelper.java index feb4ddd..ec8a22f 100644 --- a/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/helper/StringHelper.java +++ b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/helper/StringHelper.java @@ -79,6 +79,16 @@ public class StringHelper { } } + public static Stream toStream(Object content) throws IOException { + if(content instanceof InputStream) { + return toStream((InputStream) content); + } else if(content instanceof String) { + return toStream((String) content); + } else { + throw new IllegalArgumentException("Given object type is not supported."); + } + } + public static Stream toStream(InputStream stream) throws IOException { byte[] result = new byte[0]; byte[] tmp = new byte[8192];