Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Nov 10, 2024
1 parent e591b40 commit 8888252
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public String getPreview(boolean i, ChangeBehavior cb) {
preview.append(System.lineSeparator());
}
}
return preview.toString();
return preview.toString()+System.lineSeparator();
}
/**
* Compute set of CompilationUnitRewriteOperation to refactor supported situations using default encoding to make use of explicit calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@

import org.eclipse.text.edits.TextEditGroup;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.LineComment;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;

import org.eclipse.jdt.internal.common.HelperVisitor;
Expand Down Expand Up @@ -91,6 +97,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation
TextEditGroup group, ChangeBehavior cb, ReferenceHolder<ASTNode, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
ImportRewrite importRewriter= cuRewrite.getImportRewrite();
Nodedata nodedata= (Nodedata) data.get(visited);
ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding);
/**
Expand All @@ -99,9 +106,74 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation
ListRewrite listRewrite= rewrite.getListRewrite(visited, ClassInstanceCreation.ARGUMENTS_PROPERTY);
if (nodedata.replace) {
listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group);
// Remove NLS comment
// removeNLSComment(visited, cuRewrite, group);
} else {
listRewrite.insertLast(callToCharsetDefaultCharset, group);
}
removeUnsupportedEncodingException(visited, group, rewrite, importRewriter);
}

@SuppressWarnings("unused")
private void removeNLSComment(ASTNode node, CompilationUnitRewrite cuRewrite, TextEditGroup group) {
CompilationUnit unit = cuRewrite.getRoot();
ASTRewrite rewrite = cuRewrite.getASTRewrite();

// Liste aller Kommentare in der CompilationUnit
List<Comment> comments = unit.getCommentList();
boolean removed = false;

for (Comment comment : comments) {
if (comment instanceof LineComment) {
// Hole den Text des Kommentars
String commentContent = getCommentContent(comment, cuRewrite);
System.out.println("Checking comment: " + commentContent); //$NON-NLS-1$

if (commentContent != null && commentContent.contains("$NON-NLS-")) { //$NON-NLS-1$
// Stelle sicher, dass der Kommentar nach dem gegebenen node kommt
if (comment.getStartPosition() > node.getStartPosition()) {
// Versuche, den Kommentar zu entfernen
ASTNode parent = comment.getParent();
if (parent != null) {
StructuralPropertyDescriptor property = comment.getLocationInParent();
if (property != null) {
// Kommentar in seiner Elternstruktur entfernen
if (property.isChildListProperty()) {
ListRewrite listRewrite = rewrite.getListRewrite(parent, (ChildListPropertyDescriptor) property);
System.out.println("Removing comment at position: " + comment.getStartPosition()); //$NON-NLS-1$
listRewrite.remove(comment, group);
removed = true;
} else {
rewrite.remove(comment, group);
removed = true;
}
} else {
System.err.println("No valid location found for comment."); //$NON-NLS-1$
}
}
}
}
}
}

if (!removed) {
System.out.println("No NLS comment found to remove."); //$NON-NLS-1$
}
}

private String getCommentContent(Comment comment, CompilationUnitRewrite cuRewrite) {
try {
int startPosition = comment.getStartPosition();
int length = comment.getLength();

ICompilationUnit icu = (ICompilationUnit) cuRewrite.getRoot().getJavaElement();
if (icu != null) {
return icu.getSource().substring(startPosition, startPosition + length);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation
@Override
public String getPreview(boolean afterRefactoring, ChangeBehavior cb) {
if (afterRefactoring) {
return "Stream w=new PrintStream(\"out.txt\"," + cb.computeCharsetforPreview() + "));\n" + //$NON-NLS-1$ //$NON-NLS-2$
"Stream w=new PrintStream(\"out.txt\",StandardCharsets.UTF_8));\n" + //$NON-NLS-1$
"Stream w=new PrintStream(new File(\"out.txt\"),StandardCharsets.UTF_8));\n"; //$NON-NLS-1$
return "Stream w=new PrintStream(\"out.txt\"," + cb.computeCharsetforPreview() + ");\n" + //$NON-NLS-1$ //$NON-NLS-2$
"Stream w=new PrintStream(\"out.txt\",StandardCharsets.UTF_8);\n" + //$NON-NLS-1$
"Stream w=new PrintStream(new File(\"out.txt\"),StandardCharsets.UTF_8);\n"; //$NON-NLS-1$
}
return """
Stream w=new PrintStream("out.txt");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,51 +231,146 @@ void method(String filename) {
}
"""),
INPUTSTREAMREADER(
"""
package test1;
"""
package test1;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.Reader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
public class E1 {
void method(String filename) {
try {
InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt")); //$NON-NLS-1$
InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), "UTF-8"); //$NON-NLS-1$
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
""",
public class E1 {
"""
package test1;
void method(String filename) {
try {
// Standardkonstruktor ohne Encoding
InputStreamReader is1 = new InputStreamReader(new FileInputStream("file1.txt")); //$NON-NLS-1$
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.io.FileNotFoundException;
// String Literal Encodings, die nach StandardCharsets umgeschrieben werden sollten
InputStreamReader is2 = new InputStreamReader(new FileInputStream("file2.txt"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
InputStreamReader is3 = new InputStreamReader(new FileInputStream("file3.txt"), "ISO-8859-1"); //$NON-NLS-1$ //$NON-NLS-2$
InputStreamReader is4 = new InputStreamReader(new FileInputStream("file4.txt"), "US-ASCII"); //$NON-NLS-1$ //$NON-NLS-2$
public class E1 {
void method(String filename) {
try {
InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt"), Charset.defaultCharset()); //$NON-NLS-1$
InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
"""),
// String-basiertes Encoding, das in Charset umgeschrieben werden kann, jedoch ohne vordefinierte Konstante
InputStreamReader is5 = new InputStreamReader(new FileInputStream("file5.txt"), "UTF-16"); //$NON-NLS-1$ //$NON-NLS-2$
// String-basierte Encodings mit Groß-/Kleinschreibungsvarianten
InputStreamReader is6 = new InputStreamReader(new FileInputStream("file6.txt"), "utf-8"); //$NON-NLS-1$ //$NON-NLS-2$
InputStreamReader is7 = new InputStreamReader(new FileInputStream("file7.txt"), "Utf-8"); //$NON-NLS-1$ //$NON-NLS-2$
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace(); // Sollte nach Cleanup entfernt werden
}
}
void methodWithTryCatch(String filename) {
try {
// Variante, bei der UnsupportedEncodingException behandelt wird
InputStreamReader is8 = new InputStreamReader(new FileInputStream("file8.txt"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace(); // Sollte nach Cleanup entfernt werden
}
}
void methodWithoutException(String filename) throws UnsupportedEncodingException, FileNotFoundException {
// Case ohne Try-Catch-Block, sollte Charset-Konstanten direkt ersetzen
InputStreamReader is9 = new InputStreamReader(new FileInputStream("file9.txt"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
}
void methodWithVariableEncoding(String filename) throws UnsupportedEncodingException, FileNotFoundException {
// Case, bei dem das Encoding aus einer Variablen kommt, Cleanup sollte hier keine Änderungen machen
String encoding = "UTF-8"; //$NON-NLS-1$
InputStreamReader is10 = new InputStreamReader(new FileInputStream("file10.txt"), encoding); //$NON-NLS-1$
}
void methodWithNonStandardEncoding(String filename) {
try {
// Case mit nicht vordefiniertem Charset, sollte keine Umwandlung in StandardCharsets erfolgen
InputStreamReader is11 = new InputStreamReader(new FileInputStream("file11.txt"), "windows-1252"); //$NON-NLS-1$ //$NON-NLS-2$
} catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
// Methode mit "throws UnsupportedEncodingException" zur Prüfung des Cleanups
void methodWithThrows(String filename) throws FileNotFoundException, UnsupportedEncodingException {
InputStreamReader is3 = new InputStreamReader(new FileInputStream(filename), "UTF-8"); //$NON-NLS-1$
}
}
""",

"""
package test1;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class E1 {
void method(String filename) {
try {
// Standardkonstruktor ohne Encoding
InputStreamReader is1 = new InputStreamReader(new FileInputStream("file1.txt"), Charset.defaultCharset()); //$NON-NLS-1$
// String Literal Encodings, die nach StandardCharsets umgeschrieben werden sollten
InputStreamReader is2 = new InputStreamReader(new FileInputStream("file2.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$
InputStreamReader is3 = new InputStreamReader(new FileInputStream("file3.txt"), StandardCharsets.ISO_8859_1); //$NON-NLS-1$ //$NON-NLS-2$
InputStreamReader is4 = new InputStreamReader(new FileInputStream("file4.txt"), StandardCharsets.US_ASCII); //$NON-NLS-1$ //$NON-NLS-2$
// String-basiertes Encoding, das in Charset umgeschrieben werden kann, jedoch ohne vordefinierte Konstante
InputStreamReader is5 = new InputStreamReader(new FileInputStream("file5.txt"), StandardCharsets.UTF_16); //$NON-NLS-1$ //$NON-NLS-2$
// String-basierte Encodings mit Groß-/Kleinschreibungsvarianten
InputStreamReader is6 = new InputStreamReader(new FileInputStream("file6.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$
InputStreamReader is7 = new InputStreamReader(new FileInputStream("file7.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void methodWithTryCatch(String filename) {
try {
// Variante, bei der UnsupportedEncodingException behandelt wird
InputStreamReader is8 = new InputStreamReader(new FileInputStream("file8.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void methodWithoutException(String filename) throws FileNotFoundException {
// Case ohne Try-Catch-Block, sollte Charset-Konstanten direkt ersetzen
InputStreamReader is9 = new InputStreamReader(new FileInputStream("file9.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$
}
void methodWithVariableEncoding(String filename) throws UnsupportedEncodingException, FileNotFoundException {
// Case, bei dem das Encoding aus einer Variablen kommt, Cleanup sollte hier keine Änderungen machen
String encoding = "UTF-8"; //$NON-NLS-1$
InputStreamReader is10 = new InputStreamReader(new FileInputStream("file10.txt"), encoding); //$NON-NLS-1$
}
void methodWithNonStandardEncoding(String filename) {
try {
// Case mit nicht vordefiniertem Charset, sollte keine Umwandlung in StandardCharsets erfolgen
InputStreamReader is11 = new InputStreamReader(new FileInputStream("file11.txt"), "windows-1252"); //$NON-NLS-1$ //$NON-NLS-2$
} catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
// Methode mit "throws UnsupportedEncodingException" zur Prüfung des Cleanups
void methodWithThrows(String filename) throws FileNotFoundException {
InputStreamReader is3 = new InputStreamReader(new FileInputStream(filename), StandardCharsets.UTF_8); //$NON-NLS-1$
}
}
"""),
OUTPUTSTREAMWRITER(
"""
package test1;
Expand Down

0 comments on commit 8888252

Please sign in to comment.