View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript;
16  
17  import static java.nio.charset.StandardCharsets.ISO_8859_1;
18  
19  import java.io.StringWriter;
20  import java.net.URL;
21  
22  import org.apache.commons.io.IOUtils;
23  import org.apache.logging.log4j.Level;
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.core.Logger;
26  import org.apache.logging.log4j.core.appender.WriterAppender;
27  import org.apache.logging.log4j.core.layout.PatternLayout;
28  import org.htmlunit.BrowserVersion;
29  import org.htmlunit.MockWebConnection;
30  import org.htmlunit.SimpleWebTestCase;
31  import org.htmlunit.WebClient;
32  import org.htmlunit.WebConnection;
33  import org.junit.After;
34  import org.junit.Test;
35  
36  /**
37   * Tests for {@link DebugFrameImpl}.
38   *
39   * @author Marc Guillemot
40   */
41  public class DebugFrameImplTest extends SimpleWebTestCase {
42  
43      private final Logger loggerDebugFrameImpl_ = (Logger) LogManager.getLogger(DebugFrameImpl.class);
44  
45      private Level originalLogLevel_;
46      private WebClient client_;
47  
48      /**
49       * Constructor.
50       * @throws Exception if an exception occurs
51       */
52      public DebugFrameImplTest() throws Exception {
53          client_ = new WebClient(BrowserVersion.FIREFOX);
54          ((JavaScriptEngine) client_.getJavaScriptEngine()).getContextFactory().setDebugger(new DebuggerImpl());
55  
56          originalLogLevel_ = loggerDebugFrameImpl_.getLevel();
57          loggerDebugFrameImpl_.setLevel(Level.TRACE);
58      }
59  
60      /**
61       * Cleans up the client, and resets the log to its original state.
62       * @throws Exception when a problem occurs
63       */
64      @After
65      public void tearDown() throws Exception {
66          client_.getJavaScriptEngine().getContextFactory().setDebugger(null);
67          client_.close();
68          loggerDebugFrameImpl_.setLevel(originalLogLevel_);
69      }
70  
71      /**
72       * @throws Exception if the test fails
73       */
74      @Test
75      public void withCallable() throws Exception {
76          final String content = DOCTYPE_HTML
77              + "<html><head><title>debug test</title>\n"
78              + "<script>\n"
79              + "  var counter = 0;\n"
80              + "  window.__defineGetter__('foo', function(a) { return counter++ });\n"
81              + "  alert(window.foo);\n"
82              + "</script></head><body></body></html>";
83          final WebConnection old = client_.getWebConnection();
84          try {
85              final MockWebConnection mock = new MockWebConnection();
86              mock.setDefaultResponse(content);
87              client_.setWebConnection(mock);
88              client_.getPage(URL_FIRST);
89          }
90          finally {
91              client_.setWebConnection(old);
92          }
93      }
94  
95      /**
96       * @throws Exception if the test fails
97       */
98      void loggedCalls() throws Exception {
99          final URL url = getClass().getResource("debugFrameImplTest.html");
100         final String expectedLog = IOUtils.toString(getClass().getResourceAsStream("debugFrameImplTest.txt"),
101                 ISO_8859_1);
102 
103         final StringWriter stringWriter = new StringWriter();
104         final PatternLayout layout = PatternLayout.newBuilder().withPattern("%msg%n").build();
105 
106         final WriterAppender writerAppender = WriterAppender.newBuilder().setName("writeLogger").setTarget(stringWriter)
107                 .setLayout(layout).build();
108         writerAppender.start();
109 
110         loggerDebugFrameImpl_.addAppender(writerAppender);
111         try {
112             client_.getPage(url);
113         }
114         finally {
115             loggerDebugFrameImpl_.removeAppender(writerAppender);
116         }
117 
118         assertEquals(expectedLog, stringWriter.toString());
119     }
120 }