/** * Escapes a string value to a literal that can be used in Java source. * * @param stringValue the string value * @return the escaped string * @see StringLiteral#getEscapedValue() */ public static String getEscapedStringLiteral(String stringValue) { StringLiteral stringLiteral= AST.newAST(IASTSharedValues.SHARED_AST_LEVEL).newStringLiteral(); stringLiteral.setLiteralValue(stringValue); return stringLiteral.getEscapedValue(); }
/** * Escapes a string value to a literal that can be used in Java source. * * @param stringValue the string value * @return the escaped string * @see StringLiteral#getEscapedValue() */ public static String getEscapedStringLiteral(String stringValue) { StringLiteral stringLiteral= AST.newAST(ASTProvider.SHARED_AST_LEVEL).newStringLiteral(); stringLiteral.setLiteralValue(stringValue); return stringLiteral.getEscapedValue(); }
/** * Escapes a character value to a literal that can be used in Java source. * * @param ch the character value * @return the escaped string * @see CharacterLiteral#getEscapedValue() */ public static String getEscapedCharacterLiteral(char ch) { CharacterLiteral characterLiteral= AST.newAST(IASTSharedValues.SHARED_AST_LEVEL).newCharacterLiteral(); characterLiteral.setCharValue(ch); return characterLiteral.getEscapedValue(); }
/** * Escapes a character value to a literal that can be used in Java source. * * @param ch the character value * @return the escaped string * @see CharacterLiteral#getEscapedValue() */ public static String getEscapedCharacterLiteral(char ch) { CharacterLiteral characterLiteral= AST.newAST(ASTProvider.SHARED_AST_LEVEL).newCharacterLiteral(); characterLiteral.setCharValue(ch); return characterLiteral.getEscapedValue(); }
static class IntArrayList { public int[] list = new int[5]; public int length = 0; public void add(int i) { if (this.list.length == this.length) { System.arraycopy(this.list, 0, this.list = new int[this.length*2], 0, this.length); } this.list[this.length++] = i; } }
@SuppressWarnings("unchecked") @Test public void testGenerateClassCheckedExceptionWithTemplate() { TemplateSignature templateSignature = mock(TemplateSignature.class); EList<TemplateParameter> templateParameters = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get()); when(clazz.getOwnedTemplateSignature()).thenReturn(templateSignature); when(templateSignature.getParameters()).thenReturn(templateParameters); AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); TypeDeclaration typeDeclaration = exceptionGenerator.generateClass( clazz, ast, cu); assertEquals(typeDeclaration.toString(), "public class CompanyException extends Exception {\n}\n"); }
static class IntArrayList { public int[] list = new int[5]; public int length = 0; public void add(int i) { if (this.list.length == this.length) { System.arraycopy(this.list, 0, this.list = new int[this.length*2], 0, this.length); } this.list[this.length++] = i; } }
static class IntArrayList { public int[] list = new int[5]; public int length = 0; public void add(int i) { if (this.list.length == this.length) { System.arraycopy(this.list, 0, this.list = new int[this.length*2], 0, this.length); } this.list[this.length++] = i; } }
static class IntArrayList { public int[] list = new int[5]; public int length = 0; public void add(int i) { if (this.list.length == this.length) { System.arraycopy(this.list, 0, this.list = new int[this.length*2], 0, this.length); } this.list[this.length++] = i; } }
static class IntArrayList { public int[] list = new int[5]; public int length = 0; public void add(int i) { if (this.list.length == this.length) { System.arraycopy(this.list, 0, this.list = new int[this.length*2], 0, this.length); } this.list[this.length++] = i; } }
static class IntArrayList { public int[] list = new int[5]; public int length = 0; public void add(int i) { if (this.list.length == this.length) { System.arraycopy(this.list, 0, this.list = new int[this.length*2], 0, this.length); } this.list[this.length++] = i; } }
@Test public void testGeneratePackage() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); enumGenerator.generatePackage(clazz, ast, cu); String onlyPackage = cu.toString(); String expectedResult = "package de.crowdcode.kissmda.testapp.components;\n"; boolean isInside = onlyPackage.contains(expectedResult); assertTrue(isInside); }
@Test public void testGeneratePackage() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); interfaceGenerator.generatePackage(clazz, ast, cu); String onlyPackage = cu.toString(); String expectedResult = "package de.crowdcode.kissmda.testapp.components;\n"; boolean isInside = onlyPackage.contains(expectedResult); assertTrue(isInside); }
@Test public void testGenerateEnum() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); enumGenerator.generateEnum(clazz, ast, cu); assertEquals("public enum Company {}\n", cu.toString()); }
@Test public void testGenerateClassCheckedExceptionWithNoInheritance() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); TypeDeclaration typeDeclaration = exceptionGenerator.generateClass( clazz, ast, cu); assertEquals(typeDeclaration.toString(), "public class CompanyException extends Exception {\n}\n"); }
@Test public void testGenerateConstructorWithParams() { AST ast = AST.newAST(AST.JLS3); TypeDeclaration td = ast.newTypeDeclaration(); td.setName(ast.newSimpleName("CompanyException")); when(clazz.getName()).thenReturn("CompanyException"); exceptionGenerator.generateConstructorWithParams(clazz, ast, td, new String[] { "String", "Throwable" }, new String[] { "message", "cause" }); assertEquals( "class CompanyException {\n" + " public CompanyException( String message, Throwable cause){\n" + " super(message,cause);\n }\n" + "}\n", td.toString()); } }
@Test public void testGenerateConstructors() { AST ast = AST.newAST(AST.JLS3); TypeDeclaration td = ast.newTypeDeclaration(); td.setName(ast.newSimpleName("CompanyException")); when(clazz.getName()).thenReturn("CompanyException"); exceptionGenerator.generateConstructors(clazz, ast, td); assertEquals( "class CompanyException {\n public CompanyException(){\n }\n" + " public CompanyException( Throwable cause){\n" + " super(cause);\n }\n public CompanyException( String message){\n" + " super(message);\n }\n" + " public CompanyException( String message, Throwable cause){\n" + " super(message,cause);\n }\n" + "}\n", td.toString()); }
@Test public void testGenerateClassUncheckedExceptionWithNoInheritance() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); exceptionGenerator.setCheckedException(false); TypeDeclaration typeDeclaration = exceptionGenerator.generateClass( clazz, ast, cu); assertEquals(typeDeclaration.toString(), "public class CompanyException extends RuntimeException {\n}\n"); }
@Test public void testGenerateSerialVersionUID() { AST ast = AST.newAST(AST.JLS3); TypeDeclaration td = ast.newTypeDeclaration(); td.setName(ast.newSimpleName("Company")); exceptionGenerator.generateSerialVersionUID(clazz, ast, td); assertEquals( "class Company {\n private static final long serialVersionUID=1L;\n}\n", td.toString()); }
@SuppressWarnings("unchecked") @Test public void testGenerateClass() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast .newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); TypeDeclaration typeDeclaration = interfaceGenerator.generateClass( clazz, ast, cu); assertEquals(typeDeclaration.toString(), td.toString()); }