001    // Copyright 2006 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    package org.apache.tapestry.markup;
015    
016    import java.io.ByteArrayOutputStream;
017    import java.io.PrintWriter;
018    
019    import org.apache.tapestry.BaseComponentTestCase;
020    import org.apache.tapestry.json.IJSONWriter;
021    import org.apache.tapestry.json.JSONObject;
022    import org.testng.annotations.Test;
023    
024    
025    /**
026     * Tests functionality of the {@link IJSONWriter} class.
027     * 
028     * @author jkuhnert
029     */
030    @Test
031    public class TestJSONWriter extends BaseComponentTestCase
032    {
033        
034        /**
035         * Tests creating a list of key/value pairs.
036         */
037        public void testPropertyList() 
038        {
039            IJSONWriter writer = newJSONWriter();
040            
041            JSONObject json = writer.object();
042            
043            json.put("red", "ball");
044            json.put("black", "cat");
045            json.put("orange", "orange");
046            
047            assertEquals(json.get("red"), "ball");
048            assertEquals(json.get("black"), "cat");
049            assertEquals(json.get("orange"), "orange");
050        }
051        
052        /* All writer content is written to this buffer */
053        protected ByteArrayOutputStream outputBuffer;
054        
055        /**
056         * Creates a writer instance that content can be asserted
057         * against.
058         */
059        protected JSONWriterImpl newJSONWriter()
060        {
061            outputBuffer = new ByteArrayOutputStream();
062            PrintWriter pw = 
063                new PrintWriter(outputBuffer);
064            
065            JSONWriterImpl writer = new JSONWriterImpl(pw);
066            
067            return writer;
068        }
069    }