View Javadoc

1   /*
2    * $Id: EventTest.java 20371 2010-11-28 18:39:14Z mike.schilling $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.module.atom.event;
11  
12  import org.mule.tck.FunctionalTestCase;
13  
14  import javax.jcr.Node;
15  import javax.jcr.NodeIterator;
16  import javax.jcr.PathNotFoundException;
17  import javax.jcr.Repository;
18  import javax.jcr.Session;
19  import javax.jcr.SimpleCredentials;
20  
21  import org.apache.abdera.model.Document;
22  import org.apache.abdera.model.Feed;
23  import org.apache.abdera.protocol.client.AbderaClient;
24  import org.apache.abdera.protocol.client.ClientResponse;
25  
26  public class EventTest extends FunctionalTestCase
27  {
28  
29      private Repository repository;
30  
31      @Override
32      protected String getConfigResources()
33      {
34          return "eventqueue-conf.xml";
35      }
36  
37      public void testCustomerProvider() throws Exception
38      {
39          repository = (Repository) muleContext.getRegistry().lookupObject("jcrRepository");
40  
41          Thread.sleep(5000);
42  
43          AbderaClient client = new AbderaClient();
44          ClientResponse res = client.get("http://localhost:9002/events");
45  
46          Document<Feed> doc = res.getDocument();
47          Feed feed = doc.getRoot();
48  
49          assertTrue( feed.getEntries().size() >= 1);
50          EntryReceiver component = (EntryReceiver)getComponent("eventConsumer");
51  
52          assertTrue(component.getCount() > 0);
53      }
54  
55      @Override
56      protected void doSetUp() throws Exception
57      {
58          super.doSetUp();
59          EntryReceiver component = (EntryReceiver)getComponent("eventConsumer");
60          component.getReceivedEntries().set(0);
61      }
62  
63      @Override
64      protected void doTearDown() throws Exception
65      {
66          clearJcrRepository();
67  
68          super.doTearDown();
69      }
70  
71      private void clearJcrRepository()
72      {
73          try
74          {
75              if (repository == null)
76              {
77                  return;
78              }
79              Session session = repository.login(new SimpleCredentials("username", "password".toCharArray()));
80  
81              Node node = session.getRootNode();
82  
83              for (NodeIterator itr = node.getNodes(); itr.hasNext();)
84              {
85                  Node child = itr.nextNode();
86                  if (!child.getName().equals("jcr:system"))
87                  {
88                      child.remove();
89                  }
90              }
91              session.save();
92              session.logout();
93          }
94          catch (PathNotFoundException t)
95          {
96          }
97          catch (Throwable t)
98          {
99              t.printStackTrace();
100         }
101     }
102 
103 }