[seam-commits] Seam SVN: r9036 - branches/community/Seam_2_0/src/test/unit/org/jboss/seam/test/unit.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Sat Sep 20 03:08:13 EDT 2008
Author: dan.j.allen
Date: 2008-09-20 03:08:12 -0400 (Sat, 20 Sep 2008)
New Revision: 9036
Added:
branches/community/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/MockContainerTest.java
Modified:
branches/community/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/InterpolatorTest.java
Log:
create more tests for JBSEAM-2566
Modified: branches/community/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/InterpolatorTest.java
===================================================================
--- branches/community/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/InterpolatorTest.java 2008-09-20 06:59:56 UTC (rev 9035)
+++ branches/community/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/InterpolatorTest.java 2008-09-20 07:08:12 UTC (rev 9036)
@@ -3,46 +3,62 @@
import java.text.DateFormat;
import java.util.Date;
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.core.Expressions;
import org.jboss.seam.core.Interpolator;
+import org.jboss.seam.core.Locale;
import org.testng.Assert;
import org.testng.annotations.Test;
-
-public class InterpolatorTest
+public class InterpolatorTest extends MockContainerTest
{
+ private static final String CHOICE_EXPR = "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.";
- static final String CHOICE_EXPR = "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.";
+ @Override
+ protected Class[] getComponentsToInstall()
+ {
+ return new Class[] { Interpolator.class, Locale.class, Expressions.class };
+ }
+
@Test
public void testInterpolation()
{
Interpolator interpolator = Interpolator.instance();
- Assert.assertEquals("3 5 7", interpolator.interpolate("#0 #1 #2", 3, 5, 7));
- Assert.assertEquals("3 5 7", interpolator.interpolate("{0} {1} {2}", 3, 5, 7));
+ Assert.assertEquals(interpolator.interpolate("#0 #1 #2", 3, 5, 7), "3 5 7");
+ Assert.assertEquals(interpolator.interpolate("{0} {1} {2}", 3, 5, 7), "3 5 7");
// this tests that the result of an expression evaluation is not evaluated again
- Assert.assertEquals("{0}", interpolator.interpolate("{1}", "bad", "{0}"));
+ Assert.assertEquals(interpolator.interpolate("{1}", "bad", "{0}"), "{0}");
// this tests that embedded {} expressions are parsed correctly.
- Assert.assertEquals("There are no files.", interpolator.interpolate(CHOICE_EXPR, 0));
- Assert.assertEquals("There is one file.", interpolator.interpolate(CHOICE_EXPR, 1));
- Assert.assertEquals("There are 2 files.", interpolator.interpolate(CHOICE_EXPR, 2));
+ Assert.assertEquals(interpolator.interpolate(CHOICE_EXPR, 0), "There are no files.");
+ Assert.assertEquals(interpolator.interpolate(CHOICE_EXPR, 1), "There is one file.");
+ Assert.assertEquals(interpolator.interpolate(CHOICE_EXPR, 2), "There are 2 files.");
// test sequences of multiple #
- Assert.assertEquals("2", interpolator.interpolate("#0",2));
- Assert.assertEquals("#2", interpolator.interpolate("##0",2));
- Assert.assertEquals("##2", interpolator.interpolate("###0",2));
+ Assert.assertEquals(interpolator.interpolate("#0",2), "2");
+ Assert.assertEquals(interpolator.interpolate("##0",2), "#2");
+ Assert.assertEquals(interpolator.interpolate("###0",2), "##2");
+ // test a value expression in the mix
+ Contexts.getEventContext().set("contextVariable", "value");
+ Assert.assertEquals(interpolator.interpolate("#{contextVariable}"), "value");
+ Assert.assertEquals(interpolator.interpolate("#0 #{contextVariable} #1", "a", "z"), "a value z");
+ Assert.assertEquals(interpolator.interpolate("#0 ##{contextVariable} #1", "a", "z"), "a #value z");
+
Date date = new Date(0);
- Assert.assertEquals(DateFormat.getDateInstance(DateFormat.SHORT).format(date), interpolator.interpolate("{0,date,short}", date));
+ Assert.assertEquals(interpolator.interpolate("{0,date,short}", date), DateFormat.getDateInstance(DateFormat.SHORT).format(date));
// test that a messageformat error doesn't blow up
- Assert.assertEquals("{nosuchmessage}", interpolator.interpolate("{nosuchmessage}"));
+ Assert.assertEquals(interpolator.interpolate("{nosuchmessage}"), "{nosuchmessage}");
- try {
+ try
+ {
interpolator.interpolate("hello #{", (Object) null);
- } catch (Throwable t) {
+ } catch (Throwable t)
+ {
Assert.fail("interpolator raised an exception");
}
}
Added: branches/community/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/MockContainerTest.java
===================================================================
--- branches/community/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/MockContainerTest.java (rev 0)
+++ branches/community/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/MockContainerTest.java 2008-09-20 07:08:12 UTC (rev 9036)
@@ -0,0 +1,54 @@
+package org.jboss.seam.test.unit;
+
+import org.jboss.seam.Component;
+import org.jboss.seam.Seam;
+import org.jboss.seam.contexts.ApplicationContext;
+import org.jboss.seam.contexts.Context;
+import org.jboss.seam.contexts.Lifecycle;
+import org.jboss.seam.contexts.ServletLifecycle;
+import org.jboss.seam.core.Init;
+import org.jboss.seam.core.Manager;
+import org.jboss.seam.mock.MockExternalContext;
+import org.jboss.seam.mock.MockServletContext;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+
+/**
+ * A simple base test class that registers components according to class so that
+ * they can be looked up successfully using ComponentClass.instance(), as
+ * required by much of Seam's internal API.
+ */
+public abstract class MockContainerTest
+{
+ @BeforeMethod
+ protected void setUp()
+ {
+ MockServletContext servletContext = new MockServletContext();
+ ServletLifecycle.beginApplication(servletContext);
+ MockExternalContext externalContext = new MockExternalContext(servletContext);
+ Context appContext = new ApplicationContext(externalContext.getApplicationMap());
+ installComponent(appContext, Manager.class);
+ for (Class c : getComponentsToInstall())
+ {
+ installComponent(appContext, c);
+ }
+ appContext.set(Seam.getComponentName(Init.class), new Init());
+ Lifecycle.beginCall();
+ }
+
+ protected Class[] getComponentsToInstall()
+ {
+ return new Class[] {};
+ }
+
+ @AfterMethod
+ protected void tearDown()
+ {
+ Lifecycle.endCall();
+ }
+
+ protected void installComponent(Context appContext, Class clazz)
+ {
+ appContext.set(Seam.getComponentName(clazz) + ".component", new Component(clazz));
+ }
+}
More information about the seam-commits
mailing list