001package org.apache.commons.digester3.substitution;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertTrue;
024import static org.junit.Assert.fail;
025
026import org.apache.commons.digester3.Substitutor;
027import org.junit.Before;
028import org.junit.Test;
029import org.xml.sax.Attributes;
030import org.xml.sax.helpers.AttributesImpl;
031
032public final class CompoundSubstitutorTestCase
033{
034
035
036    private static class SubstitutorStub
037        extends Substitutor
038    {
039
040        private String newBodyText;
041
042        private String uri;
043
044        private String localName;
045
046        private String type;
047
048        private String value;
049
050        public SubstitutorStub( String bodyText, String uri, String localName, String type, String value )
051        {
052            this.newBodyText = bodyText;
053            this.uri = uri;
054            this.localName = localName;
055            this.type = type;
056            this.value = value;
057        }
058
059        /**
060         * @see org.apache.commons.digester3.Substitutor#substitute(org.xml.sax.Attributes)
061         */
062        @Override
063        public Attributes substitute( Attributes attributes )
064        {
065            AttributesImpl attribs = new AttributesImpl( attributes );
066            attribs.addAttribute( uri, localName, uri + ":" + localName, type, value );
067            return attribs;
068        }
069
070        /**
071         * @see org.apache.commons.digester3.Substitutor#substitute(java.lang.String)
072         */
073        @Override
074        public String substitute( String bodyText )
075        {
076            return newBodyText;
077        }
078
079    }
080
081    private Attributes attrib;
082
083    private String bodyText;
084
085    @Before
086    public void setUp()
087    {
088        AttributesImpl aImpl = new AttributesImpl();
089        aImpl.addAttribute( "", "b", ":b", "", "bcd" );
090        aImpl.addAttribute( "", "c", ":c", "", "cde" );
091        aImpl.addAttribute( "", "d", ":d", "", "def" );
092
093        attrib = aImpl;
094        bodyText = "Amazing Body Text!";
095    }
096
097    @Test
098    public void testConstructors()
099    {
100        try
101        {
102            new CompoundSubstitutor( null, null );
103            fail();
104        }
105        catch ( IllegalArgumentException e )
106        {
107            // OK
108        }
109
110        Substitutor a = new SubstitutorStub( "XYZ", "", "a", "", "abc" );
111
112        try
113        {
114            new CompoundSubstitutor( a, null );
115            fail();
116        }
117        catch ( IllegalArgumentException e )
118        {
119            // OK
120        }
121
122        try
123        {
124            new CompoundSubstitutor( null, a );
125            fail();
126        }
127        catch ( IllegalArgumentException e )
128        {
129            // OK
130        }
131    }
132
133    @Test
134    public void testChaining()
135    {
136        Substitutor a = new SubstitutorStub( "XYZ", "", "a", "", "abc" );
137        Substitutor b = new SubstitutorStub( "STU", "", "b", "", "bcd" );
138
139        Substitutor test = new CompoundSubstitutor( a, b );
140
141        AttributesImpl attribFixture = new AttributesImpl( attrib );
142        attribFixture.addAttribute( "", "a", ":a", "", "abc" );
143        attribFixture.addAttribute( "", "b", ":b", "", "bcd" );
144
145        assertTrue( areEqual( test.substitute( attrib ), attribFixture ) );
146        assertEquals( test.substitute( bodyText ), "STU" );
147    }
148
149    private boolean areEqual( Attributes a, Attributes b )
150    {
151        if ( a.getLength() != b.getLength() )
152        {
153            return false;
154        }
155
156        boolean success = true;
157        for ( int i = 0; i < a.getLength() && success; i++ )
158        {
159            success = a.getLocalName( i ).equals( b.getLocalName( i ) )
160                    && a.getQName( i ).equals( b.getQName( i ) )
161                    && a.getType( i ).equals( b.getType( i ) )
162                    && a.getURI( i ).equals( b.getURI( i ) )
163                    && a.getValue( i ).equals( b.getValue( i ) );
164        }
165
166        return success;
167    }
168
169}