1 15 package org.apache.tapestry.components; 16 17 import java.text.DateFormat ; 18 import java.text.Format ; 19 import java.util.Date ; 20 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.hivemind.Location; 23 import org.apache.tapestry.IBinding; 24 import org.apache.tapestry.IMarkupWriter; 25 import org.apache.tapestry.IPage; 26 import org.apache.tapestry.IRequestCycle; 27 import org.apache.tapestry.html.BasePage; 28 import org.apache.tapestry.spec.IComponentSpecification; 29 import org.easymock.MockControl; 30 31 37 public class TestInsert extends BaseComponentTestCase 38 { 39 42 43 private IPage newBasePage(String name) 44 { 45 BasePage page = (BasePage) newInstance(BasePage.class); 46 47 page.setPageName(name); 48 49 return page; 50 } 51 52 public void testRewinding() 53 { 54 IRequestCycle cycle = newCycle(true); 55 56 replayControls(); 57 58 Insert insert = (Insert) newInstance(Insert.class); 59 60 insert.render(null, cycle); 61 62 verifyControls(); 63 } 64 65 public void testNullValue() 66 { 67 IRequestCycle cycle = newCycle(false); 68 69 replayControls(); 70 71 Insert insert = (Insert) newInstance(Insert.class, new Object [] 72 { "value", null }); 73 74 insert.render(null, cycle); 75 76 verifyControls(); 77 } 78 79 public void testNoFormat() 80 { 81 IRequestCycle cycle = newCycle(false); 82 IMarkupWriter writer = newWriter(); 83 84 writer.print("42", false); 85 86 replayControls(); 87 88 Insert insert = (Insert) newInstance(Insert.class, new Object [] 89 { "value", new Integer (42) }); 90 91 insert.render(writer, cycle); 92 93 verifyControls(); 94 } 95 96 public void testFormat() 97 { 98 IRequestCycle cycle = newCycle(false); 99 IMarkupWriter writer = newWriter(); 100 101 Date date = new Date (); 102 DateFormat format = DateFormat.getDateInstance(); 103 String expected = format.format(date); 104 105 writer.print(expected, false); 106 107 replayControls(); 108 109 Insert insert = (Insert) newInstance(Insert.class, new Object [] 110 { "value", date, "format", format }); 111 112 insert.render(writer, cycle); 113 114 verifyControls(); 115 } 116 117 public void testUnableToFormat() 118 { 119 Object value = "xyzzyx"; 120 Location l = fabricateLocation(87); 121 IBinding binding = newBinding(l); 122 123 Format format = DateFormat.getInstance(); 124 125 IRequestCycle cycle = newCycle(false); 126 IMarkupWriter writer = newWriter(); 127 128 IPage page = newBasePage("Flintstone"); 129 130 replayControls(); 131 132 Insert insert = (Insert) newInstance(Insert.class, new Object [] 133 { "value", value, "format", format }); 134 insert.setBinding("format", binding); 135 insert.setId("fred"); 136 insert.setPage(page); 137 insert.setContainer(page); 138 139 try 140 { 141 insert.render(writer, cycle); 142 unreachable(); 143 } 144 catch (ApplicationRuntimeException ex) 145 { 146 assertEquals( 147 "Flintstone/fred unable to format value 'xyzzyx': Cannot format given Object as a Date", 148 ex.getMessage()); 149 assertSame(insert, ex.getComponent()); 150 assertSame(l, ex.getLocation()); 151 } 152 153 verifyControls(); 154 } 155 156 public void testRaw() 157 { 158 IRequestCycle cycle = newCycle(false); 159 IMarkupWriter writer = newWriter(); 160 161 writer.print("42", true); 162 163 replayControls(); 164 165 Insert insert = (Insert) newInstance(Insert.class, new Object [] 166 { "value", new Integer (42), "raw", Boolean.TRUE }); 167 168 insert.render(writer, cycle); 169 170 verifyControls(); 171 } 172 173 public void testStyleClass() 174 { 175 IBinding informal = newBinding("informal-value"); 176 177 IRequestCycle cycle = newCycle(false); 178 IMarkupWriter writer = newWriter(); 179 IComponentSpecification spec = newSpec("informal", null); 180 181 writer.begin("span"); 182 writer.attribute("class", "paisley"); 183 writer.attribute("informal", "informal-value"); 184 writer.print("42", false); 185 writer.end(); 186 187 replayControls(); 188 189 Insert insert = (Insert) newInstance(Insert.class, new Object [] 190 { "value", "42", "specification", spec, "styleClass", "paisley" }); 191 192 insert.setBinding("informal", informal); 193 194 insert.render(writer, cycle); 195 196 verifyControls(); 197 } 198 } | Popular Tags |