001package org.apache.maven.doxia.sink;
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 javax.swing.text.html.HTML.Tag;
023
024import org.apache.maven.doxia.markup.Markup;
025
026import junit.framework.TestCase;
027
028/**
029 *
030 * @author ltheussl
031 */
032public class AbstractXmlSinkTest
033        extends TestCase
034{
035    /**
036     * Test of set/getNameSpace method, of class AbstractXmlSink.
037     */
038    public void testNameSpace()
039    {
040        final Tag t = Tag.A;
041        final String ns = "ns";
042        final XmlTestSink instance = new XmlTestSink();
043
044        instance.writeStartTag( t );
045        instance.writeEndTag( t );
046        assertEquals( "<a></a>", instance.getText() );
047
048        instance.writeSimpleTag( t );
049        assertEquals( "<a />", instance.getText() );
050
051        instance.setNameSpace( ns );
052
053        instance.writeStartTag( t );
054        instance.writeEndTag( t );
055        assertEquals( "<ns:a></ns:a>", instance.getText() );
056
057        instance.writeSimpleTag( t );
058        assertEquals( "<ns:a />", instance.getText() );
059
060        assertEquals( ns, instance.getNameSpace() );
061
062        try
063        {
064            instance.writeStartTag( null );
065            fail( "null tag should fail!" );
066        }
067        catch ( IllegalArgumentException e )
068        {
069            assertNotNull( e );
070        }
071
072        try
073        {
074            instance.writeEndTag( null );
075            fail( "null tag should fail!" );
076        }
077        catch ( IllegalArgumentException e )
078        {
079            assertNotNull( e );
080        }
081    }
082
083    /**
084     * Test of writeStartTag method, of class AbstractXmlSink.
085     */
086    public void testWriteStartTag()
087    {
088        final Tag t = Tag.A;
089        final SinkEventAttributes att = new SinkEventAttributeSet( SinkEventAttributeSet.BOLD );
090        final XmlTestSink instance = new XmlTestSink();
091
092        instance.writeStartTag( t );
093        assertEquals( "<a>", instance.getText() );
094
095        instance.writeStartTag( t, att );
096        assertEquals( "<a style=\"bold\">", instance.getText() );
097
098        instance.writeStartTag( t, att, false );
099        assertEquals( "<a style=\"bold\">", instance.getText() );
100
101        instance.writeStartTag( t, att, true );
102        assertEquals( "<a style=\"bold\" />", instance.getText() );
103    }
104
105    /**
106     * Test of writeEOL method, of class AbstractXmlSink.
107     */
108    public void testWriteEOL()
109    {
110        final XmlTestSink instance = new XmlTestSink();
111
112        instance.writeEOL();
113        assertEquals( Markup.EOL, instance.getText() );
114    }
115
116    /**
117     * Test of writeSimpleTag method, of class AbstractXmlSink.
118     */
119    public void testWriteSimpleTag()
120    {
121        final Tag t = Tag.A;
122        final SinkEventAttributes att = new SinkEventAttributeSet( SinkEventAttributeSet.BOLD );
123        final XmlTestSink instance = new XmlTestSink();
124
125        instance.writeSimpleTag( t );
126        assertEquals( "<a />", instance.getText() );
127
128        instance.writeSimpleTag( t, att );
129        assertEquals( "<a style=\"bold\" />", instance.getText() );
130    }
131
132    /** Test sink. */
133    private class XmlTestSink
134            extends AbstractXmlSink
135    {
136        private final StringBuilder buffer = new StringBuilder( 0 );
137
138        public void reset()
139        {
140            buffer.setLength( 0 );
141        }
142
143        public String getText()
144        {
145            String text = buffer.toString();
146            reset();
147
148            return text;
149        }
150
151        protected void write( String text )
152        {
153            buffer.append( text );
154        }
155    }
156}