Skip to content

Commit

Permalink
Added more unit tests to increase coverage (#32)
Browse files Browse the repository at this point in the history
Added more unit tests to increase coverage
  • Loading branch information
neteinstein authored May 11, 2017
1 parent 49b1a7b commit 0eadd5a
Show file tree
Hide file tree
Showing 12 changed files with 381 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.mindera.skeletoid.generic;

import android.support.annotation.VisibleForTesting;

import com.mindera.skeletoid.logs.LOG;

import java.util.Map;
Expand All @@ -9,6 +11,11 @@
*/
public class DebugTools {

@VisibleForTesting
DebugTools() {
throw new UnsupportedOperationException();
}

public static void printAllStackTraces(Class<?> clazz) {

LOG.d(clazz.toString(), "DUMPING ALL STACK TRACES");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface ILogAppender {
* Get appender minimum log level
* @return
*/
int getMinLogLevel();
LOG.PRIORITY getMinLogLevel();

/**
* Set appender minimum log level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.logging.FileHandler;
Expand Down Expand Up @@ -63,7 +62,7 @@ public class LogFileAppender implements ILogAppender {
/**
* Minimum log level for this appender
*/
private int mMinLogLevel = LOG.PRIORITY.VERBOSE.ordinal();
private LOG.PRIORITY mMinLogLevel = LOG.PRIORITY.VERBOSE;

/**
* Contructor
Expand All @@ -72,8 +71,31 @@ public class LogFileAppender implements ILogAppender {
* @param fileName Log filename
*/
public LogFileAppender(String tag, String fileName) {
TAG = tag;
if (tag == null) {
throw new IllegalArgumentException("TAG cannot be null");
}

if (fileName == null) {
throw new IllegalArgumentException("FileName cannot be null");
}

if (!isFilenameValid(fileName)) {
throw new IllegalArgumentException("Invalid fileName");
}

LOG_FILE_NAME = fileName + ".log";

TAG = tag;
}

/**
* Check if fileName is valid
*
* @param fileName fileName
* @return true if it is, false if not
*/
protected boolean isFilenameValid(String fileName) {
return fileName.matches("\\w+");
}

/**
Expand All @@ -82,7 +104,7 @@ public LogFileAppender(String tag, String fileName) {
* @param type LOG type
* @return FileHandler level
*/
private static Level getFileHandlerLevel(LOG.PRIORITY type) {
private Level getFileHandlerLevel(LOG.PRIORITY type) {

Level level;

Expand Down Expand Up @@ -165,7 +187,7 @@ public void disableAppender() {

@Override
public void log(final LOG.PRIORITY type, final Throwable t, final String... logs) {
if (type.ordinal() > mMinLogLevel) {
if (type.ordinal() > mMinLogLevel.ordinal()) {
return;
}

Expand All @@ -183,17 +205,7 @@ public void run() {
Level level = getFileHandlerLevel(type);

try {
final StringBuilder builder = new StringBuilder();
builder.append(dateFormatter.format(new Date()));
builder.append(": ");
builder.append(type.name().charAt(0));
builder.append("/");
builder.append(TAG);
builder.append("(").append(Thread.currentThread().getId()).append(")");
builder.append(": ");
builder.append(getLogString(logs));

final String logText = builder.toString();
String logText = formatLog(type, t, logs);

LogRecord logRecord = new LogRecord(level, logText);
if (t != null) {
Expand Down Expand Up @@ -225,28 +237,54 @@ public void run() {
}

/**
* Get list of file logs
* Formats the log
*
* @param logsPath Path to the logs folder
* @return List of logs files
* @param type Type of log
* @param t Throwable (can be null)
* @param logs Log
*/
public ArrayList<String> getListLogFiles(String logsPath) {
ArrayList<String> logFiles = new ArrayList<>();

File logsDirectory = new File(logsPath);
if (logsDirectory.exists()) {
String[] fileList = logsDirectory.list();
if (fileList != null && fileList.length != 0) {
for (String file : fileList) {
if (file.contains(".log")) {
logFiles.add(file);
}
}
}
}
return logFiles;
protected String formatLog(final LOG.PRIORITY type, final Throwable t, final String... logs) {
final StringBuilder builder = new StringBuilder();
builder.append(dateFormatter.format(new Date()));
builder.append(": ");
builder.append(type.name().charAt(0));
builder.append("/");
builder.append(TAG);
builder.append("(").append(Thread.currentThread().getId());
builder.append(")");
builder.append(": ");
builder.append(getLogString(logs));

return builder.toString();

}

// /**
// * Get list of file logs
// *
// * @param logsPath Path to the logs folder
// * @return List of logs files
// */
// public ArrayList<String> getListLogFiles(String logsPath) {
// if (logsPath == null || logsPath.isEmpty()) {
// return null;
// }
//
// ArrayList<String> logFiles = new ArrayList<>();
//
// File logsDirectory = new File(logsPath);
// if (logsDirectory.exists()) {
// String[] fileList = logsDirectory.list();
// if (fileList != null && fileList.length != 0) {
// for (String file : fileList) {
// if (file.contains(".log")) {
// logFiles.add(file);
// }
// }
// }
// }
// return logFiles;
// }

public void setLogFileSize(int LOG_FILE_SIZE) {
this.mLogFileSize = LOG_FILE_SIZE;
Expand All @@ -256,18 +294,30 @@ public void setNumberOfLogFiles(int NUMBER_OF_LOG_FILES) {
this.mNumberOfLogFiles = NUMBER_OF_LOG_FILES;
}

public String getFileLogPath(Context context){
public int getLogFileSize() {
return mLogFileSize;
}

public int getNumberOfLogFiles() {
return mNumberOfLogFiles;
}

public String getFileLogPath(Context context) {
return AndroidUtils.getFileDirPath(context, File.separator + LOG_FILE_NAME);
}

@Override
public int getMinLogLevel() {
public LOG.PRIORITY getMinLogLevel() {
return mMinLogLevel;
}

@Override
public void setMinLogLevel(LOG.PRIORITY minLogLevel) {
mMinLogLevel = minLogLevel.ordinal();
mMinLogLevel = minLogLevel;
}

public boolean canWriteToFile() {
return mCanWriteToFile && mFileHandler != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mindera.skeletoid.logs.LOG;

import java.util.ArrayList;
import java.util.List;

import static com.mindera.skeletoid.logs.utils.LogAppenderUtils.getLogString;

Expand All @@ -17,7 +18,7 @@ public class LogcatAppender implements ILogAppender {
/**
* The maximum number of chars to log in a single line.
*/
private final int MAX_LINE_LENGTH = 4000;
private int MAX_LINE_LENGTH = 4000;
/**
* To check if logcat should show the complete lines or just the first 4000 chars.
*/
Expand All @@ -34,14 +35,17 @@ public class LogcatAppender implements ILogAppender {
/**
* Minimum log level for this appender
*/
private int mMinLogLevel = LOG.PRIORITY.VERBOSE.ordinal();
private LOG.PRIORITY mMinLogLevel = LOG.PRIORITY.VERBOSE;

/**
* Contructor
*
* @param tag Log tag
*/
public LogcatAppender(String tag) {
if (tag == null) {
throw new IllegalArgumentException("TAG cannot be null");
}
TAG = tag;
}

Expand All @@ -57,11 +61,11 @@ public void disableAppender() {

@Override
public void log(LOG.PRIORITY type, Throwable t, String... log) {
if(type.ordinal() > mMinLogLevel){
if (type.ordinal() > mMinLogLevel.ordinal()) {
return;
}
final String logString = getLogString(log);
final ArrayList<String> logs = formatLog(logString);
final List<String> logs = formatLog(logString);

for (String logText : logs) {

Expand Down Expand Up @@ -97,31 +101,30 @@ public void log(LOG.PRIORITY type, Throwable t, String... log) {
* @param text The log text
* @return A list of lines to log
*/
private ArrayList<String> formatLog(String text) {

final ArrayList<String> result = new ArrayList<>();
protected List<String> formatLog(String text) {
final List<String> result = new ArrayList<>();

StringBuilder textToSplit = new StringBuilder();
if (text != null) {
textToSplit.append(text);
if (text == null || text.isEmpty()) {
return result;
}

if (textToSplit.length() > MAX_LINE_LENGTH) {
if (text.length() > MAX_LINE_LENGTH) {
if (mSplitLinesAboveMaxLength) {

int chunkCount = textToSplit.length() / MAX_LINE_LENGTH;
for (int i = 0; i <= chunkCount; i++) {
int max = MAX_LINE_LENGTH * (i + 1);
if (max >= textToSplit.length()) {
result.add("[Chunk " + i + " of " + chunkCount + "] " + textToSplit.substring(4000 * i));
int chunkCount = (int) Math.ceil(1f * text.length() / MAX_LINE_LENGTH );
for (int i = 1; i <= chunkCount; i++) {
int max = MAX_LINE_LENGTH * i;
if (max < text.length()) {
result.add("[Chunk " + i + " of " + chunkCount + "] " + text.substring(MAX_LINE_LENGTH * (i - 1), max));
} else {
result.add("[Chunk " + i + " of " + chunkCount + "] " + textToSplit.substring(4000 * i, max));
result.add("[Chunk " + i + " of " + chunkCount + "] " + text.substring(MAX_LINE_LENGTH * (i - 1)));

}
}
}

} else {
result.add(textToSplit.toString());
result.add(text);
}
return result;
}
Expand All @@ -131,14 +134,25 @@ public void setSplitLinesAboveMaxLength(boolean splitLinesAboveMaxLength) {
this.mSplitLinesAboveMaxLength = splitLinesAboveMaxLength;
}

public boolean isSplitLinesAboveMaxLength() {
return mSplitLinesAboveMaxLength;
}

@Override
public int getMinLogLevel() {
public LOG.PRIORITY getMinLogLevel() {
return mMinLogLevel;
}

@Override
protected void setMaxLineLength(int maxLineLength) {
this.MAX_LINE_LENGTH = maxLineLength;
}

protected int getMaxLineLength() {
return MAX_LINE_LENGTH;
}

public void setMinLogLevel(LOG.PRIORITY minLogLevel) {
this.mMinLogLevel = minLogLevel.ordinal();
this.mMinLogLevel = minLogLevel;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.VisibleForTesting;

import com.mindera.skeletoid.generic.StringUtils;

Expand All @@ -17,8 +18,9 @@ public class Connectivity {
// private static final String LOG_TAG = "Connectivity";


private Connectivity() {

@VisibleForTesting
Connectivity() {
throw new UnsupportedOperationException();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.VisibleForTesting;

import com.mindera.skeletoid.logs.LOG;
import com.mindera.skeletoid.threads.threadpools.ThreadPoolUtils;
Expand Down Expand Up @@ -61,11 +62,11 @@ public class ConnectivityReceiver extends BroadcastReceiver {
public interface ConnectivityCallback {

void connectivityUpdate(boolean isConnectedToANetwork, boolean networkHasInternetAccess, boolean isNetworkWiFi);

}

private ConnectivityReceiver() {

@VisibleForTesting
ConnectivityReceiver() {
throw new UnsupportedOperationException();
}

@Override
Expand Down
Loading

0 comments on commit 0eadd5a

Please sign in to comment.