Skip to content

Commit

Permalink
ZK-5730: Harden smartUpdate to check for null desktop or null webapp
Browse files Browse the repository at this point in the history
  • Loading branch information
jumperchen committed Oct 9, 2024
1 parent 0a90356 commit 8f510bf
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 2 deletions.
5 changes: 3 additions & 2 deletions zk/src/main/java/org/zkoss/zk/ui/AbstractPage.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* AbstractPage.java
Purpose:
Description:
History:
Sun Oct 26 17:42:22 2008, Created by tomyeh
Expand Down Expand Up @@ -233,6 +233,7 @@ public void removeComponents() {
}

public void destroy() {
removeComponents(); // ZK-5730
_firstRoot = null;
_nRoot = 0;
_fellows = new HashMap<String, Component>(2); //not clear() since # of fellows might huge
Expand Down
1 change: 1 addition & 0 deletions zkdoc/release-note
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Feb 27, 2024
ZK-5636: fail to toggle grid's detail on iPad/iPhone
ZK-5650: cannot upload an image under websocket
ZK-5633: Client mvvm crashes a browser when model is large
ZK-5730: Harden smartUpdate to check for null desktop or null webapp

* Upgrade Notes
+ Upgrade commons-fileupload to commons-fileupload2-javax 2.0.0-M2 and commons-io to 2.13.0 to support jakarta-friendly uploads
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* B101_ZK_5730Composer.java
Purpose:
Description:
History:
12:10 PM 2024/10/9, Created by jumperchen
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
*/
package org.zkoss.zktest.test2;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.http.SimpleSession;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Div;
import org.zkoss.zul.Grid;
import org.zkoss.zul.ListModelList;

/**
* @author jumperchen
*/
public class B101_ZK_5730Composer extends SelectorComposer {

@Wire private Grid grid;
private Div rootDiv;
private ListModelList model = new ListModelList();

@Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
rootDiv = (Div) comp;
grid.setModel(model);
}

@Listen(Events.ON_CLICK + "=#logout")
public void logout() {
((SimpleSession)Sessions.getCurrent()).invalidateNow();
}

@Listen(Events.ON_CLICK + "=#op2")
public void longOp() {
fakeOperation(3);
grid.addSclass("success");
}

static public void fakeOperation(int seconds) {
long endTime = System.currentTimeMillis() + seconds * 1000;
while (System.currentTimeMillis() < endTime) {
// Just a busy-wait. In real scenarios, this could be some meaningful computation.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* B101_ZK_5730_1Composer.java
Purpose:
Description:
History:
12:32 PM 2024/10/9, Created by jumperchen
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
*/
package org.zkoss.zktest.test2;

import java.util.Optional;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.sys.PageCtrl;
import org.zkoss.zk.ui.util.Composer;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;

/**
* @author jumperchen
*/
public class B101_ZK_5730_1Composer implements Composer {
@Override
public void doAfterCompose(Component comp) throws Exception {
Desktop dt = Executions.getCurrent().getDesktop();
dt.enableServerPush(true);

Button button = new Button("schedule");
Label myLabel = new Label("my label");

comp.appendChild(button);
comp.appendChild(myLabel);

button.addEventListener(Events.ON_CLICK, event -> {
Runnable runnable = () -> {
System.out.println("runnable - run()");
myLabel.addSclass("bg-red");
};
EventListener<? super Event> executeEventListener = this::executeEventListener;
Executions.schedule(dt, executeEventListener, new Event("ExecuteEvent", null, runnable));

// This causes an NPE in the runnable above
((PageCtrl)comp.getPage()).destroy();
});

}

public void executeEventListener(Event event) {
Optional.ofNullable((Runnable)event.getData()).ifPresent(Runnable::run);
}
}
23 changes: 23 additions & 0 deletions zktest/src/main/webapp/test2/B101-ZK-5730-1.zul
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
B101-ZK-5730-1.zul
Purpose:
Description:
History:
2024/10/9, Created by jumperchen
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
-->
<zk>
<style>
.z-label.bg-red {
background-color: red;
}
</style>
<div apply="org.zkoss.zktest.test2.B101_ZK_5730_1Composer"/>
</zk>
22 changes: 22 additions & 0 deletions zktest/src/main/webapp/test2/B101-ZK-5730.zul
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
B101-ZK-5730.zul
Purpose:
Description:
History:
2024/10/9, Created by jumperchen
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
-->
<zk>
<div apply="org.zkoss.zktest.test2.B101_ZK_5730Composer">
<button id="op2" label="sync long op"/>
<button id="logout" label="logout" />
<grid id="grid"></grid>
</div>
</zk>
1 change: 1 addition & 0 deletions zktest/src/main/webapp/test2/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3150,6 +3150,7 @@ B90-ZK-4431.zul=A,E,Multislider
##zats##B101-ZK-5799.zul=A,E,ClientMVVM,pdfviewer,zonejs
##manually##B101-ZK-5801.zul=A,E,ClientMVVM,Apply
##zats##B101-ZK-5546.zul=A,E,Websocket,Session,Timeout
##zats##B101-ZK-5730.zul=A,E,NullPointerException,Desktop,Page,Session,Timeout,Destroy,SmartUpdate

##
# Features - 3.0.x version
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* B101_ZK_5730Test.java
Purpose:
Description:
History:
12:11 PM 2024/10/9, Created by jumperchen
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
*/
package org.zkoss.zktest.zats.test2;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.WindowType;

import org.zkoss.test.webdriver.WebDriverTestCase;

/**
* @author jumperchen
*/
public class B101_ZK_5730Test extends WebDriverTestCase {

@Test
public void test() {
connect();
driver.switchTo().newWindow(WindowType.TAB);
driver.get(getAddress() + "/test2/B101-ZK-5730.zul");
waitResponse();
Object[] windowHandles = driver.getWindowHandles().toArray();
driver.switchTo().window((String) windowHandles[0]);
click(jq("$op2"));
driver.switchTo().window((String) windowHandles[1]);
click(jq("$logout"));
driver.switchTo().window((String) windowHandles[0]);
waitResponse();
assertNoZKError();
}

@Test
public void testCase1() {
connect("/test2/B101-ZK-5730-1.zul");
click(jq("@button"));
waitResponse();
assertNoZKError();
}
}

0 comments on commit 8f510bf

Please sign in to comment.