KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > test > cms > stress > ConcurrentTestCase


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.test.cms.stress;
10
11 import junit.framework.TestCase;
12 import EDU.oswego.cs.dl.util.concurrent.Executor;
13 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
14 import org.jboss.portal.cms.CMS;
15 import org.jboss.portal.cms.NodeFactory;
16 import org.w3c.dom.Document JavaDoc;
17 import org.w3c.dom.Element JavaDoc;
18 import org.apache.log4j.Logger;
19 import org.apache.log4j.Appender;
20 import org.apache.log4j.ConsoleAppender;
21 import org.apache.log4j.SimpleLayout;
22
23 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 /**
29  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
30  * @version $Revision: 1.1 $
31  */

32 public class ConcurrentTestCase extends TestCase
33 {
34
35    Logger log = Logger.getLogger(ConcurrentTestCase.class);
36
37    static
38    {
39       Logger root = Logger.getRootLogger();
40       Appender appender = new ConsoleAppender(new SimpleLayout());
41       root.addAppender(appender);
42    }
43
44    public ConcurrentTestCase(String JavaDoc name)
45    {
46       super(name);
47    }
48
49    private CMS cms;
50    private NodeFactory factory;
51
52    private File JavaDoc tmp;
53
54    protected void setUp() throws Exception JavaDoc
55    {
56       tmp = File.createTempFile("cms", ".tmp");
57       if (!tmp.delete())
58       {
59          throw new Exception JavaDoc("Cannot delete tmp file");
60       }
61       if (!tmp.mkdirs())
62       {
63          throw new Exception JavaDoc("Cannot create dir");
64       }
65
66       System.setProperty("jboss.server.data.dir", tmp.getAbsolutePath());
67
68       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
69       InputStream JavaDoc in = loader.getResourceAsStream("test/domain2.xml");
70       Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
71       Element JavaDoc elt = doc.getDocumentElement();
72       cms = new CMS();
73       cms.setConfig(elt);
74       cms.create();
75       cms.start();
76
77       factory = cms.getNodeFactory();
78    }
79
80    protected void tearDown() throws Exception JavaDoc
81    {
82       cms.stop();
83       cms.destroy();
84    }
85
86    private String JavaDoc[] alphabet = {"client0","client1","client2","client3","client4"};
87
88    public void testA() throws Exception JavaDoc
89    {
90       int size = 4;
91
92       Command[][] scenarios = new Command[size][];
93       for (int i = 0;i < scenarios.length;i++)
94       {
95          scenarios[i] = new Command[]
96          {
97             new BeginTxCommand(),
98             new CreateDirCommand("/" + alphabet[i] + "/a"),
99             new CreateContentCommand("/" + alphabet[i] + "/a/b", "abc" + i),
100             new EndTxCommand(true),
101             new SleepCommand("50"),
102             new BeginTxCommand(),
103             new TestContentCommand("/" + alphabet[i] + "/a/b", "abc" + i),
104             new TestDirCommand("/" + alphabet[i] + "/a"),
105             new DeleteCommand("/" + alphabet[i] + "/a"),
106             new EndTxCommand(true),
107             new SleepCommand("50")
108          };
109       }
110
111       Client[] clients = new Client[size];
112       for (int i = 0; i < clients.length; i++)
113       {
114          clients[i] = new Client(i, 10, scenarios[i], 200, factory);
115       }
116
117       Thread JavaDoc[] threads = new Thread JavaDoc[size];
118
119       for (int i = 0; i < clients.length; i++)
120       {
121          Client client = clients[i];
122          threads[i] = new Thread JavaDoc(client, "CMS-" + i);
123          log.info("created " + i);
124          threads[i].start();
125          log.info("started " + i);
126          // pool.execute(client);
127
}
128
129
130       // Wait until all test have completed
131
// pool.shutdownAfterProcessingCurrentlyQueuedTasks();
132
// pool.awaitTerminationAfterShutdown();
133
for (int i = 0; i < threads.length; i++)
134       {
135          Thread JavaDoc thread = threads[i];
136          log.info("waiting " + i);
137          thread.join();
138          log.info("joined " + i);
139       }
140
141       for (int k = 0; k < clients.length; k++)
142       {
143          Client client = clients[k];
144          for (Iterator JavaDoc i = client.getResults().iterator(); i.hasNext();)
145          {
146             Client.Result result = (Client.Result)i.next();
147             for (Iterator JavaDoc j = result.getFailures().iterator(); j.hasNext();)
148             {
149                Failure failure = (Failure)j.next();
150                System.out.println("failure = " + failure);
151             }
152          }
153       }
154    }
155
156
157 }
158
Popular Tags