From: Pieter v. Z. <pv...@us...> - 2005-08-05 12:47:47
|
Update of /cvsroot/coefficient/coefficient/src-test/za/org/coefficient/testing In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16683/src-test/za/org/coefficient/testing Modified Files: TestDBSetup.java Log Message: moved some methods for setting up the db from MailforumTest to this class. setupBasics is now called here. This class is now a singleton. Add the results: the context into a map. Index: TestDBSetup.java =================================================================== RCS file: /cvsroot/coefficient/coefficient/src-test/za/org/coefficient/testing/TestDBSetup.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** TestDBSetup.java 2 Aug 2005 08:06:25 -0000 1.1 --- TestDBSetup.java 5 Aug 2005 12:47:38 -0000 1.2 *************** *** 4,8 **** --- 4,12 ---- package za.org.coefficient.testing; + import java.util.HashMap; import java.util.List; + import java.util.Locale; + import java.util.Map; + import java.util.TimeZone; import net.sf.hibernate.Hibernate; *************** *** 11,14 **** --- 15,19 ---- import za.org.coefficient.authentication.CoefficientUser; + import za.org.coefficient.authentication.Role; import za.org.coefficient.core.Project; import za.org.coefficient.interfaces.CoefficientContext; *************** *** 18,23 **** --- 23,32 ---- * @author pieter20 Jul 20, 2005 This class is based on the following article on * ServerSide:Unit-Testing Hibernate with HSQL DB by Alex Vollmer + * It initializes and creates the defualt db and one: project and user. + * It then adds the user as a member to the project and logins in the user */ public class TestDBSetup { + private static TestDBSetup instance; + private HashMap currentSetupMap = null; static { *************** *** 31,54 **** } } ! /** * Jul 31, 2005 ! * @author pieter20 ! * This constructor creates and initializes the test db with the basic data that is need for ! * testing. It uses the DataLoaderUtil to initialize the db. */ public TestDBSetup() { ! //pvz: make this TestDBSetup a singleton DataLoaderUtil dataLoader = new DataLoaderUtil(); dataLoader.initializeDataIfNeeded(); } /** * @author pieter20 ! * Jul 31, 2005 ! * Creates a default test project. ! * @return */ ! public Project setupDefaultProject() { Project project = new Project(); --- 40,111 ---- } } ! /** * Jul 31, 2005 ! * ! * @author pieter20 This constructor creates and initializes the test db ! * with the basic data that is need for testing. It uses the ! * DataLoaderUtil to initialize the db. */ public TestDBSetup() { ! // pvz: make this TestDBSetup a singleton DataLoaderUtil dataLoader = new DataLoaderUtil(); dataLoader.initializeDataIfNeeded(); + currentSetupMap = setupBasics(); } + /** * @author pieter20 ! * Aug 5, 2005 ! * This method enforces/implements the Singleton Pattern for this class ! * @return the current instance or a new TestDBSetup instance */ ! public static TestDBSetup getInstance() { ! if (instance == null) { ! synchronized (TestDBSetup.class) { ! if (instance == null) { ! setInstance(new TestDBSetup()); ! ! } ! } ! } ! return instance; ! } ! ! /** ! * @author pieter20 ! * Aug 5, 2005 ! * used to set the static instance object ! * @param instance_ ! */ ! private static void setInstance(TestDBSetup instance_) { ! instance = instance_; ! } ! ! /** ! * @author pieter20 Jul 31, 2005 Create's a project that will be used ! * throughout the test ! * @return object of type Project ! */ ! private Project createProject() { ! Project project = setupDefaultProject(); ! try { ! HibernateUtil.saveOrUpdate(project); ! } catch (HibernateException e) { ! System.err.println("TestDBSetup.createProject()->Project not saved" ! + e); ! } ! ! CoefficientTestCase.assertNotNull(project); ! return project; ! } ! ! /** ! * @author pieter20 Jul 31, 2005 Creates a default test project. ! * The properties of the project is hardcoded. ! * @return the newly created project ! */ ! private Project setupDefaultProject() { Project project = new Project(); *************** *** 64,74 **** /** ! * @author pieter20 ! * Jul 31, 2005 ! * Creates a custom project with parameters passed through the context. * @param ctx * @return */ ! public Project setupProject(CoefficientContext ctx) { Project prj = new Project(); --- 121,130 ---- /** ! * @author pieter20 Jul 31, 2005 Creates a custom project with parameters ! * passed through the context. * @param ctx * @return */ ! private Project setupProject(CoefficientContext ctx) { Project prj = new Project(); *************** *** 98,100 **** --- 154,386 ---- } + /** + * @param requestMap + * TODO + * @param ctx + * TODO + * @param module + * TODO + * @author pieter20 Aug 3, 2005 Using the project module: Projects + * @return + */ + private Project createPublicProject(Map requestMap, CoefficientContext ctx, + String module) { + // TODO: Try to uses the project module later. Not using it now as it + // complicates the testing. Seperation of concerns. + // requestMap.put("shortName", "test"); + // requestMap.put("name", "Testing Project"); + // requestMap.put("homePage", "none"); + // requestMap.put("description", "This project is used to test"); + // requestMap.put("isPrivate", "off"); + // + // ArrayList mods = new ArrayList(); + // mods.add(module); + // requestMap.put("modules", mods); + // + // requestMap.put("module", "project"); + // requestMap.put("op", "createProject"); + // + // + // ctx = new CoefficientTestingContext(new HashMap(), requestMap, + // new HashMap()); + // + // Object result = null; + // // call method: + // try { + // result = invokeOpOnModule(ctx); + // } catch (Exception e) { + // System.err.println(e); + // } + // // checks if the user has been created + // assertNotNull(result); + return null; + } + + /** + * + * @author pieter20 Jul 31, 2005 Create a user for this test TODO: Should i + * sent through CoefficientTestingContext or the interface: + * CoefficientContext + * @param ctx + */ + private void createUser(CoefficientTestingContext ctx) { + HashMap requestMap = (HashMap) ctx.getRequestData(); + // set user + // this could be done by calling the module UserAdmin + requestMap.put("fullName", "Pieter van Zyl"); + requestMap.put("email", "pv...@cs..."); + requestMap.put("password1", "green"); + requestMap.put("userName", "pvzyl"); + requestMap.put("language", Locale.US); + requestMap.put("timeZone", TimeZone.getDefault()); + requestMap.put("active", new Boolean(true)); + // Note: systemRole can not be populated using the BeanUtils. + // We do not cater for the Role object in: + // BaseCoefficientContext.setProperties(.. + + requestMap.put("module", "UserAdmin"); + requestMap.put("op", "saveUser"); + + // this will force the user to be added to the context. + + ctx.setRequestData(requestMap); + + Object result = null; + // call method: + try { + result = CoefficientTestCase.invokeOpOnModule(ctx); + } catch (Exception e) { + System.err.println(e); + } + // checks if the user has been created + CoefficientTestCase.assertNotNull(result); + + } + + /** + * @author pieter20 Aug 5, 2005 Create a user by not using the UserAdmin + * @return a CoefficientUser object + */ + private CoefficientUser createUserObject() { + CoefficientUser user = new CoefficientUser(); + user.setFullName("Tester"); + user.setEmail("pv...@cs..."); + user.setPassword("test"); + user.setUserName("test"); + user.setLanguage(Locale.US); + user.setTimeZone(TimeZone.getDefault()); + user.setActive(true); + return user; + } + + /** + * @author pieter20 Jul 31, 2005 Associate a user with a project. The user + * is specified as a parameter in the requestMap. + * @param ctx + */ + private void addUserToProject(CoefficientTestingContext ctx) { + HashMap requestMap = (HashMap) ctx.getRequestData(); + // associate member/user with project. Use MemberAdmin module's method: + // addMember + requestMap.put("module", "MemberAdmin"); + requestMap.put("op", "addMember"); + + ctx.setRequestData(requestMap); + + Object result = null; + // call method: + try { + result = CoefficientTestCase.invokeOpOnModule(ctx); + } catch (Exception e) { + System.err.println(e); + } + // checks if the user/member has been associated with this project: + CoefficientTestCase.assertNotNull(result); + + } + + /** + * @author pieter20 + * Aug 5, 2005 + * The user needs to be logged in to be able to make calls in the + * system + * @param ctx + */ + private void loginUser(CoefficientTestingContext ctx) { + HashMap requestMap = (HashMap) ctx.getRequestData(); + // login using the new user + requestMap.put("username", "pvzyl"); + requestMap.put("password", "green"); + requestMap.put("module", "Security"); + requestMap.put("op", "login"); + + ctx.setRequestData(requestMap); + + Object result = null; + // call method: + try { + result = CoefficientTestCase.invokeOpOnModule(ctx); + } catch (Exception e) { + System.err.println(e); + } + // checks if the login was successfull + CoefficientTestCase.assertNotNull(result); + CoefficientTestCase.assertTrue(ctx.isError()); + + } + + /* + * + * + * Utility methods: + * + */ + + /** + * @author pieter20 + * Aug 5, 2005 + * Utility method to list all the projects created. + */ + private void listAllProjects() { + try { + List list = HibernateUtil.findAll(Project.class); + System.out + .println("TestDBSetup.listAllProjects()->list size od project list:" + + list.size()); + list = HibernateUtil.findAll(Role.class); + System.out + .println("TestDBSetup.listAllProjects()->list size of all roles in db:" + + list.size()); + + } catch (HibernateException e) { + System.err + .println("TestDBSetup.listAllProjects()->Could not retrieve any projects" + + e); + } + + } + + /** + * @author pieter20 + * Aug 5, 2005 + * This method initializes and creates the defualt testing project and user. + * It then adds the user as a member to the project and logins in the user. + * The stage is then set for testing + * @return a HashMap containing the CURRENT_PROJECT =projct + * and the CURRENT_CONTEXT = ctx + */ + private HashMap setupBasics() { + HashMap currentSetupMap = new HashMap(); + + // creates a default project without going through the project module. + Project testProject = createProject(); + currentSetupMap.put(CoefficientTestCase.CURRENT_PROJECT, testProject); + CoefficientTestingContext ctx = new CoefficientTestingContext( + new HashMap(), new HashMap(), new HashMap()); + ctx.setProject(testProject); + createUser(ctx); + // login user by calling tHe nodule or + // creating the coefficient user object on my side + loginUser(ctx); + + // associate member/user with project. Use MemberAdmin module's method: + // addMember: + addUserToProject(ctx); + currentSetupMap.put(CoefficientTestCase.CURRENT_CONTEXT, ctx); + return currentSetupMap; + + } + + + /** + * @author pieter20 + * Aug 5, 2005 + * This methods is the main method of obtaining the current state that has been created for testing. + * @return a HashMap containing the CURRENT_PROJECT =projct + * and the CURRENT_CONTEXT = ctx + */ + public HashMap getCurrentSetupMap() { + return currentSetupMap; + } + } |