@KOTest public void loadError() throws InterruptedException { if (js == null) { js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.setFetched(null); js.fetchArray("http://127.0.0.1:54253/does/not/exist.txt"); } if (js.getFetchedResponse() == null) { throw new InterruptedException(); } assert "Exception".equals(js.getFetchedResponse()) : js.getFetchedResponse(); }
@KOTest public void loadAndParseArrayOfEnums() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "{'sex':['FEMALE', 'MALE', 'MALE']}", "application/json" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.fetchPeopleSex(url); } if (0 == js.getFetchedCount()) { throw new InterruptedException(); } assert js.getFetchedCount() == 1 : "Loaded"; assert js.getFetchedSex().size() == 3 : "Three values " + js.getFetchedSex(); assert js.getFetchedSex().get(0) == Sex.FEMALE : "Female first " + js.getFetchedSex(); assert js.getFetchedSex().get(1) == Sex.MALE : "male 2nd " + js.getFetchedSex(); assert js.getFetchedSex().get(2) == Sex.MALE : "male 3rd " + js.getFetchedSex(); }
@KOTest public void loadAndParseArrayOfIntegers() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "{'age':[1, 2, 3]}", "application/json" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.fetchPeopleAge(url); } if (0 == js.getFetchedCount()) { throw new InterruptedException(); } assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount(); }
@KOTest public void loadError() throws InterruptedException { if (js == null) { js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.setFetched(null); js.fetchArray("http://127.0.0.1:54253/does/not/exist.txt"); } if (js.getFetchedResponse() == null) { throw new InterruptedException(); } assertEquals("Exception", js.getFetchedResponse(), "Response " + js.getFetchedResponse()); }
@KOTest public void loadAndParseArrayOfIntegers() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "{'age':[1, 2, 3]}", "application/json" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.fetchPeopleAge(url); } if (0 == js.getFetchedCount()) { throw new InterruptedException(); } assertEquals(js.getFetchedCount(), 6, "1 + 2 + 3 is " + js.getFetchedCount()); }
@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 loadAndParseArrayOfEnums() throws InterruptedException { if (js == null) { url = Utils.prepareURL( JSONTest.class, "{'sex':['FEMALE', 'MALE', 'MALE']}", "application/json" ); js = Models.bind(new JSONik(), newContext()); js.applyBindings(); js.fetchPeopleSex(url); } if (0 == js.getFetchedCount()) { throw new InterruptedException(); } assertEquals(js.getFetchedCount(), 1, "Loaded"); assertEquals(js.getFetchedSex().size(), 3, "Three values " + js.getFetchedSex()); assertEquals(js.getFetchedSex().get(0), Sex.FEMALE, "Female first " + js.getFetchedSex()); assertEquals(js.getFetchedSex().get(1), Sex.MALE, "male 2nd " + js.getFetchedSex()); assertEquals(js.getFetchedSex().get(2), Sex.MALE, "male 3rd " + js.getFetchedSex()); }
@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 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 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 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 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(); }
@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 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 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 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 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()); }