PageRenderTime 35ms CodeModel.GetById 13ms app.highlight 18ms 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 */
 22package org.jboss.forge.test.parser.java;
 23
 24import static org.junit.Assert.assertEquals;
 25import static org.junit.Assert.assertFalse;
 26import static org.junit.Assert.assertTrue;
 27
 28import java.io.InputStream;
 29
 30import org.jboss.forge.parser.JavaParser;
 31import org.jboss.forge.parser.java.Field;
 32import org.jboss.forge.parser.java.JavaClass;
 33import org.junit.Before;
 34import org.junit.Test;
 35
 36/**
 37 * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
 38 */
 39public class FieldTest
 40{
 41   private InputStream stream;
 42   private JavaClass javaClass;
 43   private Field<JavaClass> field;
 44
 45   @Before
 46   public void reset()
 47   {
 48      stream = FieldTest.class.getResourceAsStream("/org/jboss/forge/grammar/java/MockAnnotatedField.java");
 49      javaClass = JavaParser.parse(JavaClass.class, stream);
 50      field = javaClass.getFields().get(javaClass.getFields().size() - 1);
 51   }
 52
 53   @Test
 54   public void testParse() throws Exception
 55   {
 56      assertTrue(field instanceof Field);
 57      assertEquals("field", field.getName());
 58      assertEquals("String", field.getType());
 59   }
 60
 61   @Test
 62   public void testSetName() throws Exception
 63   {
 64      assertEquals("field", field.getName());
 65      field.setName("newName");
 66      field.getOrigin();
 67      assertTrue(field.toString().contains("newName;"));
 68      assertEquals("newName", field.getName());
 69   }
 70
 71   @Test
 72   public void testSetNameWithReservedWordPart() throws Exception
 73   {
 74      assertEquals("field", field.getName());
 75      field.setName("privateIpAddress");
 76      assertTrue(javaClass.hasField("privateIpAddress"));
 77   }
 78
 79   @Test
 80   public void testIsTypeChecksImports() throws Exception
 81   {
 82      Field<JavaClass> field = javaClass.addField().setType(FieldTest.class).setPublic().setName("test");
 83      assertTrue(field.isType(FieldTest.class));
 84      assertTrue(field.isType(FieldTest.class.getName()));
 85      assertTrue(javaClass.hasImport(FieldTest.class));
 86   }
 87
 88   @Test
 89   public void testIsTypeChecksImportsIgnoresJavaLang() throws Exception
 90   {
 91      Field<JavaClass> field = javaClass.addField("private Boolean bar;").setPublic().setName("test");
 92      assertTrue(field.isType(Boolean.class));
 93      assertTrue(field.isType("Boolean"));
 94      assertTrue(field.isType(Boolean.class.getName()));
 95      assertFalse(javaClass.hasImport(Boolean.class));
 96   }
 97
 98   @Test
 99   public void testIsTypeStringChecksImports() throws Exception
100   {
101      Field<JavaClass> field = javaClass.addField().setType(FieldTest.class.getName()).setPublic().setName("test");
102      assertTrue(field.isType(FieldTest.class.getSimpleName()));
103      assertTrue(javaClass.hasImport(FieldTest.class));
104   }
105
106   @Test
107   public void testIsTypeChecksImportsTypes() throws Exception
108   {
109      Field<JavaClass> field = javaClass.addField("private org.jboss.FieldTest test;");
110      Field<JavaClass> field2 = javaClass.addField().setType(FieldTest.class).setName("test2").setPrivate();
111
112      assertTrue(field.isType(FieldTest.class.getSimpleName()));
113      assertFalse(field.isType(FieldTest.class));
114      assertTrue(field.isType("org.jboss.FieldTest"));
115
116      assertTrue(field2.isType(FieldTest.class.getSimpleName()));
117      assertTrue(field2.isType(FieldTest.class));
118      assertFalse(field2.isType("org.jboss.FieldTest"));
119   }
120
121   @Test
122   public void testSetTypeSimpleNameDoesNotAddImport() throws Exception
123   {
124      Field<JavaClass> field = javaClass.addField().setType(FieldTest.class.getSimpleName()).setPublic()
125               .setName("test");
126      assertFalse(field.isType(FieldTest.class));
127      assertFalse(javaClass.hasImport(FieldTest.class));
128   }
129
130   @Test
131   public void testSetType() throws Exception
132   {
133      assertEquals("field", field.getName());
134      field.setType(FieldTest.class);
135      field.getOrigin();
136      assertTrue(field.toString().contains("FieldTest"));
137      assertEquals(FieldTest.class.getSimpleName(), field.getType());
138   }
139
140   @Test
141   public void testSetTypeStringIntPrimitive() throws Exception
142   {
143      assertEquals("field", field.getName());
144      field.setType("int");
145      field.getOrigin();
146      assertTrue(field.toString().contains("int"));
147      assertEquals("int", field.getType());
148   }
149
150   @Test
151   public void testSetTypeClassIntPrimitive() throws Exception
152   {
153      assertEquals("field", field.getName());
154      field.setType(int.class.getName());
155      field.getOrigin();
156      assertTrue(field.toString().contains("int"));
157      assertEquals("int", field.getType());
158   }
159
160   @Test
161   public void testSetTypeString() throws Exception
162   {
163      assertEquals("field", field.getName());
164      field.setType("FooBarType");
165      field.getOrigin();
166      assertTrue(field.toString().contains("FooBarType"));
167      assertEquals("FooBarType", field.getType());
168   }
169
170   @Test
171   public void testAddField() throws Exception
172   {
173      javaClass.addField("public Boolean flag = false;");
174      Field<JavaClass> fld = javaClass.getFields().get(javaClass.getFields().size() - 1);
175      fld.getOrigin();
176
177      assertTrue(fld.toString().contains("Boolean"));
178      assertEquals("Boolean", fld.getType());
179      assertEquals("flag", fld.getName());
180      assertEquals("false", fld.getLiteralInitializer());
181   }
182
183   @Test
184   public void testAddFieldWithVisibilityScope() throws Exception
185   {
186      javaClass.addField("private String privateIpAddress;");
187      assertTrue(javaClass.hasField("privateIpAddress"));
188   }
189
190   @Test
191   public void testIsPrimitive() throws Exception
192   {
193      Field<JavaClass> objectField = javaClass.addField("public Boolean flag = false;");
194      Field<JavaClass> primitiveField = javaClass.addField("public boolean flag = false;");
195
196      assertFalse(objectField.isPrimitive());
197      assertTrue(primitiveField.isPrimitive());
198
199   }
200
201   @Test
202   public void testAddFieldInitializerLiteral() throws Exception
203   {
204      javaClass.addField("public int flag;").setLiteralInitializer("1234").setPrivate();
205      Field<JavaClass> fld = javaClass.getFields().get(javaClass.getFields().size() - 1);
206
207      assertEquals("int", fld.getType());
208      assertEquals("flag", fld.getName());
209      assertEquals("1234", fld.getLiteralInitializer());
210      assertEquals("1234", fld.getStringInitializer());
211      assertEquals("private int flag=1234;", fld.toString().trim());
212   }
213
214   @Test
215   public void testAddFieldInitializerLiteralIgnoresTerminator() throws Exception
216   {
217      javaClass.addField("public int flag;").setLiteralInitializer("1234;").setPrivate();
218      Field<JavaClass> fld = javaClass.getFields().get(javaClass.getFields().size() - 1);
219
220      assertEquals("int", fld.getType());
221      assertEquals("flag", fld.getName());
222      assertEquals("1234", fld.getLiteralInitializer());
223      assertEquals("1234", fld.getStringInitializer());
224      assertEquals("private int flag=1234;", fld.toString().trim());
225   }
226
227   @Test
228   public void testAddFieldInitializerString() throws Exception
229   {
230      javaClass.addField("public String flag;").setStringInitializer("american");
231      Field<JavaClass> fld = javaClass.getFields().get(javaClass.getFields().size() - 1);
232      fld.getOrigin();
233
234      assertEquals("String", fld.getType());
235      assertEquals("flag", fld.getName());
236      assertEquals("\"american\"", fld.getLiteralInitializer());
237      assertEquals("american", fld.getStringInitializer());
238      assertEquals("public String flag=\"american\";", fld.toString().trim());
239   }
240
241   @Test
242   public void testAddQualifiedFieldType() throws Exception
243   {
244      javaClass.addField().setName("flag").setType(String.class.getName()).setStringInitializer("american")
245               .setPrivate();
246      Field<JavaClass> fld = javaClass.getFields().get(javaClass.getFields().size() - 1);
247      fld.getOrigin();
248
249      assertEquals(String.class.getSimpleName(), fld.getType());
250      assertFalse(javaClass.hasImport(String.class));
251      assertEquals("flag", fld.getName());
252      assertEquals("\"american\"", fld.getLiteralInitializer());
253      assertEquals("american", fld.getStringInitializer());
254      assertEquals("private String flag=\"american\";", fld.toString().trim());
255   }
256
257   @Test
258   public void testHasField() throws Exception
259   {
260      javaClass.addField().setName("flag").setType(String.class.getName()).setStringInitializer("american")
261               .setPrivate();
262      Field<JavaClass> fld = javaClass.getFields().get(javaClass.getFields().size() - 1);
263      assertTrue(javaClass.hasField(fld));
264
265      Field<JavaClass> notFld = JavaParser.parse(JavaClass.class, "public class Foo {}")
266               .addField("private int foobar;");
267      assertFalse(javaClass.hasField(notFld));
268
269   }
270}