@Test public void testForeachLoopWithUndefinedCollection() { try { StatementBuilder.create() .loadVariable("list") .foreach("element", Integer.class) .finish().toJavaString(); fail("Expected OutOfScopeException"); } catch (OutOfScopeException e) { // expected } }
@Test public void testForeachLoopWithStringInParameterizedList() { String foreachWithListOfStrings = StatementBuilder.create() .declareVariable("list", new TypeLiteral<List<String>>() {}) .loadVariable("list") .foreach("element") .finish().toJavaString(); assertEquals("Failed to generate foreach loop using a List<String>", FOREACH_STRING_IN_LIST, foreachWithListOfStrings); }
@Test public void testForeachLoopWithInvalidCollectionType() { try { StatementBuilder.create() .declareVariable("list", String.class) .loadVariable("list") .foreach("element") .finish().toJavaString(); fail("Expected TypeNotIterableException"); } catch (TypeNotIterableException e) { // expected } }
@Test public void testForeachLoopWithProvidedLoopVarType() { Builder builder = StatementBuilder.create() .declareVariable("list", new TypeLiteral<List<String>>() {}) .loadVariable("list") .foreach("element", Object.class) .finish(); assertEquals("Failed to generate foreach loop with provided loop var type", FOREACH_OBJECT_IN_LIST, builder.toJavaString()); try { StatementBuilder.create() .declareVariable("list", new TypeLiteral<List<String>>() {}) .loadVariable("list") .foreach("element", Integer.class) .finish().toJavaString(); fail("Expected InvalidTypeException"); } catch (InvalidTypeException e) { // expected } }
@Test public void testForeachLoopWithStringInArray() { Statement createObject = StatementBuilder.create().newObject(String.class); String foreachWithStringArray = StatementBuilder.create(Context.create().addVariable("list", String[].class)) .loadVariable("list") .foreach("element") .append(createObject) .finish() .toJavaString(); assertEquals("Failed to generate foreach loop using a String[]", FOREACH_STRING_IN_ARRAY_ONE_STATEMENT, foreachWithStringArray); }
@Test public void testForeachLoopsNested() { Statement createObject = StatementBuilder.create().newObject(String.class); Builder outerLoop = StatementBuilder.create() .declareVariable("list", new TypeLiteral<List<String>>() {}) .loadVariable("list") .foreach("element") .append(StatementBuilder.create( ContextBuilder.create().addVariable(Variable.create("anotherList", new TypeLiteral<List<String>>() {})).getContext()) .loadVariable("anotherList") .foreach("anotherElement") .append(createObject) .finish() ).finish(); assertEquals("Failed to generate nested foreach loops", FOREACH_NESTED_STRING_IN_LIST, outerLoop.toJavaString()); }
@Test public void testForeachLoopWithStringInList() { Statement createObject = StatementBuilder.create().newObject(String.class); Statement createAnotherObject = StatementBuilder.create().newObject(Object.class); String foreachWithList = StatementBuilder.create() .declareVariable("list", List.class) .loadVariable("list") .foreach("element") .append(createObject) .append(createAnotherObject) .finish().toJavaString(); assertEquals("Failed to generate foreach loop using a List<?>", FOREACH_OBJECT_IN_LIST_TWO_STATEMENTS, foreachWithList); }
builder.append(StatementBuilder.create() .loadVariable("keys") .foreach("key", String.class) .append(Stmt.if_(Bool.or(loadVariable("key").invoke("equals", SerializationParts.ENCODED_TYPE), loadVariable("key").invoke("equals", SerializationParts.OBJECT_ID)))
builder.append(StatementBuilder.create() .loadVariable("keys") .foreach("key", String.class) .append(Stmt.if_(Bool.or(loadVariable("key").invoke("equals", SerializationParts.ENCODED_TYPE), loadVariable("key").invoke("equals", SerializationParts.OBJECT_ID)))