Search in sources :

Example 1 with BaseEntity

use of com.codename1.rad.models.BaseEntity in project CodeRAD by shannah.

the class PropertySelectorTest method manualExampleTest.

private void manualExampleTest() throws Exception {
    String xml = "<?xml version=\"1.0\"?>\n" + "<catalog>\n" + "   <book id=\"bk101\">\n" + "      <author>Gambardella, Matthew</author>\n" + "      <title>XML Developer's Guide</title>\n" + "      <genre>Computer</genre>\n" + "      <price>44.95</price>\n" + "      <publish_date>2000-10-01</publish_date>\n" + "      <description>An in-depth look at creating applications \n" + "      with XML.</description>\n" + "   </book>\n" + "   <book id=\"bk102\">\n" + "      <author>Ralls, Kim</author>\n" + "      <title>Midnight Rain</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2000-12-16</publish_date>\n" + "      <description>A former architect battles corporate zombies, \n" + "      an evil sorceress, and her own childhood to become queen \n" + "      of the world.</description>\n" + "   </book>\n" + "   <book id=\"bk103\">\n" + "      <author>Corets, Eva</author>\n" + "      <title>Maeve Ascendant</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2000-11-17</publish_date>\n" + "      <description>After the collapse of a nanotechnology \n" + "      society in England, the young survivors lay the \n" + "      foundation for a new society.</description>\n" + "   </book>\n" + "   <book id=\"bk104\">\n" + "      <author>Corets, Eva</author>\n" + "      <title>Oberon's Legacy</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2001-03-10</publish_date>\n" + "      <description>In post-apocalypse England, the mysterious \n" + "      agent known only as Oberon helps to create a new life \n" + "      for the inhabitants of London. Sequel to Maeve \n" + "      Ascendant.</description>\n" + "   </book>\n" + "   <book id=\"bk105\">\n" + "      <author>Corets, Eva</author>\n" + "      <title>The Sundered Grail</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2001-09-10</publish_date>\n" + "      <description>The two daughters of Maeve, half-sisters, \n" + "      battle one another for control of England. Sequel to \n" + "      Oberon's Legacy.</description>\n" + "   </book>\n" + "   <book id=\"bk106\">\n" + "      <author>Randall, Cynthia</author>\n" + "      <title>Lover Birds</title>\n" + "      <genre>Romance</genre>\n" + "      <price>4.95</price>\n" + "      <publish_date>2000-09-02</publish_date>\n" + "      <description>When Carla meets Paul at an ornithology \n" + "      conference, tempers fly as feathers get ruffled.</description>\n" + "   </book>\n" + "   <book id=\"bk107\">\n" + "      <author>Thurman, Paula</author>\n" + "      <title>Splish Splash</title>\n" + "      <genre>Romance</genre>\n" + "      <price>4.95</price>\n" + "      <publish_date>2000-11-02</publish_date>\n" + "      <description>A deep sea diver finds true love twenty \n" + "      thousand leagues beneath the sea.</description>\n" + "   </book>\n" + "   <book id=\"bk108\">\n" + "      <author>Knorr, Stefan</author>\n" + "      <title>Creepy Crawlies</title>\n" + "      <genre>Horror</genre>\n" + "      <price>4.95</price>\n" + "      <publish_date>2000-12-06</publish_date>\n" + "      <description>An anthology of horror stories about roaches,\n" + "      centipedes, scorpions  and other insects.</description>\n" + "   </book>\n" + "   <book id=\"bk109\">\n" + "      <author>Kress, Peter</author>\n" + "      <title>Paradox Lost</title>\n" + "      <genre>Science Fiction</genre>\n" + "      <price>6.95</price>\n" + "      <publish_date>2000-11-02</publish_date>\n" + "      <description>After an inadvertant trip through a Heisenberg\n" + "      Uncertainty Device, James Salway discovers the problems \n" + "      of being quantum.</description>\n" + "   </book>\n" + "   <book id=\"bk110\">\n" + "      <author>O'Brien, Tim</author>\n" + "      <title>Microsoft .NET: The Programming Bible</title>\n" + "      <genre>Computer</genre>\n" + "      <price>36.95</price>\n" + "      <publish_date>2000-12-09</publish_date>\n" + "      <description>Microsoft's .NET initiative is explored in \n" + "      detail in this deep programmer's reference.</description>\n" + "   </book>\n" + "   <book id=\"bk111\">\n" + "      <author>O'Brien, Tim</author>\n" + "      <title>MSXML3: A Comprehensive Guide</title>\n" + "      <genre>Computer</genre>\n" + "      <price>36.95</price>\n" + "      <publish_date>2000-12-01</publish_date>\n" + "      <description>The Microsoft MSXML3 parser is covered in \n" + "      detail, with attention to XML DOM interfaces, XSLT processing, \n" + "      SAX and more.</description>\n" + "   </book>\n" + "   <book id=\"bk112\">\n" + "      <author>Galos, Mike</author>\n" + "      <title>Visual Studio 7: A Comprehensive Guide</title>\n" + "      <genre>Computer</genre>\n" + "      <price>49.95</price>\n" + "      <publish_date>2001-04-16</publish_date>\n" + "      <description>Microsoft Visual Studio 7 is explored in depth,\n" + "      looking at how Visual Basic, Visual C++, C#, and ASP+ are \n" + "      integrated into a comprehensive development \n" + "      environment.</description>\n" + "   </book>\n" + "</catalog>";
    EntityType bookType = new EntityTypeBuilder().string(Thing.identifier).string(Thing.name).string(Thing.description).build();
    class Book extends BaseEntity {
    }
    EntityType.register(Book.class, bookType, cls -> {
        return new Book();
    });
    class Books extends EntityList<Book> {
    }
    EntityType.registerList(Books.class, Book.class, cls -> {
        return new Books();
    });
    Tag BOOKS = new Tag("Books");
    EntityType catalogType = new EntityTypeBuilder().list(Books.class, BOOKS).build();
    class Catalog extends BaseEntity {

        {
            setEntityType(catalogType);
        }
    }
    EntityType.register(Catalog.class, cls -> {
        return new Catalog();
    });
    ResultParser parser = new ResultParser(catalogType).property("./book", BOOKS).entityType(bookType).property("@id", Thing.identifier).property("title", Thing.name).property("description", Thing.description);
    Catalog catalog = (Catalog) parser.parseXML(xml, new Catalog());
    Books books = (Books) catalog.get(BOOKS);
    assertEqual("bk101", books.get(0).get(Thing.identifier));
    assertEqual("bk112", books.get(books.size() - 1).get(Thing.identifier));
    assertEqual("XML Developer's Guide", books.get(0).get(Thing.name));
    PropertySelector id1 = new PropertySelector(catalog, BOOKS).child(0, Thing.identifier);
    PropertySelector name1 = new PropertySelector(catalog, BOOKS).child(0, Thing.name);
    assertEqual("bk101", id1.getText(null));
    assertEqual("XML Developer's Guide", name1.getText(null));
    assertNull(propertySelector(catalog, BOOKS).child(Thing.name).getText(null));
    class Stats {

        int counter;
    }
    Stats stats = new Stats();
    name1.addPropertyChangeListener(evt -> {
        stats.counter++;
    });
    assertEqual(0, stats.counter);
    boolean res = name1.setText("Foo");
    assertTrue(res);
    assertEqual("Foo", name1.getText(null));
    assertEqual(1, stats.counter);
    Books newBooks = new Books();
    catalog.set(BOOKS, newBooks);
    assertEqual(2, stats.counter);
    assertNull(name1.getText(null));
    Book newBook = new Book();
    newBook.setText(Thing.name, "Bar");
    newBooks.add(newBook);
    assertEqual("Bar", name1.getText(null));
    assertEqual(3, stats.counter);
    newBook.setText(Thing.name, "Bar2");
    assertEqual(4, stats.counter);
    assertEqual("Bar2", name1.getText(null));
    name1.setText("Bar3");
    assertEqual("Bar3", newBook.getText(Thing.name));
    assertEqual("Bar3", name1.getText(null));
    assertEqual(5, stats.counter);
    books.get(0).set(Thing.name, "Changed again");
    assertEqual("Bar3", name1.getText(null));
    assertEqual("Changed again", books.get(0).getText(Thing.name));
    assertEqual(5, stats.counter);
    Book newBook2 = new Book();
    newBook2.setText(Thing.name, "newBook2");
    newBooks.add(newBook2);
    assertEqual(5, stats.counter);
    assertEqual("Bar3", name1.getText(null));
    newBooks.remove(newBook);
    assertEqual(6, stats.counter);
    assertEqual("newBook2", name1.getText(null));
}
Also used : ResultParser(com.codename1.rad.io.ResultParser)

Example 2 with BaseEntity

use of com.codename1.rad.models.BaseEntity in project CodeRAD by shannah.

the class EntityListTest method testTransactions.

private void testTransactions() throws Exception {
    EntityList el = new EntityList();
    Entity bob = new BaseEntity();
    Entity gary = new BaseEntity();
    Entity june = new BaseEntity();
    class Stats {

        private Object data;

        private int count;

        private void reset() {
            data = null;
            count = 0;
        }
    }
    // Warm it up.. make sure no NPEs or anything
    el.add(bob);
    el.add(gary);
    el.remove(bob);
    el.remove(gary);
    Stats stats = new Stats();
    ActionListener<EntityListEvent> l1 = evt -> {
        if (evt instanceof EntityAddedEvent || evt instanceof EntityRemovedEvent) {
            stats.count++;
            stats.data = evt;
        }
    };
    el.addActionListener(l1);
    el.add(bob);
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof EntityAddedEvent);
    assertEqual(1, el.size());
    EntityAddedEvent eae = (EntityAddedEvent) stats.data;
    assertEqual(bob, eae.getEntity());
    assertNull(eae.getTransaction(), "EntityAddedEvent not in transaction should return null for getTransaction()");
    stats.reset();
    el.startTransaction();
    assertEqual(0, stats.count);
    assertNull(stats.data);
    el.remove(bob);
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof EntityRemovedEvent);
    assertEqual(0, el.size());
    EntityRemovedEvent ere = (EntityRemovedEvent) stats.data;
    assertEqual(bob, ere.getEntity());
    assertNotNull(ere.getTransaction(), "EntityRemovedEvent inside transaction should return non-null for getTransaction()");
    Throwable t = null;
    try {
        el.startTransaction();
    } catch (IllegalStateException ex) {
        t = ex;
    }
    assertTrue(t instanceof IllegalStateException, "Starting a transaction while another one is active should raise an IllegalStateException");
    el.commitTransaction();
    el.removeActionListener(l1);
    l1 = evt -> {
        if (evt instanceof EntityAddedEvent || evt instanceof EntityRemovedEvent || evt instanceof TransactionEvent) {
            stats.count++;
            stats.data = evt;
        }
    };
    el.addActionListener(l1);
    stats.reset();
    el.startTransaction();
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof TransactionEvent);
    TransactionEvent te = (TransactionEvent) stats.data;
    assertTrue(te.isEmpty());
    assertTrue(!te.isComplete());
    stats.reset();
    el.add(bob);
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof EntityAddedEvent);
    eae = (EntityAddedEvent) stats.data;
    assertEqual(bob, eae.getEntity());
    assertEqual(eae.getTransaction(), te, "EntityAddedEvent.getTransaction() should return same TransactionEvent from startTransaction");
    stats.reset();
    el.add(gary);
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof EntityAddedEvent);
    eae = (EntityAddedEvent) stats.data;
    assertEqual(gary, eae.getEntity());
    stats.reset();
    el.commitTransaction();
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof TransactionEvent);
    te = (TransactionEvent) stats.data;
    assertEqual(2, te.size());
    assertTrue(te.isComplete());
    assertEqual(bob, te.get(0).getEntity());
    assertEqual(gary, te.get(1).getEntity());
    t = null;
    try {
        el.commitTransaction();
    } catch (IllegalStateException ex) {
        t = ex;
    }
    assertTrue(t instanceof IllegalStateException, "commitTransaction() on list with no current transaction should raise an IllegalStateException");
    el.startTransaction();
    stats.reset();
    el.clear();
    assertEqual(0, el.size(), "EntityList.add() and EntityList.remove() should immediately change the list even if transaction is in progress");
    assertEqual(2, stats.count, "EntityList.clear() should trigger remove events for each item in list");
    el.commitTransaction();
    assertEqual(0, el.size());
    assertEqual(3, stats.count);
    assertTrue(stats.data instanceof TransactionEvent, "commitTransaction() didn't fire a transaction event");
    te = (TransactionEvent) stats.data;
    assertEqual(2, te.size(), "TransactionEvent size() should reflect the number of add or remove operations in transaction.");
    assertEqual(bob, te.get(0).getEntity());
    assertEqual(gary, te.get(1).getEntity());
}
Also used : AbstractTest(com.codename1.testing.AbstractTest) Observer(java.util.Observer) TransactionEvent(com.codename1.rad.models.EntityList.TransactionEvent) EntityAddedEvent(com.codename1.rad.models.EntityList.EntityAddedEvent) EntityRemovedEvent(com.codename1.rad.models.EntityList.EntityRemovedEvent) EntityList(com.codename1.rad.models.EntityList) BaseEntity(com.codename1.rad.models.BaseEntity) EntityListEvent(com.codename1.rad.models.EntityList.EntityListEvent) ActionListener(com.codename1.ui.events.ActionListener) Entity(com.codename1.rad.models.Entity) BaseEntity(com.codename1.rad.models.BaseEntity) Entity(com.codename1.rad.models.Entity) EntityList(com.codename1.rad.models.EntityList) BaseEntity(com.codename1.rad.models.BaseEntity) EntityListEvent(com.codename1.rad.models.EntityList.EntityListEvent) TransactionEvent(com.codename1.rad.models.EntityList.TransactionEvent) EntityRemovedEvent(com.codename1.rad.models.EntityList.EntityRemovedEvent) EntityAddedEvent(com.codename1.rad.models.EntityList.EntityAddedEvent)

Example 3 with BaseEntity

use of com.codename1.rad.models.BaseEntity in project CodeRAD by shannah.

the class ResultParserTest method manualExampleTest.

private void manualExampleTest() throws Exception {
    String xml = "<?xml version=\"1.0\"?>\n" + "<catalog>\n" + "   <book id=\"bk101\">\n" + "      <author>Gambardella, Matthew</author>\n" + "      <title>XML Developer's Guide</title>\n" + "      <genre>Computer</genre>\n" + "      <price>44.95</price>\n" + "      <publish_date>2000-10-01</publish_date>\n" + "      <description>An in-depth look at creating applications \n" + "      with XML.</description>\n" + "   </book>\n" + "   <book id=\"bk102\">\n" + "      <author>Ralls, Kim</author>\n" + "      <title>Midnight Rain</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2000-12-16</publish_date>\n" + "      <description>A former architect battles corporate zombies, \n" + "      an evil sorceress, and her own childhood to become queen \n" + "      of the world.</description>\n" + "   </book>\n" + "   <book id=\"bk103\">\n" + "      <author>Corets, Eva</author>\n" + "      <title>Maeve Ascendant</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2000-11-17</publish_date>\n" + "      <description>After the collapse of a nanotechnology \n" + "      society in England, the young survivors lay the \n" + "      foundation for a new society.</description>\n" + "   </book>\n" + "   <book id=\"bk104\">\n" + "      <author>Corets, Eva</author>\n" + "      <title>Oberon's Legacy</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2001-03-10</publish_date>\n" + "      <description>In post-apocalypse England, the mysterious \n" + "      agent known only as Oberon helps to create a new life \n" + "      for the inhabitants of London. Sequel to Maeve \n" + "      Ascendant.</description>\n" + "   </book>\n" + "   <book id=\"bk105\">\n" + "      <author>Corets, Eva</author>\n" + "      <title>The Sundered Grail</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2001-09-10</publish_date>\n" + "      <description>The two daughters of Maeve, half-sisters, \n" + "      battle one another for control of England. Sequel to \n" + "      Oberon's Legacy.</description>\n" + "   </book>\n" + "   <book id=\"bk106\">\n" + "      <author>Randall, Cynthia</author>\n" + "      <title>Lover Birds</title>\n" + "      <genre>Romance</genre>\n" + "      <price>4.95</price>\n" + "      <publish_date>2000-09-02</publish_date>\n" + "      <description>When Carla meets Paul at an ornithology \n" + "      conference, tempers fly as feathers get ruffled.</description>\n" + "   </book>\n" + "   <book id=\"bk107\">\n" + "      <author>Thurman, Paula</author>\n" + "      <title>Splish Splash</title>\n" + "      <genre>Romance</genre>\n" + "      <price>4.95</price>\n" + "      <publish_date>2000-11-02</publish_date>\n" + "      <description>A deep sea diver finds true love twenty \n" + "      thousand leagues beneath the sea.</description>\n" + "   </book>\n" + "   <book id=\"bk108\">\n" + "      <author>Knorr, Stefan</author>\n" + "      <title>Creepy Crawlies</title>\n" + "      <genre>Horror</genre>\n" + "      <price>4.95</price>\n" + "      <publish_date>2000-12-06</publish_date>\n" + "      <description>An anthology of horror stories about roaches,\n" + "      centipedes, scorpions  and other insects.</description>\n" + "   </book>\n" + "   <book id=\"bk109\">\n" + "      <author>Kress, Peter</author>\n" + "      <title>Paradox Lost</title>\n" + "      <genre>Science Fiction</genre>\n" + "      <price>6.95</price>\n" + "      <publish_date>2000-11-02</publish_date>\n" + "      <description>After an inadvertant trip through a Heisenberg\n" + "      Uncertainty Device, James Salway discovers the problems \n" + "      of being quantum.</description>\n" + "   </book>\n" + "   <book id=\"bk110\">\n" + "      <author>O'Brien, Tim</author>\n" + "      <title>Microsoft .NET: The Programming Bible</title>\n" + "      <genre>Computer</genre>\n" + "      <price>36.95</price>\n" + "      <publish_date>2000-12-09</publish_date>\n" + "      <description>Microsoft's .NET initiative is explored in \n" + "      detail in this deep programmer's reference.</description>\n" + "   </book>\n" + "   <book id=\"bk111\">\n" + "      <author>O'Brien, Tim</author>\n" + "      <title>MSXML3: A Comprehensive Guide</title>\n" + "      <genre>Computer</genre>\n" + "      <price>36.95</price>\n" + "      <publish_date>2000-12-01</publish_date>\n" + "      <description>The Microsoft MSXML3 parser is covered in \n" + "      detail, with attention to XML DOM interfaces, XSLT processing, \n" + "      SAX and more.</description>\n" + "   </book>\n" + "   <book id=\"bk112\">\n" + "      <author>Galos, Mike</author>\n" + "      <title>Visual Studio 7: A Comprehensive Guide</title>\n" + "      <genre>Computer</genre>\n" + "      <price>49.95</price>\n" + "      <publish_date>2001-04-16</publish_date>\n" + "      <description>Microsoft Visual Studio 7 is explored in depth,\n" + "      looking at how Visual Basic, Visual C++, C#, and ASP+ are \n" + "      integrated into a comprehensive development \n" + "      environment.</description>\n" + "   </book>\n" + "</catalog>";
    EntityType bookType = new EntityTypeBuilder().string(Thing.identifier).string(Thing.name).string(Thing.description).build();
    class Book extends BaseEntity {
    }
    EntityType.register(Book.class, bookType, cls -> {
        return new Book();
    });
    class Books extends EntityList<Book> {
    }
    EntityType.registerList(Books.class, Book.class, cls -> {
        return new Books();
    });
    Tag BOOKS = new Tag("Books");
    EntityType catalogType = new EntityTypeBuilder().list(Books.class, BOOKS).build();
    class Catalog extends BaseEntity {

        {
            setEntityType(catalogType);
        }
    }
    EntityType.register(Catalog.class, cls -> {
        return new Catalog();
    });
    ResultParser parser = new ResultParser(catalogType).property("./book", BOOKS).entityType(bookType).property("@id", Thing.identifier).property("title", Thing.name).property("description", Thing.description);
    Catalog catalog = (Catalog) parser.parseXML(xml, new Catalog());
    Books books = (Books) catalog.get(BOOKS);
    assertEqual("bk101", books.get(0).get(Thing.identifier));
    assertEqual("bk112", books.get(books.size() - 1).get(Thing.identifier));
    assertEqual("XML Developer's Guide", books.get(0).get(Thing.name));
}
Also used : ResultParser(com.codename1.rad.io.ResultParser)

Example 4 with BaseEntity

use of com.codename1.rad.models.BaseEntity in project CodeRAD by shannah.

the class ResultParserTest method manualJSONTest.

private void manualJSONTest() throws Exception {
    String jsonData = "{\n" + "  \"colors\": [\n" + "    {\n" + "      \"color\": \"black\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [255,255,255,1],\n" + "        \"hex\": \"#000\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"white\",\n" + "      \"category\": \"value\",\n" + "      \"code\": {\n" + "        \"rgba\": [0,0,0,1],\n" + "        \"hex\": \"#FFF\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"red\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [255,0,0,1],\n" + "        \"hex\": \"#FF0\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"blue\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [0,0,255,1],\n" + "        \"hex\": \"#00F\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"yellow\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [255,255,0,1],\n" + "        \"hex\": \"#FF0\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"green\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"secondary\",\n" + "      \"code\": {\n" + "        \"rgba\": [0,255,0,1],\n" + "        \"hex\": \"#0F0\"\n" + "      }\n" + "    },\n" + "  ]\n" + "}";
    class Color extends BaseEntity {
    }
    Tag type = new Tag("type");
    Tag red = new Tag("red"), green = new Tag("green"), blue = new Tag("blue"), alpha = new Tag("alpha");
    EntityType colorType = entityTypeBuilder(Color.class).string(Thing.name).string(Product.category).string(type).Integer(red).Integer(green).Integer(blue).Integer(alpha).factory(cls -> {
        return new Color();
    }).build();
    class Colors extends EntityList<Color> {
    }
    EntityType.registerList(Colors.class, Color.class, cls -> {
        return new Colors();
    });
    Tag colors = new Tag("colors");
    class ColorSet extends BaseEntity {
    }
    EntityType colorsetType = entityTypeBuilder(ColorSet.class).list(Colors.class, colors).factory(cls -> {
        return new ColorSet();
    }).build();
    ResultParser parser = resultParser(ColorSet.class).property("colors", colors).entityType(Color.class).string("color", Thing.name).string("category", Product.category).string("type", type).Integer("code/rgba[0]", red).Integer("code/rgba[1]", green).Integer("code/rgba[2]", blue).Integer("code/rgba[3]", alpha);
    ColorSet colorSet = (ColorSet) parser.parseJSON(jsonData, new ColorSet());
    Colors theColors = (Colors) colorSet.get(colors);
    assertEqual(6, theColors.size());
    assertEqual(6, colorSet.getEntityList(colors).size());
    assertEqual("black", theColors.get(0).getText(Thing.name));
    assertEqual("hue", theColors.get(0).getText(Product.category));
    assertEqual("primary", theColors.get(0).getText(type));
    assertEqual(255, (int) theColors.get(0).getInt(red));
    assertEqual(255, (int) theColors.get(0).getInt(green));
    assertEqual(255, (int) theColors.get(0).getInt(blue));
    assertEqual(1, (int) theColors.get(0).getInt(alpha));
    assertEqual("green", theColors.get(5).getText(Thing.name));
    assertEqual(0, (int) theColors.get(5).getInt(red));
    assertEqual(255, (int) theColors.get(5).getInt(green));
    assertEqual(0, (int) theColors.get(5).getInt(blue));
    assertEqual(1, (int) theColors.get(5).getInt(alpha));
    class CodeParser implements PropertyParserCallback {

        private int index;

        CodeParser(int index) {
            this.index = index;
        }

        @Override
        public Object parse(Object codeMap) {
            if (!(codeMap instanceof Map)) {
                return 0;
            }
            Map m = (Map) codeMap;
            List rgba = (List) m.get("rgba");
            if (rgba == null) {
                return 0;
            }
            if (index < 0 || index >= rgba.size()) {
                return 0;
            }
            return rgba.get(index);
        }
    }
    parser = resultParser(ColorSet.class).property("colors", colors).entityType(Color.class).string("color", Thing.name).string("category", Product.category).string("type", type).property("code", red, new CodeParser(0)).property("code", green, new CodeParser(1)).property("code", blue, new CodeParser(2)).property("code", alpha, new CodeParser(3));
    colorSet = (ColorSet) parser.parseJSON(jsonData, new ColorSet());
    theColors = (Colors) colorSet.get(colors);
    assertEqual(6, theColors.size());
    assertEqual(6, colorSet.getEntityList(colors).size());
    assertEqual("black", theColors.get(0).getText(Thing.name));
    assertEqual("hue", theColors.get(0).getText(Product.category));
    assertEqual("primary", theColors.get(0).getText(type));
    assertEqual(255, (int) theColors.get(0).getInt(red));
    assertEqual(255, (int) theColors.get(0).getInt(green));
    assertEqual(255, (int) theColors.get(0).getInt(blue));
    assertEqual(1, (int) theColors.get(0).getInt(alpha));
    assertEqual("green", theColors.get(5).getText(Thing.name));
    assertEqual(0, (int) theColors.get(5).getInt(red));
    assertEqual(255, (int) theColors.get(5).getInt(green));
    assertEqual(0, (int) theColors.get(5).getInt(blue));
    assertEqual(1, (int) theColors.get(5).getInt(alpha));
}
Also used : AbstractTest(com.codename1.testing.AbstractTest) Person(com.codename1.rad.schemas.Person) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat) BaseEntity.entityTypeBuilder(com.codename1.rad.models.BaseEntity.entityTypeBuilder) Element(com.codename1.xml.Element) Log(com.codename1.io.Log) ParseException(com.codename1.l10n.ParseException) com.codename1.rad.models(com.codename1.rad.models) PropertyParserCallback(com.codename1.rad.io.ResultParser.PropertyParserCallback) XMLParser(com.codename1.xml.XMLParser) List(java.util.List) ResultParser.resultParser(com.codename1.rad.io.ResultParser.resultParser) StringReader(java.io.StringReader) ParsingService(com.codename1.rad.io.ParsingService) Product(com.codename1.rad.schemas.Product) Map(java.util.Map) Thing(com.codename1.rad.schemas.Thing) Result(com.codename1.rad.processing.Result) ResultParser(com.codename1.rad.io.ResultParser) Entity(com.codename1.rad.models.Entity) PropertyParserCallback(com.codename1.rad.io.ResultParser.PropertyParserCallback) List(java.util.List) ResultParser(com.codename1.rad.io.ResultParser) Map(java.util.Map)

Example 5 with BaseEntity

use of com.codename1.rad.models.BaseEntity in project CodeRAD by shannah.

the class ResultParserTest method parserServiceTest.

private void parserServiceTest() throws Exception {
    String jsonData = "{\n" + "  \"colors\": [\n" + "    {\n" + "      \"color\": \"black\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [255,255,255,1],\n" + "        \"hex\": \"#000\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"white\",\n" + "      \"category\": \"value\",\n" + "      \"code\": {\n" + "        \"rgba\": [0,0,0,1],\n" + "        \"hex\": \"#FFF\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"red\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [255,0,0,1],\n" + "        \"hex\": \"#FF0\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"blue\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [0,0,255,1],\n" + "        \"hex\": \"#00F\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"yellow\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [255,255,0,1],\n" + "        \"hex\": \"#FF0\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"green\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"secondary\",\n" + "      \"code\": {\n" + "        \"rgba\": [0,255,0,1],\n" + "        \"hex\": \"#0F0\"\n" + "      }\n" + "    },\n" + "  ]\n" + "}";
    class Color extends BaseEntity {
    }
    Tag type = new Tag("type");
    Tag red = new Tag("red"), green = new Tag("green"), blue = new Tag("blue"), alpha = new Tag("alpha");
    EntityType colorType = entityTypeBuilder(Color.class).string(Thing.name).string(Product.category).string(type).Integer(red).Integer(green).Integer(blue).Integer(alpha).factory(cls -> {
        return new Color();
    }).build();
    class Colors extends EntityList<Color> {
    }
    EntityType.registerList(Colors.class, Color.class, cls -> {
        return new Colors();
    });
    Tag colors = new Tag("colors");
    class ColorSet extends BaseEntity {
    }
    EntityType colorsetType = entityTypeBuilder(ColorSet.class).list(Colors.class, colors).factory(cls -> {
        return new ColorSet();
    }).build();
    ResultParser parser = resultParser(ColorSet.class).property("colors", colors).entityType(Color.class).string("color", Thing.name).string("category", Product.category).string("type", type).Integer("code/rgba[0]", red).Integer("code/rgba[1]", green).Integer("code/rgba[2]", blue).Integer("code/rgba[3]", alpha);
    ParsingService parserService = new ParsingService();
    Throwable[] errors = new Throwable[1];
    parserService.parseJSON(jsonData, parser, new ColorSet()).ready(colorSet -> {
        Colors theColors = (Colors) colorSet.get(colors);
        assertEqual(6, theColors.size());
        assertEqual(6, colorSet.getEntityList(colors).size());
        assertEqual("black", theColors.get(0).getText(Thing.name));
        assertEqual("hue", theColors.get(0).getText(Product.category));
        assertEqual("primary", theColors.get(0).getText(type));
        assertEqual(255, (int) theColors.get(0).getInt(red));
        assertEqual(255, (int) theColors.get(0).getInt(green));
        assertEqual(255, (int) theColors.get(0).getInt(blue));
        assertEqual(1, (int) theColors.get(0).getInt(alpha));
        assertEqual("green", theColors.get(5).getText(Thing.name));
        assertEqual(0, (int) theColors.get(5).getInt(red));
        assertEqual(255, (int) theColors.get(5).getInt(green));
        assertEqual(0, (int) theColors.get(5).getInt(blue));
        assertEqual(1, (int) theColors.get(5).getInt(alpha));
        System.out.println("Finished ready callback");
    }).except(err -> {
        errors[0] = err;
    }).await();
    System.out.println("Finished await");
    if (errors[0] != null) {
        if (errors[0] instanceof RuntimeException) {
            throw (RuntimeException) errors[0];
        } else {
            throw (Exception) errors[0];
        }
    }
    class CodeParser implements PropertyParserCallback {

        private int index;

        CodeParser(int index) {
            this.index = index;
        }

        @Override
        public Object parse(Object codeMap) {
            if (!(codeMap instanceof Map)) {
                return 0;
            }
            Map m = (Map) codeMap;
            List rgba = (List) m.get("rgba");
            if (rgba == null) {
                return 0;
            }
            if (index < 0 || index >= rgba.size()) {
                return 0;
            }
            return rgba.get(index);
        }
    }
    parser = resultParser(ColorSet.class).property("colors", colors).entityType(Color.class).string("color", Thing.name).string("category", Product.category).string("type", type).property("code", red, new CodeParser(0)).property("code", green, new CodeParser(1)).property("code", blue, new CodeParser(2)).property("code", alpha, new CodeParser(3));
    parserService.parseJSON(jsonData, parser, new ColorSet()).ready(colorSet -> {
        Colors theColors = (Colors) colorSet.get(colors);
        assertEqual(6, theColors.size());
        assertEqual(6, colorSet.getEntityList(colors).size());
        assertEqual("black", theColors.get(0).getText(Thing.name));
        assertEqual("hue", theColors.get(0).getText(Product.category));
        assertEqual("primary", theColors.get(0).getText(type));
        assertEqual(255, (int) theColors.get(0).getInt(red));
        assertEqual(255, (int) theColors.get(0).getInt(green));
        assertEqual(255, (int) theColors.get(0).getInt(blue));
        assertEqual(1, (int) theColors.get(0).getInt(alpha));
        assertEqual("green", theColors.get(5).getText(Thing.name));
        assertEqual(0, (int) theColors.get(5).getInt(red));
        assertEqual(255, (int) theColors.get(5).getInt(green));
        assertEqual(0, (int) theColors.get(5).getInt(blue));
        assertEqual(1, (int) theColors.get(5).getInt(alpha));
        System.out.println("Finished ready callback");
    }).except(err -> {
        errors[0] = err;
    }).await();
    System.out.println("Finished await");
    if (errors[0] != null) {
        if (errors[0] instanceof RuntimeException) {
            throw (RuntimeException) errors[0];
        } else {
            throw (Exception) errors[0];
        }
    }
    parserService.stop();
}
Also used : AbstractTest(com.codename1.testing.AbstractTest) Person(com.codename1.rad.schemas.Person) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat) BaseEntity.entityTypeBuilder(com.codename1.rad.models.BaseEntity.entityTypeBuilder) Element(com.codename1.xml.Element) Log(com.codename1.io.Log) ParseException(com.codename1.l10n.ParseException) com.codename1.rad.models(com.codename1.rad.models) PropertyParserCallback(com.codename1.rad.io.ResultParser.PropertyParserCallback) XMLParser(com.codename1.xml.XMLParser) List(java.util.List) ResultParser.resultParser(com.codename1.rad.io.ResultParser.resultParser) StringReader(java.io.StringReader) ParsingService(com.codename1.rad.io.ParsingService) Product(com.codename1.rad.schemas.Product) Map(java.util.Map) Thing(com.codename1.rad.schemas.Thing) Result(com.codename1.rad.processing.Result) ResultParser(com.codename1.rad.io.ResultParser) Entity(com.codename1.rad.models.Entity) PropertyParserCallback(com.codename1.rad.io.ResultParser.PropertyParserCallback) ParsingService(com.codename1.rad.io.ParsingService) ParseException(com.codename1.l10n.ParseException) List(java.util.List) ResultParser(com.codename1.rad.io.ResultParser) Map(java.util.Map)

Aggregations

Entity (com.codename1.rad.models.Entity)5 AbstractTest (com.codename1.testing.AbstractTest)5 ResultParser (com.codename1.rad.io.ResultParser)4 BaseEntity (com.codename1.rad.models.BaseEntity)3 BaseEntity.entityTypeBuilder (com.codename1.rad.models.BaseEntity.entityTypeBuilder)3 EntityList (com.codename1.rad.models.EntityList)3 Thing (com.codename1.rad.schemas.Thing)3 List (java.util.List)3 Log (com.codename1.io.Log)2 ParseException (com.codename1.l10n.ParseException)2 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)2 ParsingService (com.codename1.rad.io.ParsingService)2 PropertyParserCallback (com.codename1.rad.io.ResultParser.PropertyParserCallback)2 ResultParser.resultParser (com.codename1.rad.io.ResultParser.resultParser)2 com.codename1.rad.models (com.codename1.rad.models)2 EntityAddedEvent (com.codename1.rad.models.EntityList.EntityAddedEvent)2 EntityListEvent (com.codename1.rad.models.EntityList.EntityListEvent)2 EntityRemovedEvent (com.codename1.rad.models.EntityList.EntityRemovedEvent)2 TransactionEvent (com.codename1.rad.models.EntityList.TransactionEvent)2 Result (com.codename1.rad.processing.Result)2