Hi,
working on Wicket UI reminds me a little of my old times
building Swing desktop apps.
One thing that occurred to me is that I used to start
working on a new component by fleshing it just its structure,
and started testing right away by placing a little "main"
directly in the component to run it in isolation.
This of course worked only in the initial phases of development,
once I started hooking the component on the persistence backend
that became harder and harder (unless one mocked per persistence),
but it was a nice way to quickly check the basics of the component
itself.
I was wondering if anything similar was possible for Wicket
webapps, and after some fiddling, I concluded that yeah,
it was.
So I put togheter the attached classes that allow one to
just add a main and run a very quick Wicket webapp around
a single, still non Catalog dependent component.
I used it to quickly test a server side file browser
I'm building in my spare time and it worked nicely.
All I had to do in order to check interactively my
file list viewer was to add the following to its code:
public static void main(String[] args) {
WicketTestApplication.start(new IComponentFactory() {
public Component createComponent(String id) {
FileProvider provider = new FileProvider(new
File("c:/progetti/gisData"));
provider.setSort(new SortParam(FileProvider.NAME, true));
return new FileDataView(id, provider) {
@Override
protected void linkNameClicked(File modelObject,
AjaxRequestTarget target) {
System.out.println(modelObject.getAbsolutePath());
}
};
}
});
}
And voilĂ , you run it, you have a file browser on the specified
path that responds to the user clicks.
I was considering contributing this back to GeoServer:
- do people want it
- where do we put it?
The second question is a harder one, as it requires jetty
in the classpath. Maybe we can roll a module of its own,
and people add it as a provided dependency?
The testing "main" are imho to be removed or commented out
once they served their purpose, but in the meantime the
modules do need those classes and Jetty in the classpath.
Cheers
Andrea
--
Andrea Aime
OpenGeo - http://opengeo.org
Expert service straight from the developers.
package org.geoserver.wicket.test;
import java.io.Serializable;
import org.apache.wicket.Component;
/**
* Creates a component for the test runner to use
*
* @author Andrea Aime - OpenGeo
*/
public interface IComponentFactory extends Serializable {
/**
* Creates the Component to be tested (may be a Page)
*
* @param id
* The id that must be assigned to the component. Any other id
* will cause an exception
* @return
*/
Component createComponent(String id);
}
package org.geoserver.wicket.test;
import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.markup.html.WebPage;
/**
* Homepage that will host tested component, or redirect to the test page
*/
public class TestHomePage extends WebPage {
static IComponentFactory componentFactory;
/**
* Constructor that is invoked when page is invoked without a session.
*
* @param parameters
* Page parameters
*/
public TestHomePage() {
Component component = componentFactory.createComponent("component");
if (component instanceof Page)
setResponsePage((Page) component);
else {
if (!"component".equals(component.getId()))
throw new IllegalArgumentException(
"Component factory was asked to produce a componet with
"
+ "id 'component' but returned one with id '"
+ component.getId());
add(component);
}
}
}
package org.geoserver.wicket.test;
import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WicketServlet;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
/**
* Application object for your web application.
*/
public class WicketTestApplication extends WebApplication
{
/**
* Constructor
*/
public WicketTestApplication()
{
}
/**
* @see wicket.Application#getHomePage()
*/
public Class<?> getHomePage()
{
return TestHomePage.class;
}
/**
* <p>Starts a Wicket tester application that will allow for quick
interactive testing
* of the {...@link Component} generated by the factory (may be a
{...@link Page}, in that case the
* home page will redirect to it).</p>
* <p>The application will be hosted on path "/", port 8080</p>
* @param factory
*/
public static void start(IComponentFactory factory) {
start(factory, "/", 8080);
}
/**
* Same as {...@link #start(IComponentFactory)}, but allows to specify
the context path and the
* port
* @param factory
* @param contextPath
* @param port
*/
public static void start(IComponentFactory factory, String contextPath,
int port) {
// sanity check and install the factory
if(factory == null) {
throw new NullPointerException("You must provide a non null
component factory");
}
TestHomePage.componentFactory = factory;
// setup the embedded Jetty server
Server server = new Server();
SocketConnector connector = new SocketConnector();
// Set some timeout options to make debugging easier.
connector.setMaxIdleTime(1000 * 60 * 60);
connector.setSoLingerTime(-1);
connector.setPort(port);
server.setConnectors(new Connector[] { connector });
// programmatically add the wicket servlet to the context
Context root = new Context(server, contextPath ,Context.SESSIONS);
ServletHolder wicket = new ServletHolder(WicketServlet.class);
wicket.setInitParameter("applicationClassName",
WicketTestApplication.class.getName());
root.addServlet(wicket, "/*");
try {
System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY
KEY TO STOP");
server.start();
while (System.in.available() == 0) {
Thread.sleep(1000);
}
server.stop();
server.join();
} catch (Exception e) {
e.printStackTrace();
System.exit(100);
}
}
}
------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Geoserver-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geoserver-devel