001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.tynamo.test;
021
022 import com.gargoylesoftware.htmlunit.ElementNotFoundException;
023 import com.gargoylesoftware.htmlunit.WebClient;
024 import com.gargoylesoftware.htmlunit.html.ClickableElement;
025 import com.gargoylesoftware.htmlunit.html.HtmlForm;
026 import com.gargoylesoftware.htmlunit.html.HtmlInput;
027 import com.gargoylesoftware.htmlunit.html.HtmlPage;
028 import org.mortbay.jetty.Connector;
029 import org.mortbay.jetty.Handler;
030 import org.mortbay.jetty.Server;
031 import org.mortbay.jetty.handler.DefaultHandler;
032 import org.mortbay.jetty.handler.HandlerCollection;
033 import org.mortbay.jetty.nio.SelectChannelConnector;
034 import org.mortbay.jetty.webapp.WebAppContext;
035 import org.mortbay.resource.ResourceCollection;
036 import org.testng.annotations.BeforeClass;
037 import org.testng.annotations.BeforeTest;
038
039 import java.io.IOException;
040
041 import static com.gargoylesoftware.htmlunit.WebAssert.assertTextNotPresent;
042 import static com.gargoylesoftware.htmlunit.WebAssert.assertTextPresent;
043 import static org.testng.Assert.*;
044
045 public abstract class AbstractContainerTest
046 {
047 protected static PauseableServer server;
048
049 protected static final int port = 8180;
050
051 protected static final String BASEURI = "http://localhost:" + port + "/";
052
053 protected final WebClient webClient = new WebClient();
054
055 static String errorText = "You must correct the following errors before you may continue";
056
057 @BeforeClass
058 public static void startContainer() throws Exception
059 {
060 if (server == null)
061 {
062 server = new PauseableServer();
063 Connector connector = new SelectChannelConnector();
064 connector.setPort(port);
065 server.setConnectors(new Connector[]{connector});
066
067 WebAppContext context = new WebAppContext("src/main/webapp", "/");
068 ResourceCollection resourceCollection =
069 new ResourceCollection(new String[]{"src/main/webapp", "src/test/webapp"});
070 context.setBaseResource(resourceCollection);
071
072 HandlerCollection handlers = new HandlerCollection();
073 handlers.setHandlers(new Handler[]{context, new DefaultHandler()});
074 server.setHandler(handlers);
075 server.start();
076 assertTrue(server.isStarted());
077 }
078 }
079
080 @BeforeTest
081 public void configureWebClient()
082 {
083 webClient.setThrowExceptionOnFailingStatusCode(true);
084 }
085
086 public void pauseServer(boolean paused)
087 {
088 if (server != null) server.pause(paused);
089 }
090
091 public static class PauseableServer extends Server
092 {
093 public synchronized void pause(boolean paused)
094 {
095 try
096 {
097 if (paused) for (Connector connector : getConnectors())
098 connector.stop();
099 else for (Connector connector : getConnectors())
100 connector.start();
101 } catch (Exception e)
102 {
103 }
104 }
105 }
106
107 protected void assertXPathPresent(HtmlPage page, String xpath) throws Exception
108 {
109 assertNotNull(page.getByXPath(xpath).get(0));
110 }
111
112 protected void assertXPathNotPresent(HtmlPage page, String xpath) throws Exception
113 {
114 assertNull(page.getByXPath(xpath).get(0));
115 }
116
117
118 protected HtmlPage clickLink(HtmlPage page, String linkText)
119 {
120 try
121 {
122 return (HtmlPage) page.getFirstAnchorByText(linkText).click();
123 } catch (ElementNotFoundException e)
124 {
125 fail("Couldn't find a link with text '" + linkText + "' on page " + page);
126 } catch (IOException e)
127 {
128 fail("Clicking on link '" + linkText + "' on page " + page + " failed because of: ", e);
129 }
130 return null;
131 }
132
133 protected HtmlPage clickButton(HtmlPage page, String buttonId) throws IOException
134 {
135 ClickableElement button = (ClickableElement) page.getElementById(buttonId);
136 return button.click();
137 }
138
139 protected HtmlPage clickButton(HtmlForm form, String buttonValue) throws IOException
140 {
141 try
142 {
143 return form.<HtmlInput>getInputByValue(buttonValue).click();
144 } catch (ElementNotFoundException e)
145 {
146 try
147 {
148 return form.getButtonByName(buttonValue).click();
149 } catch (ElementNotFoundException e1)
150 {
151 fail("Couldn't find a button with text/name '" + buttonValue + "' on form '" + form.getNameAttribute() +
152 "'");
153 }
154 }
155 return null;
156 }
157
158 protected void assertErrorTextPresent(HtmlPage page)
159 {
160 assertTextPresent(page, errorText);
161 }
162
163 protected void assertErrorTextNotPresent(HtmlPage page)
164 {
165 assertTextNotPresent(page, errorText);
166 }
167
168 }