001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.portal.test.cms.workflow;
023:
024: import org.hibernate.Session;
025: import org.hibernate.SessionFactory;
026: import org.hibernate.Transaction;
027: import org.jboss.portal.cms.impl.interceptors.ApprovalWorkflowInterceptor;
028: import org.jboss.portal.cms.impl.jcr.JCRCMS;
029: import org.jboss.portal.cms.workflow.ApprovePublish;
030: import org.jboss.portal.cms.workflow.ApprovePublishImpl;
031: import org.jboss.portal.common.invocation.Interceptor;
032: import org.jboss.portal.identity.IdentityContext;
033: import org.jboss.portal.identity.IdentityServiceController;
034: import org.jboss.portal.identity.UserModule;
035: import org.jboss.portal.identity.User;
036: import org.jboss.portal.identity.db.HibernateRoleImpl;
037: import org.jboss.portal.identity.db.HibernateUserImpl;
038: import org.jboss.portal.server.impl.invocation.JBossInterceptorStack;
039: import org.jboss.portal.test.cms.commands.AbstractCommandTestCase;
040: import org.jboss.portal.test.cms.commands.CMSInterceptorStackFactory;
041:
042: import javax.naming.InitialContext;
043:
044: /** @author Sohil Shah - sohil.shah@jboss.com - Nov 30, 2006 */
045: public abstract class AbstractWorkflowTestCase extends
046: AbstractCommandTestCase {
047: /**
048: *
049: */
050: protected UserModule userModule = null;
051:
052: /**
053: *
054: */
055: public AbstractWorkflowTestCase() {
056: super ();
057: }
058:
059: /**
060: *
061: */
062: public void setUp() throws Exception {
063: //override the configration location to include workflow services
064: this .configuration = "org/jboss/portal/cms/jboss-beans-workflow.xml";
065: super .setUp();
066:
067: ApprovalWorkflowInterceptor workflowInterceptor = this
068: .getApprovalWorkflowInterceptor();
069: ApprovePublish approvePublish = this .getApprovePublish();
070: IdentityServiceController identityService = ((ApprovePublishImpl) approvePublish)
071: .getIdentityServiceController();
072:
073: //Setup bootstrapped Identity data into the Identity store
074: SessionFactory sessionFactory = (SessionFactory) new InitialContext()
075: .lookup("java:/SessionFactory");
076: this .loadData(sessionFactory, identityService);
077:
078: //Setup the interceptor stack
079: CMSInterceptorStackFactory stackFactory = new CMSInterceptorStackFactory();
080: Interceptor[] interceptors = new Interceptor[1];
081: interceptors[0] = workflowInterceptor;
082: JBossInterceptorStack stack = new JBossInterceptorStack(
083: interceptors);
084: stackFactory.setInterceptorStack(stack);
085:
086: this .service.setStackFactory(stackFactory);
087: this .service.setApprovePublishWorkflow(approvePublish);
088:
089: this .userModule = (UserModule) identityService
090: .getIdentityContext().getObject(
091: IdentityContext.TYPE_USER_MODULE);
092: }
093:
094: /**
095: *
096: */
097: public void tearDown() throws Exception {
098: super .tearDown();
099: this .userModule = null;
100: }
101:
102: /** @return */
103: protected ApprovalWorkflowInterceptor getApprovalWorkflowInterceptor()
104: throws Exception {
105: ApprovalWorkflowInterceptor approvalWorkflowInterceptor = (ApprovalWorkflowInterceptor) new InitialContext()
106: .lookup("java:/portal/cms/ApprovalWorkflowInterceptor");
107: return approvalWorkflowInterceptor;
108: }
109:
110: /**
111: * @return
112: * @throws Exception
113: */
114: protected ApprovePublish getApprovePublish() throws Exception {
115: return (ApprovePublish) new InitialContext()
116: .lookup("java:/portal/ApprovePublishWorkflow");
117: }
118:
119: /**
120: *
121: *
122: */
123: private void loadData(SessionFactory sessionFactory,
124: IdentityServiceController service) throws Exception {
125: UserModule userModule = (UserModule) service
126: .getIdentityContext().getObject(
127: IdentityContext.TYPE_USER_MODULE);
128:
129: Session session = sessionFactory.openSession();
130: Transaction tx = session.beginTransaction();
131: try {
132: if (userModule.getUserCount() == 0) {
133: //
134: HibernateRoleImpl adminRole = new HibernateRoleImpl(
135: "Admin", "Administrators");
136: HibernateRoleImpl userRole = new HibernateRoleImpl(
137: "User", "Users");
138:
139: //
140: HibernateUserImpl admin = new HibernateUserImpl("admin");
141: admin.setPassword(org.jboss.portal.common.util.Tools
142: .md5AsHexString("admin"));
143: admin.setRealEmail("admin@portal.com");
144: admin.setViewRealEmail(true);
145: admin.setEnabled(true);
146:
147: //
148: admin.getRoles().add(adminRole);
149: adminRole.getUsers().add(admin);
150:
151: //
152: HibernateUserImpl user = new HibernateUserImpl("user");
153: user.setPassword(org.jboss.portal.common.util.Tools
154: .md5AsHexString("user"));
155: user.setRealEmail("user@portal.com");
156: user.setViewRealEmail(true);
157: user.setEnabled(true);
158:
159: //
160: user.getRoles().add(userRole);
161: userRole.getUsers().add(user);
162:
163: //
164: session.save(adminRole);
165: session.save(userRole);
166: session.save(admin);
167: session.save(user);
168:
169: tx.commit();
170: }
171: } catch (Exception e) {
172: tx.rollback();
173: } finally {
174: if (session != null) {
175: session.close();
176: }
177: }
178: }
179:
180: /** @param username */
181: protected void runAs(String username) throws Exception {
182: User user = this.userModule.findUserByUserName(username);
183: JCRCMS.getUserInfo().set(user);
184: }
185: }
|