org.assertj.swing.launcher.AppletLauncher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of assertj-swing Show documentation
Show all versions of assertj-swing Show documentation
Fluent interface for functional GUI testing
/**
* 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.
*
* Copyright 2012-2015 the original author or authors.
*/
package org.assertj.swing.launcher;
import static org.assertj.core.util.Maps.newHashMap;
import static org.assertj.core.util.Preconditions.checkNotNull;
import static org.assertj.core.util.Preconditions.checkNotNullOrEmpty;
import static org.assertj.swing.edt.GuiActionRunner.execute;
import static org.assertj.swing.launcher.NewAppletViewerQuery.showAppletViewerWith;
import java.applet.Applet;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.assertj.swing.annotation.RunsInEDT;
import org.assertj.swing.applet.AppletViewer;
import org.assertj.swing.edt.GuiQuery;
import org.assertj.swing.exception.UnexpectedException;
/**
*
* Fluent interface for launching and testing {@code Applet}s.
*
*
*
* An {@code Applet} can be launched by passing its type as {@code String}, the actual type, or an instance of the
* {@code Applet} to launch:
*
*
*
* {@link AppletViewer} viewer = AppletLauncher.{@link #applet(String) applet}("org.assertj.swing.applet.MyApplet").{@link #start() start}();
*
* // or
*
* {@link AppletViewer} viewer = AppletLauncher.{@link #applet(Class) applet}(MyApplet.class).{@link #start() start}();
*
* // or
*
* {@link AppletViewer} viewer = AppletLauncher.{@link #launcherFor(Applet) applet}(new MyApplet()).{@link #start() start}();
*
*
*
* In addition, we can pass parameters to the applet to launch. The parameters to pass are the same that are specified
* in the HTML "param"
* tag:
*
*
*
* {@link AppletViewer} viewer = AppletLauncher.{@link #launcherFor(Applet) applet}(new MyApplet())
* .{@link #withParameters(Map) withParameters}(
* {@link AppletParameter#name(String) name}("bgcolor").{@link org.assertj.swing.launcher.AppletParameter.AppletParameterBuilder#value(String) value}("blue"),
* {@link AppletParameter#name(String) name}("color").{@link org.assertj.swing.launcher.AppletParameter.AppletParameterBuilder#value(String) value}("red"),
* {@link AppletParameter#name(String) name}("pause").{@link org.assertj.swing.launcher.AppletParameter.AppletParameterBuilder#value(String) value}("200")
* )
* .{@link #start() start}();
*
* // or
*
* Map<String, String> parameters = new HashMap<String, String>();
* parameters.put("bgcolor", "blue");
* parameters.put("color", "red");
* parameters.put("pause", "200");
*
* {@link AppletViewer} viewer = AppletLauncher.{@link #launcherFor(Applet) applet}(new MyApplet()).{@link #withParameters(Map) withParameters}(parameters).{@link #start() start}();
*
*
* @author Yvonne Wang
* @author Alex Ruiz
*/
public class AppletLauncher {
private final Applet applet;
private final Map parameters = newHashMap();
/**
* Creates a new {@link AppletLauncher}. The {@code Applet} to launch is a new instance of the given type. It is
* assumed that the given type has a default constructor.
*
* @param appletType the type of {@code Applet} to instantiate.
* @return the created {@code AppletLauncher}.
* @throws NullPointerException if the given type name is {@code null}.
* @throws IllegalArgumentException if the given type name is empty.
* @throws IllegalArgumentException if the given type is not a subclass of {@code Applet}.
* @throws UnexpectedException if the given type cannot be loaded.
* @throws UnexpectedException if a new instance of the given type cannot be instantiated.
*/
@RunsInEDT
public static @Nonnull AppletLauncher applet(@Nonnull String appletType) {
checkNotNullOrEmpty(appletType);
Class> type = load(appletType);
if (!(Applet.class.isAssignableFrom(type))) {
String msg = String.format("The given type is not a subclass of %s", Applet.class.getName());
throw new IllegalArgumentException(msg);
}
return instantiate(type);
}
@RunsInEDT
private static @Nonnull Class> load(@Nonnull String typeName) {
try {
return Class.forName(typeName);
} catch (ClassNotFoundException e) {
throw cannotLoadType(typeName, e);
} catch (Exception e) {
throw cannotLoadType(typeName, e);
}
}
private static UnexpectedException cannotLoadType(String typeName, Exception e) {
String msg = String.format("Unable to load class %s", typeName);
throw new UnexpectedException(msg, e);
}
/**
* Creates a new {@link AppletLauncher}. The {@code Applet} to launch is a new instance of the given type. It is
* assumed that the given type has a default constructor.
*
* @param appletType the type of {@code Applet} to instantiate.
* @return the created {@code AppletLauncher}.
* @throws NullPointerException if the given type is {@code null}.
* @throws UnexpectedException if a new instance of the given type cannot be instantiated.
*/
@RunsInEDT
public static @Nonnull AppletLauncher applet(@Nonnull Class extends Applet> appletType) {
return instantiate(checkNotNull(appletType));
}
private static @Nonnull AppletLauncher instantiate(final @Nonnull Class> appletType) {
try {
Object applet = execute(new GuiQuery