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 022/** 023 * @author Robert Scholte 024 */ 025import java.io.ByteArrayOutputStream; 026import java.io.Writer; 027 028import junit.framework.TestCase; 029 030public class RandomAccessSinkTest 031 extends TestCase 032{ 033 private SinkFactory factory = new AbstractXmlSinkFactory() 034 { 035 036 protected Sink createSink( Writer writer, String encoding, String languageId ) 037 { 038 return new TextSink( writer ); 039 } 040 041 protected Sink createSink( Writer writer, String encoding ) 042 { 043 return new TextSink( writer ); 044 } 045 }; 046 047 private void buildSimple( Sink sink, String text ) 048 throws Exception 049 { 050 sink.anchor( "foobar" ); 051 sink.text( text ); 052 sink.anchor_(); 053 } 054 055 public void testSimple() 056 throws Exception 057 { 058 String encoding = "UTF-8"; 059 String text = "Hello World"; 060 ByteArrayOutputStream outFlatSink = new ByteArrayOutputStream(); 061 Sink flatSink = factory.createSink( outFlatSink, encoding ); 062 buildSimple( flatSink, text ); 063 flatSink.flush(); 064 flatSink.close(); 065 066 ByteArrayOutputStream outRandomAccessSink = new ByteArrayOutputStream(); 067 RandomAccessSink randomAccessSink = new RandomAccessSink( factory, outRandomAccessSink, encoding ); 068 buildSimple( randomAccessSink, text ); 069 randomAccessSink.flush(); 070 randomAccessSink.close(); 071 072 assertEquals( outFlatSink.toString( encoding ), outRandomAccessSink.toString( encoding ) ); 073 } 074 075 public void testComplex() 076 throws Exception 077 { 078 String encoding = "UTF-8"; 079 String summaryText = "Summary text"; 080 String detailText = "Detail text"; 081 ByteArrayOutputStream outFlatSink = new ByteArrayOutputStream(); 082 Sink flatSink = factory.createSink( outFlatSink, encoding ); 083 buildSimple( flatSink, summaryText ); 084 flatSink.horizontalRule(); 085 buildSimple( flatSink, detailText ); 086 flatSink.flush(); 087 flatSink.close(); 088 089 ByteArrayOutputStream outRandomAccessSink = new ByteArrayOutputStream(); 090 RandomAccessSink randomAccessSink = new RandomAccessSink( factory, outRandomAccessSink, encoding ); 091 Sink summarySink = randomAccessSink.addSinkHook(); 092 randomAccessSink.horizontalRule(); 093 Sink detailSink = randomAccessSink.addSinkHook(); 094 095 // here's an example of the strength of randomAccessSink. Summary and detail are built in reverse order 096 buildSimple( detailSink, detailText ); 097 buildSimple( summarySink, summaryText ); 098 099 randomAccessSink.flush(); 100 randomAccessSink.close(); 101 102 assertEquals( outFlatSink.toString( encoding ), outRandomAccessSink.toString( encoding ) ); 103 } 104}