mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-07-26 08:25:23 -04:00
Complete implementation and testing of @FxmlView support
This commit is contained in:
parent
fe667bc1b2
commit
4ebc0c0e41
10 changed files with 294 additions and 14 deletions
|
@ -17,6 +17,7 @@ version = '0.1.0-SNAPSHOT'
|
||||||
sourceCompatibility = 1.8
|
sourceCompatibility = 1.8
|
||||||
|
|
||||||
sourceSets.main.resources.srcDirs += 'src/main/java'
|
sourceSets.main.resources.srcDirs += 'src/main/java'
|
||||||
|
sourceSets.test.resources.srcDirs += 'src/test/java'
|
||||||
|
|
||||||
mainClassName = "io.bitsquare.app.gui.BitsquareAppMain"
|
mainClassName = "io.bitsquare.app.gui.BitsquareAppMain"
|
||||||
|
|
||||||
|
@ -58,6 +59,7 @@ dependencies {
|
||||||
compile 'org.jetbrains:annotations:13.0'
|
compile 'org.jetbrains:annotations:13.0'
|
||||||
compile 'eu.hansolo.enzo:Enzo:0.1.5'
|
compile 'eu.hansolo.enzo:Enzo:0.1.5'
|
||||||
testCompile 'junit:junit:4.11'
|
testCompile 'junit:junit:4.11'
|
||||||
|
testCompile "org.mockito:mockito-core:1.+"
|
||||||
testCompile 'org.springframework:spring-test:4.1.1.RELEASE'
|
testCompile 'org.springframework:spring-test:4.1.1.RELEASE'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,9 +17,15 @@
|
||||||
|
|
||||||
package viewfx;
|
package viewfx;
|
||||||
|
|
||||||
|
import static java.lang.String.format;
|
||||||
|
|
||||||
public class ViewfxException extends RuntimeException {
|
public class ViewfxException extends RuntimeException {
|
||||||
|
|
||||||
public ViewfxException(Throwable cause, String format, Object... args) {
|
public ViewfxException(Throwable cause, String format, Object... args) {
|
||||||
super(String.format(format, args), cause);
|
super(format(format, args), cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ViewfxException(String format, Object... args) {
|
||||||
|
super(format(format, args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
27
src/main/java/viewfx/view/DefaultPathConvention.java
Normal file
27
src/main/java/viewfx/view/DefaultPathConvention.java
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Bitsquare.
|
||||||
|
*
|
||||||
|
* Bitsquare is free software: you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or (at
|
||||||
|
* your option) any later version.
|
||||||
|
*
|
||||||
|
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||||
|
* License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package viewfx.view;
|
||||||
|
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
|
public class DefaultPathConvention implements FxmlView.PathConvention {
|
||||||
|
@Override
|
||||||
|
public String apply(Class<? extends View> viewClass) {
|
||||||
|
return ClassUtils.convertClassNameToResourcePath(viewClass.getName()).concat(".fxml");
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,7 +23,6 @@ import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
import org.springframework.util.ClassUtils;
|
|
||||||
|
|
||||||
@Target(ElementType.TYPE)
|
@Target(ElementType.TYPE)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@ -40,12 +39,8 @@ public @interface FxmlView {
|
||||||
* By default it is the fully-qualified view class name, converted to a resource path, replacing the
|
* By default it is the fully-qualified view class name, converted to a resource path, replacing the
|
||||||
* {@code .class} suffix replaced with {@code .fxml}.
|
* {@code .class} suffix replaced with {@code .fxml}.
|
||||||
*/
|
*/
|
||||||
Class<? extends Function<Class<? extends View>, String>> convention() default DefaultFxmlPathConvention.class;
|
Class<? extends PathConvention> convention() default DefaultPathConvention.class;
|
||||||
|
|
||||||
static class DefaultFxmlPathConvention implements Function<Class<? extends View>, String> {
|
static interface PathConvention extends Function<Class<? extends View>, String> {
|
||||||
@Override
|
|
||||||
public String apply(Class<? extends View> viewClass) {
|
|
||||||
return ClassUtils.convertClassNameToResourcePath(viewClass.getName()).concat(".fxml");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,13 @@ import viewfx.view.ViewLoader;
|
||||||
|
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import org.springframework.cglib.core.ReflectUtils;
|
||||||
import org.springframework.core.annotation.AnnotationUtils;
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
import static org.springframework.core.annotation.AnnotationUtils.getDefaultValue;
|
||||||
|
|
||||||
public class FxmlViewLoader implements ViewLoader {
|
public class FxmlViewLoader implements ViewLoader {
|
||||||
|
|
||||||
|
@ -46,30 +52,70 @@ public class FxmlViewLoader implements ViewLoader {
|
||||||
this.resourceBundle = resourceBundle;
|
this.resourceBundle = resourceBundle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public View load(Class<?> clazz) {
|
public View load(Class<?> clazz) {
|
||||||
if (!View.class.isAssignableFrom(clazz))
|
if (!View.class.isAssignableFrom(clazz))
|
||||||
throw new IllegalArgumentException("Class must be of generic type Class<? extends View>: " + clazz);
|
throw new IllegalArgumentException("Class must be of generic type Class<? extends View>: " + clazz);
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Class<? extends View> viewClass = (Class<? extends View>) clazz;
|
Class<? extends View> viewClass = (Class<? extends View>) clazz;
|
||||||
|
|
||||||
FxmlView fxmlView = AnnotationUtils.getAnnotation(viewClass, FxmlView.class);
|
FxmlView fxmlView = AnnotationUtils.getAnnotation(viewClass, FxmlView.class);
|
||||||
|
|
||||||
|
final Class<? extends FxmlView.PathConvention> convention;
|
||||||
|
final Class<? extends FxmlView.PathConvention> defaultConvention =
|
||||||
|
(Class<? extends FxmlView.PathConvention>) getDefaultValue(FxmlView.class, "convention");
|
||||||
|
|
||||||
|
final String specifiedLocation;
|
||||||
|
final String defaultLocation = (String) getDefaultValue(FxmlView.class, "location");
|
||||||
|
|
||||||
|
if (fxmlView == null) {
|
||||||
|
convention = defaultConvention;
|
||||||
|
specifiedLocation = defaultLocation;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
convention = fxmlView.convention();
|
||||||
|
specifiedLocation = fxmlView.location();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (convention == null || specifiedLocation == null)
|
||||||
|
throw new IllegalStateException("Convention and location should never be null.");
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String path = fxmlView.convention().newInstance().apply(viewClass);
|
final String resolvedLocation;
|
||||||
return load(viewClass.getClassLoader().getResource(path));
|
if (specifiedLocation.equals(defaultLocation))
|
||||||
|
resolvedLocation = convention.newInstance().apply(viewClass);
|
||||||
|
else
|
||||||
|
resolvedLocation = specifiedLocation;
|
||||||
|
|
||||||
|
URL fxmlUrl = viewClass.getClassLoader().getResource(resolvedLocation);
|
||||||
|
if (fxmlUrl == null)
|
||||||
|
throw new ViewfxException(
|
||||||
|
"Failed to load view class [%s] because FXML file at [%s] could not be loaded " +
|
||||||
|
"as a classpath resource. Does it exist?", viewClass, specifiedLocation);
|
||||||
|
|
||||||
|
return load(fxmlUrl);
|
||||||
} catch (InstantiationException | IllegalAccessException ex) {
|
} catch (InstantiationException | IllegalAccessException ex) {
|
||||||
throw new ViewfxException(ex, "Failed to load View from class %s", viewClass);
|
throw new ViewfxException(ex, "Failed to load view from class %s", viewClass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public View load(URL url) {
|
public View load(URL url) {
|
||||||
|
checkNotNull(url, "FXML URL must not be null");
|
||||||
try {
|
try {
|
||||||
FXMLLoader loader = new FXMLLoader(url, resourceBundle);
|
FXMLLoader loader = new FXMLLoader(url, resourceBundle);
|
||||||
loader.setControllerFactory(viewFactory);
|
loader.setControllerFactory(viewFactory);
|
||||||
loader.load();
|
loader.load();
|
||||||
return loader.getController();
|
Object controller = loader.getController();
|
||||||
|
if (controller == null)
|
||||||
|
throw new ViewfxException("Failed to load view from FXML file at [%s]. " +
|
||||||
|
"Does it declare an fx:controller attribute?", url);
|
||||||
|
if (!(controller instanceof View))
|
||||||
|
throw new ViewfxException("Controller of type [%s] loaded from FXML file at [%s] " +
|
||||||
|
"does not implement [%s] as expected.", controller.getClass(), url, View.class);
|
||||||
|
return (View) controller;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
throw new ViewfxException(ex, "Failed to load View at location %s", url);
|
throw new ViewfxException(ex, "Failed to load view from FXML file at [%s]", url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
<!--
|
||||||
|
~ This file is part of Bitsquare.
|
||||||
|
~
|
||||||
|
~ Bitsquare is free software: you can redistribute it and/or modify it
|
||||||
|
~ under the terms of the GNU Affero General Public License as published by
|
||||||
|
~ the Free Software Foundation, either version 3 of the License, or (at
|
||||||
|
~ your option) any later version.
|
||||||
|
~
|
||||||
|
~ Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
~ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
~ FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||||
|
~ License for more details.
|
||||||
|
~
|
||||||
|
~ You should have received a copy of the GNU Affero General Public License
|
||||||
|
~ along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
-->
|
||||||
|
<?import javafx.scene.layout.*?>
|
||||||
|
<AnchorPane xmlns:fx="http://javafx.com/fxml">
|
||||||
|
|
||||||
|
</AnchorPane>
|
|
@ -0,0 +1,21 @@
|
||||||
|
<!--
|
||||||
|
~ This file is part of Bitsquare.
|
||||||
|
~
|
||||||
|
~ Bitsquare is free software: you can redistribute it and/or modify it
|
||||||
|
~ under the terms of the GNU Affero General Public License as published by
|
||||||
|
~ the Free Software Foundation, either version 3 of the License, or (at
|
||||||
|
~ your option) any later version.
|
||||||
|
~
|
||||||
|
~ Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
~ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
~ FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||||
|
~ License for more details.
|
||||||
|
~
|
||||||
|
~ You should have received a copy of the GNU Affero General Public License
|
||||||
|
~ along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<?import javafx.scene.layout.*?>
|
||||||
|
<AnchorPane fx:id="root" fx:controller="viewfx.view.support.fxml.FxmlViewLoaderTests$MissingFxmlViewAnnotation"
|
||||||
|
xmlns:fx="http://javafx.com/fxml">
|
||||||
|
</AnchorPane>
|
|
@ -0,0 +1,21 @@
|
||||||
|
<!--
|
||||||
|
~ This file is part of Bitsquare.
|
||||||
|
~
|
||||||
|
~ Bitsquare is free software: you can redistribute it and/or modify it
|
||||||
|
~ under the terms of the GNU Affero General Public License as published by
|
||||||
|
~ the Free Software Foundation, either version 3 of the License, or (at
|
||||||
|
~ your option) any later version.
|
||||||
|
~
|
||||||
|
~ Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
~ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
~ FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||||
|
~ License for more details.
|
||||||
|
~
|
||||||
|
~ You should have received a copy of the GNU Affero General Public License
|
||||||
|
~ along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<?import javafx.scene.layout.*?>
|
||||||
|
<AnchorPane fx:id="root" fx:controller="viewfx.view.support.fxml.FxmlViewLoaderTests$WellFormed"
|
||||||
|
xmlns:fx="http://javafx.com/fxml">
|
||||||
|
</AnchorPane>
|
142
src/test/java/viewfx/view/support/fxml/FxmlViewLoaderTests.java
Normal file
142
src/test/java/viewfx/view/support/fxml/FxmlViewLoaderTests.java
Normal file
|
@ -0,0 +1,142 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Bitsquare.
|
||||||
|
*
|
||||||
|
* Bitsquare is free software: you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or (at
|
||||||
|
* your option) any later version.
|
||||||
|
*
|
||||||
|
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||||
|
* License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package viewfx.view.support.fxml;
|
||||||
|
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
import viewfx.ViewfxException;
|
||||||
|
import viewfx.view.FxmlView;
|
||||||
|
import viewfx.view.View;
|
||||||
|
import viewfx.view.ViewFactory;
|
||||||
|
import viewfx.view.ViewLoader;
|
||||||
|
import viewfx.view.support.AbstractView;
|
||||||
|
|
||||||
|
import javafx.fxml.LoadException;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
|
import static org.mockito.Matchers.contains;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
public class FxmlViewLoaderTests {
|
||||||
|
|
||||||
|
private ViewLoader viewLoader;
|
||||||
|
private ViewFactory viewFactory;
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
viewFactory = mock(ViewFactory.class);
|
||||||
|
ResourceBundle resourceBundle = mock(ResourceBundle.class);
|
||||||
|
viewLoader = new FxmlViewLoader(viewFactory, resourceBundle);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@FxmlView
|
||||||
|
static class WellFormed extends AbstractView {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void wellFormedFxmlFileShouldSucceed() {
|
||||||
|
given(viewFactory.call(WellFormed.class)).willReturn(new WellFormed());
|
||||||
|
View view = viewLoader.load(WellFormed.class);
|
||||||
|
assertThat(view, instanceOf(WellFormed.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@FxmlView
|
||||||
|
static class MissingFxController extends AbstractView {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fxmlFileMissingFxControllerAttributeShouldThrow() {
|
||||||
|
thrown.expect(ViewfxException.class);
|
||||||
|
thrown.expectMessage("Does it declare an fx:controller attribute?");
|
||||||
|
viewLoader.load(MissingFxController.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static class MissingFxmlViewAnnotation extends AbstractView {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fxmlViewAnnotationShouldBeOptional() {
|
||||||
|
given(viewFactory.call(MissingFxmlViewAnnotation.class)).willReturn(new MissingFxmlViewAnnotation());
|
||||||
|
View view = viewLoader.load(MissingFxmlViewAnnotation.class);
|
||||||
|
assertThat(view, instanceOf(MissingFxmlViewAnnotation.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static class NonView {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void nonViewClassShouldThrow() {
|
||||||
|
thrown.expect(IllegalArgumentException.class);
|
||||||
|
thrown.expectMessage("Class must be of generic type");
|
||||||
|
viewLoader.load(NonView.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@FxmlView
|
||||||
|
static class Malformed extends AbstractView {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void malformedFxmlFileShouldThrow() {
|
||||||
|
thrown.expect(ViewfxException.class);
|
||||||
|
thrown.expectMessage("Failed to load view from FXML file");
|
||||||
|
thrown.expectCause(instanceOf(LoadException.class));
|
||||||
|
viewLoader.load(Malformed.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@FxmlView
|
||||||
|
static class MissingFxmlFile extends AbstractView {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void missingFxmlFileShouldThrow() {
|
||||||
|
thrown.expect(ViewfxException.class);
|
||||||
|
thrown.expectMessage("Does it exist?");
|
||||||
|
viewLoader.load(MissingFxmlFile.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@FxmlView(location = "unconventionally/located.fxml")
|
||||||
|
static class CustomLocation extends AbstractView {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void customFxmlFileLocationShouldOverrideDefaultConvention() {
|
||||||
|
thrown.expect(ViewfxException.class);
|
||||||
|
thrown.expectMessage("Failed to load view class");
|
||||||
|
thrown.expectMessage("CustomLocation");
|
||||||
|
thrown.expectMessage("[unconventionally/located.fxml] could not be loaded");
|
||||||
|
viewLoader.load(CustomLocation.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue