KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > dso > DsoRootTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.dso;
5
6 import org.apache.commons.httpclient.HttpClient;
7
8 import com.tc.object.config.schema.AutoLock;
9 import com.tc.object.config.schema.LockLevel;
10 import com.tc.object.config.schema.Root;
11 import com.tc.test.server.appserver.unit.AbstractAppServerTestCase;
12 import com.tc.test.server.util.HttpUtil;
13
14 import java.io.IOException JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Random JavaDoc;
20
21 import javax.servlet.http.HttpServlet JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 public class DsoRootTest extends AbstractAppServerTestCase {
26   private static final int TOTAL_REQUEST_COUNT = 100;
27
28   protected boolean isSessionTest() {
29     return false;
30   }
31
32   public void testRoot() throws Throwable JavaDoc {
33     List JavaDoc roots = new ArrayList JavaDoc();
34     String JavaDoc rootName = "counterObject";
35     String JavaDoc fieldName = RootCounterServlet.class.getName() + ".counterObject";
36     roots.add(new Root(rootName, fieldName));
37     addRoots(roots);
38
39     List JavaDoc locks = new ArrayList JavaDoc();
40     LockLevel lockLevel = LockLevel.WRITE;
41     String JavaDoc methodExpression = "* " + RootCounterServlet.class.getName() + "$Counter.*(..)";
42     locks.add(new AutoLock(methodExpression, lockLevel));
43     addLocks(locks);
44
45     startDsoServer();
46     runNodes(2);
47   }
48
49   private void runNodes(int nodeCount) throws Throwable JavaDoc {
50     HttpClient client = HttpUtil.createHttpClient();
51
52     int[] ports = new int[nodeCount];
53     URL JavaDoc[] urls = new URL JavaDoc[nodeCount];
54
55     for (int i = 0; i < nodeCount; i++) {
56       ports[i] = startAppServer(true).serverPort();
57       urls[i] = createUrl(ports[i], RootCounterServlet.class);
58     }
59
60     Random JavaDoc random = new Random JavaDoc();
61     for (int i = 0, currentRequestCount = 0; i < TOTAL_REQUEST_COUNT && currentRequestCount < TOTAL_REQUEST_COUNT; i++) {
62       int remainingRequests = TOTAL_REQUEST_COUNT - currentRequestCount;
63       for (int j = 0; j < random.nextInt(remainingRequests + 1); j++) {
64         int newVal = HttpUtil.getInt(urls[i % nodeCount], client);
65         currentRequestCount++;
66         assertEquals(currentRequestCount, newVal);
67       }
68     }
69   }
70
71   public static class RootCounterServlet extends HttpServlet JavaDoc {
72     private final Counter counterObject = new Counter();
73
74     private static class Counter {
75       private int counter;
76
77       public Counter() {
78         counter = 0;
79       }
80
81       public synchronized void increment() {
82         counter++;
83       }
84
85       public synchronized void setValue(int newValue) {
86         counter = newValue;
87       }
88
89       public synchronized int getValue() {
90         return counter;
91       }
92     }
93
94     private int getCurrentCountValue() {
95       counterObject.increment();
96       return counterObject.getValue();
97     }
98
99     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
100       response.setContentType("text/html");
101       PrintWriter JavaDoc out = response.getWriter();
102       out.println(getCurrentCountValue());
103     }
104   }
105 }
106
Popular Tags