1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *
15 *  See the License for the specific language governing permissions and
16 *  limitations under the License.
17 */
18
19/**
20 * @author Anton V. Karnachuk
21 */
22
23/**
24 * Created on 06.04.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.Events;
27
28import org.apache.harmony.jpda.tests.framework.TestErrorException;
29import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
30import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
31import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
32import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
33import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
34
35/**
36 * JDWP Unit test for CLASS_PREPARE event.
37 */
38public class ClassPrepareTest extends JDWPEventTestCase {
39
40    protected String getDebuggeeClassName() {
41        return ClassPrepareDebuggee.class.getName();
42    }
43
44    /**
45     * This testcase is for CLASS_PREPARE event.
46     * <BR>It runs ClassPrepareDebuggee to load Class2Prepare class
47     * and verify that requested CLASS_PREPARE event occurs.
48     */
49    public void testClassPrepareEvent() {
50        logWriter.println("testClassPrepareEvent started");
51
52        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
53
54        String class2prepareRegexp = "org.apache.harmony.jpda.tests.jdwp.Events.Class2Prepare";
55        String class2prepareSignature = "Lorg/apache/harmony/jpda/tests/jdwp/Events/Class2Prepare;";
56
57        ReplyPacket reply = debuggeeWrapper.vmMirror.setClassPrepared(class2prepareRegexp);
58        checkReplyPacket(reply, "Set CLASS_PREPARE event");
59
60        // start loading Class2Prepare class
61        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
62
63        CommandPacket event = debuggeeWrapper.vmMirror.receiveEvent();
64        ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(event);
65
66        assertEquals("Invalid number of events,", 1, parsedEvents.length);
67        assertEquals("Invalid event kind,", JDWPConstants.EventKind.CLASS_PREPARE, parsedEvents[0].getEventKind()
68                , JDWPConstants.EventKind.getName(JDWPConstants.EventKind.CLASS_PREPARE)
69                , JDWPConstants.EventKind.getName(parsedEvents[0].getEventKind()));
70        assertEquals("Invalid signature of prepared class,", class2prepareSignature
71                , ((ParsedEvent.Event_CLASS_PREPARE)parsedEvents[0]).getSignature());
72
73        //logWriter.println("parsedEvent="+parsedEvents[0].getClass().getCanonicalName());
74        //logWriter.println(((ParsedEvent.Event_CLASS_PREPARE)parsedEvents[0]).getSignature());
75    }
76
77    /*
78     * This testcase is for CLASS_PREPARE event.
79     * <BR>It runs ClassPrepareDebuggee to load Class2Prepare class
80     * and verify that requested CLASS_PREPARE event occurs in case of SourceNameMatch.
81     * Class2Prepare doesn't contain source name field in SourceDebugExtension attribute.
82     * expectedSourceNamePattern is used to assign the source name's pattern
83     */
84    public void testClassPrepareEventWithoutSourceDebugExtension(String expectedSourceNamePattern){
85        debuggeeWrapper.vmMirror.capabilities();
86        if (!debuggeeWrapper.vmMirror.targetVMCapabilities.canUseSourceNameFilters) {
87            logWriter.println("##WARNING: this VM doesn't possess capability: canUseSourceNameFilters");
88            return;
89        }
90
91        String expectedClassSignature = "Lorg/apache/harmony/jpda/tests/jdwp/Events/Class2Prepare;";
92        logWriter.println("==> testClassPrepareEventForSourceNameMatch started");
93        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
94
95        // Set ClassPrepare Event in case of SourceNameMatch
96        ReplyPacket reply = debuggeeWrapper.vmMirror.setClassPreparedForSourceNameMatch(expectedSourceNamePattern);
97        checkReplyPacket(reply, "Set CLASS_PREPARE event");
98
99        // start loading SourceDebugExtensionMockClass class
100        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
101
102        CommandPacket receiveEvent = debuggeeWrapper.vmMirror.receiveEvent();
103        ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(receiveEvent);
104
105        assertEquals("Invalid number of events,", 1, parsedEvents.length);
106        assertEquals("Invalid event kind,", JDWPConstants.EventKind.CLASS_PREPARE, parsedEvents[0].getEventKind()
107                , JDWPConstants.EventKind.getName(JDWPConstants.EventKind.CLASS_PREPARE)
108                , JDWPConstants.EventKind.getName(parsedEvents[0].getEventKind()));
109        assertEquals("Invalid signature of prepared class,", expectedClassSignature, ((ParsedEvent.Event_CLASS_PREPARE)parsedEvents[0]).getSignature());
110    }
111
112    /**
113     * Test ClassPrepareEvent without SourceDebugExtension attribute
114     * matching exactly.
115     */
116    public void testClassPrepareEventWithoutSourceDebugExtension001(){
117        String expectedSourceNamePattern = "Class2Prepare.java";
118        testClassPrepareEventWithoutSourceDebugExtension(expectedSourceNamePattern);
119
120    }
121
122    /**
123     * Test ClassPrepareEvent without SourceDebugExtension attribute
124     * matching the former part.
125     */
126    public void testClassPrepareEventWithoutSourceDebugExtension002(){
127        String expectedSourceNamePattern = "*Class2Prepare.java";
128        testClassPrepareEventWithoutSourceDebugExtension(expectedSourceNamePattern);
129    }
130
131    /**
132     * Test ClassPrepareEvent without SourceDebugExtension attribute
133     * matching the latter part.
134     */
135    public void testClassPrepareEventWithoutSourceDebugExtension003(){
136        String expectedSourceNamePattern = "Class2Prepare.*";
137        testClassPrepareEventWithoutSourceDebugExtension(expectedSourceNamePattern);
138    }
139
140    /*
141     * This testcase is for CLASS_PREPARE event.
142     * <BR>It runs ClassPrepareDebuggee to load SourceDebugExtensionMockClass
143     * and verify that requested CLASS_PREPARE event occurs in case of SourceNameMatch.
144     * SourceDebugExtensionMockClass contains source name field in SourceDebugExtension attribute.
145     * expectedSourceNamePattern is used to assign the source name's pattern
146     */
147    private void testClassPrepareEventWithSourceDebugExtension(String expectedSourceNamePattern){
148        debuggeeWrapper.vmMirror.capabilities();
149        if (!debuggeeWrapper.vmMirror.targetVMCapabilities.canUseSourceNameFilters) {
150            logWriter.println("##WARNING: this VM doesn't possess capability: canUseSourceNameFilters");
151            return;
152        }
153
154        String expectedClassSignature = "Lorg/apache/harmony/jpda/tests/jdwp/Events/SourceDebugExtensionMockClass;";
155
156        logWriter.println("==> testClassPrepareEventForSourceNameMatch started");
157        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
158
159        // Set ClassPrepare Event in case of SourceNameMatch
160        ReplyPacket reply = debuggeeWrapper.vmMirror.setClassPreparedForSourceNameMatch(expectedSourceNamePattern);
161        checkReplyPacket(reply, "Set CLASS_PREPARE event");
162
163        // start loading SourceDebugExtensionMockClass class
164        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
165
166        CommandPacket receiveEvent = null;
167        try {
168            receiveEvent = debuggeeWrapper.vmMirror.receiveEvent();
169        } catch (TestErrorException e) {
170            printErrorAndFail("There is no event received.");
171        }
172        ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(receiveEvent);
173
174        assertEquals("Invalid number of events,", 1, parsedEvents.length);
175        assertEquals("Invalid event kind,", JDWPConstants.EventKind.CLASS_PREPARE, parsedEvents[0].getEventKind()
176                , JDWPConstants.EventKind.getName(JDWPConstants.EventKind.CLASS_PREPARE)
177                , JDWPConstants.EventKind.getName(parsedEvents[0].getEventKind()));
178        assertEquals("Invalid signature of prepared class,", expectedClassSignature, ((ParsedEvent.Event_CLASS_PREPARE)parsedEvents[0]).getSignature());
179    }
180
181    /**
182     * Test ClassPrepareEvent with SourceDebugExtension attribute.
183     * matching exactly.
184     */
185    public void testClassPrepareEventWithSourceDebugExtension001(){
186        String expectedSourceNamePattern = "helloworld_jsp.java";
187        testClassPrepareEventWithSourceDebugExtension(expectedSourceNamePattern);
188    }
189
190    /**
191     * Test ClassPrepareEvent with SourceDebugExtension attribute
192     * matching the former part.
193     */
194    public void testClassPrepareEventWithSourceDebugExtension002(){
195        String expectedSourceNamePattern = "*helloworld_jsp.java";
196        testClassPrepareEventWithSourceDebugExtension(expectedSourceNamePattern);
197    }
198
199    /**
200     * Test ClassPrepareEvent with SourceDebugExtension attribute.
201     * matching the latter part.
202     */
203    public void testClassPrepareEventWithSourceDebugExtension003(){
204        String expectedSourceNamePattern = "helloworld*";
205        testClassPrepareEventWithSourceDebugExtension(expectedSourceNamePattern);
206    }
207
208}
209