001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.util.io;
016    
017    import java.io.BufferedInputStream;
018    import java.io.BufferedReader;
019    import java.io.CharArrayWriter;
020    import java.io.InputStream;
021    import java.io.InputStreamReader;
022    import java.io.LineNumberReader;
023    import java.io.ObjectOutputStream;
024    import java.io.Reader;
025    import java.util.ArrayList;
026    import java.util.List;
027    
028    import org.apache.tapestry.BaseComponentTestCase;
029    import org.testng.annotations.Test;
030    
031    /**
032     * Tests for {@link org.apache.tapestry.util.io.BinaryDumpOutputStream}.
033     * 
034     * @author Howard Lewis Ship
035     * @since 4.0
036     */
037    @Test
038    public class TestBinaryDumpOutputStream extends BaseComponentTestCase
039    {
040        /**
041         * Reads the content of a file, and forms a string. Converts line-number endings in the file
042         * into the correct platform value (this should help the test run properly on both Windows and
043         * *nix).
044         */
045        private String contentsOf(String path) throws Exception
046        {
047            String sep = System.getProperty("line.separator");
048            
049            InputStream is = getClass().getResourceAsStream(path);
050    
051            is = new BufferedInputStream(is);
052    
053            Reader ir = new InputStreamReader(is);
054    
055            ir = new BufferedReader(ir);
056    
057            LineNumberReader lnr = new LineNumberReader(ir);
058    
059            StringBuffer buffer = new StringBuffer();
060    
061            while (true)
062            {
063                String line = lnr.readLine();
064    
065                if (line == null)
066                    break;
067    
068                buffer.append(line);
069                buffer.append(sep);
070            }
071    
072            ir.close();
073    
074            return buffer.toString();
075        }
076    
077        public void testBasic() throws Exception
078        {
079            CharArrayWriter writer = new CharArrayWriter();
080    
081            BinaryDumpOutputStream bdos = new BinaryDumpOutputStream(writer);
082            
083            ObjectOutputStream oos = new ObjectOutputStream(bdos);
084            oos.writeObject(createOutputObject());
085            
086            oos.close();
087            
088            assertEquals(contentsOf("Basic.txt"), writer.toString());
089        }
090    
091        /**
092         * Creates and returns the object to be written out to the stream. The tests are dependenent on
093         * the serialization of HashMap and String not changing between JDKs. If such a change does
094         * occur, we'll need to devise an Externalizable object to write to the stream.
095         * 
096         * I've changed this to use a List instead, as ordering of a list shouldn't change while
097         * ordering of a hashmap isn't guaranteed at all.
098         */
099        private List createOutputObject()
100        {
101            List list = new ArrayList();
102            list.add("alpha");
103            list.add("beta");
104            list.add("gabba gabba");
105            return list;
106        }
107    
108        public void testOptions() throws Exception
109        {
110    
111            CharArrayWriter writer = new CharArrayWriter();
112    
113            BinaryDumpOutputStream bdos = new BinaryDumpOutputStream(writer);
114    
115            bdos.setAsciiBegin(" { ");
116            bdos.setAsciiEnd(" }");
117            bdos.setOffsetSeperator(" = ");
118            bdos.setSubstituteChar('?');
119            bdos.setBytesPerLine(48);
120            bdos.setSpacingInterval(8);
121    
122            ObjectOutputStream oos = new ObjectOutputStream(bdos);
123    
124            oos.writeObject(createOutputObject());
125    
126            oos.close();
127    
128            assertEquals(contentsOf("Options.txt"), writer.toString());
129        }
130    
131        public void testNoOffset() throws Exception
132        {
133            CharArrayWriter writer = new CharArrayWriter();
134    
135            BinaryDumpOutputStream bdos = new BinaryDumpOutputStream(writer);
136            bdos.setShowOffset(false);
137    
138            ObjectOutputStream oos = new ObjectOutputStream(bdos);
139    
140            oos.writeObject(createOutputObject());
141    
142            oos.close();
143    
144            assertEquals(contentsOf("NoOffset.txt"), writer.toString());
145        }
146    
147        public void testNoAscii() throws Exception
148        {
149            CharArrayWriter writer = new CharArrayWriter();
150    
151            BinaryDumpOutputStream bdos = new BinaryDumpOutputStream(writer);
152            bdos.setShowAscii(false);
153    
154            ObjectOutputStream oos = new ObjectOutputStream(bdos);
155    
156            oos.writeObject(createOutputObject());
157    
158            oos.close();
159    
160            assertEquals(contentsOf("NoAscii.txt"), writer.toString());
161        }
162    }