StyleBundleInvocationHandler implements InvocationHandler { @SuppressWarnings("unchecked") static <T extends StyleBundle> T create(final StyleResolver resolver, final Class<T> type) { requireNonNull(resolver); requireNonNull(type); if (log.isLoggable(Level.FINEST)) { log.finest(String.format("Using style-group: %s for type: %s", resolver.getGroup(), type.getName())); } StyleBundleInvocationHandler handler = new StyleBundleInvocationHandler(type, resolver); return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[]{type}, handler); } StyleBundleInvocationHandler(final Class<? extends StyleBundle> type, final StyleResolver resolver); @Override Object invoke(final Object proxy, final Method method, final Object[] args); @Override String toString();  }
@Test public void bundleMissingStyleGroup() { try { StyleBundleInvocationHandler.create(source, MissingStyleGroupStyles.class); assert false; } catch (StyleBundleInvocationHandler.InvalidStyleGroupException e) { } }
@Test public void bundleDefaultStyle() { Styles styles = StyleBundleInvocationHandler.create(source, Styles.class); AttributedString string = styles.boldRed("foo bar"); System.out.println(string.toAnsi()); assert string.equals(new AttributedString("foo bar", BOLD.foreground(RED))); }
@Test public void bundleDefaultStyleMissing() { Styles styles = StyleBundleInvocationHandler.create(source, Styles.class); try { styles.missingDefaultStyle("foo bar"); assert false; } catch (StyleBundleInvocationHandler.StyleBundleMethodMissingDefaultStyleException e) { } }
@Test public void bundleDefaultStyleMissingButSourceReferenced() { source.set("test", "missingDefaultStyle", "bold"); Styles styles = StyleBundleInvocationHandler.create(source, Styles.class); AttributedString string = styles.missingDefaultStyle("foo bar"); System.out.println(string.toAnsi()); assert string.equals(new AttributedString("foo bar", BOLD)); }
@Test public void bundleStyleNameWithDefaultStyle() { Styles styles = StyleBundleInvocationHandler.create(source, Styles.class); AttributedString string = styles.boldRedObjectWithStyleName("foo bar"); System.out.println(string.toAnsi()); assert string.equals(new AttributedString("foo bar", BOLD.foreground(RED))); }
@Test public void bundleSourcedStyle() { source.set("test", "boldRed", "bold,fg:yellow"); Styles styles = StyleBundleInvocationHandler.create(source, Styles.class); AttributedString string = styles.boldRed("foo bar"); System.out.println(string.toAnsi()); assert string.equals(new AttributedString("foo bar", BOLD.foreground(YELLOW))); }
@Test public void bundleExplicitStyleGroup() { source.set("test2", "boldRed", "bold,fg:yellow"); Styles styles = StyleBundleInvocationHandler.create(new StyleResolver(source, "test2"), Styles.class); AttributedString string = styles.boldRed("foo bar"); System.out.println(string.toAnsi()); assert string.equals(new AttributedString("foo bar", BOLD.foreground(YELLOW))); }
@Test public void bundleMethodValidation() { Styles styles = StyleBundleInvocationHandler.create(source, Styles.class); try { styles.invalidReturn("foo"); assert false; } catch (StyleBundleInvocationHandler.InvalidStyleBundleMethodException e) { } try { styles.notEnoughArguments(); assert false; } catch (StyleBundleInvocationHandler.InvalidStyleBundleMethodException e) { } try { styles.tooManyArguments(1, 2); assert false; } catch (StyleBundleInvocationHandler.InvalidStyleBundleMethodException e) { } }