01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tctest.dso;
06:
07: import com.meterware.httpunit.WebConversation;
08: import com.tc.test.server.appserver.deployment.AbstractTwoServerDeploymentTest;
09: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
10: import com.tc.test.server.appserver.deployment.WebApplicationServer;
11: import com.tc.test.server.util.TcConfigBuilder;
12: import com.tctest.webapp.servlets.RootCounterServlet;
13:
14: import java.util.Random;
15:
16: import junit.framework.Test;
17:
18: public class DsoRootTest extends AbstractTwoServerDeploymentTest {
19:
20: private static final int TOTAL_REQUEST_COUNT = 100;
21: private static final String CONTEXT = "DsoRootTest";
22: private static final String MAPPING = "count";
23:
24: public static Test suite() {
25: return new DsoRootTestSetup();
26: }
27:
28: protected boolean isSessionTest() {
29: return false;
30: }
31:
32: private int getCount(WebApplicationServer server,
33: WebConversation con) throws Exception {
34: return Integer.parseInt(server.ping(
35: "/" + CONTEXT + "/" + MAPPING, con).getText().trim());
36: }
37:
38: public void testRoot() throws Exception {
39: int nodeCount = 2;
40: WebConversation conversation = new WebConversation();
41: WebApplicationServer[] servers = new WebApplicationServer[] {
42: server0, server1 };
43:
44: Random random = new Random();
45: for (int i = 0, currentRequestCount = 0; i < TOTAL_REQUEST_COUNT
46: && currentRequestCount < TOTAL_REQUEST_COUNT; i++) {
47: int remainingRequests = TOTAL_REQUEST_COUNT
48: - currentRequestCount;
49: for (int j = 0; j < random.nextInt(remainingRequests + 1); j++) {
50: int newVal = getCount(servers[i % nodeCount],
51: conversation);
52: currentRequestCount++;
53: assertEquals(currentRequestCount, newVal);
54: }
55: }
56: }
57:
58: private static class DsoRootTestSetup extends TwoServerTestSetup {
59:
60: public DsoRootTestSetup() {
61: super (DsoRootTest.class, CONTEXT);
62: }
63:
64: protected void configureWar(DeploymentBuilder builder) {
65: builder.addServlet("RootCounterServlet", "/" + MAPPING
66: + "/*", RootCounterServlet.class, null, false);
67: }
68:
69: protected void configureTcConfig(TcConfigBuilder tcConfigBuilder) {
70: String rootName = "counterObject";
71: String fieldName = RootCounterServlet.class.getName()
72: + ".counterObject";
73: tcConfigBuilder.addRoot(fieldName, rootName);
74:
75: String methodExpression = "* "
76: + RootCounterServlet.class.getName()
77: + "$Counter.*(..)";
78: tcConfigBuilder.addAutoLock(methodExpression, "write");
79:
80: tcConfigBuilder.addInstrumentedClass(
81: RootCounterServlet.class.getName() + "$Counter",
82: false);
83: }
84:
85: }
86: }
|