TRSWCMBaseTest.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. *Created: 2005-3-31 9:30:42
  3. * Last Modified: 2005-3-31/2005-3-31
  4. * Description:
  5. * class AbstractTest.java
  6. */
  7. package com.trs;
  8. import java.io.File;
  9. import java.util.ArrayList;
  10. import java.util.Properties;
  11. import junit.framework.TestCase;
  12. import org.apache.log4j.Logger;
  13. import com.trs.cms.ContextHelper;
  14. import com.trs.cms.auth.persistent.Group;
  15. import com.trs.cms.auth.persistent.Groups;
  16. import com.trs.cms.auth.persistent.User;
  17. import com.trs.components.wcm.content.persistent.Channel;
  18. import com.trs.components.wcm.content.persistent.WebSite;
  19. import com.trs.infra.ServerControl;
  20. import com.trs.infra.common.WCMException;
  21. import com.trs.infra.config.domain.ConfigHelper;
  22. import com.trs.infra.util.CMyFile;
  23. import com.trs.infra.util.WCMDiscoveryFactory;
  24. import com.trs.infra.util.store.FileService;
  25. import com.trs.infra.util.store.FileStore;
  26. /**
  27. * Title: TRS 内容协作平台(TRS WCM) <BR>
  28. * Description: <BR>
  29. * 测试用例的父类 <BR>
  30. * Copyright: Copyright (c) 2004-2005 TRS信息技术有限公司 <BR>
  31. * Company: TRS信息技术有限公司(www.trs.com.cn) <BR>
  32. *
  33. * @author TRS信息技术有限公司
  34. * @version 1.0
  35. */
  36. public class TRSWCMBaseTest extends TestCase {
  37. private Logger s_logger = Logger.getLogger(TRSWCMBaseTest.class);
  38. protected User m_oLoginUser = null;
  39. protected String m_sCurrUserName = "admin";
  40. public TRSWCMBaseTest(String _sName) {
  41. super(_sName);
  42. }
  43. public TRSWCMBaseTest() {
  44. super();
  45. }
  46. private static String getWCMLibs() {
  47. Properties sysProperties = System.getProperties();
  48. String[] pClassPaths = sysProperties.getProperty("java.class.path")
  49. .split(";");
  50. for (int i = 0; i < pClassPaths.length; i++) {
  51. String sClassPath = pClassPaths[i];
  52. File file = new File(sClassPath);
  53. if (file.isFile() && (sClassPath.indexOf("trswcmv6.jar") >= 0)) {
  54. return CMyFile.extractFilePath(file.getAbsolutePath());
  55. }
  56. }
  57. return null;
  58. }
  59. /*
  60. * (non-Javadoc)
  61. *
  62. * @see junit.framework.TestCase#setUp()
  63. */
  64. protected void setUp() throws Exception {
  65. super.setUp();
  66. // 设置当前在Eclipse中运行
  67. ConfigHelper.RUN_IN_ECLISPE = true;
  68. ArrayList arClassPaths = ConfigHelper.getClassPaths();
  69. // File[] pClassesDirs = TestHelper.getClassFilePaths();
  70. FileStore[] pClassesDirs = new FileService[arClassPaths.size()];
  71. for (int i = 0; i < pClassesDirs.length; i++) {
  72. pClassesDirs[i] = new FileService((String) arClassPaths.get(i));
  73. }
  74. FileService fileWCMLib = new FileService("./libs/WCM/");
  75. String sExtLibPath = getWCMLibs();
  76. System.out.println("sExtLibPath:" + sExtLibPath);
  77. if (sExtLibPath != null) {
  78. fileWCMLib = new FileService(sExtLibPath);
  79. }
  80. // System.out.println("the lib paht exists:" + fileWCMLib.exists());
  81. FileService[] pLibDirs = { fileWCMLib };
  82. WCMDiscoveryFactory.initDiscovery(pLibDirs, pClassesDirs);
  83. ServerControl control = ServerControl.createInstance();
  84. control.startup();
  85. // wenyh@2005-7-7 10:41:41 add comment:modify:for ant test...
  86. m_oLoginUser = User.findByName(m_sCurrUserName);
  87. ContextHelper.initContext(m_oLoginUser);
  88. }
  89. /*
  90. * (non-Javadoc)
  91. *
  92. * @see junit.framework.TestCase#tearDown()
  93. */
  94. protected void tearDown() throws Exception {
  95. super.tearDown();
  96. ServerControl control = ServerControl.createInstance();
  97. control.shutdown();
  98. }
  99. protected void descTest(String _sTestDesc) {
  100. this.setName(_sTestDesc);
  101. s_logger.info(_sTestDesc);
  102. }
  103. protected Channel getChannelById(int _nChannelId) {
  104. Channel aChannel = null;
  105. try {
  106. aChannel = Channel.findById(_nChannelId);
  107. } catch (Exception ex) {
  108. s_logger.error("测试[getChannelById]出现异常!", ex);
  109. fail("测试[getChannelById]出现异常!");
  110. }
  111. if (aChannel == null) {
  112. s_logger.error("[getChannelById]所需的测试数据不正确!ChannelId:["
  113. + _nChannelId + "]");
  114. fail("[getChannelById]所需的测试数据不正确!ChannelId:[" + _nChannelId + "]");
  115. }
  116. return aChannel;
  117. }
  118. /**
  119. * @param _nSiteId
  120. * @return
  121. * @throws WCMException
  122. */
  123. protected WebSite findSiteById(int _nSiteId) throws WCMException {
  124. WebSite currSite = WebSite.findById(_nSiteId);
  125. if (currSite == null)
  126. fail("指定的Site[" + currSite + "]不存在!");
  127. return currSite;
  128. }
  129. /**
  130. * @param _nUserId
  131. * @return
  132. * @throws WCMException
  133. */
  134. protected User findUserById(int _nUserId) throws WCMException {
  135. User currUser = User.findById(_nUserId);
  136. if (currUser == null)
  137. fail("指定的User[" + _nUserId + "]不存在!");
  138. return currUser;
  139. }
  140. protected Group findGroup(User _user) throws WCMException {
  141. Groups groups = _user.getGroups();
  142. Group group = null;
  143. for (int i = 0, nSize = groups.size(); i < nSize; i++) {
  144. group = (Group) groups.getAt(i);
  145. if (group == null)
  146. continue;
  147. break;
  148. }
  149. if (group == null) {
  150. fail("用户[" + _user.getId() + "]没有所属组织!");
  151. }
  152. return group;
  153. }
  154. }