<commit_before>package com.thoughtworks.xstream.io.json;

import junit.framework.TestCase;
import com.thoughtworks.xstream.XStream;

import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import java.awt.Color;
import java.io.Reader;
import java.io.InputStream;

/**
 * Some of these test cases are taken from example JSON listed at http://www.json.org/example.html
 *
 * @author <NAME>
 */
public class JsonHierarchicalStreamDriverTest extends TestCase {

    public void testReadingFromReaderNotSupported() {
        try {
            new JsonHierarchicalStreamDriver().createReader((Reader) null);
            fail("should have barfed");
        } catch (UnsupportedOperationException uoe) {
            // expected
        }
    }

    public void testReadingFromInputStreamNotSupported() {
        try {
            new JsonHierarchicalStreamDriver().createReader((InputStream) null);
            fail("should have barfed");
        } catch (UnsupportedOperationException uoe) {
            // expected
        }
    }

    public void testSimpleTypesCanBeRepresentedAsJson() {

        String expected = (
                "{'innerMessage': {\n" +
                        "  'greeting': 'hello',\n" +
                        "  'num1': 2,\n" +
                        "  'num2': 3,\n" +
                        "  'bool': true,\n" +
                        "  'bool2': true,\n" +
                        "  'innerMessage': {\n" +
                        "    'greeting': 'bonjour',\n" +
                        "    'num1': 3,\n" +
                        "    'bool': false\n" +
                        "  }\n" +
                        "}}").replace('\'', '"');

        XStream xs = new XStream(new JsonHierarchicalStreamDriver());

        xs.alias("innerMessage", Message.class);

        Message message = new Message("hello");
        message.num1 = 2;
        message.num2 = new Integer(3);
        message.bool = true;
        message.bool2 = Boolean.TRUE;

        Message message2 = new Message("bonjour");
        message2.num1 = 3;

        message.innerMessage = message2;

        assertEquals(expected, xs.toXML(message));
    }

    public static class Message {
        String greeting;
        int num1;
        Integer num2;
        boolean bool;
        Boolean bool2;
        Message innerMessage;

        public Message(String greeting) {
            this.greeting = greeting;
        }
    }

    String expected = (
            "{'menu': {\n" +
                    "  'id': 'file',\n" +
                    "  'value': 'File:',\n" +
                    "  'popup': {\n" +
                    "    'menuitem': [\n" +
                    "      {\n" +
                    "        'value': 'New',\n" +
                    "        'onclick': 'CreateNewDoc()'\n" +
                    "      },\n" +
                    "      {\n" +
                    "        'value': 'Open',\n" +
                    "        'onclick': 'OpenDoc()'\n" +
                    "      },\n" +
                    "      {\n" +
                    "        'value': 'Close',\n" +
                    "        'onclick': 'CloseDoc()'\n" +
                    "      }\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}}").replace('\'', '"');

    public void testListsRepresentedCorrectlyAsJson() {

        // This from http://www.json.org/example.html

        XStream xs = new XStream(new JsonHierarchicalStreamDriver());
        xs.alias("menu", MenuWithList.class);
        xs.alias("menuitem", MenuItem.class);

        MenuWithList menu = new MenuWithList();

        assertEquals(expected, xs.toXML(menu));
    }

    public void testArraysRepresentedCorrectlyAsJson() {


        XStream xs = new XStream(new JsonHierarchicalStreamDriver());
        xs.alias("menu", MenuWithArray.class);
        xs.alias("menuitem", MenuItem.class);

        MenuWithArray menu = new MenuWithArray();

        assertEquals(expected, xs.toXML(menu));
    }

    public void testSetsRepresentedCorrectlyAsJson() {

        // This from http://www.json.org/example.html

        XStream xs = new XStream(new JsonHierarchicalStreamDriver());
        xs.alias("menu", MenuWithSet.class);
        xs.alias("menuitem", MenuItem.class);

        MenuWithSet menu = new MenuWithSet();

        assertEquals(expected, xs.toXML(menu));
    }


    public static class MenuWithList {
        String id = "file";
        String value = "File:";
        PopupWithList popup = new PopupWithList();
    }

    public static class PopupWithList {
        List menuitem;
        {
            menuitem = new ArrayList();
            menuitem.add(new MenuItem("New", "CreateNewDoc()"));
            menuitem.add(new MenuItem("Open", "OpenDoc()"));
            menuitem.add(new MenuItem("Close", "CloseDoc()"));
        }
    }

    public static class MenuWithArray {
        String id = "file";
        String value = "File:";
        PopupWithArray popup = new PopupWithArray();
    }

    public static class PopupWithArray {<commit_msg>Better JSON test for sets, fixes invariant on JDK 6 (XSTR-370) and JDK 1.3.<commit_after>package com.thoughtworks.xstream.io.json;

import junit.framework.TestCase;
import com.thoughtworks.xstream.XStream;

import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import java.awt.Color;
import java.io.Reader;
import java.io.InputStream;

/**
 * Some of these test cases are taken from example JSON listed at http://www.json.org/example.html
 *
 * @author <NAME>
 */
public class JsonHierarchicalStreamDriverTest extends TestCase {

    public void testReadingFromReaderNotSupported() {
        try {
            new JsonHierarchicalStreamDriver().createReader((Reader) null);
            fail("should have barfed");
        } catch (UnsupportedOperationException uoe) {
            // expected
        }
    }

    public void testReadingFromInputStreamNotSupported() {
        try {
            new JsonHierarchicalStreamDriver().createReader((InputStream) null);
            fail("should have barfed");
        } catch (UnsupportedOperationException uoe) {
            // expected
        }
    }

    public void testSimpleTypesCanBeRepresentedAsJson() {

        String expected = (
                "{'innerMessage': {\n" +
                        "  'greeting': 'hello',\n" +
                        "  'num1': 2,\n" +
                        "  'num2': 3,\n" +
                        "  'bool': true,\n" +
                        "  'bool2': true,\n" +
                        "  'innerMessage': {\n" +
                        "    'greeting': 'bonjour',\n" +
                        "    'num1': 3,\n" +
                        "    'bool': false\n" +
                        "  }\n" +
                        "}}").replace('\'', '"');

        XStream xs = new XStream(new JsonHierarchicalStreamDriver());

        xs.alias("innerMessage", Message.class);

        Message message = new Message("hello");
        message.num1 = 2;
        message.num2 = new Integer(3);
        message.bool = true;
        message.bool2 = Boolean.TRUE;

        Message message2 = new Message("bonjour");
        message2.num1 = 3;

        message.innerMessage = message2;

        assertEquals(expected, xs.toXML(message));
    }

    public static class Message {
        String greeting;
        int num1;
        Integer num2;
        boolean bool;
        Boolean bool2;
        Message innerMessage;

        public Message(String greeting) {
            this.greeting = greeting;
        }
    }

    String expectedNew = "" + 
                "      {\n" +
                "        'value': 'New',\n" +
                "        'onclick': 'CreateNewDoc()'\n" +
                "      }";
    String expectedOpen = "" + 
                "      {\n" +
                "        'value': 'Open',\n" +
                "        'onclick': 'OpenDoc()'\n" +
                "      }";
    String expectedClose = "" + 
                "      {\n" +
                "        'value': 'Close',\n" +
                "        'onclick': 'CloseDoc()'\n" +
                "      }";
    String expected = (
            "{'menu': {\n" +
                    "  'id': 'file',\n" +
                    "  'value': 'File:',\n" +
                    "  'popup': {\n" +
                    "    'menuitem': [\n" +
                    expectedNew + ",\n" +
                    expectedOpen + ",\n" +
                    expectedClose + "\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}}").replace('\'', '"');

    public void testListsRepresentedCorrectlyAsJson() {

        // This from http://www.json.org/example.html

        XStream xs = new XStream(new JsonHierarchicalStreamDriver());
        xs.alias("menu", MenuWithList.class);
        xs.alias("menuitem", MenuItem.class);

        MenuWithList menu = new MenuWithList();

        assertEquals(expected, xs.toXML(menu));
    }

    public void testArraysRepresentedCorrectlyAsJson() {


        XStream xs = new XStream(new JsonHierarchicalStreamDriver());
        xs.alias("menu", MenuWithArray.class);
        xs.alias("menuitem", MenuItem.class);

        MenuWithArray menu = new MenuWithArray();

        assertEquals(expected, xs.toXML(menu));
    }

    public void testSetsRepresentedCorrectlyAsJson() {

        // This from http://www.json.org/example.html

        XStream xs = new XStream(new JsonHierarchicalStreamDriver());
        xs.alias("menu", MenuWithSet.class);
        xs.alias("menuitem", MenuItem.class);

        MenuWithSet menu = new MenuWithSet();

        String json = xs.toXML(menu);
        assertTrue(json.indexOf(expectedNew.replace('\'', '"')) > 0);
        assertTrue(json.indexOf(expectedOpen.replace('\'', '"')) > 0);
        assertTrue(json.indexOf(expectedClose.replace('\'', '"')) > 0);
    }


    public static class MenuWithList {
        String id = "file";
        String value = "File:";
        PopupWithList popup = new PopupWithList();
    }

    public static class PopupWithList {
        List menuitem;
        {
            menuitem = new ArrayList();
            menuitem.add(new MenuItem("New", "CreateNewDoc()"));
            menuitem.add(new MenuItem("Open", "OpenDoc()"));
            menuitem.add(new MenuItem("Close", "CloseDoc()"));
        }
    }

    public static class MenuWithArray {
        String id = "file";
        String value = "File:";
        PopupWithArray popup = new PopupWithArray();
    }

    public static class PopupWithArray {
{'fraction_non_alphanumeric': 0.13244411046402405, 'fraction_numerical': 0.004602667668607928, 'mean_word_length': 1.612760736196319, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3407421', 'n_tokens_mistral': 3093, 'n_tokens_neox': 2888, 'n_words': 607}