View Javadoc
1   /*
2    * Copyright (C) 2003-2009 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.wiki.mow.core.api;
18  
19  import javax.jcr.Node;
20  import javax.jcr.NodeIterator;
21  import javax.jcr.Session;
22  import javax.jcr.query.Query;
23  import javax.jcr.query.QueryManager;
24  import javax.jcr.query.QueryResult;
25  
26  import junit.framework.TestCase;
27  
28  import org.exoplatform.container.StandaloneContainer;
29  import org.exoplatform.container.component.RequestLifeCycle;
30  import org.exoplatform.services.jcr.RepositoryService;
31  import org.exoplatform.services.security.ConversationState;
32  import org.exoplatform.services.security.Identity;
33  import org.exoplatform.services.security.IdentityConstants;
34  import org.exoplatform.wiki.WikiException;
35  import org.exoplatform.wiki.mow.core.api.wiki.Model;
36  import org.exoplatform.wiki.mow.api.WikiType;
37  import org.exoplatform.wiki.mow.core.api.wiki.GroupWiki;
38  import org.exoplatform.wiki.mow.core.api.wiki.PortalWiki;
39  import org.exoplatform.wiki.mow.core.api.wiki.UserWiki;
40  import org.exoplatform.wiki.mow.core.api.wiki.WikiContainer;
41  import org.exoplatform.wiki.mow.core.api.wiki.WikiHome;
42  import org.exoplatform.wiki.mow.core.api.wiki.WikiImpl;
43  
44  /**
45   * @version $Revision$
46   */
47  public abstract class AbstractMOWTestcase extends TestCase {
48  
49    protected static RepositoryService    repositoryService;
50  
51    protected static StandaloneContainer  container;
52  
53    protected final static String         WIKI_WS           = "collaboration".intern();
54  
55    protected static Node                 root_                  = null;
56  
57    protected static MOWService          mowService;
58  
59    boolean syncStarted;
60  
61    protected void begin() {
62      initContainer();
63  
64      RequestLifeCycle.begin(container);
65    }
66  
67    protected void end() {
68      RequestLifeCycle.end();
69  
70      // TODO stopping or disposing the container does not delete data. We should find a way to do it after each test to make sure tests are really independent.
71      //stopContainer();
72    }
73  
74    protected void setUp() throws Exception {
75      begin();
76      Identity systemIdentity = new Identity(IdentityConstants.SYSTEM);
77      ConversationState.setCurrent(new ConversationState(systemIdentity));
78      System.setProperty("gatein.email.domain.url", "localhost");
79    }
80  
81    protected void tearDown() throws Exception {
82      end();
83    }
84  
85    private void initContainer() {
86      try {
87        String containerConf = Thread.currentThread().getContextClassLoader().getResource("conf/standalone/configuration.xml").toString();
88        StandaloneContainer.addConfigurationURL(containerConf);
89        //
90        String loginConf = Thread.currentThread().getContextClassLoader().getResource("conf/standalone/login.conf").toString();
91        System.setProperty("java.security.auth.login.config", loginConf);
92        //System.setProperty("gatein.data.dir", Files.createTempDirectory("wiki-data", null).getFileName().toString());
93        //
94        container = StandaloneContainer.getInstance();
95  
96        mowService = container.getComponentInstanceOfType(MOWService.class);
97      } catch (Exception e) {
98        throw new RuntimeException("Failed to initialize standalone container: " + e.getMessage(), e);
99      }
100   }
101 
102   private void stopContainer() {
103     try {
104       container = StandaloneContainer.getInstance();
105       container.dispose();
106     } catch (Exception e) {
107       throw new RuntimeException("Failed to stop standalone container: " + e.getMessage(), e);
108     }
109   }
110 
111   private static void initJCR() {
112     try {
113       repositoryService = (RepositoryService) container.getComponentInstanceOfType(RepositoryService.class);
114       // Initialize datas
115       Session session = repositoryService.getCurrentRepository().getSystemSession(WIKI_WS);
116       root_ = session.getRootNode();
117       // Remove old data before to starting test case.
118       StringBuffer stringBuffer = new StringBuffer();
119       stringBuffer.append("/jcr:root").append("//*[fn:name() = 'eXoWiki' or fn:name() = 'ApplicationData']");
120       QueryManager qm = session.getWorkspace().getQueryManager();
121       Query query = qm.createQuery(stringBuffer.toString(), Query.XPATH);
122       QueryResult result = query.execute();
123       NodeIterator iter = result.getNodes();
124       while (iter.hasNext()) {
125         Node node = iter.nextNode();
126         try {
127           removeNodes(node);
128         } catch (Exception e) {}
129       }
130       session.save();
131     } catch (Exception e) {
132       throw new RuntimeException("Failed to initialize JCR: ", e);
133     }
134   }
135   
136   private static void removeNodes(Node node) throws Exception {
137     NodeIterator iter = node.getNodes();
138     while (iter.hasNext()) {
139       iter.nextNode().remove();
140     }
141   }
142   
143   protected WikiImpl getWiki(WikiType wikiType, String wikiName, Model model) throws WikiException {
144     WikiStoreImpl wStore = (WikiStoreImpl) mowService.getWikiStore();
145     WikiImpl wiki = null;
146     switch (wikiType) {
147       case PORTAL:
148         WikiContainer<PortalWiki> portalWikiContainer = wStore.getWikiContainer(WikiType.PORTAL);
149         wiki = portalWikiContainer.getWiki(wikiName);
150         break;
151       case GROUP:
152         WikiContainer<GroupWiki> groupWikiContainer = wStore.getWikiContainer(WikiType.GROUP);
153         wiki = groupWikiContainer.getWiki(wikiName);
154         break;
155       case USER:
156         WikiContainer<UserWiki> userWikiContainer = wStore.getWikiContainer(WikiType.USER);
157         wiki = userWikiContainer.getWiki(wikiName);
158         break;
159     }
160     mowService.persist();
161     return wiki;
162   }
163   
164   protected WikiHome getWikiHomeOfWiki(WikiType wikiType, String wikiName, Model model) throws WikiException {
165     WikiHome wikiHomePage = getWiki(wikiType, wikiName, model).getWikiHome();
166     return wikiHomePage;
167   }
168   
169   protected void startSessionAs(String user) {
170     Identity userIdentity = new Identity(user);
171     ConversationState.setCurrent(new ConversationState(userIdentity));
172   }
173   
174 }