Java tutorial
/* * Copyright (c) 2004 - 2006, Jonathan Ross <jonross@alum.mit.edu> * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ package org.cauldron.tests; import java.io.StringReader; import java.util.HashMap; import edu.emory.mathcs.backport.java.util.concurrent.Executors; import org.springframework.beans.factory.BeanFactory; import org.cauldron.Context; import org.cauldron.DataException; import org.cauldron.MultiTask; import org.cauldron.Task; import org.cauldron.TaskException; import org.cauldron.TaskFactory; import org.cauldron.tasks.PassThru; import org.cauldron.tasks.TextBlock; import org.cauldron.tests.util.HttpReflector; // TODO: comment me public class TestTasks extends MyTestCase { private final static String johnAndJane = "John is 35 years old\nJane is 32 years old\n"; public void testPassThru() throws Exception { Task task = new PassThru(); task.compile(); Object input = new Object(); Object output = TaskFactory.newContext(null).run(task, input); assertSame(input, output); } public void testHttp() throws Exception { HttpReflector hr = new HttpReflector(33445); hr.responseBody = "<people>" + "<person name='John' age='35'/>" + "<person name='Jane' age='32'/>" + "</people>"; BeanFactory fac = getContext("files/http-post-bind.xml"); Task task = (Task) fac.getBean("interaction"); Object output = TaskFactory.newContext(fac).run(task, null); assertEquals(johnAndJane, output); } public void testJdbcToText() throws Exception { BeanFactory fac = getContext("files/jdbc-to-text.xml"); Context context = TaskFactory.newContext(fac); Task task = (Task) fac.getBean("makedb"); context.run(task, null); task = (Task) fac.getBean("listdb"); Object output = context.run(task, null); assertEquals(johnAndJane, output); } public void testFanOut() throws Exception { MultiTask task = TaskFactory.newFanOut(Executors.newFixedThreadPool(10)); for (int i = 0; i < 100; i++) task.addChild(new TextBlock("" + i)); Object[] output = (Object[]) TaskFactory.newContext(null).run(task, null); for (int i = 0; i < 100; i++) assertEquals("" + i, output[i]); } public void testTextSubbing() throws Exception { Context context = TaskFactory.newContext(null); context.put("b", "B"); context.addSubContext("c", new HashMap()); context.put("c:d", "CD"); TextBlock task = new TextBlock("a=#{a} b=#{b} c:d=#{c:d}"); task.setSubstitute(true); String output = (String) context.run(task, null); assertEquals("a= b=B c:d=CD", output); task.setText("#{e:f}"); try { output = (String) context.run(task, null); fail("Expected exception"); } catch (DataException e) { assertTrue(-1 != e.getMessage().indexOf("No subcontext")); } } public void testTextBlock() throws Exception { Context context = TaskFactory.newContext(null); context.put("a", "b"); TextBlock task = new TextBlock("#{a}"); assertEquals("#{a}", context.run(task, null)); task.setSubstitute(true); assertEquals("b", context.run(task, null)); task.setText(null); assertEquals("c", context.run(task, "c")); assertEquals("d", context.run(task, new StringReader("d"))); try { context.run(task, new Object()); fail("Expected exception"); } catch (TaskException e) { assertTrue(-1 != e.getMessage().indexOf("not convertible")); } } }