jtulach commented on code in PR #9789: URL: https://github.com/apache/netbeans/pull/9789#discussion_r4516239619
########## profiler/profiler.heapwalker/test/unit/src/org/netbeans/modules/profiler/heapwalk/details/basic/StringDetailsProviderTest.java: ########## @@ -0,0 +1,133 @@ +/* + * 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. + */ +package org.netbeans.modules.profiler.heapwalk.details.basic; + +import com.sun.jdi.AbsentInformationException; +import java.io.File; +import java.net.URI; +import junit.framework.Test; +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertNotNull; +import org.netbeans.api.debugger.DebuggerManager; +import org.netbeans.api.debugger.Session; +import org.netbeans.api.debugger.jpda.CallStackFrame; +import org.netbeans.api.debugger.jpda.ExceptionBreakpoint; +import org.netbeans.api.debugger.jpda.JPDADebugger; +import org.netbeans.api.debugger.jpda.JPDASupport; +import org.netbeans.api.debugger.jpda.This; +import org.netbeans.api.debugger.jpda.event.JPDABreakpointListener; +import org.netbeans.junit.NbTestCase; +import org.netbeans.lib.profiler.heap.Instance; +import org.netbeans.lib.profiler.heap.JavaClass; +import org.netbeans.modules.debugger.jpda.heapwalk.HeapImpl; +import org.openide.util.Utilities; + +public class StringDetailsProviderTest extends NbTestCase { + private JPDASupport support; + private ExceptionBreakpoint breakPoint; + + + public StringDetailsProviderTest(String name) { + super(name); + } + + public static Test suite() { + return JPDASupport.createTestSuite(StringDetailsProviderTest.class); + } + + @Override + protected void setUp() throws Exception { + breakPoint = ExceptionBreakpoint.create( + "java.lang.IllegalStateException", + ExceptionBreakpoint.TYPE_EXCEPTION_CAUGHT_UNCAUGHT + ); + DebuggerManager.getDebuggerManager().addBreakpoint(breakPoint); + + final URI cpUrl = test.SampleMain.class.getProtectionDomain().getCodeSource().getLocation().toURI(); + System.setProperty ("test.dir.src", test.SampleMain.findSourcePath()); + support = JPDASupport.attach(new String[0], "test.SampleMain", new String[0], new File[] { + Utilities.toFile(cpUrl) + }); + } + + + + /** + * Check fields of {@link test.SampleMain}. + */ + public void testHeapRepresentationWhenDebuggerIsStopped() throws Throwable { + final Session session = support.getDebugger().getSession(); + + HeapImpl heap = new HeapImpl(support.getDebugger()); + + assertOnBreakpoint((event) -> { + try { + CallStackFrame frame = event.getThread().getCallStack(0, 1)[0]; + + This self = frame.getThisVariable(); + String value = self.getField("when").getValue(); + assertEquals("\"Now\"", value); + + Instance thiz = null; + for (JavaClass clazz : heap.getAllClasses()) { + if (test.SampleMain.class.getName().equals(clazz.getName())) { + thiz = (Instance) clazz.getInstances().get(0); + break; + } + } + assertNotNull("This instance found in the heap", thiz); + + Instance str = (Instance) thiz.getValueOfField("when"); + assertNotNull("When field is found", str); + assertEquals("java.lang.String", str.getJavaClass().getName()); + + StringDetailsProvider p = new StringDetailsProvider(); + String detail = p.getDetailsString("java.lang.String", str, heap); + assertEquals("Now", detail); + } catch (AbsentInformationException ex) { + throw new AssertionError(ex); + } + }); + } + + private void assertOnBreakpoint(JPDABreakpointListener listener) { + int[] cnt = { 0 }; + JPDABreakpointListener countingListener = (event) -> { + cnt[0]++; + listener.breakpointReached(event); Review Comment: Done in 0a4a09e3b1ea -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
