PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/parser-java/src/test/java/org/jboss/forge/test/parser/java/FieldTest.java

https://github.com/includeeasy/core
Java | 270 lines | 210 code | 36 blank | 24 comment | 0 complexity | 2b947fa10e7b5e7c520be7615430c2e5 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2010, Red Hat, Inc., and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.forge.test.parser.java;
  23. import static org.junit.Assert.assertEquals;
  24. import static org.junit.Assert.assertFalse;
  25. import static org.junit.Assert.assertTrue;
  26. import java.io.InputStream;
  27. import org.jboss.forge.parser.JavaParser;
  28. import org.jboss.forge.parser.java.Field;
  29. import org.jboss.forge.parser.java.JavaClass;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32. /**
  33. * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
  34. */
  35. public class FieldTest
  36. {
  37. private InputStream stream;
  38. private JavaClass javaClass;
  39. private Field<JavaClass> field;
  40. @Before
  41. public void reset()
  42. {
  43. stream = FieldTest.class.getResourceAsStream("/org/jboss/forge/grammar/java/MockAnnotatedField.java");
  44. javaClass = JavaParser.parse(JavaClass.class, stream);
  45. field = javaClass.getFields().get(javaClass.getFields().size() - 1);
  46. }
  47. @Test
  48. public void testParse() throws Exception
  49. {
  50. assertTrue(field instanceof Field);
  51. assertEquals("field", field.getName());
  52. assertEquals("String", field.getType());
  53. }
  54. @Test
  55. public void testSetName() throws Exception
  56. {
  57. assertEquals("field", field.getName());
  58. field.setName("newName");
  59. field.getOrigin();
  60. assertTrue(field.toString().contains("newName;"));
  61. assertEquals("newName", field.getName());
  62. }
  63. @Test
  64. public void testSetNameWithReservedWordPart() throws Exception
  65. {
  66. assertEquals("field", field.getName());
  67. field.setName("privateIpAddress");
  68. assertTrue(javaClass.hasField("privateIpAddress"));
  69. }
  70. @Test
  71. public void testIsTypeChecksImports() throws Exception
  72. {
  73. Field<JavaClass> field = javaClass.addField().setType(FieldTest.class).setPublic().setName("test");
  74. assertTrue(field.isType(FieldTest.class));
  75. assertTrue(field.isType(FieldTest.class.getName()));
  76. assertTrue(javaClass.hasImport(FieldTest.class));
  77. }
  78. @Test
  79. public void testIsTypeChecksImportsIgnoresJavaLang() throws Exception
  80. {
  81. Field<JavaClass> field = javaClass.addField("private Boolean bar;").setPublic().setName("test");
  82. assertTrue(field.isType(Boolean.class));
  83. assertTrue(field.isType("Boolean"));
  84. assertTrue(field.isType(Boolean.class.getName()));
  85. assertFalse(javaClass.hasImport(Boolean.class));
  86. }
  87. @Test
  88. public void testIsTypeStringChecksImports() throws Exception
  89. {
  90. Field<JavaClass> field = javaClass.addField().setType(FieldTest.class.getName()).setPublic().setName("test");
  91. assertTrue(field.isType(FieldTest.class.getSimpleName()));
  92. assertTrue(javaClass.hasImport(FieldTest.class));
  93. }
  94. @Test
  95. public void testIsTypeChecksImportsTypes() throws Exception
  96. {
  97. Field<JavaClass> field = javaClass.addField("private org.jboss.FieldTest test;");
  98. Field<JavaClass> field2 = javaClass.addField().setType(FieldTest.class).setName("test2").setPrivate();
  99. assertTrue(field.isType(FieldTest.class.getSimpleName()));
  100. assertFalse(field.isType(FieldTest.class));
  101. assertTrue(field.isType("org.jboss.FieldTest"));
  102. assertTrue(field2.isType(FieldTest.class.getSimpleName()));
  103. assertTrue(field2.isType(FieldTest.class));
  104. assertFalse(field2.isType("org.jboss.FieldTest"));
  105. }
  106. @Test
  107. public void testSetTypeSimpleNameDoesNotAddImport() throws Exception
  108. {
  109. Field<JavaClass> field = javaClass.addField().setType(FieldTest.class.getSimpleName()).setPublic()
  110. .setName("test");
  111. assertFalse(field.isType(FieldTest.class));
  112. assertFalse(javaClass.hasImport(FieldTest.class));
  113. }
  114. @Test
  115. public void testSetType() throws Exception
  116. {
  117. assertEquals("field", field.getName());
  118. field.setType(FieldTest.class);
  119. field.getOrigin();
  120. assertTrue(field.toString().contains("FieldTest"));
  121. assertEquals(FieldTest.class.getSimpleName(), field.getType());
  122. }
  123. @Test
  124. public void testSetTypeStringIntPrimitive() throws Exception
  125. {
  126. assertEquals("field", field.getName());
  127. field.setType("int");
  128. field.getOrigin();
  129. assertTrue(field.toString().contains("int"));
  130. assertEquals("int", field.getType());
  131. }
  132. @Test
  133. public void testSetTypeClassIntPrimitive() throws Exception
  134. {
  135. assertEquals("field", field.getName());
  136. field.setType(int.class.getName());
  137. field.getOrigin();
  138. assertTrue(field.toString().contains("int"));
  139. assertEquals("int", field.getType());
  140. }
  141. @Test
  142. public void testSetTypeString() throws Exception
  143. {
  144. assertEquals("field", field.getName());
  145. field.setType("FooBarType");
  146. field.getOrigin();
  147. assertTrue(field.toString().contains("FooBarType"));
  148. assertEquals("FooBarType", field.getType());
  149. }
  150. @Test
  151. public void testAddField() throws Exception
  152. {
  153. javaClass.addField("public Boolean flag = false;");
  154. Field<JavaClass> fld = javaClass.getFields().get(javaClass.getFields().size() - 1);
  155. fld.getOrigin();
  156. assertTrue(fld.toString().contains("Boolean"));
  157. assertEquals("Boolean", fld.getType());
  158. assertEquals("flag", fld.getName());
  159. assertEquals("false", fld.getLiteralInitializer());
  160. }
  161. @Test
  162. public void testAddFieldWithVisibilityScope() throws Exception
  163. {
  164. javaClass.addField("private String privateIpAddress;");
  165. assertTrue(javaClass.hasField("privateIpAddress"));
  166. }
  167. @Test
  168. public void testIsPrimitive() throws Exception
  169. {
  170. Field<JavaClass> objectField = javaClass.addField("public Boolean flag = false;");
  171. Field<JavaClass> primitiveField = javaClass.addField("public boolean flag = false;");
  172. assertFalse(objectField.isPrimitive());
  173. assertTrue(primitiveField.isPrimitive());
  174. }
  175. @Test
  176. public void testAddFieldInitializerLiteral() throws Exception
  177. {
  178. javaClass.addField("public int flag;").setLiteralInitializer("1234").setPrivate();
  179. Field<JavaClass> fld = javaClass.getFields().get(javaClass.getFields().size() - 1);
  180. assertEquals("int", fld.getType());
  181. assertEquals("flag", fld.getName());
  182. assertEquals("1234", fld.getLiteralInitializer());
  183. assertEquals("1234", fld.getStringInitializer());
  184. assertEquals("private int flag=1234;", fld.toString().trim());
  185. }
  186. @Test
  187. public void testAddFieldInitializerLiteralIgnoresTerminator() throws Exception
  188. {
  189. javaClass.addField("public int flag;").setLiteralInitializer("1234;").setPrivate();
  190. Field<JavaClass> fld = javaClass.getFields().get(javaClass.getFields().size() - 1);
  191. assertEquals("int", fld.getType());
  192. assertEquals("flag", fld.getName());
  193. assertEquals("1234", fld.getLiteralInitializer());
  194. assertEquals("1234", fld.getStringInitializer());
  195. assertEquals("private int flag=1234;", fld.toString().trim());
  196. }
  197. @Test
  198. public void testAddFieldInitializerString() throws Exception
  199. {
  200. javaClass.addField("public String flag;").setStringInitializer("american");
  201. Field<JavaClass> fld = javaClass.getFields().get(javaClass.getFields().size() - 1);
  202. fld.getOrigin();
  203. assertEquals("String", fld.getType());
  204. assertEquals("flag", fld.getName());
  205. assertEquals("\"american\"", fld.getLiteralInitializer());
  206. assertEquals("american", fld.getStringInitializer());
  207. assertEquals("public String flag=\"american\";", fld.toString().trim());
  208. }
  209. @Test
  210. public void testAddQualifiedFieldType() throws Exception
  211. {
  212. javaClass.addField().setName("flag").setType(String.class.getName()).setStringInitializer("american")
  213. .setPrivate();
  214. Field<JavaClass> fld = javaClass.getFields().get(javaClass.getFields().size() - 1);
  215. fld.getOrigin();
  216. assertEquals(String.class.getSimpleName(), fld.getType());
  217. assertFalse(javaClass.hasImport(String.class));
  218. assertEquals("flag", fld.getName());
  219. assertEquals("\"american\"", fld.getLiteralInitializer());
  220. assertEquals("american", fld.getStringInitializer());
  221. assertEquals("private String flag=\"american\";", fld.toString().trim());
  222. }
  223. @Test
  224. public void testHasField() throws Exception
  225. {
  226. javaClass.addField().setName("flag").setType(String.class.getName()).setStringInitializer("american")
  227. .setPrivate();
  228. Field<JavaClass> fld = javaClass.getFields().get(javaClass.getFields().size() - 1);
  229. assertTrue(javaClass.hasField(fld));
  230. Field<JavaClass> notFld = JavaParser.parse(JavaClass.class, "public class Foo {}")
  231. .addField("private int foobar;");
  232. assertFalse(javaClass.hasField(notFld));
  233. }
  234. }