All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.assertj.swing.launcher.AppletLauncher Maven / Gradle / Ivy

There is a newer version: 3.17.1
Show newest version
/**
 * 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 appletType) { return instantiate(checkNotNull(appletType)); } private static @Nonnull AppletLauncher instantiate(final @Nonnull Class appletType) { try { Object applet = execute(new GuiQuery() { @Override protected @Nullable Object executeInEDT() throws Exception { return appletType.newInstance(); } }); return launcherFor(checkNotNull((Applet) applet)); } catch (Exception e) { String msg = String.format("Unable to create a new instance of %s", appletType.getName()); throw new UnexpectedException(msg, e); } } /** * Creates a new {@link AppletLauncher}. * * @param applet the {@code Applet} to launch. * @return the created {@code AppletLauncher}. * @throws NullPointerException if the given {@code Applet} is {@code null}. */ public static @Nonnull AppletLauncher launcherFor(@Nonnull Applet applet) { return new AppletLauncher(applet); } private AppletLauncher(@Nonnull Applet applet) { this.applet = checkNotNull(applet); } /** * Sets the parameters for the {@code Applet} to launch, as an alternative to * {@link #withParameters(AppletParameter...)}. * * @param newParameters the parameters for the {@code Applet} to launch. * @return this launcher. * @throws NullPointerException if {@code newParameters} is {@code null}. */ public @Nonnull AppletLauncher withParameters(@Nonnull Map newParameters) { parameters.clear(); parameters.putAll(checkNotNull(newParameters)); return this; } /** * Sets the parameters for the {@code Applet} to launch, as an alternative to {@link #withParameters(Map)}. * * @param newParameters the parameters for the {@code Applet} to launch. * @return this launcher. * @throws NullPointerException if {@code newParameters} is {@code null}. * @throws NullPointerException if any parameter is {@code null}. */ public @Nonnull AppletLauncher withParameters(@Nonnull AppletParameter... newParameters) { checkNotNull(newParameters); parameters.clear(); for (AppletParameter parameter : newParameters) { add(checkNotNull(parameter)); } return this; } private void add(@Nonnull AppletParameter parameter) { parameters.put(parameter.name, parameter.value); } /** * Launches the {@code Applet} in a {@link AppletViewer} (using implementations of * {@link org.assertj.swing.applet.BasicAppletStub} and {@link org.assertj.swing.applet.BasicAppletContext}. To * provide your own {@code AppletStub} create a new {@link AppletViewer} directly. The {@code AppletViewer} is created * and launched in the event dispatch thread (EDT.) * * @return the created {@code AppletViewer}. */ public @Nonnull AppletViewer start() { return showAppletViewerWith(applet, parameters); } }