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.contrib.components;
016    
017    import java.io.BufferedInputStream;
018    import java.io.BufferedReader;
019    import java.io.InputStream;
020    import java.io.InputStreamReader;
021    import java.io.LineNumberReader;
022    import java.io.Reader;
023    
024    import org.apache.tapestry.BaseComponentTestCase;
025    import org.apache.tapestry.IMarkupWriter;
026    import org.apache.tapestry.IRequestCycle;
027    import org.apache.tapestry.test.Creator;
028    import org.testng.annotations.Test;
029    
030    /**
031     * Tests for {@link org.apache.tapestry.contrib.components.DumpObject}
032     * 
033     * @author Howard Lewis Ship
034     * @since 4.0
035     */
036    @Test
037    public class TestDumpObject extends BaseComponentTestCase
038    {
039        /**
040         * Reads the content of a file, and forms a string. Converts line-number endings in the file
041         * into the correct platform value (this should help the test run properly on both Windows and
042         * *nix).
043         */
044        private String contentsOf(String path) throws Exception
045        {
046            String sep = System.getProperty("line.separator");
047    
048            InputStream is = getClass().getResourceAsStream(path);
049    
050            is = new BufferedInputStream(is);
051    
052            Reader ir = new InputStreamReader(is);
053    
054            ir = new BufferedReader(ir);
055    
056            LineNumberReader lnr = new LineNumberReader(ir);
057    
058            StringBuffer buffer = new StringBuffer();
059    
060            while (true)
061            {
062                String line = lnr.readLine();
063    
064                if (line == null)
065                    break;
066    
067                buffer.append(line);
068                buffer.append(sep);
069            }
070    
071            ir.close();
072    
073            return buffer.toString();
074        }
075    
076        public void testNotSerializable()
077        {
078            Creator creator = new Creator();
079            Object object = new Object();
080    
081            DumpObject dumpObject = (DumpObject) creator.newInstance(DumpObject.class);
082    
083            assertEquals("java.io.NotSerializableException: java.lang.Object", dumpObject
084                    .convert(object));
085        }
086    
087        public void testRewinding()
088        {
089            IMarkupWriter writer = newWriter();
090            IRequestCycle cycle = newCycle(true);
091    
092            Creator creator = new Creator();
093            DumpObject dumpObject = (DumpObject) creator.newInstance(DumpObject.class);
094    
095            replay();
096    
097            dumpObject.renderComponent(writer, cycle);
098    
099            verify();
100        }
101    
102        public void testNormal() throws Exception
103        {
104            IMarkupWriter writer = newWriter();
105            IRequestCycle cycle = newCycle(false);
106    
107            Creator creator = new Creator();
108            DumpObject dumpObject = (DumpObject) creator.newInstance(DumpObject.class, new Object[]
109            { "object", "a serialized string" });
110    
111            String expected = contentsOf("Normal.txt");
112    
113            writer.print(expected);
114    
115            replay();
116    
117            dumpObject.renderComponent(writer, cycle);
118    
119            verify();
120        }
121    }