Author: justin
Date: Tue May 4 21:17:25 2010
New Revision: 941045
URL: http://svn.apache.org/viewvc?rev=941045&view=rev
Log:
SLING-1518 - making unit tests compatible with jmock 2.5.1
Modified:
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
sling/trunk/bundles/extensions/adapter/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/EventUtilTest.java
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/RequestPropertyTest.java
sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/bootstrapcommands/BootstrapCommandFileTest.java
sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/bootstrapcommands/UninstallBundleCommandTest.java
Modified:
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java?rev=941045&r1=941044&r2=941045&view=diff
==============================================================================
---
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
(original)
+++
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
Tue May 4 21:17:25 2010
@@ -256,9 +256,9 @@ public class ResourceUtilTest {
// the resource resolver
final ResourceResolver resolver =
this.context.mock(ResourceResolver.class);
// the resource to test
- final Resource r = this.context.mock(Resource.class);
- final Resource r2 = this.context.mock(Resource.class);
- final Resource typeResource = this.context.mock(Resource.class);
+ final Resource r = this.context.mock(Resource.class, "resource1");
+ final Resource r2 = this.context.mock(Resource.class, "resource2");
+ final Resource typeResource = this.context.mock(Resource.class,
"typeResource");
this.context.checking(new Expectations() {{
allowing(r).getResourceType(); will(returnValue("a:b"));
allowing(r).getResourceResolver(); will(returnValue(resolver));
@@ -288,8 +288,8 @@ public class ResourceUtilTest {
// the resource resolver
final ResourceResolver resolver =
this.context.mock(ResourceResolver.class);
// the resource to test
- final Resource r = this.context.mock(Resource.class);
- final Resource typeResource = this.context.mock(Resource.class);
+ final Resource r = this.context.mock(Resource.class, "resource");
+ final Resource typeResource = this.context.mock(Resource.class,
"typeResource");
this.context.checking(new Expectations() {{
allowing(r).getResourceType(); will(returnValue("a:b"));
allowing(r).getResourceSuperType(); will(returnValue("d:e"));
@@ -326,9 +326,9 @@ public class ResourceUtilTest {
// none is adaptable to String
// b and c are adaptable to long
// a and c are adaptable to boolean
- final Resource a = this.context.mock(Resource.class);
- final Resource b = this.context.mock(Resource.class);
- final Resource c = this.context.mock(Resource.class);
+ final Resource a = this.context.mock(Resource.class, "a");
+ final Resource b = this.context.mock(Resource.class, "b");
+ final Resource c = this.context.mock(Resource.class, "c");
final List<Resource> l = new ArrayList<Resource>();
l.add(a); l.add(b); l.add(c);
this.context.checking(new Expectations() {{
@@ -377,20 +377,20 @@ public class ResourceUtilTest {
}
return count;
}
-
+
@Test public void testIsStarResource() {
- final Resource nonStar = context.mock(Resource.class);
+ final Resource nonStar = context.mock(Resource.class,
"nonStarResource");
final String starPath = "/foo/*";
- final Resource star = context.mock(Resource.class);
+ final Resource star = context.mock(Resource.class,
"starResource");
final String nonStarPath = "/foo/*";
this.context.checking(new Expectations() {{
allowing(star).getPath(); will(returnValue(starPath));
allowing(nonStar).getPath(); will(returnValue(nonStarPath));
}});
-
- assertTrue("expecting star==true for path" + starPath,
+
+ assertTrue("expecting star==true for path" + starPath,
ResourceUtil.isStarResource(star));
- assertTrue("expecting star==false for path" + starPath,
+ assertTrue("expecting star==false for path" + starPath,
ResourceUtil.isStarResource(nonStar));
}
@Test public void testIsSyntheticResource() {
@@ -400,12 +400,12 @@ public class ResourceUtilTest {
allowing(star).getPath(); will(returnValue("/foo/*"));
}});
final Resource wrapped = new ResourceWrapper(synth);
-
- assertTrue("expecting synthetic==true for SyntheticResource",
+
+ assertTrue("expecting synthetic==true for SyntheticResource",
ResourceUtil.isSyntheticResource(synth));
- assertFalse("expecting synthetic==false for star resource",
+ assertFalse("expecting synthetic==false for star resource",
ResourceUtil.isSyntheticResource(star));
- assertTrue("expecting synthetic==true for wrapped Resource",
+ assertTrue("expecting synthetic==true for wrapped Resource",
ResourceUtil.isSyntheticResource(wrapped));
}
}
Modified:
sling/trunk/bundles/extensions/adapter/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/adapter/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java?rev=941045&r1=941044&r2=941045&view=diff
==============================================================================
---
sling/trunk/bundles/extensions/adapter/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
(original)
+++
sling/trunk/bundles/extensions/adapter/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
Tue May 4 21:17:25 2010
@@ -61,8 +61,8 @@ public class AdapterManagerTest {
/**
* Helper method to create a mock bundle
*/
- protected Bundle createBundle() {
- final Bundle bundle = this.context.mock(Bundle.class);
+ protected Bundle createBundle(String name) {
+ final Bundle bundle = this.context.mock(Bundle.class, name);
this.context.checking(new Expectations() {{
allowing(bundle).getBundleId();
will(returnValue(1L));
@@ -96,8 +96,8 @@ public class AdapterManagerTest {
* Helper method to create a mock service reference
*/
protected ServiceReference createServiceReference() {
- final Bundle bundle = this.createBundle();
- final ServiceReference ref = this.context.mock(ServiceReference.class);
+ final Bundle bundle = this.createBundle("bundle");
+ final ServiceReference ref = this.context.mock(ServiceReference.class,
"serviceReference");
this.context.checking(new Expectations() {{
allowing(ref).getProperty(Constants.SERVICE_ID);
will(returnValue(1L));
@@ -115,8 +115,8 @@ public class AdapterManagerTest {
* Helper method to create a mock service reference
*/
protected ServiceReference createServiceReference2() {
- final Bundle bundle = this.createBundle();
- final ServiceReference ref = this.context.mock(ServiceReference.class);
+ final Bundle bundle = this.createBundle("bundle2");
+ final ServiceReference ref = this.context.mock(ServiceReference.class,
"serviceReference2");
this.context.checking(new Expectations() {{
allowing(ref).getProperty(Constants.SERVICE_ID);
will(returnValue(2L));
Modified:
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/EventUtilTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/EventUtilTest.java?rev=941045&r1=941044&r2=941045&view=diff
==============================================================================
---
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/EventUtilTest.java
(original)
+++
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/EventUtilTest.java
Tue May 4 21:17:25 2010
@@ -77,8 +77,8 @@ public class EventUtilTest {
assertFalse(EventUtil.isLocal(remoteEvent));
}
- protected Value getValueOfType(final int type) {
- final Value v = this.context.mock(Value.class);
+ protected Value getValueOfType(final int type, String name) {
+ final Value v = this.context.mock(Value.class, name);
this.context.checking(new Expectations() {{
allowing(v).getType();will(returnValue(type));
}});
@@ -89,15 +89,15 @@ public class EventUtilTest {
final ValueFactory factory = this.context.mock(ValueFactory.class);
this.context.checking(new Expectations() {{
allowing(factory).createValue(true);
- will(returnValue(getValueOfType(PropertyType.BOOLEAN)));
+ will(returnValue(getValueOfType(PropertyType.BOOLEAN,
"booleanValue1")));
allowing(factory).createValue(false);
- will(returnValue(getValueOfType(PropertyType.BOOLEAN)));
+ will(returnValue(getValueOfType(PropertyType.BOOLEAN,
"booleanValue2")));
allowing(factory).createValue(with(any(Long.class)));
- will(returnValue(getValueOfType(PropertyType.LONG)));
+ will(returnValue(getValueOfType(PropertyType.LONG, "longValue")));
allowing(factory).createValue(with(any(String.class)));
- will(returnValue(getValueOfType(PropertyType.STRING)));
+ will(returnValue(getValueOfType(PropertyType.STRING,
"stringValue")));
allowing(factory).createValue(with(any(Calendar.class)));
- will(returnValue(getValueOfType(PropertyType.DATE)));
+ will(returnValue(getValueOfType(PropertyType.DATE, "dateValue")));
}});
// boolean
assertEquals(PropertyType.BOOLEAN,
EventHelper.getNodePropertyValue(factory, true).getType());
Modified:
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java?rev=941045&r1=941044&r2=941045&view=diff
==============================================================================
---
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
(original)
+++
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
Tue May 4 21:17:25 2010
@@ -109,13 +109,13 @@ public abstract class AbstractRepository
this.handler.threadPool = new ThreadPoolImpl();
// lets set up the bundle context
- final BundleContext bundleContext =
this.getMockery().mock(BundleContext.class);
+ final BundleContext bundleContext =
this.getMockery().mock(BundleContext.class, "beforeBundleContext");
// lets set up the component configuration
final Dictionary<String, Object> componentConfig =
this.getComponentConfig();
// lets set up the compnent context
- final ComponentContext componentContext =
this.getMockery().mock(ComponentContext.class);
+ final ComponentContext componentContext =
this.getMockery().mock(ComponentContext.class, "beforeComponentContext");
this.getMockery().checking(new Expectations() {{
allowing(componentContext).getBundleContext();
will(returnValue(bundleContext));
@@ -130,9 +130,9 @@ public abstract class AbstractRepository
@org.junit.After public void shutdown() throws Exception {
// lets set up the bundle context with the sling id
- final BundleContext bundleContext =
this.getMockery().mock(BundleContext.class);
+ final BundleContext bundleContext =
this.getMockery().mock(BundleContext.class, "afterBundleContext");
- final ComponentContext componentContext =
this.getMockery().mock(ComponentContext.class);
+ final ComponentContext componentContext =
this.getMockery().mock(ComponentContext.class, "afterComponentContext");
this.getMockery().checking(new Expectations() {{
allowing(componentContext).getBundleContext();
will(returnValue(bundleContext));
Modified:
sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/RequestPropertyTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/RequestPropertyTest.java?rev=941045&r1=941044&r2=941045&view=diff
==============================================================================
---
sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/RequestPropertyTest.java
(original)
+++
sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/RequestPropertyTest.java
Tue May 4 21:17:25 2010
@@ -216,20 +216,21 @@ public class RequestPropertyTest {
@SuppressWarnings("unchecked")
private Map<String, RequestProperty> collectContent(Param... kvs) throws
Throwable {
final List<Map.Entry<String, RequestParameter>> params = new
ArrayList<Map.Entry<String, RequestParameter>>();
- for (final Param kv : kvs) {
+ for (int i = 0; i < kvs.length; i++) {
+ final Param kv = kvs[i];
final RequestParameter[] param = new
RequestParameter[kv.value.length];
- for (int i = 0; i < kv.value.length; i++) {
- final String strValue = kv.value[i];
- final RequestParameter aparam =
context.mock(RequestParameter.class);
+ for (int j = 0; j < kv.value.length; j++) {
+ final String strValue = kv.value[j];
+ final RequestParameter aparam =
context.mock(RequestParameter.class, "requestParameter" + i + "#" + j);
context.checking(new Expectations() {
{
allowing(aparam).getString();
will(returnValue(strValue));
}
});
- param[i] = aparam;
+ param[j] = aparam;
}
- final Map.Entry<String, RequestParameter> entry =
context.mock(Map.Entry.class);
+ final Map.Entry<String, RequestParameter> entry =
context.mock(Map.Entry.class, "entry" + i);
context.checking(new Expectations() {
{
allowing(entry).getKey();
Modified:
sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/bootstrapcommands/BootstrapCommandFileTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/bootstrapcommands/BootstrapCommandFileTest.java?rev=941045&r1=941044&r2=941045&view=diff
==============================================================================
---
sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/bootstrapcommands/BootstrapCommandFileTest.java
(original)
+++
sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/bootstrapcommands/BootstrapCommandFileTest.java
Tue May 4 21:17:25 2010
@@ -33,26 +33,30 @@ import java.util.List;
import org.apache.felix.framework.Logger;
import org.jmock.Expectations;
import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Version;
+...@runwith(JMock.class)
public class BootstrapCommandFileTest {
private final Logger logger = new Logger();
private final File nonExistentFile = new File("/nonexistent." +
System.currentTimeMillis());
- static final Mockery mockery = new Mockery();
+ final Mockery mockery = new JUnit4Mockery();
private File dataFile;
private File cmdFile;
private BundleContext bundleContext;
-
+
@Before
public void setUp() throws IOException,BundleException {
dataFile = File.createTempFile(getClass().getSimpleName(), "txt");
-
+
final Bundle b1 = mockery.mock(Bundle.class);
mockery.checking(new Expectations() {{
allowing(b1).getSymbolicName();
@@ -62,8 +66,8 @@ public class BootstrapCommandFileTest {
allowing(b1).uninstall();
}});
final Bundle [] bundles = { b1 };
-
-
+
+
bundleContext = mockery.mock(BundleContext.class);
mockery.checking(new Expectations() {{
allowing(bundleContext).getDataFile(with(any(String.class)));
@@ -71,7 +75,7 @@ public class BootstrapCommandFileTest {
allowing(bundleContext).getBundles();
will(returnValue(bundles));
}});
-
+
cmdFile = File.createTempFile(getClass().getSimpleName(), "cmd");
final PrintWriter w = new PrintWriter(new FileWriter(cmdFile));
w.println("# Test command file, this is a comment");
@@ -81,7 +85,7 @@ public class BootstrapCommandFileTest {
w.flush();
w.close();
}
-
+
@After
public void tearDown() throws IOException {
dataFile.delete();
@@ -91,24 +95,24 @@ public class BootstrapCommandFileTest {
@Test
public void testNoFileNoExecution() {
final BootstrapCommandFile bcf = new BootstrapCommandFile(logger,
nonExistentFile);
- assertFalse("Expecting anythingToExecute false for non-existing file",
+ assertFalse("Expecting anythingToExecute false for non-existing file",
bcf.anythingToExecute(bundleContext));
}
-
+
@Test
public void testExecuteOnceOnly() throws IOException {
final BootstrapCommandFile bcf = new BootstrapCommandFile(logger,
cmdFile);
- assertTrue("Expecting anythingToExecute true for existing file",
+ assertTrue("Expecting anythingToExecute true for existing file",
bcf.anythingToExecute(bundleContext));
assertEquals("Expecting two commands to be executed", 2,
bcf.execute(bundleContext));
- assertFalse("Expecting anythingToExecute false after execution",
+ assertFalse("Expecting anythingToExecute false after execution",
bcf.anythingToExecute(bundleContext));
}
-
+
@Test
public void testParsing() throws IOException {
final BootstrapCommandFile bcf = new BootstrapCommandFile(logger,
cmdFile);
- final String cmdString =
+ final String cmdString =
"# a comment\n"
+ "uninstall symbolicname1 1.0\n"
+ "\n"
@@ -119,16 +123,16 @@ public class BootstrapCommandFileTest {
assertEquals("Expecting two commands after parsing", 2, c.size());
int index = 0;
for(Command cmd : c) {
- assertTrue("Expecting an UninstallBundleCommand at index " +
index,
+ assertTrue("Expecting an UninstallBundleCommand at index " + index,
cmd instanceof UninstallBundleCommand);
index++;
}
}
-
+
@Test
public void testSyntaxError() throws IOException {
final BootstrapCommandFile bcf = new BootstrapCommandFile(logger,
cmdFile);
- final String cmdString =
+ final String cmdString =
"# a comment\n"
+ "uninstall only_one_field\n"
+ "\n"
@@ -139,22 +143,22 @@ public class BootstrapCommandFileTest {
bcf.parse(new ByteArrayInputStream(cmdString.getBytes()));
fail("Expecting IOException for syntax error");
} catch(IOException ioe) {
- assertTrue("Exception message (" + ioe.getMessage() + ") should
contain command line",
+ assertTrue("Exception message (" + ioe.getMessage() + ") should
contain command line",
ioe.getMessage().contains("only_one_field"));
}
}
-
+
@Test
public void testInvalidCommand() throws IOException {
final BootstrapCommandFile bcf = new BootstrapCommandFile(logger,
cmdFile);
- final String cmdString =
+ final String cmdString =
"foo\n"
;
try {
bcf.parse(new ByteArrayInputStream(cmdString.getBytes()));
fail("Expecting IOException for invalid command");
} catch(IOException ioe) {
- assertTrue("Exception message (" + ioe.getMessage() + ") should
contain command line",
+ assertTrue("Exception message (" + ioe.getMessage() + ") should
contain command line",
ioe.getMessage().contains("foo"));
}
}
Modified:
sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/bootstrapcommands/UninstallBundleCommandTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/bootstrapcommands/UninstallBundleCommandTest.java?rev=941045&r1=941044&r2=941045&view=diff
==============================================================================
---
sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/bootstrapcommands/UninstallBundleCommandTest.java
(original)
+++
sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/bootstrapcommands/UninstallBundleCommandTest.java
Tue May 4 21:17:25 2010
@@ -23,15 +23,19 @@ import static org.junit.Assert.assertNot
import org.apache.felix.framework.Logger;
import org.jmock.Expectations;
import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Version;
/** Test the UninstallBundleCommand */
+...@runwith(JMock.class)
public class UninstallBundleCommandTest {
- static final Mockery mockery = new Mockery();
+ final Mockery mockery = new JUnit4Mockery();
private BundleContext bundleContext;
private final Logger logger = new Logger();
@@ -39,7 +43,7 @@ public class UninstallBundleCommandTest
public void setUp() throws Exception {
final Bundle [] b = new Bundle[3];
for(int i=0; i < b.length; i++) {
- b[i] = mockery.mock(Bundle.class);
+ b[i] = mockery.mock(Bundle.class, "bundle" + i);
}
// b0 is in version range, will be uninstalled
@@ -50,7 +54,7 @@ public class UninstallBundleCommandTest
will(returnValue(new Version("1.0.0")));
exactly(1).of(b[0]).uninstall();
}});
-
+
// b1 is not in version range, not uninstalled
mockery.checking(new Expectations() {{
allowing(b[1]).getSymbolicName();
@@ -58,7 +62,7 @@ public class UninstallBundleCommandTest
allowing(b[1]).getVersion();
will(returnValue(new Version("2.0.0")));
}});
-
+
// b2 has different symbolic name, not uninstalled
mockery.checking(new Expectations() {{
allowing(b[2]).getSymbolicName();
@@ -66,14 +70,14 @@ public class UninstallBundleCommandTest
allowing(b[2]).getVersion();
will(returnValue(new Version("1.0.0")));
}});
-
+
bundleContext = mockery.mock(BundleContext.class);
mockery.checking(new Expectations() {{
allowing(bundleContext).getBundles();
will(returnValue(b));
}});
}
-
+
@Test
public void testExplicitVersion() throws Exception {
final UninstallBundleCommand proto = new UninstallBundleCommand();
@@ -82,7 +86,7 @@ public class UninstallBundleCommandTest
assertNotNull("Expecting parsing to succeed", cmd);
cmd.execute(logger, bundleContext);
}
-
+
@Test
public void testVersionRange() throws Exception {
final UninstallBundleCommand proto = new UninstallBundleCommand();