Skip to content

Commit

Permalink
Replace "UTF8" with StandardCharsets.UTF_8
Browse files Browse the repository at this point in the history
Avoids NON-NLS and possible UnsupportedEncodingException
  • Loading branch information
EcljpseB0T committed Jul 2, 2024
1 parent 15d2e1d commit 14cbcef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand Down Expand Up @@ -709,7 +709,7 @@ public static void writeRefactoringSession(final OutputStream stream, final Refa
}

private static void writeNode(final OutputStream stream, Document document) {
try (DOMWriter writer= new DOMWriter(new OutputStreamWriter(stream, Charset.forName("UTF-8")))){ //$NON-NLS-1$
try (DOMWriter writer= new DOMWriter(new OutputStreamWriter(stream, StandardCharsets.UTF_8))) {
writer.printDocument(document);
writer.flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -49,42 +48,34 @@ private static void compareReadHistory(RefactoringDescriptor[] descriptors, int
for (RefactoringDescriptor descriptor : descriptors) {
list.add(new RefactoringDescriptorProxyAdapter(descriptor));
}
try {
ByteArrayInputStream stream= null;
if (ioException) {
stream= new ByteArrayInputStream(xml.getBytes("utf-8")) {

@Override
public int read(byte[] b) throws IOException {
throw new IOException();
}
};
} else
stream= new ByteArrayInputStream(xml.getBytes("utf-8"));
RefactoringHistory result= RefactoringCore.getHistoryService().readRefactoringHistory(stream, flags);
RefactoringDescriptorProxy[] actualProxies= result.getDescriptors();
RefactoringDescriptorProxy[] expectedProxies= list.toArray(new RefactoringDescriptorProxy[list.size()]);
assertEquals("The number of refactoring descriptors is incorrect.", expectedProxies.length, actualProxies.length);
for (int index= 0; index < expectedProxies.length; index++) {
RefactoringDescriptor expectedDescriptor= expectedProxies[index].requestDescriptor(null);
assertNotNull("Expected refactoring descriptor cannot be resolved.", expectedDescriptor);
RefactoringDescriptor actualDescriptor= actualProxies[index].requestDescriptor(null);
assertNotNull("Actual refactoring descriptor cannot be resolved.", actualDescriptor);
assertEquals("Expected refactoring descriptor is not equal to actual one:", expectedDescriptor.toString(), actualDescriptor.toString());
}
} catch (UnsupportedEncodingException exception) {
fail("Unsupported encoding for ByteArrayOutputStream.");
ByteArrayInputStream stream= null;
if (ioException) {
stream= new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)) {

@Override
public int read(byte[] b) throws IOException {
throw new IOException();
}
};
} else
stream= new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
RefactoringHistory result= RefactoringCore.getHistoryService().readRefactoringHistory(stream, flags);
RefactoringDescriptorProxy[] actualProxies= result.getDescriptors();
RefactoringDescriptorProxy[] expectedProxies= list.toArray(new RefactoringDescriptorProxy[list.size()]);
assertEquals("The number of refactoring descriptors is incorrect.", expectedProxies.length, actualProxies.length);
for (int index= 0; index < expectedProxies.length; index++) {
RefactoringDescriptor expectedDescriptor= expectedProxies[index].requestDescriptor(null);
assertNotNull("Expected refactoring descriptor cannot be resolved.", expectedDescriptor);
RefactoringDescriptor actualDescriptor= actualProxies[index].requestDescriptor(null);
assertNotNull("Actual refactoring descriptor cannot be resolved.", actualDescriptor);
assertEquals("Expected refactoring descriptor is not equal to actual one:", expectedDescriptor.toString(), actualDescriptor.toString());
}
}

private static void compareWrittenDescriptor(RefactoringSessionDescriptor descriptor, boolean time, String xml) throws CoreException {
ByteArrayOutputStream stream= new ByteArrayOutputStream();
RefactoringCore.getHistoryService().writeRefactoringSession(descriptor, stream, time);
try {
assertEquals("The refactoring descriptor has not been correctly serialized:", convertLineDelimiters(xml), stream.toString("utf-8"));
} catch (UnsupportedEncodingException exception) {
fail("Unsupported encoding for ByteArrayOutputStream.");
}
assertEquals("The refactoring descriptor has not been correctly serialized:", convertLineDelimiters(xml), stream.toString(StandardCharsets.UTF_8));
}

private static String concatenate(String[] lines, String delimiter) {
Expand Down

0 comments on commit 14cbcef

Please sign in to comment.