Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into jira/HZN-1020
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse White committed Mar 29, 2017
2 parents 3b28685 + b456086 commit c107f1d
Show file tree
Hide file tree
Showing 164 changed files with 730 additions and 615 deletions.
5 changes: 5 additions & 0 deletions checkstyle/src/main/resources/nms_checks.xml
Expand Up @@ -14,5 +14,10 @@
<property name="forbiddenImportsExcludesRegexp" value="" />
<message key="forbid.certain.imports" value="Use ''java.nio.charset.StandardCharsets'' instead of ''{0}''" />
</module>
<module name="IllegalTokenText">
<property name="tokens" value="STRING_LITERAL"/>
<property name="format" value="^UTF-8$"/>
<message key="forbid.UTF8.string.literal" value="Use ''java.nio.charset.StandardCharsets.UTF_8.name()'' instead of ''{0}''" />
</module>
</module>
</module>
Expand Up @@ -34,6 +34,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -93,7 +94,7 @@ public static List<String> fetch(final String url) {

// check to see if the file exists
if (stream != null) {
isr = new InputStreamReader(stream, "UTF-8");
isr = new InputStreamReader(stream, StandardCharsets.UTF_8);
br = new BufferedReader(isr);

String ipLine = null;
Expand Down
Expand Up @@ -37,6 +37,7 @@
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -51,7 +52,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -180,7 +180,7 @@ public InstallerDb() {
* @throws java.lang.Exception if any.
*/
public void readTables() throws Exception {
readTables(new InputStreamReader(new FileInputStream(m_createSqlLocation), "UTF-8"));
readTables(new InputStreamReader(new FileInputStream(m_createSqlLocation), StandardCharsets.UTF_8));
}

/**
Expand Down Expand Up @@ -547,7 +547,7 @@ public void setupPlPgsqlIplike() throws Exception {
throw new Exception(message);
}

final BufferedReader in = new BufferedReader(new InputStreamReader(sqlfile, "UTF-8"));
final BufferedReader in = new BufferedReader(new InputStreamReader(sqlfile, StandardCharsets.UTF_8));
final StringBuffer createFunction = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
Expand Down Expand Up @@ -612,7 +612,7 @@ public void addStoredProcedures() throws Exception {

m_out.print("\n - " + element.getName() + "... ");

final BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(element), "UTF-8"));
final BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(element), StandardCharsets.UTF_8));
while ((line = r.readLine()) != null) {
line = line.trim();

Expand Down
Expand Up @@ -38,7 +38,7 @@ class V4PingReply extends ICMPEchoPacket implements PingReply {

// The below long is equivalent to the next line and is more efficient than
// manipulation as a string
// Charset.forName("US-ASCII").encode("OpenNMS!").getLong(0);
// StandardCharsets.US_ASCII.encode("OpenNMS!").getLong(0);
public static final long COOKIE = 0x4F70656E4E4D5321L;

private long m_receivedTimeNanos;
Expand Down
Expand Up @@ -38,7 +38,7 @@ class V6PingReply extends ICMPv6EchoPacket implements PingReply {

// The below long is equivalent to the next line and is more efficient than
// manipulation as a string
// Charset.forName("US-ASCII").encode("OpenNMS!").getLong(0);
// StandardCharsets.US_ASCII.encode("OpenNMS!").getLong(0);
public static final long COOKIE = 0x4F70656E4E4D5321L;

private long m_receivedTimeNanos;
Expand Down
Expand Up @@ -28,24 +28,26 @@

package org.opennms.jicmp.jna;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.net.InetAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import com.sun.jna.LastErrorException;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.ptr.IntByReference;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestName;


/**
Expand Down Expand Up @@ -101,7 +103,7 @@ public void testPassing() throws Exception {
int socket = -1;
try {
InetAddress svrAddr = server.getInetAddress();
byte[] data = msg.getBytes("US-ASCII");
byte[] data = msg.getBytes(StandardCharsets.US_ASCII);
String sent = msg.substring(4,7);
ByteBuffer buf = ByteBuffer.wrap(data, 4, 3).slice();

Expand Down Expand Up @@ -133,7 +135,7 @@ public void testPassing() throws Exception {
byte[] b = new byte[rBuf.remaining()];
rBuf.get(b);

String results = new String(b, "US-ASCII");
String results = new String(b, StandardCharsets.US_ASCII);

printf("Received: %s\n", results);

Expand Down
Expand Up @@ -28,24 +28,26 @@

package org.opennms.jicmp.jna;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.net.InetAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import com.sun.jna.LastErrorException;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.ptr.IntByReference;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestName;


/**
Expand Down Expand Up @@ -83,11 +85,11 @@ public void tearDown() throws InterruptedException {
}

@Test
public void testWrap() throws Exception {
public void testWrap() {

String msg = "OpenNMS!";

byte[] data = msg.getBytes("US-ASCII");
byte[] data = msg.getBytes(StandardCharsets.US_ASCII);

ByteBuffer buf = ByteBuffer.wrap(data, 2, 4);

Expand All @@ -108,16 +110,11 @@ public void testStringDecoding() {
* accessing the byte array that may or may NOT be behind it
*/

Charset ascii = Charset.forName("US-ASCII");

ByteBuffer buf = ascii.encode("OpenNMS!");
ByteBuffer buf = StandardCharsets.US_ASCII.encode("OpenNMS!");

String decoded = ascii.decode(buf).toString();
String decoded = StandardCharsets.US_ASCII.decode(buf).toString();

assertThat(decoded, is(equalTo("OpenNMS!")));



}

@Test(timeout=30000)
Expand All @@ -137,7 +134,7 @@ public void testPassing() throws Exception {
int socket = -1;
try {

byte[] data = msg.getBytes("US-ASCII");
byte[] data = msg.getBytes(StandardCharsets.US_ASCII);
String sent = msg.substring(4,7);
ByteBuffer buf = ByteBuffer.wrap(data, 4, 3).slice();

Expand All @@ -163,7 +160,7 @@ public void testPassing() throws Exception {
byte[] b = new byte[rBuf.remaining()];
rBuf.get(b);

String results = new String(b, "US-ASCII");
String results = new String(b, StandardCharsets.US_ASCII);

printf("Received: %s\n", results);

Expand Down
Expand Up @@ -35,7 +35,7 @@
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -57,7 +57,6 @@
@Ignore
public class NativeSocketTest {

private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final ExecutorService m_executor = Executors.newCachedThreadPool();

Server m_server;
Expand Down Expand Up @@ -99,7 +98,7 @@ public void testServer() throws Exception {
@Override public DatagramPacket call() throws Exception {
printf("Sending cmd: %s\n", cmd);

final byte[] data = cmd.getBytes("UTF-8");
final byte[] data = cmd.getBytes(StandardCharsets.UTF_8);
final DatagramPacket p = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), sock.getLocalPort());
sock.send(p);

Expand All @@ -116,7 +115,7 @@ public void testServer() throws Exception {
final DatagramPacket r = task.get(10, TimeUnit.SECONDS);
assertNotNull(r);

final String response = new String(r.getData(), r.getOffset(), r.getLength(), "UTF-8");
final String response = new String(r.getData(), r.getOffset(), r.getLength(), StandardCharsets.UTF_8);
printf("Received Response: %s from %s:%d\n", response, r.getAddress().getHostAddress(), r.getPort());
assertEquals(cmd, response);
}
Expand Down Expand Up @@ -148,7 +147,7 @@ private void testNative(final int family, final InetAddress address) throws Exce
final FutureTask<NativeDatagramPacket> task = new FutureTask<NativeDatagramPacket>(new Callable<NativeDatagramPacket>() {
@Override public NativeDatagramPacket call() throws Exception {
printf("Sending cmd: %s\n", cmd);
final ByteBuffer buf = UTF_8.encode(cmd);
final ByteBuffer buf = StandardCharsets.UTF_8.encode(cmd);
final NativeDatagramPacket p = new NativeDatagramPacket(buf, address, m_port);
sock.send(p);

Expand All @@ -166,7 +165,7 @@ private void testNative(final int family, final InetAddress address) throws Exce
final NativeDatagramPacket r = task.get(10, TimeUnit.SECONDS);
assertNotNull(r);

final String response = UTF_8.decode(r.getContent()).toString();
final String response = StandardCharsets.UTF_8.decode(r.getContent()).toString();
printf("Received Response: %s from %s:%d\n", response, r.getAddress().getHostAddress(), r.getPort());

assertEquals(cmd, response);
Expand All @@ -183,7 +182,7 @@ public void testCloseInReceive() throws Exception {
try(final NativeDatagramSocket socket = NativeDatagramSocket.create(NativeDatagramSocket.PF_INET, NativeDatagramSocket.IPPROTO_UDP, 1234)) {
final FutureTask<NativeDatagramPacket> task = new FutureTask<NativeDatagramPacket>(new Callable<NativeDatagramPacket>() {
@Override public NativeDatagramPacket call() throws Exception {
final ByteBuffer buf = UTF_8.encode("msg1");
final ByteBuffer buf = StandardCharsets.UTF_8.encode("msg1");
final NativeDatagramPacket p = new NativeDatagramPacket(buf, InetAddress.getLocalHost(), m_port);
socket.send(p);

Expand All @@ -199,7 +198,7 @@ public void testCloseInReceive() throws Exception {
final NativeDatagramPacket r = task.get(10, TimeUnit.SECONDS);
assertNotNull(r);

final String response = UTF_8.decode(r.getContent()).toString();
final String response = StandardCharsets.UTF_8.decode(r.getContent()).toString();
printf("Received Response: %s from %s:%d\n", response, r.getAddress().getHostAddress(), r.getPort());
}
}
Expand Down
Expand Up @@ -31,6 +31,7 @@
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -87,7 +88,7 @@ public void run() {
while (!m_stopped.get()) {
DatagramPacket p = new DatagramPacket(new byte[128], 128);
m_socket.receive(p);
String cmd = new String(p.getData(), p.getOffset(), p.getLength(), "UTF-8");
String cmd = new String(p.getData(), p.getOffset(), p.getLength(), StandardCharsets.UTF_8);
System.err.print(String.format("SERVER: %s\n", cmd));
m_socket.send(p);
if (cmd.startsWith("quit")) {
Expand Down
Expand Up @@ -36,6 +36,7 @@
import java.io.File;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -217,13 +218,13 @@ public static String prettyXml(String xml) throws UnsupportedEncodingException,
Transformer transformer = transFactory.newTransformer();

// Set options on the transformer so that it will indent the XML properly
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

StreamResult result = new StreamResult(out);
Source source = new StreamSource(new ByteArrayInputStream(xml.getBytes("UTF-8")));
Source source = new StreamSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));

// Run the transformer to put the XML into the StringWriter
transformer.transform(source, result);
Expand Down
Expand Up @@ -32,6 +32,7 @@
import static org.junit.Assert.assertTrue;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import org.junit.Test;

Expand Down Expand Up @@ -59,13 +60,13 @@ public void testAllBytesDisplayable() throws UnsupportedEncodingException {
strings[3] = "56697275732f4d616c776172653a2045696361725f746573745f66696c6520436f6d70757465723a2049542d30303220446f6de46e653a2050632d6974206f686e65206669726577616c6c5c2044617465693a20473a5c6569636172202d204b6f7069655c6569636172202d204b6f7069652e746172202865696361722e636f6d2920446174756d2f5568727a6569743a2030312e31312e323031332031313a34383a34382045726765626e69733a2045732077757264652065696e20566972757320656e746465636b742e2053e4756265726e206e69636874206df6676c6963682e202851756172616e74e46e652920";

for (String string : strings) {
System.out.println(new String(hexStringToBytes(string), "UTF-8"));
System.out.println(new String(hexStringToBytes(string), "ISO-8859-1"));
System.out.println(new String(hexStringToBytes(string), StandardCharsets.UTF_8));
System.out.println(new String(hexStringToBytes(string), StandardCharsets.ISO_8859_1));
}

for (String string : strings) {
System.out.println(new String(hexStringToBytes(string)));
assertTrue(new String(hexStringToBytes(string), "ISO-8859-1"), AbstractSnmpValue.allBytesDisplayable(hexStringToBytes(string)));
assertTrue(new String(hexStringToBytes(string), StandardCharsets.ISO_8859_1), AbstractSnmpValue.allBytesDisplayable(hexStringToBytes(string)));
}
}

Expand Down

0 comments on commit c107f1d

Please sign in to comment.