Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
Minor refactor to extract reusable test code
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Jun 25, 2013
1 parent 457e09e commit 6443ce9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@

package org.exoplatform.component.test;

import java.util.concurrent.Callable;

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.exoplatform.container.PortalContainer;
import org.exoplatform.container.RootContainer;
import org.exoplatform.container.component.RequestLifeCycle;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
Expand Down Expand Up @@ -59,6 +67,10 @@ public static void fail(Throwable t) {
throw afe;
}

public static Error failure(String msg) {
throw new AssertionFailedError(msg);
}

public static Error failure(Throwable t) {
AssertionFailedError afe = new AssertionFailedError();
afe.initCause(t);
Expand All @@ -70,4 +82,52 @@ public static Error failure(String msg, Throwable t) {
afe.initCause(t);
return afe;
}

/**
* Execute the <code>callback</code> argument within the context of the portal container.
*
* @param callback the callback to execute
*/
public static void inPortalContainer(final Runnable callback) {
inPortalContainer(new Callable<Void>() {
@Override
public Void call() throws Exception {
callback.run();
return null;
}
});
}

/**
* Execute the <code>callback</code> argument within the context of the portal container and return the callback value.
*
* @param callback the callback to execute
*/
public static <V> V inPortalContainer(Callable<V> callback) {
PortalContainer portalContainer = PortalContainer.getInstanceIfPresent();
boolean remove;
if (portalContainer == null) {
remove = true;
RootContainer rootContainer = RootContainer.getInstance();
portalContainer = (PortalContainer) rootContainer.getComponentInstanceOfType(PortalContainer.class);
if (portalContainer == null) {
throw failure("Could not obtain a valid portal container");
} else {
PortalContainer.setInstance(portalContainer);
}
} else {
remove = false;
}
RequestLifeCycle.begin(portalContainer);
try {
return callback.call();
} catch (Exception e) {
throw failure(e);
} finally {
RequestLifeCycle.end();
if (remove) {
PortalContainer.setInstance(null);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
package org.gatein.portal.ui.register;

import java.net.URL;
import java.util.concurrent.Callable;

import junit.framework.AssertionFailedError;
import juzu.arquillian.Helper;
import org.exoplatform.component.test.BaseGateInTest;
import org.exoplatform.container.PortalContainer;
import org.exoplatform.container.RootContainer;
import org.exoplatform.container.component.RequestLifeCycle;
import org.exoplatform.services.organization.OrganizationService;
import org.exoplatform.services.organization.User;
import org.exoplatform.services.organization.UserHandler;
Expand Down Expand Up @@ -112,21 +112,21 @@ public void testRegisterSuccess() {
@Test
@InSequence(2)
public void testUserExist() throws Exception {
PortalContainer portalContainer = (PortalContainer) RootContainer.getComponent(PortalContainer.class);
RequestLifeCycle.begin(portalContainer);
try {
OrganizationService orgService = (OrganizationService)portalContainer.getComponentInstanceOfType(OrganizationService.class);
UserHandler handler = orgService.getUserHandler();
User user = handler.findUserByName("test_user_name");
assertNotNull(user);
assertEquals(null, user.getPassword());
assertEquals("test_first_name", user.getFirstName());
assertEquals("test_last_name", user.getLastName());
assertEquals("test_display_name", user.getDisplayName());
assertEquals("test_email_address", user.getEmail());
} finally {
RequestLifeCycle.end();
}
BaseGateInTest.inPortalContainer(new Callable<Void>() {
@Override
public Void call() throws Exception {
OrganizationService orgService = (OrganizationService)PortalContainer.getComponent(OrganizationService.class);
UserHandler handler = orgService.getUserHandler();
User user = handler.findUserByName("test_user_name");
assertNotNull(user);
assertEquals(null, user.getPassword());
assertEquals("test_first_name", user.getFirstName());
assertEquals("test_last_name", user.getLastName());
assertEquals("test_display_name", user.getDisplayName());
assertEquals("test_email_address", user.getEmail());
return null;
}
});
}

@Test
Expand Down

0 comments on commit 6443ce9

Please sign in to comment.