You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2007/02/15 00:27:55 UTC
svn commit: r507740 - in /jackrabbit/trunk/jackrabbit-jcr-commons: pom.xml
src/test/java/org/apache/jackrabbit/commons/
src/test/java/org/apache/jackrabbit/commons/AbstractRepositoryTest.java
src/test/java/org/apache/jackrabbit/commons/MockCase.java
Author: jukka
Date: Wed Feb 14 15:27:53 2007
New Revision: 507740
URL: http://svn.apache.org/viewvc?view=rev&rev=507740
Log:
JCR-742: Added test cases for AbstractRepository. Note that I used the cglib byte code library to implement the MockCase test class to simplify testing of the abstract base classes.
Added:
jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/
jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/AbstractRepositoryTest.java (with props)
jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/MockCase.java (with props)
Modified:
jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml
Modified: jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml?view=diff&rev=507740&r1=507739&r2=507740
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml Wed Feb 14 15:27:53 2007
@@ -68,6 +68,12 @@
<version>3.8.1</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>cglib</groupId>
+ <artifactId>cglib</artifactId>
+ <version>2.1_3</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/AbstractRepositoryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/AbstractRepositoryTest.java?view=auto&rev=507740
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/AbstractRepositoryTest.java (added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/AbstractRepositoryTest.java Wed Feb 14 15:27:53 2007
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.jackrabbit.commons;
+
+import javax.jcr.Credentials;
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.jcr.SimpleCredentials;
+
+/**
+ * Test cases for the {@link AbstractRepositoryTest} class.
+ */
+public class AbstractRepositoryTest extends MockCase {
+
+ /**
+ * Tests the {@link AbstractRepository#login()} method.
+ *
+ * @throws RepositoryException if an error occurs
+ */
+ public void testLogin() throws RepositoryException {
+ Repository repository = (Repository) record(AbstractRepository.class);
+ repository.login(null, null);
+
+ replay();
+ repository.login();
+
+ verify();
+ }
+
+ /**
+ * Tests the {@link AbstractRepository#login(Credentials)} method.
+ *
+ * @throws RepositoryException if an error occurs
+ */
+ public void testLoginWithCredentials() throws RepositoryException {
+ Credentials credentials = new SimpleCredentials("", "".toCharArray());
+
+ Repository repository = (Repository) record(AbstractRepository.class);
+ repository.login(credentials, null);
+
+ replay();
+ repository.login(credentials);
+
+ verify();
+ }
+
+ /**
+ * Tests the {@link AbstractRepository#login(Credentials)} method with
+ * <code>null</code> credentials.
+ *
+ * @throws RepositoryException if an error occurs
+ */
+ public void testLoginWithNullCredentials() throws RepositoryException {
+ Repository repository = (Repository) record(AbstractRepository.class);
+ repository.login(null, null);
+
+ replay();
+ repository.login((Credentials) null);
+
+ verify();
+ }
+
+ /**
+ * Tests the {@link AbstractRepository#login(String)} method.
+ *
+ * @throws RepositoryException if an error occurs
+ */
+ public void testLoginWithWorkspace() throws RepositoryException {
+ Repository repository = (Repository) record(AbstractRepository.class);
+ repository.login(null, "workspace");
+
+ replay();
+ repository.login("workspace");
+
+ verify();
+ }
+
+ /**
+ * Tests the {@link AbstractRepository#login(String)} method with a
+ * <code>null</code> workspace name.
+ *
+ * @throws RepositoryException if an error occurs
+ */
+ public void testLoginWithNullWorkspace() throws RepositoryException {
+ Repository repository = (Repository) record(AbstractRepository.class);
+ repository.login(null, null);
+
+ replay();
+ repository.login((String) null);
+
+ verify();
+ }
+}
Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/AbstractRepositoryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/MockCase.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/MockCase.java?view=auto&rev=507740
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/MockCase.java (added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/MockCase.java Wed Feb 14 15:27:53 2007
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.jackrabbit.commons;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.LinkedList;
+
+import junit.framework.TestCase;
+
+import net.sf.cglib.proxy.Enhancer;
+import net.sf.cglib.proxy.MethodInterceptor;
+import net.sf.cglib.proxy.MethodProxy;
+
+/**
+ * Simple {@link TestCase} base class for mock-testing the abstract
+ * JCR implementation classes in this package.
+ */
+public class MockCase extends TestCase implements MethodInterceptor {
+
+ /**
+ * Mock test state. Set to <code>true</code> when the mock methods
+ * are being recorded, and to <code>false</code> when the methods are
+ * being played back.
+ */
+ private boolean recording;
+
+ /**
+ * The recorded mock call sequence. List of {@link MethodCall} objects.
+ */
+ private LinkedList calls;
+
+ /**
+ * The abstract base class being tested.
+ */
+ private Class base;
+
+ /**
+ * Creates a mocked proxy object for the given abstract base class and
+ * starts recording calls to the object.
+ *
+ * @param base the abstract base class being tested
+ * @return mocked proxy object
+ */
+ protected Object record(Class base) {
+ this.recording = true;
+ this.calls = new LinkedList();
+ this.base = base;
+ return Enhancer.create(base, base.getInterfaces(), this);
+ }
+
+ /**
+ * Switches the test state to replaying and verifying the recorded
+ * call sequence.
+ */
+ protected void replay() {
+ recording = false;
+ }
+
+ /**
+ * Verifies that all recorded method calls were executed during
+ * the verification phase.
+ */
+ protected void verify() {
+ assertTrue(calls.isEmpty());
+ }
+
+ //---------------------------------------------------< MethodInterceptor >
+
+ /**
+ * Intercepts a method call to the mocked proxy object. Passes the
+ * call through if the abstract base class being tested implements the
+ * method, and records or verifies the method call otherwise.
+ *
+ * @param object the object on which the method is being called
+ * @param method the method being called
+ * @param args method arguments
+ * @param proxy proxy for re-invoking the called method
+ * @return method return value
+ * @throws Throwable if an error occurs
+ */
+ public Object intercept(
+ Object object, Method method, Object[] args, MethodProxy proxy)
+ throws Throwable {
+ try {
+ base.getDeclaredMethod(method.getName(), method.getParameterTypes());
+ return proxy.invokeSuper(object, args);
+ } catch (NoSuchMethodException e) {
+ if (recording) {
+ calls.addLast(new MethodCall(method, args));
+ } else {
+ assertFalse(calls.isEmpty());
+ MethodCall call = (MethodCall) calls.removeFirst();
+ call.assertCall(method, args);
+ }
+ return null;
+ }
+ }
+
+ //----------------------------------------------------------< MethodCall >
+
+ /**
+ * Record of a method call.
+ */
+ private static class MethodCall {
+
+ /**
+ * The method that was called.
+ */
+ private final Method method;
+
+ /**
+ * The arguments that were passed to the method.
+ */
+ private final Object[] args;
+
+ /**
+ * Creates a new method call record.
+ *
+ * @param method the method that was called
+ * @param args the arguments that were passed to the method
+ */
+ public MethodCall(Method method, Object[] args) {
+ this.method = method;
+ this.args = args;
+ }
+
+ /**
+ * Verifies that the given method is the same as was recorded.
+ *
+ * @param method the method that was called
+ * @param args the arguments that were passed to the method
+ */
+ public void assertCall(Method method, Object[] args) {
+ assertEquals(this.method, method);
+ assertTrue(Arrays.equals(this.args, args));
+ }
+
+ }
+
+}
Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/MockCase.java
------------------------------------------------------------------------------
svn:eol-style = native