/* * Copyright (c) 2002-2008 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 * http://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 com.gargoylesoftware.htmlunit; import static java.util.Arrays.asList; import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStreamWriter; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.net.URI; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import com.gargoylesoftware.base.testing.EventCatcher; import com.gargoylesoftware.htmlunit.html.HtmlAnchor; import com.gargoylesoftware.htmlunit.html.HtmlButton; import com.gargoylesoftware.htmlunit.html.HtmlElement; import com.gargoylesoftware.htmlunit.html.HtmlInlineFrame; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.SubmittableElement; import com.gargoylesoftware.htmlunit.xml.XmlPage; /** * Tests for {@link WebClient}. * * @version $Revision$ * @author Mike Bowler * @author Christian Sell * @author Ben Curren * @author Marc Guillemot * @author David D. Kilzer * @author Chris Erskine * @author Hans Donner * @author Paul King * @author Ahmed Ashour * @author Daniel Gredler * @author Sudhan Moghe */ public class WebClientTest extends WebTestCase { /** * Tests if all JUnit 4 candidate test methods declare @Test annotation. * @throws Exception if the test fails */ @Test public void testTests() throws Exception { testTests(new File("src/test/java")); } private void testTests(final File dir) throws Exception { for (final File file : dir.listFiles()) { if (file.isDirectory()) { if (!file.getName().equals(".svn")) { testTests(file); } } else { if (file.getName().endsWith(".java")) { final int index = new File("src/test/java").getAbsolutePath().length(); String name = file.getAbsolutePath(); name = name.substring(index + 1, name.length() - 5); name = name.replace(File.separatorChar, '.'); final Class< ? > clazz = Class.forName(name); for (Constructor< ? > ctor : clazz.getConstructors()) { if (ctor.getParameterTypes().length == 0) { for (final Method method : clazz.getDeclaredMethods()) { if (Modifier.isPublic(method.getModifiers()) && method.getAnnotation(Before.class) == null && method.getAnnotation(BeforeClass.class) == null && method.getAnnotation(After.class) == null && method.getAnnotation(AfterClass.class) == null && method.getAnnotation(Test.class) == null && method.getReturnType() == Void.TYPE && method.getParameterTypes().length == 0) { fail("Method '" + method.getName() + "' in " + name + " does not declare @Test annotation"); } } } } } } } } /** * Test the situation where credentials are required but they haven't been specified. * * @throws Exception if something goes wrong */ @Test public void testCredentialProvider_NoCredentials() throws Exception { final String htmlContent = "