Merge pull request #51 from GideonLeGrange/3.0.4-cleanup

Code cleanups
This commit is contained in:
Gideon le Grange 2017-08-07 20:59:36 +02:00 committed by GitHub
commit 3b45c541db
8 changed files with 39 additions and 71 deletions

1
.travis.yml Normal file
View File

@ -0,0 +1 @@
language: java

View File

@ -6,10 +6,20 @@ package me.legrange.mikrotik;
*/
public class ApiConnectionException extends MikrotikApiException {
/**
* Create a new exception.
*
* @param msg The message
*/
public ApiConnectionException(String msg) {
super(msg);
}
/**
* Create a new exception
* @param msg The message
* @param err The underlying cause
*/
public ApiConnectionException(String msg, Throwable err) {
super(msg, err);
}

View File

@ -7,10 +7,21 @@ package me.legrange.mikrotik;
*/
public class MikrotikApiException extends Exception {
/**
* Create a new exception
* @param msg The message
*/
public MikrotikApiException(String msg) {
super(msg);
}
/**
* Create a new exception
* @param msg The message
* @param err The underlying cause
*/
public MikrotikApiException(String msg, Throwable err) {
super(msg, err);
}

View File

@ -31,6 +31,7 @@ public final class ApiConnectionImpl extends ApiConnection {
/**
* Create a new API connection to the give device on the supplied port
*
* @param fact The socket factory used to construct the connection socket.
* @param host The host to which to connect.
* @param port The TCP port to use.
* @param timeOut The connection timeout
@ -201,9 +202,7 @@ public final class ApiConnectionImpl extends ApiConnection {
while (connected) {
try {
String s = Util.decode(in);
if (s != null) {
put(s);
}
} catch (ApiDataException ex) {
put(ex);
} catch (ApiConnectionException ex) {
@ -420,35 +419,22 @@ public final class ApiConnectionImpl extends ApiConnection {
return err;
}
private void queue(Response res) {
String tag = res.getTag();
if (tag != null) {
ResultListener rl = listeners.get(tag);
if (rl != null) {
if (res instanceof Result) {
rl.receive((Result) res);
} else {
// rl.error((Error)res);
}
}
}
}
private final List<String> lines = new LinkedList<>();
private String line;
}
private class SyncListener implements ResultListener {
private static class SyncListener implements ResultListener {
@Override
public synchronized void error(MikrotikApiException ex) {
this.err = ex;
notify();
notifyAll();
}
@Override
public synchronized void completed() {
complete = true;
notify();
notifyAll();
}
synchronized void completed(Done done) {
@ -458,7 +444,7 @@ public final class ApiConnectionImpl extends ApiConnection {
results.add(res);
}
complete = true;
notify();
notifyAll();
}
@Override

View File

@ -175,7 +175,7 @@ class Parser {
}
next();
}
cmd.addProperty(props.toArray(new String[]{}));
cmd.addProperty(props.toArray(new String[props.size()]));
}
private void expect(Token...tokens) throws ParseException {

View File

@ -15,6 +15,7 @@
*/
package me.legrange.mikrotik.impl;
import java.util.Locale;
import static me.legrange.mikrotik.impl.Scanner.Token.*;
/**
* A simple scanner.
@ -117,7 +118,7 @@ class Scanner {
text.append(c);
nextChar();
}
String val = text.toString().toLowerCase();
String val = text.toString().toLowerCase(Locale.getDefault());
switch (val) {
case "where":
return WHERE;
@ -184,17 +185,6 @@ class Scanner {
}
}
/**
* look ahead one character
*/
private char peek() {
if (pos < line.length()) {
return line.charAt(pos);
} else {
return '\n';
}
}
/**
* check if the character matches the give expression
*/

View File

@ -4,6 +4,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
@ -71,7 +72,7 @@ final class Util {
}
buf[i] = (byte) (c & 0xFF);
}
String res = new String(buf);
String res = new String(buf, Charset.forName("UTF-8"));
if (result.length() > 0) {
result.append("\n");
}
@ -121,11 +122,11 @@ final class Util {
* @return - converted string.
*/
static String hexStrToStr(String s) {
String ret = "";
StringBuilder ret = new StringBuilder();
for (int i = 0; i < s.length(); i += 2) {
ret += (char) Integer.parseInt(s.substring(i, i + 2), 16);
ret.append((char) Integer.parseInt(s.substring(i, i + 2), 16));
}
return ret;
return ret.toString();
}
/**

View File

@ -1,31 +0,0 @@
package me.legrange.mikrotik;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
public class ApiConnectionTest extends TestCase {
private static final String HOSTNAME = "192.168.88.1";
private static final String ADMIN = "admin";
private static final String PASSWORD = "plasma";
public void testConnectTLSStringInt() throws MikrotikApiException, InterruptedException {
/* ApiConnection connect = ApiConnection.connectTLS(HOSTNAME, 8729);
connect.login(ADMIN, PASSWORD);
List<Map<String, String>> result = connect.execute("/system/resource/print");
assertEquals(1, result.size());
assertEquals("MikroTik", result.get(0).get("platform")); */
}
public void testConnectStringInt() throws MikrotikApiException, InterruptedException {
/* ApiConnection connect = ApiConnection.connect(HOSTNAME, 8728);
connect.login(ADMIN, PASSWORD);
List<Map<String, String>> result = connect.execute("/system/resource/print");
assertEquals(1, result.size());
assertEquals("MikroTik", result.get(0).get("platform")); */
}
}