notify-dpml AT lists.ibiblio.org
Subject: DPML Notify
List archive
r1759 - in trunk/tutorials/components/context/src: main/org/acme test/org/acme/test
- From: mcconnell at BerliOS <mcconnell AT mail.berlios.de>
- To: notify-dpml AT lists.ibiblio.org
- Subject: r1759 - in trunk/tutorials/components/context/src: main/org/acme test/org/acme/test
- Date: Tue, 19 Sep 2006 10:57:34 +0200
Author: mcconnell
Date: 2006-09-19 10:57:33 +0200 (Tue, 19 Sep 2006)
New Revision: 1759
Added:
trunk/tutorials/components/context/src/test/org/acme/test/MockTestCase.java
trunk/tutorials/components/context/src/test/org/acme/test/ProxyTestCase.java
Modified:
trunk/tutorials/components/context/src/main/org/acme/Demo.java
trunk/tutorials/components/context/src/test/org/acme/test/DemoTestCase.java
Log:
add test-cases dealing with mock-object based context arguments and
proxy-based context arguments as alternative unit test strategies
Modified: trunk/tutorials/components/context/src/main/org/acme/Demo.java
===================================================================
--- trunk/tutorials/components/context/src/main/org/acme/Demo.java
2006-09-17 18:58:21 UTC (rev 1758)
+++ trunk/tutorials/components/context/src/main/org/acme/Demo.java
2006-09-19 08:57:33 UTC (rev 1759)
@@ -80,6 +80,13 @@
}
//------------------------------------------------------------------
+ // state
+ //------------------------------------------------------------------
+
+ private final Logger m_logger;
+ private final Context m_context;
+
+ //------------------------------------------------------------------
// constructor
//------------------------------------------------------------------
@@ -90,22 +97,31 @@
*/
public Demo( final Logger logger, final Context context )
{
- if( logger.isLoggable( Level.INFO ) )
+ m_logger = logger;
+ m_context = context;
+
+ if( m_logger.isLoggable( Level.INFO ) )
{
- final String owner = context.getOwner();
- final String activity = context.getActivity();
- final String target = context.getTarget();
- final String color = context.getColor();
- final String message =
- activity
- + " "
- + owner
- + "'s "
- + target
- + " "
- + color
- + ".";
- logger.info( message );
+ String message = getMessage();
+ m_logger.info( message );
}
}
+
+ public String getMessage()
+ {
+ final String owner = m_context.getOwner();
+ final String activity = m_context.getActivity();
+ final String target = m_context.getTarget();
+ final String color = m_context.getColor();
+ final String message =
+ activity
+ + " "
+ + owner
+ + "'s "
+ + target
+ + " "
+ + color
+ + ".";
+ return message;
+ }
}
Modified:
trunk/tutorials/components/context/src/test/org/acme/test/DemoTestCase.java
===================================================================
---
trunk/tutorials/components/context/src/test/org/acme/test/DemoTestCase.java
2006-09-17 18:58:21 UTC (rev 1758)
+++
trunk/tutorials/components/context/src/test/org/acme/test/DemoTestCase.java
2006-09-19 08:57:33 UTC (rev 1759)
@@ -44,6 +44,8 @@
URI uri = getPartURI();
Part part = Part.load( uri );
Demo demo = (Demo) part.getContent();
+ String message = demo.getMessage();
+ assertEquals( "message", "Painting mcconnell's bike silver.",
message );
}
private URI getPartURI() throws Exception
Added:
trunk/tutorials/components/context/src/test/org/acme/test/MockTestCase.java
===================================================================
---
trunk/tutorials/components/context/src/test/org/acme/test/MockTestCase.java
2006-09-17 18:58:21 UTC (rev 1758)
+++
trunk/tutorials/components/context/src/test/org/acme/test/MockTestCase.java
2006-09-19 08:57:33 UTC (rev 1759)
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2006 Stephen J. McConnell.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.acme.test;
+
+import java.util.logging.Logger;
+
+import junit.framework.TestCase;
+
+import org.acme.Demo;
+import org.acme.Demo.Context;
+
+/**
+ * Deployment of the demo component.
+ *
+ * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
+ * @version @PROJECT-VERSION@
+ */
+public class MockTestCase extends TestCase
+{
+ private static final String ACTIVITY = "Painting";
+ private static final String OWNER = System.getProperty( "user.name" );
+ private static final String TARGET = "house";
+ private static final String COLOR = "red";
+ private static final String MESSAGE =
+ ACTIVITY + " " + OWNER + "'s " + TARGET + " " + COLOR + ".";
+
+ /**
+ * Test construction of the demo instance using a mock context object.
+ * @exception Exception if an error occurs
+ */
+ public void testComponent() throws Exception
+ {
+ Logger logger = Logger.getLogger( "test" );
+ Context context = new MockContext();
+ Demo demo = new Demo( logger, context );
+ String message = demo.getMessage();
+ assertEquals( "message", MESSAGE, message );
+ }
+
+ /**
+ * The mock context object.
+ */
+ private static final class MockContext implements Context
+ {
+ /**
+ * Return a string describing an activity that our object should
+ * perform. An activity is a word such as "painting" or "coloring"
+ * or any other color related activity you can think of. The
component
+ * implementation will construct a phrase using this word as the
operative
+ * activity.
+ *
+ * @return the activity verb
+ */
+ public String getActivity()
+ {
+ return ACTIVITY;
+ }
+
+ /**
+ * When constructing a phrase the implementation uses a owner to
+ * distringuish the ownership of the subject to which it is applying
+ * an activity. The value returned by this method could be a user's
name
+ * or an alias such as "batman".
+ *
+ * @return the owner's name
+ */
+ public String getOwner()
+ {
+ return OWNER;
+ }
+
+ /**
+ * The object implementation applies an activity to an owners object.
The
+ * name of the object is provided in the form of a target. A target
could
+ * be a house, a bike, a car, or whatever object appeals to the
manager of
+ * the object.
+ *
+ * @return the name of the owner's target to which the activity will
+ * be applied
+ */
+ public String getTarget()
+ {
+ return TARGET;
+ }
+
+ /**
+ * Returns the color to be used during construction of the activity
statement.
+ *
+ * @return the color value
+ */
+ public String getColor()
+ {
+ return COLOR;
+ }
+ }
+}
Added:
trunk/tutorials/components/context/src/test/org/acme/test/ProxyTestCase.java
===================================================================
---
trunk/tutorials/components/context/src/test/org/acme/test/ProxyTestCase.java
2006-09-17 18:58:21 UTC (rev 1758)
+++
trunk/tutorials/components/context/src/test/org/acme/test/ProxyTestCase.java
2006-09-19 08:57:33 UTC (rev 1759)
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2006 Stephen J. McConnell.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.acme.test;
+
+import java.util.logging.Logger;
+import java.util.Hashtable;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import net.dpml.util.ContextInvocationHandler;
+
+import org.acme.Demo;
+import org.acme.Demo.Context;
+
+/**
+ * Deployment of the demo component.
+ *
+ * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
+ * @version @PROJECT-VERSION@
+ */
+public class ProxyTestCase extends TestCase
+{
+ private static final String ACTIVITY = "Painting";
+ private static final String OWNER = System.getProperty( "user.name" );
+ private static final String TARGET = "GTV";
+ private static final String COLOR = "white";
+
+ private static final String MESSAGE =
+ ACTIVITY + " " + OWNER + "'s " + TARGET + " " + COLOR + ".";
+
+ /**
+ * Test construction of the demo instance using a proxy context object.
+ * @exception Exception if an error occurs
+ */
+ public void testComponent() throws Exception
+ {
+ Logger logger = Logger.getLogger( "test" );
+ Class clazz = Demo.Context.class;
+ Map map = buildContextMap();
+ Context context =
+ (Context) ContextInvocationHandler.getProxiedInstance( clazz, map
);
+ Demo demo = new Demo( logger, context );
+ String message = demo.getMessage();
+ assertEquals( "message", MESSAGE, message );
+ }
+
+ private Map buildContextMap()
+ {
+ Map map = new Hashtable();
+ map.put( "activity", ACTIVITY );
+ map.put( "owner", OWNER );
+ map.put( "target", TARGET );
+ map.put( "color", COLOR );
+ return map;
+ }
+}
- r1759 - in trunk/tutorials/components/context/src: main/org/acme test/org/acme/test, mcconnell at BerliOS, 09/19/2006
Archive powered by MHonArc 2.6.24.