[jboss-cvs] JBossAS SVN: r107594 - in projects/cluster/ha-server-ispn/trunk/src: test/java/org/jboss/ha/ispn/config/xml and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Aug 13 13:59:09 EDT 2010
Author: pferraro
Date: 2010-08-13 13:59:08 -0400 (Fri, 13 Aug 2010)
New Revision: 107594
Added:
projects/cluster/ha-server-ispn/trunk/src/test/java/org/jboss/ha/ispn/config/xml/PropertyReplacementFilterTest.java
Modified:
projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/config/xml/PropertyReplacementFilter.java
projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/config/xml/XMLCacheContainerRegistryConfigurationSource.java
Log:
Fix bug in PropertyReplacementFilter attribute filtering.
Complete unit test for PropertyReplacementFilter.
Modified: projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/config/xml/PropertyReplacementFilter.java
===================================================================
--- projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/config/xml/PropertyReplacementFilter.java 2010-08-13 14:22:21 UTC (rev 107593)
+++ projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/config/xml/PropertyReplacementFilter.java 2010-08-13 17:59:08 UTC (rev 107594)
@@ -24,6 +24,7 @@
import org.jboss.util.StringPropertyReplacer;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.XMLFilterImpl;
@@ -33,6 +34,16 @@
*/
public class PropertyReplacementFilter extends XMLFilterImpl
{
+ public PropertyReplacementFilter()
+ {
+ super();
+ }
+
+ public PropertyReplacementFilter(XMLReader parent)
+ {
+ super(parent);
+ }
+
/**
* {@inheritDoc}
* @see org.xml.sax.helpers.XMLFilterImpl#characters(char[], int, int)
@@ -53,10 +64,10 @@
{
AttributesImpl attributes = (attrs instanceof AttributesImpl) ? (AttributesImpl) attrs : new AttributesImpl(attrs);
- int length = attrs.getLength();
+ int length = attributes.getLength();
for (int i = 0; i < length; ++i)
{
- attributes.setValue(i, StringPropertyReplacer.replaceProperties(attrs.getValue(i)));
+ attributes.setValue(i, StringPropertyReplacer.replaceProperties(attributes.getValue(i)));
}
super.startElement(uri, localName, qName, attributes);
Modified: projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/config/xml/XMLCacheContainerRegistryConfigurationSource.java
===================================================================
--- projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/config/xml/XMLCacheContainerRegistryConfigurationSource.java 2010-08-13 14:22:21 UTC (rev 107593)
+++ projects/cluster/ha-server-ispn/trunk/src/main/java/org/jboss/ha/ispn/config/xml/XMLCacheContainerRegistryConfigurationSource.java 2010-08-13 17:59:08 UTC (rev 107594)
@@ -38,7 +38,7 @@
import org.jboss.ha.ispn.config.CacheContainerRegistryConfigurationSource;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
-import org.xml.sax.XMLFilter;
+import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
@@ -115,9 +115,8 @@
Unmarshaller unmarshaller = JAXBContext.newInstance(CacheContainerRegistryConfiguration.class).createUnmarshaller();
unmarshaller.setSchema(schemaFactory.newSchema(this.schemaURL));
- XMLFilter filter = new PropertyReplacementFilter();
- filter.setParent(XMLReaderFactory.createXMLReader());
- Source source = new SAXSource(filter, new InputSource(this.xmlURL.toString()));
+ XMLReader reader = new PropertyReplacementFilter(XMLReaderFactory.createXMLReader());
+ Source source = new SAXSource(reader, new InputSource(this.xmlURL.toString()));
return (CacheContainerRegistryConfiguration) unmarshaller.unmarshal(source);
}
}
Added: projects/cluster/ha-server-ispn/trunk/src/test/java/org/jboss/ha/ispn/config/xml/PropertyReplacementFilterTest.java
===================================================================
--- projects/cluster/ha-server-ispn/trunk/src/test/java/org/jboss/ha/ispn/config/xml/PropertyReplacementFilterTest.java (rev 0)
+++ projects/cluster/ha-server-ispn/trunk/src/test/java/org/jboss/ha/ispn/config/xml/PropertyReplacementFilterTest.java 2010-08-13 17:59:08 UTC (rev 107594)
@@ -0,0 +1,634 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This 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.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ha.ispn.config.xml;
+
+import java.io.IOException;
+
+import org.easymock.Capture;
+import org.easymock.EasyMock;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.DTDHandler;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLFilterImpl;
+
+/**
+ * @author Paul Ferraro
+ */
+public class PropertyReplacementFilterTest
+{
+ private ContentHandler contentHandler = EasyMock.createStrictMock(ContentHandler.class);
+ private DTDHandler dtdHandler = EasyMock.createStrictMock(DTDHandler.class);
+ private ErrorHandler errorHandler = EasyMock.createStrictMock(ErrorHandler.class);
+ private EntityResolver resolver = EasyMock.createStrictMock(EntityResolver.class);
+ private XMLReader parent = EasyMock.createStrictMock(XMLReader.class);
+
+ private XMLFilterImpl filter = new PropertyReplacementFilter(this.parent);
+
+ private static String oldExistingValue;
+ private static String oldNonExistingValue;
+
+ @BeforeClass
+ public static void beforeClass()
+ {
+ oldExistingValue = System.setProperty("existing", "Release");
+ oldNonExistingValue = System.clearProperty("non-existing");
+ }
+
+ @AfterClass
+ public static void afterClass()
+ {
+ if (oldExistingValue != null)
+ {
+ System.setProperty("existing", oldExistingValue);
+ }
+ else
+ {
+ System.clearProperty("existing");
+ }
+
+ if (oldNonExistingValue != null)
+ {
+ System.setProperty("non-existing", oldNonExistingValue);
+ }
+ }
+
+ @Before
+ public void before()
+ {
+ this.filter.setContentHandler(this.contentHandler);
+ this.filter.setDTDHandler(this.dtdHandler);
+ this.filter.setEntityResolver(this.resolver);
+ this.filter.setErrorHandler(this.errorHandler);
+ }
+
+ @Test
+ public void characters() throws SAXException
+ {
+ String plain = "test";
+
+ this.contentHandler.characters(EasyMock.aryEq(plain.toCharArray()), EasyMock.eq(0), EasyMock.eq(plain.length()));
+
+ EasyMock.replay(this.contentHandler);
+
+ this.filter.characters(plain.toCharArray(), 0, plain.length());
+
+ EasyMock.verify(this.contentHandler);
+ EasyMock.reset(this.contentHandler);
+
+
+ String string = "${existing:Free} the ${non-existing:Kraken}!";
+ String expected = "Release the Kraken!";
+
+ this.contentHandler.characters(EasyMock.aryEq(expected.toCharArray()), EasyMock.eq(0), EasyMock.eq(expected.length()));
+
+ EasyMock.replay(this.contentHandler);
+
+ this.filter.characters(string.toCharArray(), 0, string.length());
+
+ EasyMock.verify(this.contentHandler);
+ EasyMock.reset(this.contentHandler);
+ }
+
+ @Test
+ public void startElement() throws SAXException
+ {
+ Attributes attributes = EasyMock.createNiceMock(Attributes.class);
+ Capture<Attributes> capturedAttributes = new Capture<Attributes>();
+
+ String plain = "test";
+ String attributeURI = "uri";
+ String attributeLocalName = "localName";
+ String attributeQName = "qName";
+ String attributeType = "type";
+ String elementURI = "uri";
+ String elementLocalName = "localName";
+ String elementQName = "qName";
+
+ EasyMock.expect(attributes.getLength()).andReturn(1);
+ EasyMock.expect(attributes.getURI(0)).andReturn(attributeURI);
+ EasyMock.expect(attributes.getLocalName(0)).andReturn(attributeLocalName);
+ EasyMock.expect(attributes.getQName(0)).andReturn(attributeQName);
+ EasyMock.expect(attributes.getType(0)).andReturn(attributeType);
+ EasyMock.expect(attributes.getValue(0)).andReturn(plain);
+
+ this.contentHandler.startElement(EasyMock.eq(elementURI), EasyMock.eq(elementLocalName), EasyMock.eq(elementQName), EasyMock.capture(capturedAttributes));
+
+ EasyMock.replay(this.contentHandler, attributes);
+
+ this.filter.startElement(elementURI, elementLocalName, elementQName, attributes);
+
+ EasyMock.verify(this.contentHandler, attributes);
+
+ Attributes attr = capturedAttributes.getValue();
+ Assert.assertEquals(1, attr.getLength());
+ Assert.assertSame(attributeURI, attr.getURI(0));
+ Assert.assertSame(attributeLocalName, attr.getLocalName(0));
+ Assert.assertSame(attributeQName, attr.getQName(0));
+ Assert.assertSame(attributeType, attr.getType(0));
+ Assert.assertEquals(plain, attr.getValue(0));
+
+ EasyMock.reset(this.contentHandler, attributes);
+
+
+ String string = "${existing:Free} the ${non-existing:Kraken}!";
+ String expected = "Release the Kraken!";
+
+ EasyMock.expect(attributes.getLength()).andReturn(1);
+ EasyMock.expect(attributes.getURI(0)).andReturn(attributeURI);
+ EasyMock.expect(attributes.getLocalName(0)).andReturn(attributeLocalName);
+ EasyMock.expect(attributes.getQName(0)).andReturn(attributeQName);
+ EasyMock.expect(attributes.getValue(0)).andReturn(string);
+
+ this.contentHandler.startElement(EasyMock.eq(elementURI), EasyMock.eq(elementLocalName), EasyMock.eq(elementQName), EasyMock.capture(capturedAttributes));
+
+ EasyMock.replay(this.contentHandler, attributes);
+
+ this.filter.startElement(elementURI, elementLocalName, elementQName, attributes);
+
+ EasyMock.verify(this.contentHandler, attributes);
+
+ attr = capturedAttributes.getValue();
+ Assert.assertEquals(1, attr.getLength());
+ Assert.assertSame(attributeURI, attr.getURI(0));
+ Assert.assertSame(attributeLocalName, attr.getLocalName(0));
+ Assert.assertSame(attributeQName, attr.getQName(0));
+ Assert.assertEquals(expected, attr.getValue(0));
+
+ EasyMock.reset(this.contentHandler, attributes);
+ }
+
+ @Test
+ public void getContentHandler()
+ {
+ Assert.assertSame(this.contentHandler, this.filter.getContentHandler());
+ }
+
+ @Test
+ public void getDTDHandler()
+ {
+ Assert.assertSame(this.dtdHandler, this.filter.getDTDHandler());
+ }
+
+ @Test
+ public void getEntityResolver()
+ {
+ Assert.assertSame(this.resolver, this.filter.getEntityResolver());
+ }
+
+ @Test
+ public void getErrorHandler()
+ {
+ Assert.assertSame(this.errorHandler, this.filter.getErrorHandler());
+ }
+
+ @Test
+ public void getFeature() throws SAXException
+ {
+ String feature = "name";
+
+ EasyMock.expect(this.parent.getFeature(feature)).andReturn(true);
+
+ EasyMock.replay(this.parent);
+
+ SAXException exception = null;
+ Boolean result = null;
+ try
+ {
+ result = this.filter.getFeature(feature);
+ }
+ catch (SAXException e)
+ {
+ exception = e;
+ }
+
+ EasyMock.verify(this.parent);
+
+ Assert.assertNull(exception);
+ Assert.assertNotNull(result);
+ Assert.assertTrue(result);
+
+ EasyMock.reset(this.parent);
+
+
+ this.getFeature(feature, new SAXNotRecognizedException());
+ this.getFeature(feature, new SAXNotSupportedException());
+ }
+
+ private void getFeature(String feature, SAXException expected) throws SAXException
+ {
+ EasyMock.expect(this.parent.getFeature(feature)).andThrow(expected);
+
+ EasyMock.replay(this.parent);
+
+ SAXException exception = null;
+ Boolean result = null;
+ try
+ {
+ result = this.filter.getFeature(feature);
+ }
+ catch (SAXException e)
+ {
+ exception = e;
+ }
+
+ EasyMock.verify(this.parent);
+
+ Assert.assertNull(result);
+ Assert.assertNotNull(exception);
+ Assert.assertSame(expected, exception);
+
+ EasyMock.reset(this.parent);
+ }
+
+ @Test
+ public void getParent()
+ {
+ EasyMock.replay(this.parent);
+
+ XMLReader result = this.filter.getParent();
+
+ EasyMock.verify(this.parent);
+
+ Assert.assertSame(this.parent, result);
+
+ EasyMock.reset(this.parent);
+ }
+
+ @Test
+ public void getProperty() throws SAXException
+ {
+ String property = "name";
+ Object expected = new Object();
+
+ EasyMock.expect(this.parent.getProperty(property)).andReturn(expected);
+
+ EasyMock.replay(this.parent);
+
+ Object result = this.filter.getProperty(property);
+
+ EasyMock.verify(this.parent);
+
+ Assert.assertSame(expected, result);
+
+ EasyMock.reset(this.parent);
+
+
+ this.getProperty(property, new SAXNotRecognizedException());
+ this.getProperty(property, new SAXNotSupportedException());
+ }
+
+ private void getProperty(String property, SAXException expected) throws SAXException
+ {
+ EasyMock.expect(this.parent.getProperty(property)).andThrow(expected);
+
+ EasyMock.replay(this.parent);
+
+ SAXException exception = null;
+
+ try
+ {
+ this.filter.getProperty(property);
+ }
+ catch (SAXException e)
+ {
+ exception = e;
+ }
+
+ EasyMock.verify(this.parent);
+
+ Assert.assertNotNull(exception);
+ Assert.assertSame(expected, exception);
+
+ EasyMock.reset(this.parent);
+ }
+
+ @Test
+ public void parseInputSource() throws IOException, SAXException
+ {
+ InputSource source = EasyMock.createMock(InputSource.class);
+
+ EasyMock.checkOrder(this.parent, false);
+ this.parent.setContentHandler(this.filter);
+ this.parent.setDTDHandler(this.filter);
+ this.parent.setEntityResolver(this.filter);
+ this.parent.setErrorHandler(this.filter);
+ EasyMock.checkOrder(this.parent, true);
+
+ this.parent.parse(source);
+
+ EasyMock.replay(this.parent);
+
+ this.filter.parse(source);
+
+ EasyMock.verify(this.parent);
+ EasyMock.reset(this.parent);
+ }
+
+ @Test
+ public void parseSystemId() throws IOException, SAXException
+ {
+ Capture<InputSource> capturedSource = new Capture<InputSource>();
+ String systemId = "";
+
+ EasyMock.checkOrder(this.parent, false);
+ this.parent.setContentHandler(this.filter);
+ this.parent.setDTDHandler(this.filter);
+ this.parent.setEntityResolver(this.filter);
+ this.parent.setErrorHandler(this.filter);
+ EasyMock.checkOrder(this.parent, true);
+
+ this.parent.parse(EasyMock.capture(capturedSource));
+
+ EasyMock.replay(this.parent);
+
+ this.filter.parse(systemId);
+
+ EasyMock.verify(this.parent);
+
+ InputSource source = capturedSource.getValue();
+ Assert.assertSame(systemId, source.getSystemId());
+
+ EasyMock.reset(this.parent);
+ }
+
+ @Test
+ public void setFeature() throws SAXException
+ {
+ String feature = "feature";
+ boolean enabled = true;
+
+ this.parent.setFeature(feature, enabled);
+
+ EasyMock.replay(this.parent);
+
+ SAXException exception = null;
+ try
+ {
+ this.filter.setFeature(feature, enabled);
+ }
+ catch (SAXException e)
+ {
+ exception = e;
+ }
+
+ EasyMock.verify(this.parent);
+
+ Assert.assertNull(exception);
+
+ EasyMock.reset(this.parent);
+
+ this.setFeature(feature, enabled, new SAXNotRecognizedException());
+ this.setFeature(feature, enabled, new SAXNotSupportedException());
+ }
+
+ private void setFeature(String feature, boolean enabled, SAXException expected) throws SAXException
+ {
+ this.parent.setFeature(feature, enabled);
+ EasyMock.expectLastCall().andThrow(expected);
+
+ EasyMock.replay(this.parent);
+
+ SAXException exception = null;
+ try
+ {
+ this.filter.setFeature(feature, enabled);
+ }
+ catch (SAXException e)
+ {
+ exception = e;
+ }
+
+ EasyMock.verify(this.parent);
+
+ Assert.assertSame(expected, exception);
+
+ EasyMock.reset(this.parent);
+ }
+
+ @Test
+ public void endDocument() throws SAXException
+ {
+ this.contentHandler.endDocument();
+
+ EasyMock.replay(this.contentHandler);
+
+ this.filter.endDocument();
+
+ EasyMock.verify(this.contentHandler);
+ EasyMock.reset(this.contentHandler);
+ }
+
+ @Test
+ public void endElement() throws SAXException
+ {
+ this.contentHandler.endElement("uri", "localName", "qName");
+
+ EasyMock.replay(this.contentHandler);
+
+ this.filter.endElement("uri", "localName", "qName");
+
+ EasyMock.verify(this.contentHandler);
+ EasyMock.reset(this.contentHandler);
+ }
+
+ @Test
+ public void endPrefixMapping() throws SAXException
+ {
+ this.contentHandler.endPrefixMapping("prefix");
+
+ EasyMock.replay(this.contentHandler);
+
+ this.filter.endPrefixMapping("prefix");
+
+ EasyMock.verify(this.contentHandler);
+ EasyMock.reset(this.contentHandler);
+ }
+
+ @Test
+ public void error() throws SAXException
+ {
+ SAXParseException error = new SAXParseException("", "publicId", "systemId", 1, 2);
+
+ this.errorHandler.error(error);
+
+ EasyMock.replay(this.errorHandler);
+
+ this.filter.error(error);
+
+ EasyMock.verify(this.errorHandler);
+ EasyMock.reset(this.errorHandler);
+ }
+
+ @Test
+ public void fatalError() throws SAXException
+ {
+ SAXParseException error = new SAXParseException("", "publicId", "systemId", 1, 2);
+
+ this.errorHandler.fatalError(error);
+
+ EasyMock.replay(this.errorHandler);
+
+ this.filter.fatalError(error);
+
+ EasyMock.verify(this.errorHandler);
+ EasyMock.reset(this.errorHandler);
+ }
+
+ @Test
+ public void ignorableWhitespace() throws SAXException
+ {
+ String space = " ";
+
+ this.contentHandler.ignorableWhitespace(EasyMock.aryEq(space.toCharArray()), EasyMock.eq(0), EasyMock.eq(space.length()));
+
+ EasyMock.replay(this.contentHandler);
+
+ this.filter.ignorableWhitespace(space.toCharArray(), 0, space.length());
+
+ EasyMock.verify(this.contentHandler);
+ EasyMock.reset(this.contentHandler);
+ }
+
+ @Test
+ public void notationDecl() throws SAXException
+ {
+ this.dtdHandler.notationDecl("name", "publicId", "systemId");
+
+ EasyMock.replay(this.dtdHandler);
+
+ this.filter.notationDecl("name", "publicId", "systemId");
+
+ EasyMock.verify(this.dtdHandler);
+ EasyMock.reset(this.dtdHandler);
+ }
+
+ @Test
+ public void processingInstruction() throws SAXException
+ {
+ this.contentHandler.processingInstruction("target", "data");
+
+ EasyMock.replay(this.contentHandler);
+
+ this.filter.processingInstruction("target", "data");
+
+ EasyMock.verify(this.contentHandler);
+ EasyMock.reset(this.contentHandler);
+ }
+
+ @Test
+ public void resolveEntity() throws SAXException, IOException
+ {
+ InputSource expected = EasyMock.createMock(InputSource.class);
+
+ EasyMock.expect(this.resolver.resolveEntity("publicId", "systemId")).andReturn(expected);
+
+ EasyMock.replay(this.resolver);
+
+ InputSource result = this.filter.resolveEntity("publicId", "systemId");
+
+ EasyMock.verify(this.resolver);
+
+ Assert.assertSame(expected, result);
+
+ EasyMock.reset(this.resolver);
+ }
+
+ @Test
+ public void skippedEntity() throws SAXException
+ {
+ this.contentHandler.skippedEntity("name");
+
+ EasyMock.replay(this.contentHandler);
+
+ this.filter.skippedEntity("name");
+
+ EasyMock.verify(this.contentHandler);
+ EasyMock.reset(this.contentHandler);
+ }
+
+ @Test
+ public void startDocument() throws SAXException
+ {
+ this.contentHandler.startDocument();
+
+ EasyMock.replay(this.contentHandler);
+
+ this.filter.startDocument();
+
+ EasyMock.verify(this.contentHandler);
+ EasyMock.reset(this.contentHandler);
+ }
+
+ @Test
+ public void startPrefixMapping() throws SAXException
+ {
+ this.contentHandler.startPrefixMapping("prefix", "uri");
+
+ EasyMock.replay(this.contentHandler);
+
+ this.filter.startPrefixMapping("prefix", "uri");
+
+ EasyMock.verify(this.contentHandler);
+ EasyMock.reset(this.contentHandler);
+ }
+
+ @Test
+ public void unparsedEntityDecl() throws SAXException
+ {
+ this.dtdHandler.unparsedEntityDecl("name", "publicId", "systemId", "notationName");
+
+ EasyMock.replay(this.dtdHandler);
+
+ this.filter.unparsedEntityDecl("name", "publicId", "systemId", "notationName");
+
+ EasyMock.verify(this.dtdHandler);
+ EasyMock.reset(this.dtdHandler);
+ }
+
+ @Test
+ public void warning() throws SAXException
+ {
+ SAXParseException error = new SAXParseException("", "publicId", "systemId", 1, 2);
+
+ this.errorHandler.warning(error);
+
+ EasyMock.replay(this.errorHandler);
+
+ this.filter.warning(error);
+
+ EasyMock.verify(this.errorHandler);
+ EasyMock.reset(this.errorHandler);
+ }
+}
More information about the jboss-cvs-commits
mailing list