@KOTest public void loadAndParseArrayInPeople() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}", "application/json" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.fetchPeople(url); } if (0 == js.getFetchedCount()) { throw new InterruptedException(); } assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount(); Person p = js.getFetched(); assert p != null : "We should get our person back: " + p; assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName(); assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex(); }
Person p = js.getFetched(); if (p == null) { throw new InterruptedException();
@KOTest public void loadAndParseJSONArray() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "[{'firstName': 'Gitar', 'sex': 'FEMALE'}," + "{'firstName': 'Peter', 'sex': 'MALE'}" + "]", "application/json" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.setFetched(null); js.fetchArray(url); } Person p = js.getFetched(); if (p == null) { throw new InterruptedException(); } assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount(); assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName(); assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex(); }
@KOTest public void asyncOperation() throws InterruptedException { if (js == null) { if (Utils.skipIfNoFullJDK()) { return; } js = Models.bind(new JSONik(), Utils.newContext(OperationsTest.class)); p = new Person("Sitar", "Gitar", Sex.MALE, null); js.applyBindings(); js.setFetched(null); new Thread(new Runnable() { @Override public void run() { js.assignFetched(p); } }).start(); } Person p = js.getFetched(); if (p == null) { throw new InterruptedException(); } assertEquals("Sitar", p.getFirstName(), "Expecting Sitar: " + p.getFirstName()); assertEquals(Sex.MALE, p.getSex(), "Expecting MALE: " + p.getSex()); } }
@KOTest public void loadAndParseArrayInPeopleWithHeaders() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "{'info':[{'firstName': '$0$1$2$3$4', 'sex': 'FEMALE'}]}", "application/json", "http.header.Easy", "http.header.H-a!r*d^e.r", "http.header.Repeat-ed", "http.header.Repeat*ed", "http.header.Same-URL" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.fetchPeopleWithHeaders(url, "easy", "harder", "rep"); } if (0 == js.getFetchedCount()) { throw new InterruptedException(); } assertEquals(js.getFetchedCount(), 1, "One person loaded: " + js.getFetchedCount()); Person p = js.getFetched(); assertNotNull(p, "We should get our person back: " + p); assertEquals("easyharderreprep" + url, p.getFirstName(), "Expecting header mess: " + p.getFirstName()); assertEquals(Sex.FEMALE, p.getSex(), "Expecting FEMALE: " + p.getSex()); }
@KOTest public void loadAndParseJSON() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "{'firstName': 'Sitar', 'sex': 'MALE'}", "application/json" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.setFetched(null); js.fetch(url); } Person p = js.getFetched(); if (p == null) { throw new InterruptedException(); } assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName(); assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex(); }
@KOTest public void loadAndParseArrayInPeople() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}", "application/json" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.fetchPeople(url); } if (0 == js.getFetchedCount()) { throw new InterruptedException(); } assertEquals(js.getFetchedCount(), 1, "One person loaded: " + js.getFetchedCount()); Person p = js.getFetched(); assertNotNull(p, "We should get our person back: " + p); assertEquals("Gitar", p.getFirstName(), "Expecting Gitar: " + p.getFirstName()); assertEquals(Sex.FEMALE, p.getSex(), "Expecting FEMALE: " + p.getSex()); }
@KOTest public void loadAndParseJSONArraySingle() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]", "application/json" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.setFetched(null); js.fetch(url); } Person p = js.getFetched(); if (p == null) { throw new InterruptedException(); } assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName(); assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex(); }
@KOTest public void loadAndParseJSONArray() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "[{'firstName': 'Gitar', 'sex': 'FEMALE'}," + "{'firstName': 'Peter', 'sex': 'MALE'}" + "]", "application/json" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.setFetched(null); js.fetchArray(url); } Person p = js.getFetched(); if (p == null) { throw new InterruptedException(); } assertEquals(js.getFetchedCount(), 2, "We got two values: " + js.getFetchedCount()); assertEquals("Gitar", p.getFirstName(), "Expecting Gitar: " + p.getFirstName()); assertEquals(Sex.FEMALE, p.getSex(), "Expecting FEMALE: " + p.getSex()); }
@KOTest public void syncOperation() { js = Models.bind(new JSONik(), Utils.newContext(OperationsTest.class)); p = new Person("Sitar", "Gitar", Sex.MALE, null); js.applyBindings(); js.setFetched(p); Person p = js.getFetched(); assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar immediately: " + p.getFirstName(); assert Sex.MALE.equals(p.getSex()) : "Expecting MALE immediately: " + p.getSex(); }
@KOTest public void loadAndParseJSONSentToArray() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "{'firstName': 'Sitar', 'sex': 'MALE'}", "application/json" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.setFetched(null); js.fetchArray(url); } Person p = js.getFetched(); if (p == null) { throw new InterruptedException(); } assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName(); assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex(); }
@KOTest public void loadAndParseJSONP() throws InterruptedException, Exception { if (js == null) { url = Utils.prepareURL( JSONTest.class, "$0({'firstName': 'Mitar', 'sex': 'MALE'})", "application/javascript", "callme" ); orig = scriptElements(); assert orig > 0 : "There should be some scripts on the page"; js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.setFetched(null); js.fetchViaJSONP(url); } Person p = js.getFetched(); if (p == null) { throw new InterruptedException(); } assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName(); assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex(); int now = scriptElements(); assert orig == now : "The set of elements is unchanged. Delta: " + (now - orig); }
@KOTest public void loadAndParseJSON() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "{'firstName': 'Sitar', 'sex': 'MALE'}", "application/json" ); js = Models.bind(new JSONik(), ctx = newContext()); js.applyBindings(); js.setFetched(null); js.fetch(url); } Person p = js.getFetched(); if (p == null) { throw new InterruptedException(); } assertEquals("Sitar", p.getFirstName(), "Expecting Sitar: " + p.getFirstName()); assertEquals(Sex.MALE, p.getSex(), "Expecting MALE: " + p.getSex()); assertEquals(ctx, onCallback, "Context is the same"); }
@KOTest public void loadAndParseJSONSentToArray() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "{'firstName': 'Sitar', 'sex': 'MALE'}", "application/json" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.setFetched(null); js.fetchArray(url); } Person p = js.getFetched(); if (p == null) { throw new InterruptedException(); } assertEquals("Sitar", p.getFirstName(), "Expecting Sitar: " + p.getFirstName()); assertEquals(Sex.MALE, p.getSex(), "Expecting MALE: " + p.getSex()); }
@KOTest public void loadAndParseJSONArraySingle() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]", "application/json" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.setFetched(null); js.fetch(url); } Person p = js.getFetched(); if (p == null) { throw new InterruptedException(); } assertEquals("Gitar", p.getFirstName(), "Expecting Gitar: " + p.getFirstName()); assertEquals(Sex.FEMALE, p.getSex(), "Expecting FEMALE: " + p.getSex()); }
@KOTest public void syncOperation() { js = Models.bind(new JSONik(), Utils.newContext(OperationsTest.class)); p = new Person("Sitar", "Gitar", Sex.MALE, null); js.applyBindings(); js.setFetched(p); Person p = js.getFetched(); assertEquals("Sitar", p.getFirstName(), "Expecting Sitar immediately: " + p.getFirstName()); assertEquals(Sex.MALE, p.getSex(), "Expecting MALE immediately: " + p.getSex()); }
@KOTest public void loadAndParseJSONP() throws InterruptedException, Exception { if (js == null) { url = Utils.prepareURL( JSONTest.class, "$0({'firstName': 'Mitar', 'sex': 'MALE'})", "application/javascript", "callme" ); orig = scriptElements(); assertTrue(orig > 0, "There should be some scripts on the page"); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.setFetched(null); js.fetchViaJSONP(url); } Person p = js.getFetched(); if (p == null) { throw new InterruptedException(); } assertEquals("Mitar", p.getFirstName(), "Unexpected: " + p.getFirstName()); assertEquals(Sex.MALE, p.getSex(), "Expecting MALE: " + p.getSex()); int now = scriptElements(); assertEquals(orig, now, "The set of elements is unchanged. Delta: " + (now - orig)); }