You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DesignFormatterTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.design;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertTrue;
  19. import java.io.File;
  20. import java.math.BigDecimal;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Date;
  23. import java.util.HashSet;
  24. import java.util.TimeZone;
  25. import org.junit.Assert;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import com.vaadin.data.util.converter.Converter.ConversionException;
  29. import com.vaadin.event.ShortcutAction;
  30. import com.vaadin.event.ShortcutAction.KeyCode;
  31. import com.vaadin.event.ShortcutAction.ModifierKey;
  32. import com.vaadin.server.ExternalResource;
  33. import com.vaadin.server.FileResource;
  34. import com.vaadin.server.FontAwesome;
  35. import com.vaadin.server.FontIcon;
  36. import com.vaadin.server.GenericFontIcon;
  37. import com.vaadin.server.Resource;
  38. import com.vaadin.server.ThemeResource;
  39. import com.vaadin.shared.ApplicationConstants;
  40. import com.vaadin.shared.util.SharedUtil;
  41. import com.vaadin.ui.declarative.DesignFormatter;
  42. /**
  43. * Various tests related to formatter.
  44. *
  45. * @since 7.4
  46. * @author Vaadin Ltd
  47. */
  48. public class DesignFormatterTest {
  49. private DesignFormatter formatter;
  50. @Before
  51. public void setUp() {
  52. // initialise with default classes
  53. formatter = new DesignFormatter();
  54. }
  55. @Test
  56. public void testSupportedClasses() {
  57. for (Class<?> type : new Class<?>[] { boolean.class, char.class,
  58. byte.class, short.class, int.class, long.class, float.class,
  59. double.class, Boolean.class, Character.class, Byte.class,
  60. Short.class, Integer.class, Long.class, Float.class,
  61. Double.class, BigDecimal.class, String.class,
  62. ShortcutAction.class, Date.class, FileResource.class,
  63. ExternalResource.class, ThemeResource.class, Resource.class,
  64. TimeZone.class }) {
  65. assertTrue("not supported " + type.getSimpleName(),
  66. formatter.canConvert(type));
  67. }
  68. }
  69. @Test
  70. public void testBoolean() {
  71. assertEquals("", formatter.format(true));
  72. assertEquals("false", formatter.format(false));
  73. assertEquals(true, formatter.parse("true", boolean.class));
  74. assertEquals(true, formatter.parse("foobar", boolean.class));
  75. assertEquals(true, formatter.parse("", boolean.class));
  76. assertEquals(false, formatter.parse("false", boolean.class));
  77. assertEquals(true, formatter.parse("true", Boolean.class));
  78. assertEquals(true, formatter.parse("foobar", Boolean.class));
  79. assertEquals(true, formatter.parse("", Boolean.class));
  80. assertEquals(false, formatter.parse("false", Boolean.class));
  81. }
  82. @Test
  83. public void testIntegral() {
  84. byte b = 123;
  85. assertEquals("123", formatter.format(b));
  86. assertEquals(b, (byte) formatter.parse("123", byte.class));
  87. assertEquals((Byte) b, formatter.parse("123", Byte.class));
  88. b = -123;
  89. assertEquals("-123", formatter.format(b));
  90. assertEquals(b, (byte) formatter.parse("-123", byte.class));
  91. assertEquals((Byte) b, formatter.parse("-123", Byte.class));
  92. short s = 12345;
  93. assertEquals("12345", formatter.format(s));
  94. assertEquals(s, (short) formatter.parse("12345", short.class));
  95. assertEquals((Short) s, formatter.parse("12345", Short.class));
  96. s = -12345;
  97. assertEquals("-12345", formatter.format(s));
  98. assertEquals(s, (short) formatter.parse("-12345", short.class));
  99. assertEquals((Short) s, formatter.parse("-12345", Short.class));
  100. int i = 123456789;
  101. assertEquals("123456789", formatter.format(i));
  102. assertEquals(i, (int) formatter.parse("123456789", int.class));
  103. assertEquals((Integer) i, formatter.parse("123456789", Integer.class));
  104. i = -123456789;
  105. assertEquals("-123456789", formatter.format(i));
  106. assertEquals(i, (int) formatter.parse("-123456789", int.class));
  107. assertEquals((Integer) i, formatter.parse("-123456789", Integer.class));
  108. long l = 123456789123456789L;
  109. assertEquals("123456789123456789", formatter.format(l));
  110. assertEquals(l,
  111. (long) formatter.parse("123456789123456789", long.class));
  112. assertEquals((Long) l,
  113. formatter.parse("123456789123456789", Long.class));
  114. l = -123456789123456789L;
  115. assertEquals("-123456789123456789", formatter.format(l));
  116. assertEquals(l,
  117. (long) formatter.parse("-123456789123456789", long.class));
  118. assertEquals((Long) l,
  119. formatter.parse("-123456789123456789", Long.class));
  120. }
  121. @Test
  122. public void testFloatingPoint() {
  123. float f = 123.4567f;
  124. assertEquals("123.457", formatter.format(f));
  125. assertEquals(f, formatter.parse("123.4567", float.class), 1e-4);
  126. assertEquals(f, formatter.parse("123.4567", Float.class), 1e-4);
  127. double d = 123456789.123456789;
  128. assertEquals("123456789.123", formatter.format(d));
  129. assertEquals(d, formatter.parse("123456789.123456789", double.class),
  130. 1e-9);
  131. assertEquals(d, formatter.parse("123456789.123456789", Double.class),
  132. 1e-9);
  133. }
  134. @Test
  135. public void testBigDecimal() {
  136. BigDecimal bd = new BigDecimal("123456789123456789.123456789123456789");
  137. assertEquals("123456789123456789.123", formatter.format(bd));
  138. assertEquals(bd, formatter.parse(
  139. "123456789123456789.123456789123456789", BigDecimal.class));
  140. }
  141. @Test
  142. public void testChar() {
  143. char c = '\uABCD';
  144. assertEquals("\uABCD", formatter.format(c));
  145. assertEquals(c, (char) formatter.parse("\uABCD", char.class));
  146. assertEquals((Character) c, formatter.parse("\uABCD", Character.class));
  147. c = 'y';
  148. assertEquals(c, (char) formatter.parse("yes", char.class));
  149. }
  150. @Test
  151. public void testString() {
  152. for (String s : new String[] { "", "foobar", "\uABCD", "驯鹿" }) {
  153. assertEquals(s, formatter.format(s));
  154. assertEquals(s, formatter.parse(s, String.class));
  155. }
  156. }
  157. @Test
  158. public void testDate() throws Exception {
  159. Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2012-02-17");
  160. String formatted = formatter.format(date);
  161. Date result = formatter.parse(formatted, Date.class);
  162. // writing will always give full date string
  163. assertEquals("2012-02-17 00:00:00+0200", formatted);
  164. assertEquals(date, result);
  165. // try short date as well
  166. result = formatter.parse("2012-02-17", Date.class);
  167. assertEquals(date, result);
  168. }
  169. @Test
  170. public void testShortcutActions() {
  171. ShortcutAction action = new ShortcutAction("&^d");
  172. String formatted = formatter.format(action);
  173. // note the space here - it separates key combination from caption
  174. assertEquals("alt-ctrl-d d", formatted);
  175. ShortcutAction result = formatter
  176. .parse(formatted, ShortcutAction.class);
  177. assertTrue(equals(action, result));
  178. }
  179. @Test
  180. public void testShortcutActionNoCaption() {
  181. ShortcutAction action = new ShortcutAction(null, KeyCode.D, new int[] {
  182. ModifierKey.ALT, ModifierKey.CTRL });
  183. String formatted = formatter.format(action);
  184. assertEquals("alt-ctrl-d", formatted);
  185. ShortcutAction result = formatter
  186. .parse(formatted, ShortcutAction.class);
  187. assertTrue(equals(action, result));
  188. }
  189. @Test
  190. public void testInvalidShortcutAction() {
  191. assertInvalidShortcut("-");
  192. assertInvalidShortcut("foo");
  193. assertInvalidShortcut("atl-ctrl");
  194. assertInvalidShortcut("-a");
  195. }
  196. protected void assertInvalidShortcut(String shortcut) {
  197. try {
  198. formatter.parse(shortcut, ShortcutAction.class);
  199. Assert.fail("Invalid shortcut '" + shortcut + "' should throw");
  200. } catch (ConversionException e) {
  201. // expected
  202. }
  203. }
  204. @Test
  205. public void testTimeZone() {
  206. TimeZone zone = TimeZone.getTimeZone("GMT+2");
  207. String formatted = formatter.format(zone);
  208. assertEquals("GMT+02:00", formatted);
  209. TimeZone result = formatter.parse(formatted, TimeZone.class);
  210. assertEquals(zone, result);
  211. // try shorthand notation as well
  212. result = formatter.parse("GMT+2", TimeZone.class);
  213. assertEquals(zone, result);
  214. }
  215. @Test
  216. public void testExternalResource() {
  217. String url = "://example.com/my%20icon.png?a=b";
  218. for (String scheme : new String[] { "http", "https", "ftp", "ftps" }) {
  219. Resource resource = formatter.parse(scheme + url, Resource.class);
  220. assertTrue(scheme + " url should be parsed as ExternalResource",
  221. resource instanceof ExternalResource);
  222. assertEquals("parsed ExternalResource", scheme + url,
  223. ((ExternalResource) resource).getURL());
  224. String formatted = formatter.format(new ExternalResource(scheme
  225. + url));
  226. assertEquals("formatted ExternalResource", scheme + url, formatted);
  227. }
  228. }
  229. @Test
  230. public void testResourceFormat() {
  231. String httpUrl = "http://example.com/icon.png";
  232. String httpsUrl = "https://example.com/icon.png";
  233. String themePath = "icons/icon.png";
  234. String fontAwesomeUrl = "fonticon://FontAwesome/f0f9";
  235. String someOtherFontUrl = "fonticon://SomeOther/F0F9";
  236. String fileSystemPath = "c:\\app\\resources\\icon.png";
  237. assertEquals(httpUrl, formatter.format(new ExternalResource(httpUrl)));
  238. assertEquals(httpsUrl, formatter.format(new ExternalResource(httpsUrl)));
  239. assertEquals(ApplicationConstants.THEME_PROTOCOL_PREFIX + themePath,
  240. formatter.format(new ThemeResource(themePath)));
  241. assertEquals(fontAwesomeUrl, formatter.format(FontAwesome.AMBULANCE));
  242. assertEquals(someOtherFontUrl.toLowerCase(),
  243. formatter.format(new GenericFontIcon("SomeOther", 0xf0f9))
  244. .toLowerCase());
  245. assertEquals(fileSystemPath,
  246. formatter.format(new FileResource(new File(fileSystemPath))));
  247. }
  248. @Test(expected = ConversionException.class)
  249. public void testResourceParseException() {
  250. String someRandomResourceUrl = "random://url";
  251. formatter.parse(someRandomResourceUrl, Resource.class);
  252. }
  253. @Test(expected = ConversionException.class)
  254. public void testResourceFormatException() {
  255. formatter.format(new Resource() { // must use unknown resource type
  256. @Override
  257. public String getMIMEType() {
  258. // TODO Auto-generated method stub
  259. return null;
  260. }
  261. });
  262. }
  263. @Test
  264. public void testResourceParse() {
  265. String httpUrl = "http://example.com/icon.png";
  266. String httpsUrl = "https://example.com/icon.png";
  267. String themePath = "icons/icon.png";
  268. String fontAwesomeUrl = "fonticon://FontAwesome/f0f9";
  269. String someOtherFont = "fonticon://SomeOther/F0F9";
  270. String fontAwesomeUrlOld = "font://AMBULANCE";
  271. String fileSystemPath = "c:\\app\\resources\\icon.png";
  272. assertEquals(new ExternalResource(httpUrl).getURL(),
  273. formatter.parse(httpUrl, ExternalResource.class).getURL());
  274. assertEquals(new ExternalResource(httpsUrl).getURL(),
  275. formatter.parse(httpsUrl, ExternalResource.class).getURL());
  276. assertEquals(
  277. new ThemeResource(themePath),
  278. formatter.parse(ApplicationConstants.THEME_PROTOCOL_PREFIX
  279. + themePath, ThemeResource.class));
  280. assertEquals(FontAwesome.AMBULANCE,
  281. formatter.parse(fontAwesomeUrlOld, FontAwesome.class));
  282. assertEquals(FontAwesome.AMBULANCE,
  283. formatter.parse(fontAwesomeUrl, FontAwesome.class));
  284. assertEquals(new GenericFontIcon("SomeOther", 0xF0F9),
  285. formatter.parse(someOtherFont, FontIcon.class));
  286. assertEquals(
  287. new FileResource(new File(fileSystemPath)).getSourceFile(),
  288. formatter.parse(fileSystemPath, FileResource.class)
  289. .getSourceFile());
  290. }
  291. /**
  292. * A static method to allow comparison two different actions.
  293. *
  294. * @param act
  295. * One action to compare.
  296. * @param other
  297. * Second action to compare.
  298. * @return <b>true</b> when both actions are the same (caption, icon, and
  299. * key combination).
  300. */
  301. public static final boolean equals(ShortcutAction act, ShortcutAction other) {
  302. if (SharedUtil.equals(other.getCaption(), act.getCaption())
  303. && SharedUtil.equals(other.getIcon(), act.getIcon())
  304. && act.getKeyCode() == other.getKeyCode()
  305. && act.getModifiers().length == other.getModifiers().length) {
  306. HashSet<Integer> thisSet = new HashSet<Integer>(
  307. act.getModifiers().length);
  308. // this is a bit tricky comparison, but there is no nice way of
  309. // making int[] into a Set
  310. for (int mod : act.getModifiers()) {
  311. thisSet.add(mod);
  312. }
  313. for (int mod : other.getModifiers()) {
  314. thisSet.remove(mod);
  315. }
  316. return thisSet.isEmpty();
  317. }
  318. return false;
  319. }
  320. }