Testing Wicket panels with constructor arguments - wicket

I got a page with several panels that takes several parameters in their constructors.
One of them being a menu that takes a list for the different buttons in the menu.
I've tried to test it as components but get a null pointer exception. Using a dummy page and creating the panel on the dummy page works.
I'm not entirely happy with this approach since it introduces a lot of new code in my tests and more possibilities for errors.
Is there a better way of testing panels that takes arguments in their constructor?
Sure thing:
The code that gives an null pointer error:
public void testVisitPanel(){
VisitPanel v = new VisitPanel("visitPanel");
tester.startComponent(v);
tester.assertContains("DATE");
}
The panel
public VisitPanel(String id) {
super(id);
add( new Label("visitDate", "DATE"));
add( new Label("visitStage", "VISIT SIGNED"));
}
And the html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:wicket="http://wicket.sourceforge.net/"
xml:lang="en"
lang="en">
<wicket:head>
</wicket:head>
<body>
<wicket:panel>
<span wicket:id="visitDate">VISIT DATE</span>
<span wicket:id="visitStage">STAGE</span>
</wicket:panel>
</body>
</html>

If you're using Wicket 1.4, you probably want wicketTester.startPanel.
What I do is create an implementation of ITestPanelSource in the test, doing something like:
private class TestPanelSource implements ITestPanelSource {
private static final long serialVersionUID = 1L;
public Panel getTestPanel(String panelId) {
return new MyPanel(panelId, myArg1, myArg2);
}
}
with the myArgN being fields in the test class (frequently mocks) that suit the constructor, and then call it in the test or in a setUp method with
wicketTester.startPanel(new TestPanelSource());
This is basically doing some of the DummyPage work for you, so it may not be that far from what you're doing now, but might at least save on implementation of dummy pages for test infrastructure.
In Wicket 1.5, this is deprecated in favor the component testing that you referenced in the question. That should also work, so it might be worthwhile to post some actual code that is giving you trouble with that technique.

Related

eclipse stops responding while running a .jsp file

i have created a basic dynamic web project in eclipse.
my jsp file contains following code
<html>
<head>
<title>Insert title here</title>
</head>
<body>
this is the sum of 5 and 3 <%com.testing.SumOfNumbers.addTwoNumbers(3,5); %>
</body>
</html>
and in package "com.testing" i have created SumOfNumbers class with addTwoNumbers method.
package com.testing;
public class SumOfNumbers {
public static int addTwoNumbers(int a,int b)
{
return a+b;
}
}
i have downloaded tomcat v10.0 server.
I tried several times but same result. I don't know where the problem is ,is it in tomcat server or eclipse bug?
thanks in advance.
It's unlikely to be your code, although you're using a scriptlet instead of an expression, so the sum won't be in the output like you seem to expect.
https://wiki.eclipse.org/How_to_report_a_deadlock includes directions on how to get a stack dump to see what's going on.

How to navigate DOM from Wicket

I wonder if it is possible to modify the HTML code of the parent of a wicket component in the Java code to modify its attribute without making it a component in wicket. For example, I would like to add active to the li tag from Java.
<li>
<a wicket:id="home" href="#">
<i class="icon-home"></i>
<span>Home</span>
</a>
</li>
and the say add an Attribute to that parent without referencing it in code as a component.
I don't think this is possible, it would be completely against the modular nature of Wicket. (Not to mention the fact that pages are actually rendered as a stream, there's no DOM tree built.)
Components in Wicket shouldn't depend on what's outside of them. What if you want to change the logic of active/inactive controls? Or, in a more likely scenario, you just want to change the markup. Or if you just want to unit test your component without any surrounding markup.
Wicket was designed to avoid these "spooky actions at a distance", to create components that are genuinely testable on their own.
You need a component that encapsulates the entire list, which keeps track of which of its items is active (via its model). It might seem at first like a lot of work but when you look at the result, you'll realise how much easier it is to understand what's going on.
You can do it using javascript. In this example I overrided the renderHead() of the link, but it can also be done with a Behavior.
public class TestPage extends WebPage {
public TestPage(final PageParameters parameters) {
super(parameters);
add(new AjaxLink<Void>("link") {
boolean active = false;
#Override
public void onClick(AjaxRequestTarget target) {
active = !active;
target.add(this);
}
#Override
public void renderHead(IHeaderResponse response) {
super.renderHead(response);
String addOrRemove = (active) ? ".addClass('active')" : ".removeClass('active')";
response.render(OnDomReadyHeaderItem.forScript("$('#" + getMarkupId() + "').parent('li')" + addOrRemove + ";"));
}
});
}
}
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<style type="text/css">
li.active {background-color: red;}
</style>
</head>
<body>
<ul>
<li><a wicket:id="link">TOGGLE</a></li>
</ul>
</body>
</html>
Yes, it will couple the code to the markup, but this is not always a problem. If you feel you are copying and pasting this code over and over, consider creating a proper component :)

I am not able to run my apache wicket sample program

I am very new to java wicket. I don't understand how to run this code. My wicket program follows the structure below. My problem is that I am not able to run this. I am getting a 404 error.
This is my wicket code to get a hello world message.
HelloWorld.html:
<html xmlns:wicket>
<title>Hello World</title>
</head>
<body>
<span wicket:id="message" id="message">Message goes here</span>
</body>
</html>
</html>
HelloWorld.java:
package com.sensiple.wicket;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
public class HelloWorld extends WebPage {
public HelloWorld() {
add(new Label("message", "Hello World!"));
}
}
This class is returning hello world which is to be printed in HelloWorld.html
HelloWorldApplication.java:
package com.sensiple.wicket;
import org.apache.wicket.protocol.http.WebApplication;
public class HelloWorldApplication extends WebApplication {
//what is the need of this constructor, need of this class in this program
public HelloWorldApplication() {
}
I need to know what is the use of getHomePage-method too, as I am not getting what is the use of one more class here which returns return type as HelloWorld. Hardly I am not able to run this code. I went through many resources which didn't help.
public Class<HelloWorld> getHomePage() {
System.out.println("initialized!!!!");
return HelloWorld.class;
}
}
To start answering your many questions:
Your program is most likely returning a 404 due to the malformed HTML in your HelloWord.html. Wicket required valis XHTML to work with.
The original signature of getHomePage() is public abstract Class<? extends Page> getHomePage(). You can implement it as you did as it fullfills the contract. The method returns the class that is used to render the homepage of your web application. That's the page that is shown at the base-url of your application without any mountpoints or parameters.
You don't need the default constructor to your WebApplication but you can implement one to do some of the initialisations needed for your application. It's run once at the start of your application (or in most cases your container).
Try using this html:
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<title>Hello World</title>
</head>
<body>
<span wicket:id="message"></span>
</body>
</html>
When your application is running, just call http://localhost:8080 (unless you changed the port) and wicket should redirect you to your HelloWorld page

Kentico CMS: Add a class attribute to the html tag in a Kentico Masterpage

How do you edit the HTML tag in a Kentico CMS Masterpage, i.e. to add a class attribute?
Currently I have this:
But I want:
<!DOCTYPE html>
<html class="myClass">
<head>
<title>
From the screenshot you can see that the HTML tag is not editable.
Is there a way to edit the html tag or tell Kentico to write a class attribute to the html tag?
I have found one solution:
I manually added a class attribute to the HTML tag in this file:-
<solution>/CMSPages/PortalTemplate.aspx
You can also use the "Head HTML" web part on your page template(s).
It is quite disappointing that Kentico still does not support this out of the box. A request to be able to modify the <html> tag directly from code has been filed: http://ideas.kentico.com/forums/239189-kentico-product-ideas/suggestions/5947264-ability-to-modify-page-head-section-and-html-tag
In the meantime use the following solutions:
Portal Engine
I solved this the following way, based on Dave Haigh suggestion. In the file /CMSPages/PortalTemplate.aspx change the following line:
<html xmlns="http://www.w3.org/1999/xhtml" <%=XmlNamespace%>
lang="<%=CMSContext.PreferredCultureCode%>">
ASPX Engine
In your master page, add the following snippet to your code behind file:
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (CurrentDocument != null)
{
CMS.UIControls.ContentPage page= this.Page as CMS.UIControls.ContentPage;
if (page != null)
{
string lang= CMS.Localization.LocalizationContext.CurrentCulture.CultureCode;
page.XmlNamespace += " lang=\"" + lang + "\"";
}
}
}
</script>
Source: http://devnet.kentico.com/questions/kentico-9-how-to-define-language-in-html

AJAXControlToolkit TabContainer - bug in dynamically adding/removing tabs?

I am using AjaxControlToolkit version 4.1.40412.0, .NET 4.0, VS2010
Using the TabContainer control I want to add/remove tabs dynamically, but it looks like all of my dynamic changes are not persistent. Here is my scenario: I start with a tabcontainer with 1 tabpanel (hardcoded, i.e. added at design time), then dynamically I add more tabpanels and hide the original tabpanel (run time). As expected I see only the new tabpanels on the page, however any time I try to select a different tab the whole control reverts back to its design time state, i.e. only shows the original tabpanel, which was supposed to be gone and the new tabpanels are nowhere to be found. What am I missing? I guess as a workaround I can add 50 or so tabs at design time and then dynamically hide/display rather than remove/add, but this seems clunky, sloppy and unnecessary.
Here is my code if you want to duplicate the issue:
ASPX
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"/>
<asp:TabContainer ID="tcMain" runat="server" AutoPostBack="true" ScrollBars="auto" >
<asp:TabPanel ID="tbTab0" runat="server" HeaderText="Tab0"/>
</asp:TabContainer>
</div>
</form>
</body>
</html>
ASPX.VB
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
For i As Integer = 0 To 3
Dim ol As New Label
ol.Text = i.ToString
Dim oT As New AjaxControlToolkit.TabPanel
oT.Controls.Add(ol)
oT.HeaderText = i.ToString
tcMain.Tabs.Add(oT)
Next
For i As Integer = 1 To tcMain.Tabs.Count
If tcMain.Tabs(tcMain.Tabs.Count - i).HeaderText = "Tab0" Then tcMain.Tabs.RemoveAt(tcMain.Tabs.Count - i)
Next
End If
End Sub
End Class
Note: If you comment out "If Not Page.IsPostBack Then" , i.e. run the code under it on every page load, the tabcontainer works as expected - I can select any tab without problems. In my real project this cannot be the solution though - I will be adding/removing tabs based on user input, so unless I keep a log of all changes ever made to the control I cannot load those changes every time the page loads.
You need to run your code in the page init or pre init, because by the page load, the page is already built along with the view state.