/** * Tests that spacing is correct. * * @throws IOException */ @Test public void spacing_no_constructor() throws IOException { // Class structure JClass c = new JClass("sido.test", "Test"); c.addImport(List.class); c.addImport(ArrayList.class); c.addField("String", "name"); c.addField("List<String>", "otherNames").setInitialisation("new ArrayList<String>()"); c.addMethod("getName", "String").addContent("return name;"); c.addMethod("setName").addParam("String", "value").addContent("this.name = value;"); // Output StringWriter s = new StringWriter(); PrintWriter writer = new PrintWriter(s); // Generation c.write(writer); // Output List<String> actual = readLines(s.toString()); // Gets the expected content List<String> expected = readReference("/jclass/SpacingNoConstructor.java"); // Difference between the two sets Patch diff = DiffUtils.diff(expected, actual); List<Delta> deltas = diff.getDeltas(); if (!deltas.isEmpty()) { // Creates the diff String original = StringUtils.join(expected, "\n"); String revised = StringUtils.join(actual, "\n"); List<String> unifiedDiff = DiffUtils.generateUnifiedDiff(original, revised, expected, diff, 3); String diffDisplay = StringUtils.join(unifiedDiff, "\n"); System.err.println(diffDisplay); fail("spacing-no-constructor"); } }
private void checkJCompUnit() { JCompUnit unit = new JCompUnit("com.acme", "JCompUnitTest.java"); JClass testClass = new JClass("Test"); testClass.addImport("java.util.Vector"); testClass.addMember(new JField(JType.INT, "x")); JField field = null; field = new JField(JType.INT, "_z"); field.getModifiers().setStatic(true); testClass.addField(field); testClass.getStaticInitializationCode().add("_z = 75;"); JClass jcString = new JClass("String"); field = new JField(jcString, "myString"); field.getModifiers().makePrivate(); testClass.addMember(field); // -- create constructor JConstructor cons = testClass.createConstructor(); testClass.addConstructor(cons); cons.getSourceCode().add("this.x = 6;"); JMethod jMethod = new JMethod("getX", JType.INT, null); jMethod.setSourceCode("return this.x;"); testClass.addMethod(jMethod); unit.addClass(testClass); JClass fooClass = new JClass("Foo"); unit.addClass(fooClass); unit.print(getOutputDirectory().toString(), null); assertTrue(new File(getOutputDirectory(), "com/acme/JCompUnitTest.java").exists()); }
/** Parameters in fields */ @Test public void parameters_in_fields() throws IOException { JClass c = new JClass("test", "MyClass"); c.addField(new JClass("test.api", "Model").addParameter(String.class), "model"); assertJClass(c, "/jclass/ParametersInFields.java", "parameters_in_fields"); }
private void checkJClass() { JClass testClass = new JClass("org.acme.JClassTest"); testClass.addSourceCode("// source code"); testClass.addSourceCode("// source code 2"); testClass.addImport("java.util.Vector"); testClass.appendAnnotation("@SuppressWarnings( \"all\" )"); testClass.appendAnnotation("@Deprecated"); testClass.addMember(new JField(JType.INT, "x")); JClass jcString = new JClass("String"); JField field = null; field = new JField(JType.INT, "_z"); field.appendAnnotation("@Deprecated"); field.getModifiers().setStatic(true); testClass.addField(field); testClass.getStaticInitializationCode().add("_z = 75;"); field = new JField(jcString, "myString"); field.getModifiers().makePrivate(); testClass.addMember(field); // generics test JType type = new JCollectionType("java.util.List", jcString, true); field = new JField(type, "generics"); testClass.addMember(field); type = new JCollectionType("java.util.List", jcString, false); field = new JField(type, "noGenerics"); testClass.addMember(field); // -- create constructor JConstructor cons = testClass.createConstructor(); cons.appendAnnotation("@Deprecated"); cons.getSourceCode().add("this.x = 6;"); JMethod jMethod = new JMethod("getX", JType.INT, null); jMethod.appendAnnotation("@Deprecated"); jMethod.setSourceCode("return this.x;"); testClass.addMethod(jMethod); jMethod = new JMethod("setX", null, null); JParameter param = new JParameter(JType.INT, "x"); jMethod.addParameter(param); jMethod.setSourceCode("this.x = x;"); testClass.addMethod(jMethod); jMethod = new JMethod("checkParameterAnotation", JType.INT, null); param = new JParameter(JType.LONG, "param"); param.appendAnnotation("@Test"); param.appendAnnotation("@Test2"); jMethod.addParameter(param); jMethod.setSourceCode("return param;"); testClass.addMethod(jMethod); // -- create inner-class JClass innerClass = testClass.createInnerClass("Foo"); innerClass.appendAnnotation("@Deprecated"); innerClass.addImport("java.util.Hashtable"); innerClass.addMember(new JField(JType.INT, "_type")); field = new JField(jcString, "_name"); field.getModifiers().makePrivate(); innerClass.addMember(field); testClass.createInnerClass("Bar"); // -- create constructor cons = innerClass.createConstructor(); cons.getSourceCode().add("_name = \"foo\";"); jMethod = new JMethod("getName", jcString, null); jMethod.setSourceCode("return _name;"); innerClass.addMethod(jMethod); testClass.print(getOutputDirectory().toString(), null); assertTrue(new File(getOutputDirectory(), "org/acme/JClassTest.java").exists()); }