Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bugs and improved tests #373

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public Environment deserialize(final File file) throws RepositoryException {
try {
Properties dataProperties = new Properties();
String fromVersion;
try {
dataProperties.load(new FileInputStream(new File(Info.PATHS.PATH_DATA_FOLDER + "/data.properties")));
try (FileInputStream fis = new FileInputStream(new File(Info.PATHS.PATH_DATA_FOLDER + "/data.properties"))){
dataProperties.load(fis);
fromVersion = dataProperties.getProperty("data.version");
} catch (IOException iOException) {
// Fallback to a default version for older version without that properties file
Expand All @@ -104,7 +104,9 @@ public Environment deserialize(final File file) throws RepositoryException {
@Override
public void persist(Environment environment)
throws RepositoryException {
directory.mkdirs();
if (!directory.mkdirs()){
throw new RepositoryException("Unable to create dir "+directory.getName());
}
if (!directory.isDirectory()) {
throw new RepositoryException(directory.getAbsoluteFile() + " is not a valid environment folder. Skipped");
}
Expand All @@ -126,8 +128,11 @@ public void persist(Environment environment)
@Override
public void delete(Environment environment)
throws RepositoryException {
File deleteMe = new File(directory + "/" + getEnvFilename(environment));
deleteMe.delete();
String dir = directory + "/" + getEnvFilename(environment);
File deleteMe = new File(dir);
if(!deleteMe.delete()){
throw new RepositoryException("Unable to delete "+dir);
}
}

private String getEnvFilename(Environment environment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
public class AccountEvent extends EventTemplate {

private final String DEFAULT_DESTINATION = "app.event.sensor.account.change";
private static final String DEFAULT_DESTINATION = "app.event.sensor.account.change";

public enum AccountActions {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class GenericEvent extends EventTemplate {

private static final long serialVersionUID = 6029054631809171990L;
private String DEFAULT_DESTINATION = "app.event.sensor";
private static String DEFAULT_DESTINATION = "app.event.sensor";

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
public final class LocationEvent extends EventTemplate {

private final String DEFAULT_DESTINATION = "app.event.sensor.person.movement.detected";
private static final String DEFAULT_DESTINATION = "app.event.sensor.person.movement.detected";
private final String uuid;
private final int x;
private final int y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
public class LuminosityEvent
extends EventTemplate {

private final String DEFAULT_DESTINATION = "app.event.sensor.luminosity";

private static final String DEFAULT_DESTINATION = "app.event.sensor.luminosity";
private static final long serialVersionUID = 1605869382477368794L;
private int luminosity;
private String zone;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class MotionEvent
extends EventTemplate {

private static final long serialVersionUID = 4965942901211451802L;
private final String DEFAULT_DESTINATION = "app.event.sensor.motion";
private static final String DEFAULT_DESTINATION = "app.event.sensor.motion";
private String zoneName;
private int motionArea;
private int centerOfGravity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ObjectHasChangedBehavior extends EventTemplate {

private static final long serialVersionUID = 6892968576173017195L;
private static final Logger LOG = LoggerFactory.getLogger(ObjectHasChangedBehavior.class.getName());
private final String DEFAULT_DESTINATION = "app.event.sensor.object.behavior.change";
private static final String DEFAULT_DESTINATION = "app.event.sensor.object.behavior.change";

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ObjectReceiveClick
extends EventTemplate {

private static final long serialVersionUID = 8985824879207319982L;
private final String DEFAULT_DESTINATION = "app.event.sensor.object.behavior.clicked";
private static final String DEFAULT_DESTINATION = "app.event.sensor.object.behavior.clicked";

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public final class PersonEntersZone extends EventTemplate {

private final String DEFAULT_DESTINATION = "app.event.sensor.person.zone.enter";
private static final String DEFAULT_DESTINATION = "app.event.sensor.person.zone.enter";
private final String uuid;
private final String zoneName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public final class PersonExitsZone extends EventTemplate {

private final String DEFAULT_DESTINATION = "app.event.sensor.person.zone.exit";
private static final String DEFAULT_DESTINATION = "app.event.sensor.person.zone.exit";
private final String uuid;
private final String zoneName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class PluginHasChanged
extends EventTemplate {

private static final long serialVersionUID = 5203339184820441643L;
private final String DEFAULT_DESTINATION = "app.event.sensor.plugin.change";
private static final String DEFAULT_DESTINATION = "app.event.sensor.plugin.change";

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ScheduledEvent
extends EventTemplate {

private static final long serialVersionUID = 7508683624189475354L;
private final String DEFAULT_DESTINATION = "app.event.sensor.calendar.event.schedule";
private static final String DEFAULT_DESTINATION = "app.event.sensor.calendar.event.schedule";

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class TemperatureEvent
extends EventTemplate {

private static final long serialVersionUID = 2965942901211451802L;
private final String DEFAULT_DESTINATION = "app.event.sensor.temperature";
private static final String DEFAULT_DESTINATION = "app.event.sensor.temperature";

private int temperature;
private String zone;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ZoneHasChanged
extends EventTemplate {

private static final long serialVersionUID = -2676123835322299252L;
private final String DEFAULT_DESTINATION = "app.event.sensor.environment.zone.change";
private static final String DEFAULT_DESTINATION = "app.event.sensor.environment.zone.change";

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ private int getOldestVersion(String clientVersion, String infoVersion) {
}

if ((i < vals1.length) && (i < vals2.length)) {
return Integer.compare(new Integer(vals1[i]), new Integer(vals2[i]));
return Integer.compare(Integer.valueOf(vals1[i]), Integer.valueOf(vals2[i]));
}

return Integer.compare(vals1.length, vals2.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,11 @@ private void loadPluginResources(File directory)
reactionRepository.loadReactions(new File(directory + "/data/rea"));

//create ad-hoc subfolders of temp
File destination = new File(Info.PATHS.PATH_RESOURCES_FOLDER + "/temp/" + directory.getName());
destination.mkdir();
String dirName= Info.PATHS.PATH_RESOURCES_FOLDER + "/temp/" + directory.getName();
File destination = new File(dirName);
if (!destination.mkdir()){
throw new PluginLoadingException("Unable to create dir : "+dirName);
}
recursiveCopy(new File(directory + "/data/resources"), destination);

File templatesFolder = new File(directory + "/data/templates/");
Expand Down Expand Up @@ -321,8 +324,8 @@ private void loadPluginResources(File directory)

private void recursiveCopy(File source, File target) {
if (source.isDirectory()) {
if (!target.exists()) {
target.mkdir();
if (!target.exists() && !target.mkdir()){
LOG.warn("Error in creating targetDir "+target.getPath());
}

String[] children = source.list();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ public boolean accept(File file) {
try {
Properties dataProperties = new Properties();
String fromVersion;
try {
dataProperties.load(new FileInputStream(new File(Info.PATHS.PATH_DATA_FOLDER + "/data.properties")));
try (FileInputStream fis = new FileInputStream(new File(Info.PATHS.PATH_DATA_FOLDER + "/data.properties"))){
dataProperties.load(fis);
fromVersion = dataProperties.getProperty("data.version");
} catch (IOException iOException) {
LOG.error(Freedomotic.getStackTraceInfo(iOException));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ public boolean accept(File file) {
try {
Properties dataProperties = new Properties();
String fromVersion;
try {
dataProperties.load(new FileInputStream(new File(Info.PATHS.PATH_DATA_FOLDER + "/data.properties")));
try (FileInputStream fis = new FileInputStream(new File(Info.PATHS.PATH_DATA_FOLDER + "/data.properties"))){
dataProperties.load(fis);
fromVersion = dataProperties.getProperty("data.version");
} catch (IOException iOException) {
LOG.error(Freedomotic.getStackTraceInfo(iOException));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ public int hashCode() {
*/
@Override
public void onMessage(ObjectMessage message) {
long start = System.currentTimeMillis();
Object payload = null;

try {
Expand All @@ -406,7 +405,6 @@ public void onMessage(ObjectMessage message) {
+ "' on channel " + this.getChannel());

checker.check(event, this);
long end = System.currentTimeMillis();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.List;
import java.util.Properties;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -175,8 +176,8 @@ public boolean accept(File file) {
try {
Properties dataProperties = new Properties();
String fromVersion;
try {
dataProperties.load(new FileInputStream(new File(Info.PATHS.PATH_DATA_FOLDER + "/data.properties")));
try (FileInputStream fis = new FileInputStream(new File(Info.PATHS.PATH_DATA_FOLDER + "/data.properties"))){
dataProperties.load(fis);
fromVersion = dataProperties.getProperty("data.version");
} catch (IOException iOException) {
LOG.error(Freedomotic.getStackTraceInfo(iOException));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public GreaterEqualThan(String left, String right) {
@Override
public Boolean evaluate() {
try {
Integer intRightValue = new Integer(getRight());
Integer intLeftValue = new Integer(getLeft());
Integer intRightValue = Integer.valueOf(getRight());
Integer intLeftValue = Integer.valueOf(getLeft());
return intLeftValue >= intRightValue;
} catch (NumberFormatException nfe) {
LOG.warn(OPERATOR + " operator can be applied only to integer values");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public GreaterThan(String left, String right) {
@Override
public Boolean evaluate() {
try {
Integer intRightValue = new Integer(getRight());
Integer intLeftValue = new Integer(getLeft());
Integer intRightValue = Integer.valueOf(getRight());
Integer intLeftValue = Integer.valueOf(getLeft());
return intLeftValue > intRightValue;
} catch (NumberFormatException nfe) {
LOG.warn(Statement.GREATER_THAN + " operator can be applied only to integer values");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public LessEqualThan(String left, String right) {
@Override
public Boolean evaluate() {
try {
Integer intRightValue = new Integer(getRight());
Integer intLeftValue = new Integer(getLeft());
Integer intRightValue = Integer.valueOf(getRight());
Integer intLeftValue = Integer.valueOf(getLeft());

return intLeftValue <= intRightValue;
} catch (NumberFormatException nfe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public LessThan(String left, String right) {
@Override
public Boolean evaluate() {
try {
Integer intRightValue = new Integer(getRight());
Integer intLeftValue = new Integer(getLeft());
Integer intRightValue = Integer.valueOf(getRight());
Integer intLeftValue = Integer.valueOf(getLeft());

return intLeftValue < intRightValue;
} catch (NumberFormatException nfe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ public int getIntProperty(String key, int defaultValue) {
try {
Integer result = Integer.parseInt(properties.getProperty(key));

if (result != null) {
return result;
} else {
return defaultValue;
}
return result;
} catch (NumberFormatException e) {
return defaultValue;
}
Expand All @@ -116,11 +112,7 @@ public boolean getBooleanProperty(String key, boolean defaultValue) {
public double getDoubleProperty(String key, double defaultValue) {
Double result = Double.parseDouble(properties.getProperty(key));

if (result != null) {
return result;
} else {
return defaultValue;
}
return result;
}

@Override
Expand Down Expand Up @@ -184,18 +176,18 @@ public String toString() {

@Override
public final AppConfig load() {
try {
properties.load(new FileInputStream(Info.PATHS.PATH_CONFIG_FOLDER + "/config.xml"));
try (FileInputStream fis = new FileInputStream(Info.PATHS.PATH_CONFIG_FOLDER + "/config.xml")){
properties.load(fis);
} catch (IOException ex) {
LOG.error(ex.getMessage());
}
}
return this;
}

@Override
public void save() {
try {
properties.store(new FileOutputStream(Info.PATHS.PATH_CONFIG_FOLDER + "/config.xml"), null);
try (FileOutputStream fos = new FileOutputStream(Info.PATHS.PATH_CONFIG_FOLDER + "/config.xml")){
properties.store(fos, null);
} catch (IOException ex) {
LOG.error(ex.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,6 @@ private static void remove(EnvObjectLogic input) {
input.destroy(); //free memory
}

private static List<String> getObjectsNames() {
List<String> list = new ArrayList<>();
for (EnvObjectLogic obj : objectList.values()) {
list.add(obj.getPojo().getName());
}
return list;
}

/**
* Add an object to the environment. You can use
* EnvObjectPersistnce.MAKE_UNIQUE to saveAll an object that will surely be
Expand Down Expand Up @@ -569,8 +561,8 @@ public EnvObjectLogic load(File file) throws RepositoryException {
try {
Properties dataProperties = new Properties();
String fromVersion;
try {
dataProperties.load(new FileInputStream(new File(Info.PATHS.PATH_DATA_FOLDER + "/data.properties")));
try (FileInputStream fis = new FileInputStream(new File(Info.PATHS.PATH_DATA_FOLDER + "/data.properties"))){
dataProperties.load(fis);
fromVersion = dataProperties.getProperty("data.version");
} catch (IOException iOException) {
// Fallback to a default version for older version without that properties file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.freedomotic.model.geometry.FreedomPolygon;
import com.freedomotic.model.geometry.FreedomShape;
import java.awt.Color;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
Expand Down Expand Up @@ -101,11 +100,6 @@ public static Color convertColorToAWT(FreedomColor color) {
return awtColor;
}

private static Point convertToAWT(FreedomPoint fPoint) {
return new Point(fPoint.getX(),
fPoint.getY());
}

private static Ellipse2D convertToAWT(FreedomEllipse fEllipse) {
throw new UnsupportedOperationException("Not yet implemented");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static int getNextUid() {
public static String getNextStringUid() {
lastId++;

return Integer.valueOf(lastId).toString();
return new Integer(lastId).toString();
}

private UidGenerator() {
Expand Down