summaryrefslogtreecommitdiff
path: root/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/share/JDWPUnitDebuggeeWrapper.java
blob: 629fb4ef412eaa1ca8860f017cee9508c24dd359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @author Vitaly A. Provodin
 */

/**
 * Created on 29.01.2005
 */
package org.apache.harmony.jpda.tests.jdwp.share;

import java.io.IOException;

import org.apache.harmony.jpda.tests.framework.LogWriter;
import org.apache.harmony.jpda.tests.framework.TestErrorException;
import org.apache.harmony.jpda.tests.framework.jdwp.TransportWrapper;
import org.apache.harmony.jpda.tests.share.JPDATestOptions;

/**
 * This class provides DebuggeeWrapper implementation based on JUnit framework.
 * Debuggee is always launched on local machine and attaches to debugger.
 */
public class JDWPUnitDebuggeeWrapper extends JDWPUnitDebuggeeProcessWrapper {

    /**
     * Auxiliary options passed to the target VM on its launch.
     */
    public String savedVMOptions = null;

    /**
     * Wrapper around JDWP transport connection.
     */
    protected TransportWrapper transport;

    /**
     * JDWP transport address.
     */
    protected String address;

    /**
     * Is this a "listen" JDWP connection? (If false, it is a an "attach" connection.)
     */
    boolean isListenConnection;


    /**
     * Creates new instance with given data.
     *
     * @param settings
     *            test run options
     * @param logWriter
     *            where to print log messages
     */
    public JDWPUnitDebuggeeWrapper(JPDATestOptions settings, LogWriter logWriter) {
        super(settings, logWriter);
    }

    /**
     * Set up server side JDWP connection before launching the debuggee.
     */
    public void setUpConnection() {
        isListenConnection = settings.isListenConnectorKind();
        transport = createTransportWrapper();
        address = settings.getTransportAddress();

        if (isListenConnection) {
            logWriter.println("Start listening on: " + address);
            try {
                address = transport.startListening(address);
            } catch (IOException e) {
                throw new TestErrorException(e);
            }
            logWriter.println("Listening on: " + address);
        } else {
            logWriter.println("Attach to: " + address);
        }
    }

    /**
     * Launches new debuggee process according to test run options and
     * establishes JDWP connection.
     */
    public void start() {
        String cmdLine = settings.getDebuggeeJavaPath() + " -cp \""
                + settings.getDebuggeeClassPath() + "\" -agentlib:"
                + settings.getDebuggeeAgentName() + "="
                + settings.getDebuggeeAgentOptions(address, isListenConnection)
                + " " + settings.getDebuggeeVMExtraOptions() + " "
                + (savedVMOptions != null ? savedVMOptions : "") + " "
                + settings.getDebuggeeClassName();

        logWriter.println("Launch: " + cmdLine);

        try {
            launchProcessAndRedirectors(cmdLine);
            logWriter.println("Launched debuggee process");
            openConnection();
            logWriter.println("Established transport connection");
        } catch (Exception e) {
            throw new TestErrorException(e);
        }
    }

    /**
     * Closes all connections, stops redirectors, and waits for debuggee process
     * exit for default timeout.
     */
    public void stop() {
        disposeConnection();

        try {
            finishProcessAndRedirectors();
        } finally {
            // If the test has failed (e.g. a TestErrorException is
            // thrown), make sure that the transport server socket (if
            // any) is closed before leaving, as we may otherwise
            // block the transport port for subsequent JDWP tests.
            tearDownConnection();
        }
    }

    /**
     * Opens connection with debuggee.
     */
    protected void openConnection() {
        try {
            if (settings.isListenConnectorKind()) {
                logWriter.println("Accepting JDWP connection");
                transport.accept(settings.getTimeout(), settings.getTimeout());
            } else {
                String address = settings.getTransportAddress();
                logWriter.println("Attaching for JDWP connection");
                transport.attach(address, settings.getTimeout(), settings.getTimeout());
            }
            setConnection(transport);
        } catch (IOException e) {
            logWriter.printError(e);
            throw new TestErrorException(e);
        }
    }

    /**
     * Disposes JDWP connection stored in VmMirror.
     */
    protected void disposeConnection() {
        if (vmMirror != null) {
            try {
                vmMirror.dispose();
            } catch (Exception e) {
                logWriter.println("Ignoring exception in disposing debuggee VM: " + e);
            }
        }
    }

    /**
     * Closes JDWP connection stored in VmMirror.
     */
    protected void closeConnection() {
        if (vmMirror != null) {
            try {
                vmMirror.closeConnection();
            } catch (IOException e) {
                logWriter.println("Ignoring exception in closing connection: " + e);
            }
        }
    }

    /**
     * Closes JDWP connection and for listen connectors, stops listening to transport.
     */
    private void tearDownConnection() {
        closeConnection();
        if (settings.isListenConnectorKind()) {
            try {
                transport.stopListening();
            } catch (IOException e) {
                logWriter.println("IOException in transport listening stopping: " + e);
            }
        }
    }
}