Ejemplo n.º 1
0
        static void testAsList()
        {
            JSONParse parser = new JSONParse(SAMPLE3);

            System.assertEquals(false, parser.isObject());
            System.assertEquals(true, parser.isArray());
            List <JSONParse> items = parser.asList();

            System.assertEquals(5, items.size());
            System.assertEquals("*****@*****.**", items[0].get("init-param.configGlossary:adminEmail").getValue());
            System.assertEquals(true, items[1].get("init-param").isObject());
            System.assertEquals("cofaxEmail", items[1].get("servlet-name").getStringValue());
        }
Ejemplo n.º 2
0
        static void testGetNotAnArrayException()
        {
            JSONParse parser = new JSONParse(SAMPLE1);

            try
            {
                parser.get("menu.popup.[0]");
                System.assert(false, "Node is not an array, should have seen an exception about that.");
            }
            catch (JSONParse.NotAnArrayException e)
            {
                System.assert(e.getMessage().startsWith("The wrapped value is not a JSON array:"));
            }
        }
Ejemplo n.º 3
0
        static void testRootNotAnArrayException()
        {
            JSONParse parser = new JSONParse(SAMPLE2);

            try
            {
                parser.asList();
                System.assert(false, "Root node is not an array, should have seen an exception about that.");
            }
            catch (JSONParse.NotAnArrayException e)
            {
                System.assert(e.getMessage().startsWith("The wrapped value is not a JSON array:"));
            }
        }
Ejemplo n.º 4
0
        static void testRootNotAnObjectException()
        {
            JSONParse parser = new JSONParse(SAMPLE3);

            try
            {
                parser.asMap();
                System.assert(false, "Root node is not an object, should have seen an exception about that.");
            }
            catch (JSONParse.NotAnObjectException e)
            {
                System.assert(e.getMessage().startsWith("The wrapped value is not a JSON object:"));
            }
        }
Ejemplo n.º 5
0
        static void testMissingKeyException()
        {
            JSONParse parser = new JSONParse(SAMPLE2);

            try
            {
                parser.get("badKey");
                System.assert(false, "Our key was invalid, should have seen an exception about that.");
            }
            catch (JSONParse.MissingKeyException e)
            {
                System.assertEquals("No match found for <badKey>: {widget}", e.getMessage());
            }
        }
Ejemplo n.º 6
0
        static void testOutOfBoundsException()
        {
            JSONParse parser = new JSONParse(SAMPLE3);

            try
            {
                parser.get("[7]");
                System.assert(false, "We used an index outside the bounds of our array, should have seen an exception about that.");
            }
            catch (ListException e)
            {
                System.assertEquals("List index out of bounds: 7", e.getMessage());
            }
        }
Ejemplo n.º 7
0
        static void testAsMap()
        {
            JSONParse parser = new JSONParse(SAMPLE1);

            System.assertEquals(true, parser.isObject());
            System.assertEquals(false, parser.isArray());
            Map <string, JSONParse> theMap = parser.get("menu").asMap();

            System.assertEquals(3, theMap.size());
            System.assertEquals(false, theMap.get("id").isObject());
            System.assertEquals(false, theMap.get("id").isArray());
            System.assertEquals("file", theMap.get("id").getValue());
            System.assertEquals(true, theMap.get("popup").isObject());
            System.assertEquals(false, theMap.get("popup").isArray());
        }
Ejemplo n.º 8
0
        static void testGetTimeValue()
        {
            JSONParse parser = new JSONParse("\"2011-03-22T13:01:23\"");

            System.assertEquals(Time.newInstance(13, 1, 23, 0), parser.getTimeValue());
            parser = new JSONParse("1538783039073");
            System.assertEquals(Time.newInstance(23, 43, 59, 73), parser.getTimeValue());
            try
            {
                parser = new JSONParse("[1,2,3]");
                parser.getTimeValue();
                System.assert(false, "Node is not a valid Time, should have seen an exception about that.");
            }
            catch (JSONParse.InvalidConversionException e)
            {
                System.assertEquals("Only Long and String values can be converted to a Time: [ 1, 2, 3 ]", e.getMessage());
            }
        }
Ejemplo n.º 9
0
        static void testGetDateValue()
        {
            JSONParse parser = new JSONParse("\"2011-03-22\"");

            System.assertEquals(Date.newInstance(2011, 3, 22), parser.getDateValue());
            parser = new JSONParse("1538783039073");
            System.assertEquals(Date.newInstance(2018, 10, 05), parser.getDateValue());
            try
            {
                parser = new JSONParse("[1,2,3]");
                parser.getDateValue();
                System.assert(false, "Node is not a valid Date, should have seen an exception about that.");
            }
            catch (JSONParse.InvalidConversionException e)
            {
                System.assertEquals("Only Long and String values can be converted to a Date: [ 1, 2, 3 ]", e.getMessage());
            }
        }
Ejemplo n.º 10
0
        static void testGetBlobValue()
        {
            Blob      helloWorld = Blob.valueOf("HelloWorld");
            string    encoded    = EncodingUtil.base64Encode(helloWorld);
            JSONParse parser     = new JSONParse("\"" + encoded + "\"");

            System.assertEquals(encoded, parser.getStringValue());
            System.assertEquals(helloWorld, parser.getBlobValue());
            try
            {
                parser = new JSONParse("42");
                parser.getBlobValue();
                System.assert(false, "Node is not a valid Blob, should have seen an exception about that.");
            }
            catch (JSONParse.InvalidConversionException e)
            {
                System.assertEquals("Only String values can be converted to a Blob: 42", e.getMessage());
            }
        }
Ejemplo n.º 11
0
        static void testGetIdValue()
        {
            Account a = new Account {
                Name = "Acme"
            };

            Soql.insert(a);
            JSONParse parser = new JSONParse("\"" + a.Id.ToString() + "\"");

            System.assertEquals(a.Id, parser.getIdValue());
            try
            {
                parser = new JSONParse("[1,2,3]");
                parser.getIdValue();
                System.assert(false, "Node is not a valid Id, should have seen an exception about that.");
            }
            catch (JSONParse.InvalidConversionException e)
            {
                System.assertEquals("This value cannot be converted to an Id: [ 1, 2, 3 ]", e.getMessage());
            }
        }
Ejemplo n.º 12
0
        static void testGetLongValue()
        {
            JSONParse parser = new JSONParse("12");

            System.assertEquals(12, parser.getLongValue());
            parser = new JSONParse("\"12\"");
            System.assertEquals(12, parser.getLongValue());
            parser = new JSONParse("12.5");
            System.assertEquals(12, parser.getLongValue());
            parser = new JSONParse("1538783039073");
            System.assertEquals(1538783039073L, parser.getLongValue());
            try
            {
                parser = new JSONParse("[1,2,3]");
                parser.getLongValue();
                System.assert(false, "Node is not a valid Long, should have seen an exception about that.");
            }
            catch (JSONParse.InvalidConversionException e)
            {
                System.assertEquals("This value cannot be converted to a Long: [ 1, 2, 3 ]", e.getMessage());
            }
        }
Ejemplo n.º 13
0
        static void testGetIntegerValue()
        {
            JSONParse parser = new JSONParse("12");

            System.assertEquals(12, parser.getIntegerValue());
            parser = new JSONParse("\"12\"");
            System.assertEquals(12, parser.getIntegerValue());
            parser = new JSONParse("12.5");
            System.assertEquals(12, parser.getIntegerValue());
            parser = new JSONParse("1538783039073");
            System.assertEquals(1184747105, parser.getIntegerValue()); // integer gets truncated
            try
            {
                parser = new JSONParse("[1,2,3]");
                parser.getIntegerValue();
                System.assert(false, "Node is not a valid Integer, should have seen an exception about that.");
            }
            catch (JSONParse.InvalidConversionException e)
            {
                System.assertEquals("This value cannot be converted to an Integer: [ 1, 2, 3 ]", e.getMessage());
            }
        }