001    package org.apache.tapestry.json;
002    // Copyright Jul 8, 2006 The Apache Software Foundation
003    //
004    // Licensed under the Apache License, Version 2.0 (the "License");
005    // you may not use this file except in compliance with the License.
006    // You may obtain a copy of the License at
007    //
008    //     http://www.apache.org/licenses/LICENSE-2.0
009    //
010    // Unless required by applicable law or agreed to in writing, software
011    // distributed under the License is distributed on an "AS IS" BASIS,
012    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013    // See the License for the specific language governing permissions and
014    // limitations under the License.
015    
016    import java.text.DecimalFormatSymbols;
017    import java.util.Locale;
018    
019    import org.apache.tapestry.BaseComponentTestCase;
020    import org.testng.annotations.Test;
021    
022    /**
023     * Tests functionality of the core {@link JSONObject} .
024     * 
025     * @author jkuhnert
026     */
027    @Test
028    public class TestJsonProperties extends BaseComponentTestCase
029    {
030     
031        public void test_Literal_Arrays()
032        {
033            JSONObject json = new JSONObject();
034            
035            json.accumulate("key", new JSONLiteral("[value1]"));
036            json.accumulate("key", new JSONLiteral("[value2]"));
037            json.accumulate("key", new JSONLiteral("[value3]"));
038            
039            assertEquals("{\"key\":[[value1],[value2],[value3]]}",
040                    json.toString());
041            
042            JSONObject container = new JSONObject();
043            container.put("container", json);
044            
045            assertEquals("{\"container\":{\"key\":[[value1],[value2],[value3]]}}",
046                    container.toString());
047        }
048        
049        public void test_Literal_Array_Container()
050        {
051            JSONObject profile = new JSONObject();
052            profile.put("constraints", new JSONObject());
053            
054            JSONObject cons = profile.getJSONObject("constraints");
055            
056            if (!cons.has("key"))
057                cons.put("key", new JSONArray());
058            
059            Locale locale = Locale.ENGLISH;
060            DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
061            
062            cons.accumulate("key", 
063                    new JSONLiteral("[something,value1:{key:\"value\",key2:"
064                            + JSONObject.quote(symbols.getGroupingSeparator()) + "}]"));
065            cons.accumulate("key", new JSONLiteral("[value2]"));
066            cons.accumulate("key", new JSONLiteral("[value3]"));
067            
068            assertEquals("{\"constraints\":{\"key\":[[something,value1:{key:\"value\",key2:\",\"}],[value2],[value3]]}}",
069                    profile.toString());
070        }
071        
072        public void test_Multiple_Element_Accumulate()
073        {
074            JSONObject profile = new JSONObject();
075            
076            profile.accumulate("required", "val1");
077            profile.accumulate("required", "val2");
078            
079            assertEquals(profile.toString(), "{\"required\":[\"val1\",\"val2\"]}");
080        }
081        
082        public void test_Single_Element_Accumulate()
083        {
084            JSONObject profile = new JSONObject();
085            
086            profile.accumulate("required", "val1");
087            
088            assertEquals(profile.toString(), "{\"required\":[\"val1\"]}");
089        }
090    }