Skip to content

Commit

Permalink
make HtmlDomTreeWalker serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Jan 22, 2025
1 parent 6cf9e2b commit ed2d44b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/org/htmlunit/html/HtmlDomTreeWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.htmlunit.html;

import java.io.Serializable;

import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.NodeFilter;
Expand All @@ -30,7 +32,7 @@
* @author Ahmed Ashour
* @author Ronald Brill
*/
public class HtmlDomTreeWalker {
public class HtmlDomTreeWalker implements Serializable {

private final DomNode root_;
private DomNode currentNode_;
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/org/htmlunit/html/HtmlDomTreeWalkerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2002-2025 Gargoyle Software Inc.
*
* Licensed 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
* https://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.htmlunit.html;

import org.apache.commons.lang3.SerializationUtils;
import org.htmlunit.SimpleWebTestCase;
import org.htmlunit.junit.BrowserRunner;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* Tests for {@link HtmlDomTreeWalker}.
*
* @author Ronald Brill
*/
@RunWith(BrowserRunner.class)
public class HtmlDomTreeWalkerTest extends SimpleWebTestCase {

/**
* @throws Exception if an error occurs
*/
@Test
public void serialization() throws Exception {
final String html = "<html><head></head><body>\n"
+ "<div style='display:inline'>1</div><div style='display:inline'>2</div>\n"
+ "</body></html>";

final HtmlPage page = loadPage(html);

final byte[] bytes = SerializationUtils.serialize(new HtmlDomTreeWalker(page.getBody(), 0, null, false));

final HtmlDomTreeWalker deserialized = (HtmlDomTreeWalker) SerializationUtils.deserialize(bytes);
Assert.assertEquals(page.getBody().getNodeName(), deserialized.getRoot().getNodeName());
}
}

0 comments on commit ed2d44b

Please sign in to comment.