From: Adam R. <ad...@ex...> - 2011-06-14 08:04:31
|
Great to see someone else making use of EasyMock in eXist-db to do Unit testing as opposed to integration testing - very very pleased :-) On 13 June 2011 23:47, <rwa...@us...> wrote: > Revision: 14679 > http://exist.svn.sourceforge.net/exist/?rev=14679&view=rev > Author: rwalpole > Date: 2011-06-13 21:47:52 +0000 (Mon, 13 Jun 2011) > > Log Message: > ----------- > New EXI serializer [feature] > > Added Paths: > ----------- > trunk/eXist/src/org/exist/util/serializer/EXISerializer.java > trunk/eXist/test/src/org/exist/util/serializer/EXISerializerTest.java > > Added: trunk/eXist/src/org/exist/util/serializer/EXISerializer.java > =================================================================== > --- trunk/eXist/src/org/exist/util/serializer/EXISerializer.java (rev 0) > +++ trunk/eXist/src/org/exist/util/serializer/EXISerializer.java 2011-06-13 21:47:52 UTC (rev 14679) > @@ -0,0 +1,193 @@ > +/* > + * eXist Open Source Native XML Database > + * Copyright (C) 2011 The eXist Project > + * http://exist-db.org > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU Lesser General Public License > + * as published by the Free Software Foundation; either version 2 > + * of the License, or (at your option) any later version. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU Lesser General Public License for more details. > + * > + * You should have received a copy of the GNU Lesser General Public > + * License along with this library; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA > + */ > +package org.exist.util.serializer; > + > +import java.io.OutputStream; > + > +import org.exist.dom.QName; > +import org.exist.dom.StoredNode; > +import org.w3c.dom.Document; > +import org.xml.sax.Attributes; > +import org.xml.sax.ContentHandler; > +import org.xml.sax.Locator; > +import org.xml.sax.SAXException; > +import org.xml.sax.helpers.AttributesImpl; > + > +import com.siemens.ct.exi.EXIFactory; > +import com.siemens.ct.exi.api.sax.SAXEncoder; > +import com.siemens.ct.exi.exceptions.EXIException; > +import com.siemens.ct.exi.helpers.DefaultEXIFactory; > + > +public class EXISerializer implements ContentHandler, Receiver { > + > + static final String UNKNOWN_TYPE = ""; > + > + private SAXEncoder encoder; > + > + public EXISerializer(OutputStream outputStream) throws EXIException { > + EXIFactory exiFactory = DefaultEXIFactory.newInstance(); > + encoder = new SAXEncoder(exiFactory, outputStream); > + } > + > + public void startDocument() throws SAXException { > + encoder.startDocument(); > + } > + > + public void endDocument() throws SAXException { > + encoder.endDocument(); > + } > + > + @Override > + public void startPrefixMapping(String prefix, String namespaceURI) > + throws SAXException { > + encoder.startPrefixMapping(prefix, namespaceURI); > + } > + > + @Override > + public void endPrefixMapping(String prefix) throws SAXException { > + encoder.endPrefixMapping(prefix); > + } > + > + @Override > + public void startElement(QName qname, AttrList attribs) throws SAXException { > + AttributesImpl attributes = null; > + if(attribs != null) { > + attributes = new AttributesImpl(); > + for(int x=0; x < attribs.size; x++) { > + QName attribQName = attribs.getQName(x); > + attributes.addAttribute(attribQName.getNamespaceURI(), > + attribQName.getLocalName(), > + attribQName.getStringValue(), > + UNKNOWN_TYPE, > + attribs.getValue(x)); > + } > + } > + encoder.startElement(qname.getNamespaceURI(), qname.getLocalName(), null, attributes); > + > + } > + > + @Override > + public void endElement(QName qname) throws SAXException { > + encoder.endElement(qname.getNamespaceURI(), qname.getLocalName(), null); > + > + } > + > + @Override > + public void characters(CharSequence seq) throws SAXException { > + String sequence = seq.toString(); > + encoder.characters(sequence.toCharArray(), 0, sequence.length()); > + > + } > + > + @Override > + public void attribute(QName qname, String value) throws SAXException { > + // TODO Auto-generated method stub > + > + } > + > + @Override > + public void comment(char[] ch, int start, int length) throws SAXException { > + // TODO Auto-generated method stub > + > + } > + > + @Override > + public void cdataSection(char[] ch, int start, int len) throws SAXException { > + // TODO Auto-generated method stub > + > + } > + > + @Override > + public void processingInstruction(String target, String data) > + throws SAXException { > + // TODO Auto-generated method stub > + > + } > + > + @Override > + public void documentType(String name, String publicId, String systemId) > + throws SAXException { > + // TODO Auto-generated method stub > + > + } > + > + @Override > + public void highlightText(CharSequence seq) throws SAXException { > + // TODO Auto-generated method stub > + > + } > + > + @Override > + public void setCurrentNode(StoredNode node) { > + // TODO Auto-generated method stub > + > + } > + > + @Override > + public Document getDocument() { > + // TODO Auto-generated method stub > + return null; > + } > + > + @Override > + public void setDocumentLocator(Locator locator) { > + // TODO Auto-generated method stub > + > + } > + > + @Override > + public void startElement(String uri, String localName, String qName, > + Attributes atts) throws SAXException { > + encoder.startElement(uri, localName, null, atts); > + > + } > + > + @Override > + public void endElement(String uri, String localName, String qName) > + throws SAXException { > + encoder.endElement(uri, localName, null); > + > + } > + > + @Override > + public void characters(char[] ch, int start, int length) > + throws SAXException { > + encoder.characters(ch, start, length); > + > + } > + > + @Override > + public void ignorableWhitespace(char[] ch, int start, int length) > + throws SAXException { > + // TODO Auto-generated method stub > + > + } > + > + @Override > + public void skippedEntity(String name) throws SAXException { > + // TODO Auto-generated method stub > + > + } > + > + void setEncoder(SAXEncoder encoder) { > + this.encoder = encoder; > + } > + > +} > \ No newline at end of file > > Added: trunk/eXist/test/src/org/exist/util/serializer/EXISerializerTest.java > =================================================================== > --- trunk/eXist/test/src/org/exist/util/serializer/EXISerializerTest.java (rev 0) > +++ trunk/eXist/test/src/org/exist/util/serializer/EXISerializerTest.java 2011-06-13 21:47:52 UTC (rev 14679) > @@ -0,0 +1,98 @@ > +package org.exist.util.serializer; > + > +import static org.easymock.EasyMock.capture; > +import static org.easymock.EasyMock.isNull; > +import static org.easymock.EasyMock.matches; > +import static org.easymock.classextension.EasyMock.createMock; > +import static org.easymock.classextension.EasyMock.replay; > +import static org.easymock.classextension.EasyMock.verify; > + > +import java.io.OutputStream; > +import java.util.List; > + > +import junit.framework.Assert; > + > +import org.easymock.Capture; > +import org.exist.dom.QName; > +import org.junit.Before; > +import org.junit.Ignore; > +import org.junit.Test; > +import org.xml.sax.Attributes; > + > +import com.siemens.ct.exi.api.sax.SAXEncoder; > + > +public class EXISerializerTest { > + > + private EXISerializer serializer; > + private OutputStream mockOutputStream; > + private SAXEncoder mockEncoder; > + > + @Before > + public void setUp() throws Exception { > + mockOutputStream = createMock(OutputStream.class); > + serializer = new EXISerializer(mockOutputStream); > + mockEncoder = createMock(SAXEncoder.class); > + serializer.setEncoder(mockEncoder); > + } > + > + @Test > + public void testStartDocument() throws Exception { > + mockEncoder.startDocument(); > + replay(mockEncoder); > + serializer.startDocument(); > + verify(mockEncoder); > + } > + > + @Test > + public void testEndDocument() throws Exception { > + mockEncoder.endDocument(); > + replay(mockEncoder); > + serializer.endDocument(); > + verify(mockEncoder); > + } > + > + @Test > + public void testStartPrefixMapping() throws Exception { > + mockEncoder.startPrefixMapping("prefix", "uri"); > + replay(mockEncoder); > + serializer.startPrefixMapping("prefix", "uri"); > + verify(mockEncoder); > + } > + > + @Test > + public void testEndPrefixMapping() throws Exception { > + mockEncoder.endPrefixMapping("prefix"); > + replay(mockEncoder); > + serializer.endPrefixMapping("prefix"); > + verify(mockEncoder); > + } > + > + @Test > + public void testStartElement() throws Exception { > + QName testQName = new QName("local", "uri", "prefix"); > + AttrList testAttrList = new AttrList(); > + testAttrList.addAttribute(new QName("local", "uri"), "value"); > + Capture<Attributes> capturedAttributes = new Capture<Attributes>(); > + mockEncoder.startElement(matches("uri"), matches("local"), (String)isNull(), capture(capturedAttributes)); > + replay(mockEncoder); > + serializer.startElement(testQName, testAttrList); > + verify(mockEncoder); > + List<Attributes> capturedAttributeList = capturedAttributes.getValues(); > + Assert.assertEquals("local", capturedAttributeList.get(0).getLocalName(0)); > + Assert.assertEquals("uri", capturedAttributeList.get(0).getURI(0)); > + Assert.assertEquals("value", capturedAttributeList.get(0).getValue(0)); > + } > + > + @Ignore("incomplete") > + @Test > + public void testEndElement() { > + // TODO > + } > + > + @Ignore("incomplete") > + @Test > + public void testCharacters() { > + // TODO > + } > + > +} > \ No newline at end of file > > > This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. > > ------------------------------------------------------------------------------ > EditLive Enterprise is the world's most technically advanced content > authoring tool. Experience the power of Track Changes, Inline Image > Editing and ensure content is compliant with Accessibility Checking. > http://p.sf.net/sfu/ephox-dev2dev > _______________________________________________ > Exist-commits mailing list > Exi...@li... > https://lists.sourceforge.net/lists/listinfo/exist-commits > -- Adam Retter eXist Developer { United Kingdom } ad...@ex... irc://irc.freenode.net/existdb |