diff --git a/commons/pom.xml b/commons/pom.xml index 92f50c5630c2faac555ca47ddb9755c1e83500d4..ab50229ee2536ca855c84e9e6514bd5054577f98 100644 --- a/commons/pom.xml +++ b/commons/pom.xml @@ -12,11 +12,25 @@ <description>Common classes and configuration files</description> <dependencies> + + <!-- Log4J version 1--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> + + <!-- Log4J2--> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <version>${log4j2.version}</version> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <version>${log4j2.version}</version> + </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> diff --git a/commons/src/main/java/lcsb/mapviewer/common/Comparator.java b/commons/src/main/java/lcsb/mapviewer/common/Comparator.java index 474bf8ce7ff720e5d8cd69b11c82d9da62f96e95..c7b192b1f4a4f373c278685b5d560267b0fa4eab 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/Comparator.java +++ b/commons/src/main/java/lcsb/mapviewer/common/Comparator.java @@ -3,7 +3,8 @@ package lcsb.mapviewer.common; import java.util.ArrayList; import java.util.List; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import lcsb.mapviewer.common.exception.NotImplementedException; @@ -11,7 +12,7 @@ public abstract class Comparator<T extends Object> implements java.util.Comparat /** * Default class logger. */ - private static Logger logger = Logger.getLogger(Comparator.class); + private static Logger logger = LogManager.getLogger(Comparator.class); private Class<T> comparatorClazz; private boolean exactClassMatch; private List<Comparator<? extends T>> subClassComparatorList = new ArrayList<>(); diff --git a/commons/src/main/java/lcsb/mapviewer/common/Configuration.java b/commons/src/main/java/lcsb/mapviewer/common/Configuration.java index 36806a836881c3e6a6c27f8445ab03033ca97f9b..b9c9da41d0a788aa4dcddf7c1b1fadf06c063a2e 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/Configuration.java +++ b/commons/src/main/java/lcsb/mapviewer/common/Configuration.java @@ -7,7 +7,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; /** * Basic configuration parameters of the system (these values cannot be modified @@ -30,7 +31,7 @@ public final class Configuration { /** * Default class logger. */ - private static Logger logger = Logger.getLogger(Configuration.class); + private static Logger logger = LogManager.getLogger(Configuration.class); /** * How many elements should be visible in auto-complete lists. diff --git a/commons/src/main/java/lcsb/mapviewer/common/EventStorageLoggerAppender.java b/commons/src/main/java/lcsb/mapviewer/common/EventStorageLoggerAppender.java index d0ae40dd5e7e221e37a605677ff35f89ff851800..aa44753353cff6c5b67c98a0230117c5fa8556fd 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/EventStorageLoggerAppender.java +++ b/commons/src/main/java/lcsb/mapviewer/common/EventStorageLoggerAppender.java @@ -145,4 +145,4 @@ public class EventStorageLoggerAppender extends AppenderSkeleton { public List<LoggingEvent> getFatals() { return fatalEvents; } -} +} \ No newline at end of file diff --git a/commons/src/main/java/lcsb/mapviewer/common/MinervaLoggerAppender.java b/commons/src/main/java/lcsb/mapviewer/common/MinervaLoggerAppender.java new file mode 100644 index 0000000000000000000000000000000000000000..bbe0a409b74c3e0a9d86ad48d496bb032da363f8 --- /dev/null +++ b/commons/src/main/java/lcsb/mapviewer/common/MinervaLoggerAppender.java @@ -0,0 +1,202 @@ +package lcsb.mapviewer.common; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.lf5.LogLevel; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.Layout; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.AbstractAppender; +import org.apache.logging.log4j.core.config.Property; +import org.apache.logging.log4j.core.layout.PatternLayout; + +/** + * Custom log4j {@link AbstractAppender}. This class is used to store logs in + * the memory. Such logs should be stored for short period of time. Only logs + * for the {@link Thread} that created the object will be stored. + * + * @author Piotr Gawron + * + */ +public class MinervaLoggerAppender extends AbstractAppender { + + /** + * List of {@link LogLevel#DEBUG} logs. + */ + private List<LogEvent> debugEvents = new ArrayList<>(); + + /** + * List of {@link LogLevel#INFO} logs. + */ + private List<LogEvent> infoEvents = new ArrayList<>(); + + /** + * List of {@link LogLevel#WARN} logs. + */ + private List<LogEvent> warnEvents = new ArrayList<>(); + + /** + * List of {@link LogLevel#ERROR} logs. + */ + private List<LogEvent> errorEvents = new ArrayList<>(); + + /** + * List of {@link LogLevel#FATAL} logs. + */ + private List<LogEvent> fatalEvents = new ArrayList<>(); + + /** + * List of logs with unknown log level. + */ + private List<LogEvent> otherEvents = new ArrayList<>(); + + /** + * Identifier of {@link Thread} that created this object. + */ + private long threadId; + + /** + * Flag that describe if we log only entries for current thread ( + * <code>true</code>) or for all threads (<code>false</code>). + */ + private boolean currentThreadLogOnly = true; + + /** + * Every logger must have different name. We use this counter to create new + * names. + */ + private static int loggerCounter = 0; + + /** + * Private constructor preventing instantiation. Appender should be created + * using factory method: {@link MinervaLoggerAppender#createAppender()}. + * + * @param name + * @param filter + * @param layout + * @param ignoreExceptions + * @param currentThreadLogOnly + */ + private MinervaLoggerAppender(String name, Filter filter, Layout<? extends Serializable> layout, + final boolean ignoreExceptions, boolean currentThreadLogOnly) { + super(name, filter, layout, ignoreExceptions, new Property[0]); + this.threadId = Thread.currentThread().getId(); + this.currentThreadLogOnly = currentThreadLogOnly; + } + + /** + * Creates appender that will store logs in memory. + * + * @return appender + */ + public static MinervaLoggerAppender createAppender() { + return createAppender(true); + } + + /** + * Creates appender that will store logs in memory. + * + * @param currentThreadLogOnly + * if <code>true</code> logs should be taken only from thread that + * created object, if <code>false</code> all logs will be stored + * @return appender + */ + public static MinervaLoggerAppender createAppender(boolean currentThreadLogOnly) { + LoggerContext context = LoggerContext.getContext(false); + org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration(); + PatternLayout layout = PatternLayout.createDefaultLayout(config); + if (layout == null) { + layout = PatternLayout.createDefaultLayout(); + } + + final Filter filter = null; + + final MinervaLoggerAppender appender = new MinervaLoggerAppender("MinervaAppender" + loggerCounter++, filter, + layout, true, currentThreadLogOnly); + + appender.start(); + config.addAppender(appender); + final Level level = Level.DEBUG; + config.getRootLogger().addAppender(appender, level, filter); + + // Configurator.setAllLevels(LogManager.getRootLogger().getName(), level); + + return appender; + + } + + public static void unregisterLogEventStorage(MinervaLoggerAppender appender) { + if (appender != null) { + final LoggerContext context = LoggerContext.getContext(false); + final org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration(); + config.getRootLogger().removeAppender(appender.getName()); + } + } + + @Override + public void append(LogEvent event) { + // store information for all thread only if it is flagged by + // currentThreadLogOnly, if not only logs from current thread should be + // stored + if (!currentThreadLogOnly || threadId == Thread.currentThread().getId()) { + if (event.getLevel().equals(Level.DEBUG)) { + debugEvents.add(event); + } else if (event.getLevel().equals(Level.INFO)) { + infoEvents.add(event); + } else if (event.getLevel().equals(Level.WARN)) { + warnEvents.add(event); + } else if (event.getLevel().equals(Level.ERROR)) { + errorEvents.add(event); + } else if (event.getLevel().equals(Level.FATAL)) { + fatalEvents.add(event); + } else { + otherEvents.add(event); + } + } + } + + /** + * Returns list of warning logs. + * + * @return list of warning logs + */ + public List<LogEvent> getWarnings() { + return warnEvents; + } + + /** + * Returns list of error logs. + * + * @return list of error logs + */ + public List<LogEvent> getErrors() { + return errorEvents; + } + + /** + * Returns list of fatal logs. + * + * @return list of fatal logs + */ + public List<LogEvent> getFatals() { + return fatalEvents; + } + + /** + * Returns list of warning logs. + * + * @return list of warning logs + */ + public List<LogEvent> getInfos() { + return infoEvents; + } + + public List<LogEvent> getDebugs() { + return debugEvents; + } + +} diff --git a/commons/src/main/java/lcsb/mapviewer/common/SystemClipboard.java b/commons/src/main/java/lcsb/mapviewer/common/SystemClipboard.java index f69c4c2cb6aa07fd21e67d33a30435d3cdedc096..0976a382a1e28928fb9ae7eca71736b81c84c891 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/SystemClipboard.java +++ b/commons/src/main/java/lcsb/mapviewer/common/SystemClipboard.java @@ -7,7 +7,8 @@ import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; /** * Class allowing access to system clipboard. @@ -19,7 +20,7 @@ public class SystemClipboard implements ClipboardOwner { /** * Default class logger. */ - private final Logger logger = Logger.getLogger(SystemClipboard.class); + private final Logger logger = LogManager.getLogger(SystemClipboard.class); @Override public void lostOwnership(Clipboard clipboard, Transferable contents) { diff --git a/commons/src/main/java/lcsb/mapviewer/common/XmlParser.java b/commons/src/main/java/lcsb/mapviewer/common/XmlParser.java index 7c8a5614e878b04ce342d07652280202eb2475bb..9d597981152e7e0efc98106b0d08c3520f2b5e59 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/XmlParser.java +++ b/commons/src/main/java/lcsb/mapviewer/common/XmlParser.java @@ -28,7 +28,8 @@ import javax.xml.transform.stream.StreamSource; import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.commons.text.StringEscapeUtils; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; @@ -51,7 +52,7 @@ final public class XmlParser { /** * Default class logger. */ - private static Logger logger = Logger.getLogger(XmlParser.class); + private static Logger logger = LogManager.getLogger(XmlParser.class); /** * Base of the hex representation. diff --git a/commons/src/main/java/lcsb/mapviewer/common/comparator/LineComparator.java b/commons/src/main/java/lcsb/mapviewer/common/comparator/LineComparator.java index cbf00430871d1ef70ddd8571e66a76ad2f184687..f747b5bc064d48aa61689e74c9d7c266dce741c3 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/comparator/LineComparator.java +++ b/commons/src/main/java/lcsb/mapviewer/common/comparator/LineComparator.java @@ -3,7 +3,8 @@ package lcsb.mapviewer.common.comparator; import java.awt.geom.Line2D; import java.util.Comparator; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import lcsb.mapviewer.common.Configuration; @@ -15,7 +16,7 @@ import lcsb.mapviewer.common.Configuration; */ public class LineComparator implements Comparator<Line2D> { - private static Logger logger = Logger.getLogger(LineComparator.class); + private static Logger logger = LogManager.getLogger(LineComparator.class); private PointComparator pointComparator; diff --git a/commons/src/main/java/lcsb/mapviewer/common/comparator/ListComparator.java b/commons/src/main/java/lcsb/mapviewer/common/comparator/ListComparator.java index ed362b8c58296b9d14994c03723a01175af0a0f7..bded8af204b5cee366ae3414e79c6db493b0c850 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/comparator/ListComparator.java +++ b/commons/src/main/java/lcsb/mapviewer/common/comparator/ListComparator.java @@ -3,7 +3,9 @@ package lcsb.mapviewer.common.comparator; import java.util.Comparator; import java.util.List; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + /** * Comparator used for comparing lists of objects. @@ -15,7 +17,7 @@ public class ListComparator<T> implements Comparator<List<T>> { /** * Default class logger. */ - private Logger logger = Logger.getLogger(ListComparator.class); + private Logger logger = LogManager.getLogger(ListComparator.class); private Comparator<T> objectComparator; diff --git a/commons/src/main/java/lcsb/mapviewer/common/comparator/SetComparator.java b/commons/src/main/java/lcsb/mapviewer/common/comparator/SetComparator.java index bc3658717b5c155a0b630a499ee891657a80a7a0..36465f5b78fbcf238515977fa23dfce3af0a0861 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/comparator/SetComparator.java +++ b/commons/src/main/java/lcsb/mapviewer/common/comparator/SetComparator.java @@ -3,7 +3,9 @@ package lcsb.mapviewer.common.comparator; import java.util.Comparator; import java.util.Set; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + /** * Comparator used for comparing sets of strings. @@ -15,7 +17,7 @@ public class SetComparator<T> implements Comparator<Set<T>> { /** * Default class logger. */ - private Logger logger = Logger.getLogger(SetComparator.class); + private Logger logger = LogManager.getLogger(SetComparator.class); private Comparator<T> objectComparator; diff --git a/commons/src/main/java/lcsb/mapviewer/common/comparator/StringComparator.java b/commons/src/main/java/lcsb/mapviewer/common/comparator/StringComparator.java index 183e49593e0d898bf0c1878fe4f5284b44937ca2..a0d5f24480a93a222fe8f78d4b08c794de082241 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/comparator/StringComparator.java +++ b/commons/src/main/java/lcsb/mapviewer/common/comparator/StringComparator.java @@ -2,7 +2,9 @@ package lcsb.mapviewer.common.comparator; import java.util.Comparator; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + /** * Comparator used for {@link String} class. It's null safe (it allows to @@ -16,7 +18,7 @@ public class StringComparator implements Comparator<String> { * Default class logger. */ @SuppressWarnings("unused") - private Logger logger = Logger.getLogger(StringComparator.class); + private Logger logger = LogManager.getLogger(StringComparator.class); @Override public int compare(String arg0, String arg1) { diff --git a/commons/src/main/java/lcsb/mapviewer/common/comparator/StringListComparator.java b/commons/src/main/java/lcsb/mapviewer/common/comparator/StringListComparator.java index 92ccd809e3818430e0c5de2973c58a3bf2179f23..64e715cff2e27035f9f9ca07ef552583ca8e895e 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/comparator/StringListComparator.java +++ b/commons/src/main/java/lcsb/mapviewer/common/comparator/StringListComparator.java @@ -3,7 +3,8 @@ package lcsb.mapviewer.common.comparator; import java.util.Comparator; import java.util.List; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; /** * Comparator used for list of strings. @@ -12,45 +13,45 @@ import org.apache.log4j.Logger; * */ public class StringListComparator implements Comparator<List<String>> { - /** - * Default class logger. - */ - private static Logger logger = Logger.getLogger(StringListComparator.class); - - /** - * String comparator used for comparing strings. - */ - private StringComparator stringComparator = new StringComparator(); - /** - * Integer comparator used for comparing integers. - */ - private IntegerComparator integerComparator = new IntegerComparator(); - - @Override - public int compare(List<String> arg0, List<String> arg1) { - if (arg0 == null) { - if (arg1 == null) { - return 0; - } else { - return 1; - } - } else if (arg1 == null) { - return -1; - } - - if (arg0.size() != arg1.size()) { - logger.debug("String lists have different size: " + arg0.size() + ", " + arg1.size()); - return integerComparator.compare(arg0.size(), arg1.size()); - } - - for (int i = 0; i < arg0.size(); i++) { - if (stringComparator.compare(arg0.get(i), arg1.get(i)) != 0) { - logger.debug("Strings in list different: \"" + arg0.get(i) + "\", \"" + arg1.get(i) + "\""); - return stringComparator.compare(arg0.get(i), arg1.get(i)); - } - } - - return 0; - } + /** + * Default class logger. + */ + private static Logger logger = LogManager.getLogger(StringListComparator.class); + + /** + * String comparator used for comparing strings. + */ + private StringComparator stringComparator = new StringComparator(); + /** + * Integer comparator used for comparing integers. + */ + private IntegerComparator integerComparator = new IntegerComparator(); + + @Override + public int compare(List<String> arg0, List<String> arg1) { + if (arg0 == null) { + if (arg1 == null) { + return 0; + } else { + return 1; + } + } else if (arg1 == null) { + return -1; + } + + if (arg0.size() != arg1.size()) { + logger.debug("String lists have different size: " + arg0.size() + ", " + arg1.size()); + return integerComparator.compare(arg0.size(), arg1.size()); + } + + for (int i = 0; i < arg0.size(); i++) { + if (stringComparator.compare(arg0.get(i), arg1.get(i)) != 0) { + logger.debug("Strings in list different: \"" + arg0.get(i) + "\", \"" + arg1.get(i) + "\""); + return stringComparator.compare(arg0.get(i), arg1.get(i)); + } + } + + return 0; + } } diff --git a/commons/src/main/java/lcsb/mapviewer/common/comparator/StringSetComparator.java b/commons/src/main/java/lcsb/mapviewer/common/comparator/StringSetComparator.java index 1e3c4759d40fdc9747cb2cd3a22a9d2f62b4c7df..7042047c65ad40baf3f10a248318ae12239d421d 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/comparator/StringSetComparator.java +++ b/commons/src/main/java/lcsb/mapviewer/common/comparator/StringSetComparator.java @@ -3,7 +3,9 @@ package lcsb.mapviewer.common.comparator; import java.util.Comparator; import java.util.Set; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + /** * Comparator used for comparing sets of strings. @@ -15,7 +17,7 @@ public class StringSetComparator implements Comparator<Set<String>> { /** * Default class logger. */ - private Logger logger = Logger.getLogger(StringSetComparator.class); + private Logger logger = LogManager.getLogger(StringSetComparator.class); @Override public int compare(Set<String> arg0, Set<String> arg1) { diff --git a/commons/src/main/java/lcsb/mapviewer/common/geometry/EllipseTransformation.java b/commons/src/main/java/lcsb/mapviewer/common/geometry/EllipseTransformation.java index 03e0c3cb5877aa52ce19b93621a799e4c96b4879..6c55966ee363f347898039f1e35cdacac9198922 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/geometry/EllipseTransformation.java +++ b/commons/src/main/java/lcsb/mapviewer/common/geometry/EllipseTransformation.java @@ -3,9 +3,11 @@ package lcsb.mapviewer.common.geometry; import java.awt.geom.Ellipse2D; import java.awt.geom.Point2D; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import lcsb.mapviewer.common.Configuration; -import org.apache.log4j.Logger; /** * This class contains basic operators on ellipse used by converters. @@ -14,140 +16,142 @@ import org.apache.log4j.Logger; * */ public class EllipseTransformation { - /** - * PI value. - */ - private static final double PI = Math.PI; - /** - * PI/2 value. - */ - private static final double PI_1_2 = Math.PI / 2; - /** - * 3/2 PI value. - */ - private static final double PI_3_2 = PI_1_2 * 3; - - /** - * Default class logger. - */ - @SuppressWarnings("unused") - private static Logger logger = Logger.getLogger(EllipseTransformation.class.getName()); - - /** - * Method returns a cross point between ellipse and a line (from center point - * with angle between the line and X axis). - * - * @param ellipse - * ellipse object on which we want to find point - * @param angle - * angle in degrees - * @return point on the ellipse that connect center of the ellipse with the - * line that cross X axis by angle - */ - public Point2D getPointOnEllipseByDegree(final Ellipse2D ellipse, final double angle) { - return getPointOnEllipseByRadian(ellipse, Math.toRadians(angle)); - } - - /** - * Method returns a cross point between ellipse and a line (from center point - * with angle between the line and X axis). - * - * @param x - * x coordinate of the ellipse - * @param y - * y coordinate of the ellipse - * @param width - * width of the ellipse - * @param height - * height of the ellipse - * @param angle - * angle in degrees - * @return point on the ellipse that connect center of the ellipse with the - * line that cross X axis by angle - */ - public Point2D getPointOnEllipseByDegree(final double x, final double y, final double width, final double height, final double angle) { - return getPointOnEllipseByRadian(x, y, width, height, Math.toRadians(angle)); - } - - /** - * Method returns a cross point between ellipse and a line (from center point - * with angle between the line and X axis). - * - * @param x - * x coordinate of the ellipse - * @param y - * y coordinate of the ellipse - * @param width - * width of the ellipse - * @param height - * height of the ellipse - * @param angle - * angle in radians - * @return point on the ellipse that connect center of the ellipse with the - * line that cross X axis by angle - */ - public Point2D getPointOnEllipseByRadian(final double x, final double y, final double width, final double height, final double angle) { - double boundedAngle = angle; - - while (boundedAngle < 0) { - boundedAngle += 2 * Math.PI; - } - while (boundedAngle > (2 * Math.PI - Configuration.EPSILON)) { - boundedAngle -= 2 * Math.PI; - } - Point2D result = new Point2D.Double(); - - double a = width / 2; - double b = height / 2; - - double resX = 0; - double resY = 0; - // special cases (atan is infinity) - if (Math.abs(boundedAngle - 0) < Configuration.EPSILON) { - resX = -a; - resY = 0; - } else if (Math.abs(boundedAngle - PI) < Configuration.EPSILON) { - resX = a; - resY = 0; - } else if (Math.abs(boundedAngle - PI_1_2) < Configuration.EPSILON) { - resX = 0; - resY = -b; - } else if (Math.abs(boundedAngle - PI_3_2) < Configuration.EPSILON) { - resX = 0; - resY = b; - } else { - double tan = Math.tan(boundedAngle); - - resX = a * b / Math.sqrt(b * b + a * a * tan * tan); - resY = resX * tan; - if (boundedAngle <= PI_1_2 || boundedAngle > PI_3_2) { - resX = -resX; - resY = -resY; - } - } - - resX += x + a; - resY += y + b; - - result.setLocation(resX, resY); - return result; - } - - /** - * Method returns a cross point between ellipse and a line (from center point - * with angle between the line and X axis). - * - * @param ellipse - * ellipse object on which we want to find point - * @param angle - * angle in radians - * @return point on the ellipse that connect center of the ellipse with the - * line that cross X axis by angle - */ - public Point2D getPointOnEllipseByRadian(final Ellipse2D ellipse, final double angle) { - double width = ellipse.getWidth(); - double height = ellipse.getHeight(); - - return getPointOnEllipseByRadian(ellipse.getX(), ellipse.getY(), width, height, angle); - } + /** + * PI value. + */ + private static final double PI = Math.PI; + /** + * PI/2 value. + */ + private static final double PI_1_2 = Math.PI / 2; + /** + * 3/2 PI value. + */ + private static final double PI_3_2 = PI_1_2 * 3; + + /** + * Default class logger. + */ + @SuppressWarnings("unused") + private static Logger logger = LogManager.getLogger(EllipseTransformation.class.getName()); + + /** + * Method returns a cross point between ellipse and a line (from center point + * with angle between the line and X axis). + * + * @param ellipse + * ellipse object on which we want to find point + * @param angle + * angle in degrees + * @return point on the ellipse that connect center of the ellipse with the line + * that cross X axis by angle + */ + public Point2D getPointOnEllipseByDegree(final Ellipse2D ellipse, final double angle) { + return getPointOnEllipseByRadian(ellipse, Math.toRadians(angle)); + } + + /** + * Method returns a cross point between ellipse and a line (from center point + * with angle between the line and X axis). + * + * @param x + * x coordinate of the ellipse + * @param y + * y coordinate of the ellipse + * @param width + * width of the ellipse + * @param height + * height of the ellipse + * @param angle + * angle in degrees + * @return point on the ellipse that connect center of the ellipse with the line + * that cross X axis by angle + */ + public Point2D getPointOnEllipseByDegree(final double x, final double y, final double width, final double height, + final double angle) { + return getPointOnEllipseByRadian(x, y, width, height, Math.toRadians(angle)); + } + + /** + * Method returns a cross point between ellipse and a line (from center point + * with angle between the line and X axis). + * + * @param x + * x coordinate of the ellipse + * @param y + * y coordinate of the ellipse + * @param width + * width of the ellipse + * @param height + * height of the ellipse + * @param angle + * angle in radians + * @return point on the ellipse that connect center of the ellipse with the line + * that cross X axis by angle + */ + public Point2D getPointOnEllipseByRadian(final double x, final double y, final double width, final double height, + final double angle) { + double boundedAngle = angle; + + while (boundedAngle < 0) { + boundedAngle += 2 * Math.PI; + } + while (boundedAngle > (2 * Math.PI - Configuration.EPSILON)) { + boundedAngle -= 2 * Math.PI; + } + Point2D result = new Point2D.Double(); + + double a = width / 2; + double b = height / 2; + + double resX = 0; + double resY = 0; + // special cases (atan is infinity) + if (Math.abs(boundedAngle - 0) < Configuration.EPSILON) { + resX = -a; + resY = 0; + } else if (Math.abs(boundedAngle - PI) < Configuration.EPSILON) { + resX = a; + resY = 0; + } else if (Math.abs(boundedAngle - PI_1_2) < Configuration.EPSILON) { + resX = 0; + resY = -b; + } else if (Math.abs(boundedAngle - PI_3_2) < Configuration.EPSILON) { + resX = 0; + resY = b; + } else { + double tan = Math.tan(boundedAngle); + + resX = a * b / Math.sqrt(b * b + a * a * tan * tan); + resY = resX * tan; + if (boundedAngle <= PI_1_2 || boundedAngle > PI_3_2) { + resX = -resX; + resY = -resY; + } + } + + resX += x + a; + resY += y + b; + + result.setLocation(resX, resY); + return result; + } + + /** + * Method returns a cross point between ellipse and a line (from center point + * with angle between the line and X axis). + * + * @param ellipse + * ellipse object on which we want to find point + * @param angle + * angle in radians + * @return point on the ellipse that connect center of the ellipse with the line + * that cross X axis by angle + */ + public Point2D getPointOnEllipseByRadian(final Ellipse2D ellipse, final double angle) { + double width = ellipse.getWidth(); + double height = ellipse.getHeight(); + + return getPointOnEllipseByRadian(ellipse.getX(), ellipse.getY(), width, height, angle); + } } diff --git a/commons/src/main/java/lcsb/mapviewer/common/geometry/LineTransformation.java b/commons/src/main/java/lcsb/mapviewer/common/geometry/LineTransformation.java index 143273978ab78689afbc40e0f47f644088b68e8e..b1acefbd5c74926d5ef9211e4bbf6a4f96561fde 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/geometry/LineTransformation.java +++ b/commons/src/main/java/lcsb/mapviewer/common/geometry/LineTransformation.java @@ -4,7 +4,8 @@ import java.awt.geom.Line2D; import java.awt.geom.PathIterator; import java.awt.geom.Point2D; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import lcsb.mapviewer.common.Configuration; @@ -34,7 +35,7 @@ public class LineTransformation { * Default class logger. */ @SuppressWarnings("unused") - private static Logger logger = Logger.getLogger(LineTransformation.class.getName()); + private static Logger logger = LogManager.getLogger(LineTransformation.class.getName()); /** * Returns a cross point between path and a line. diff --git a/commons/src/main/java/lcsb/mapviewer/common/geometry/PointTransformation.java b/commons/src/main/java/lcsb/mapviewer/common/geometry/PointTransformation.java index 6472057afccabae67e6afef2c9a65275f5d310bb..8a9aa9b03db93ca056f26abcf48cc2d841b3418f 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/geometry/PointTransformation.java +++ b/commons/src/main/java/lcsb/mapviewer/common/geometry/PointTransformation.java @@ -2,7 +2,8 @@ package lcsb.mapviewer.common.geometry; import java.awt.geom.Point2D; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; /** * Class for basic point transformations. @@ -15,7 +16,7 @@ public class PointTransformation { * Default class logger. */ @SuppressWarnings("unused") - private final Logger logger = Logger.getLogger(PointTransformation.class.getName()); + private final Logger logger = LogManager.getLogger(PointTransformation.class.getName()); /** * Rotates point around center using the angle. diff --git a/commons/src/test/java/lcsb/mapviewer/common/AllCommonTests.java b/commons/src/test/java/lcsb/mapviewer/common/AllCommonTests.java index ddbabc8f96de613d6f89dd9f492596eea53d6d03..20ccdc6b4a6c95e0d8dd8703bf349de8909d2953 100644 --- a/commons/src/test/java/lcsb/mapviewer/common/AllCommonTests.java +++ b/commons/src/test/java/lcsb/mapviewer/common/AllCommonTests.java @@ -13,7 +13,7 @@ import lcsb.mapviewer.common.geometry.AllGeometryTests; AllExceptionTests.class, AllGeometryTests.class, ConfigurationTest.class, - EventStorageLoggerAppenderTest.class, + GlobalLoggerAppenderTest.class, HttpConnectionMethodTypeTest.class, MimeTypeTest.class, ObjectUtilsTest.class, diff --git a/commons/src/test/java/lcsb/mapviewer/common/CommonTestFunctions.java b/commons/src/test/java/lcsb/mapviewer/common/CommonTestFunctions.java index d702ff7dfe03708948a68bdbd067a8c8e8a669e6..b13706a0c64b95160f369e2ad7fde2f858202ec7 100644 --- a/commons/src/test/java/lcsb/mapviewer/common/CommonTestFunctions.java +++ b/commons/src/test/java/lcsb/mapviewer/common/CommonTestFunctions.java @@ -2,35 +2,31 @@ package lcsb.mapviewer.common; import java.util.List; -import org.apache.log4j.Logger; -import org.apache.log4j.spi.LoggingEvent; +import org.apache.logging.log4j.core.LogEvent; import org.junit.After; import org.junit.Before; -import lcsb.mapviewer.common.EventStorageLoggerAppender; - public class CommonTestFunctions { - private EventStorageLoggerAppender appender; + private MinervaLoggerAppender appender; - @Before - public final void _setUp() throws Exception { - Logger.getRootLogger().removeAppender(appender); - appender = new EventStorageLoggerAppender(); - Logger.getRootLogger().addAppender(appender); - } + @Before + public final void _setUp() throws Exception { + MinervaLoggerAppender.unregisterLogEventStorage(appender); + appender = MinervaLoggerAppender.createAppender(); + } - @After - public final void _tearDown() throws Exception { - Logger.getRootLogger().removeAppender(appender); - } + @After + public final void _tearDown() throws Exception { + MinervaLoggerAppender.unregisterLogEventStorage(appender); + } - protected List<LoggingEvent> getWarnings() { - return appender.getWarnings(); - } + protected List<LogEvent> getWarnings() { + return appender.getWarnings(); + } - protected List<LoggingEvent> getErrors() { - return appender.getErrors(); - } + protected List<LogEvent> getErrors() { + return appender.getErrors(); + } } diff --git a/commons/src/test/java/lcsb/mapviewer/common/ConfigurationTest.java b/commons/src/test/java/lcsb/mapviewer/common/ConfigurationTest.java index a1e6ba03e7c358da139ca6c334d3b7d84fd2b7a4..8956dca3c6ab6b362580df208fa3b5908a544d0a 100644 --- a/commons/src/test/java/lcsb/mapviewer/common/ConfigurationTest.java +++ b/commons/src/test/java/lcsb/mapviewer/common/ConfigurationTest.java @@ -9,14 +9,15 @@ import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.List; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.junit.After; import org.junit.Before; import org.junit.Test; public class ConfigurationTest extends CommonTestFunctions{ - Logger logger = Logger.getLogger(ConfigurationTest.class); + Logger logger = LogManager.getLogger(ConfigurationTest.class); @Before public void setUp() throws Exception { diff --git a/commons/src/test/java/lcsb/mapviewer/common/EventStorageLoggerAppenderTest.java b/commons/src/test/java/lcsb/mapviewer/common/EventStorageLoggerAppenderTest.java deleted file mode 100644 index d03f01a0fc885b123811bd0a30a8c6b735f3ff47..0000000000000000000000000000000000000000 --- a/commons/src/test/java/lcsb/mapviewer/common/EventStorageLoggerAppenderTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package lcsb.mapviewer.common; - -import static org.junit.Assert.assertEquals; - -import org.apache.log4j.Logger; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.Test; - -public class EventStorageLoggerAppenderTest { - Logger logger = Logger.getLogger(EventStorageLoggerAppenderTest.class); - - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testLogCatching() { - try { - EventStorageLoggerAppender appender = new EventStorageLoggerAppender(); - logger.addAppender(appender); - logger.warn("test"); - logger.debug("1"); - logger.error("2"); - logger.info("3"); - logger.fatal("4"); - logger.trace("5"); - assertEquals(1, appender.getWarnings().size()); - assertEquals(1, appender.getFatals().size()); - assertEquals(1, appender.getErrors().size()); - logger.removeAppender(appender); - logger.warn("test"); - assertEquals(1, appender.getWarnings().size()); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testGetters() { - try { - EventStorageLoggerAppender appender = new EventStorageLoggerAppender(); - appender.requiresLayout(); - appender.close(); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - -} diff --git a/commons/src/test/java/lcsb/mapviewer/common/GlobalLoggerAppenderTest.java b/commons/src/test/java/lcsb/mapviewer/common/GlobalLoggerAppenderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..c9078548bae0c655195bf895c0c14bc8db2d0fb5 --- /dev/null +++ b/commons/src/test/java/lcsb/mapviewer/common/GlobalLoggerAppenderTest.java @@ -0,0 +1,50 @@ +package lcsb.mapviewer.common; + +import static org.junit.Assert.assertEquals; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; + +public class GlobalLoggerAppenderTest { + Logger logger = LogManager.getLogger(GlobalLoggerAppenderTest.class); + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testLogCatching() { + try { + MinervaLoggerAppender appender = MinervaLoggerAppender.createAppender(); + logger.warn("test"); + logger.debug("1"); + logger.error("2"); + logger.info("3"); + logger.fatal("4"); + logger.trace("5"); + assertEquals(1, appender.getDebugs().size()); + assertEquals(1, appender.getWarnings().size()); + assertEquals(1, appender.getFatals().size()); + assertEquals(1, appender.getErrors().size()); + MinervaLoggerAppender.unregisterLogEventStorage(appender); + logger.warn("test"); + assertEquals(1, appender.getWarnings().size()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + +} diff --git a/commons/src/test/java/lcsb/mapviewer/common/SystemClipboardTest.java b/commons/src/test/java/lcsb/mapviewer/common/SystemClipboardTest.java index 88ba17059b9e2a6fd06cfd679dcf7e2e1f837aab..6d4de972a6142f605d7ae7d6e35cdc78d4b42f8d 100644 --- a/commons/src/test/java/lcsb/mapviewer/common/SystemClipboardTest.java +++ b/commons/src/test/java/lcsb/mapviewer/common/SystemClipboardTest.java @@ -10,13 +10,14 @@ import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.junit.After; import org.junit.Before; import org.junit.Test; public class SystemClipboardTest { - Logger logger = Logger.getLogger(SystemClipboardTest.class); + Logger logger = LogManager.getLogger(SystemClipboardTest.class); @Before public void setUp() throws Exception { diff --git a/commons/src/test/java/lcsb/mapviewer/common/XmlParserTest.java b/commons/src/test/java/lcsb/mapviewer/common/XmlParserTest.java index be6e624130e9d278cd82b4d33e713d622529f2bd..018d6e5ffc30f94b3bc370846cfb93d78fbb60c7 100644 --- a/commons/src/test/java/lcsb/mapviewer/common/XmlParserTest.java +++ b/commons/src/test/java/lcsb/mapviewer/common/XmlParserTest.java @@ -14,7 +14,8 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.List; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.xerces.dom.DocumentImpl; import org.apache.xerces.dom.ElementImpl; import org.junit.After; @@ -30,7 +31,7 @@ import lcsb.mapviewer.common.exception.InvalidArgumentException; import lcsb.mapviewer.common.exception.InvalidXmlSchemaException; public class XmlParserTest { - Logger logger = Logger.getLogger(XmlParserTest.class); + Logger logger = LogManager.getLogger(XmlParserTest.class); @Before public void setUp() throws Exception { diff --git a/commons/src/test/java/lcsb/mapviewer/common/geometry/EllipseTransformationTest.java b/commons/src/test/java/lcsb/mapviewer/common/geometry/EllipseTransformationTest.java index eeeca94a548a9e08f9c9fa204fab79ab15b68d72..72b096c563f7fc319895d620285223feaea23f72 100644 --- a/commons/src/test/java/lcsb/mapviewer/common/geometry/EllipseTransformationTest.java +++ b/commons/src/test/java/lcsb/mapviewer/common/geometry/EllipseTransformationTest.java @@ -5,7 +5,8 @@ import static org.junit.Assert.*; import java.awt.geom.Ellipse2D; import java.awt.geom.Point2D; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -13,7 +14,7 @@ import org.junit.Test; import lcsb.mapviewer.common.Configuration; public class EllipseTransformationTest { - Logger logger = Logger.getLogger(EllipseTransformationTest.class); + Logger logger = LogManager.getLogger(EllipseTransformationTest.class); @Before public void setUp() throws Exception { diff --git a/commons/src/test/java/lcsb/mapviewer/common/geometry/LineTransformationTest.java b/commons/src/test/java/lcsb/mapviewer/common/geometry/LineTransformationTest.java index 078abe659154adfac59c5ecbca2a7b16fbc61935..70ede68dd57529b4e06092fe573365be21a612a6 100644 --- a/commons/src/test/java/lcsb/mapviewer/common/geometry/LineTransformationTest.java +++ b/commons/src/test/java/lcsb/mapviewer/common/geometry/LineTransformationTest.java @@ -13,7 +13,8 @@ import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.awt.geom.RoundRectangle2D; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -21,194 +22,199 @@ import org.junit.Test; import lcsb.mapviewer.common.Configuration; public class LineTransformationTest { - static Logger logger = Logger.getLogger(LineTransformationTest.class); - static double EPSILON = Configuration.EPSILON; - LineTransformation lineTransformation = new LineTransformation(); - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testIntersection() { - Shape s = new RoundRectangle2D.Double(0.0, 0.0, 100.0, 100.0, 10.0, 10.0); - Line2D line = new Line2D.Double(50, 50, 50, 200); - Point2D p = lineTransformation.getIntersectionWithPathIterator(line, s.getPathIterator(new AffineTransform())); - Point2D intPoint = new Point2D.Double(50, 100); - double dist = p.distance(intPoint); - assertEquals("Invalid intersection point: " + p, 0, dist, EPSILON); - } - - @Test - public void testIntersectionWithBezierCurve() { - double x = 0; - double y = 0; - double w = 100; - double h = 200; - GeneralPath path = new GeneralPath(); - - path.moveTo((float) (x + 0.25 * w), (float) (y + 0.25 * h)); - path.quadTo((float) (x + 0.05 * w), (float) (y + 0.25 * h), x, (float) (y + 0.5 * h)); - path.curveTo(x, (float) (y + 0.66 * h), (float) (x + 0.18 * w), (float) (y + 0.9 * h), (float) (x + 0.31 * w), (float) (y + 0.8 * h)); - path.curveTo((float) (x + 0.4 * w), (y + h), (float) (x + 0.7 * w), (y + h), (float) (x + 0.8 * w), (float) (y + 0.8 * h)); - path.curveTo((x + w), (float) (y + 0.8 * h), (x + w), (float) (y + 0.6 * h), (float) (x + 0.875 * w), (float) (y + 0.5 * h)); - path.curveTo((x + w), (float) (y + 0.3 * h), (float) (x + 0.8 * w), (float) (y + 0.1 * h), (float) (x + 0.625 * w), (float) (y + 0.2 * h)); - path.curveTo((float) (x + 0.5 * w), (float) (y + 0.05 * h), (float) (x + 0.3 * w), (float) (y + 0.05 * h), (float) (x + 0.25 * w), (float) (y + 0.25 * h)); - path.closePath(); - - Line2D line = new Line2D.Double(50, 50, 50, 300); - Point2D p = lineTransformation.getIntersectionWithPathIterator(line, path.getPathIterator(new AffineTransform())); - assertNotNull(p); - } - - @Test - public void testIntersectionToInvalidPath() { - Line2D line = new Line2D.Double(50, 50, 50, 200); - Point2D p = lineTransformation.getIntersectionWithPathIterator(line, null); - assertNull(p); - - Shape s = new Rectangle2D.Double(0.0, 0.0, 10.0, 0.0); - p = lineTransformation.getIntersectionWithPathIterator(line, s.getPathIterator(new AffineTransform())); - assertNull(p); - - p = lineTransformation.getIntersectionWithPathIterator(line, new PathIterator() { - - @Override - public void next() { - // TODO Auto-generated method stub - - } - - @Override - public boolean isDone() { - return true; - } - - @Override - public int getWindingRule() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public int currentSegment(double[] coords) { - // TODO Auto-generated method stub - return 0; - } - - @Override - public int currentSegment(float[] coords) { - // TODO Auto-generated method stub - return 0; - } - }); - assertNull(p); - } - - @Test - public void testDistance() { - assertNotNull(lineTransformation); - Point2D p1 = new Point2D.Double(2, 2); - Point2D p2 = new Point2D.Double(3, 3); - Point2D p3 = new Point2D.Double(4, 4); - - double dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); - double d = Math.sqrt(2); - assertEquals(dist, d, EPSILON); - - p3 = new Point2D.Double(3, 4); - dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); - assertEquals(dist, 1, EPSILON); - - p3 = new Point2D.Double(3, 3); - dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); - assertEquals(dist, 0, EPSILON); - - p3 = new Point2D.Double(2.5, 2.5); - dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); - assertEquals(dist, 0, EPSILON); - - p3 = new Point2D.Double(2, 2); - dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); - assertEquals(dist, 0, EPSILON); - - p3 = new Point2D.Double(2, 0); - dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); - assertEquals(dist, 2, EPSILON); - - p3 = new Point2D.Double(3, 2); - dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); - d = Math.sqrt(0.5); - assertEquals(dist, d, EPSILON); - - } - - @Test - public void testDistanceToEmptyLine() { - Point2D p1 = new Point2D.Double(2, 2); - Point2D p2 = new Point2D.Double(3, 3); - double dist = lineTransformation.distBetweenPointAndLineSegment(p1, p1, p2); - assertEquals(p1.distance(p2), dist, EPSILON); - - // the same with line - Line2D line = new Line2D.Double(p1, p1); - dist = lineTransformation.distBetweenPointAndLineSegment(line, p2); - assertEquals(p1.distance(p2), dist, EPSILON); - } - - @Test - public void testClosestPointOnSegmentLineToPointOnEmptyLine() { - Point2D p1 = new Point2D.Double(2, 2); - Point2D p2 = new Point2D.Double(3, 3); - Point2D p3 = lineTransformation.closestPointOnSegmentLineToPoint(p1, p1, p2); - assertEquals(p3, p1); - - // the same with line - Line2D line = new Line2D.Double(p1, p1); - p3 = lineTransformation.closestPointOnSegmentLineToPoint(line, p2); - assertEquals(p3, p1); - - } - - @Test - public void testClosestPoint() { - assertNotNull(lineTransformation); - Point2D p1 = new Point2D.Double(2, 2); - Point2D p2 = new Point2D.Double(3, 3); - Point2D p3 = new Point2D.Double(4, 4); - - Point2D res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); - double d = Math.sqrt(2); - assertEquals(res.distance(p3), d, EPSILON); - - p3 = new Point2D.Double(3, 4); - res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); - assertEquals(res.distance(p3), 1, EPSILON); - - p3 = new Point2D.Double(3, 3); - res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); - assertEquals(res.distance(p3), 0, EPSILON); - - p3 = new Point2D.Double(2.5, 2.5); - res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); - assertEquals(res.distance(p3), 0, EPSILON); - - p3 = new Point2D.Double(2, 2); - res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); - assertEquals(res.distance(p3), 0, EPSILON); - - p3 = new Point2D.Double(2, 0); - res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); - assertEquals(res.distance(p3), 2, EPSILON); - - p3 = new Point2D.Double(3, 2); - res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); - d = Math.sqrt(0.5); - assertEquals(res.distance(p3), d, EPSILON); - - } + static Logger logger = LogManager.getLogger(LineTransformationTest.class); + static double EPSILON = Configuration.EPSILON; + LineTransformation lineTransformation = new LineTransformation(); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testIntersection() { + Shape s = new RoundRectangle2D.Double(0.0, 0.0, 100.0, 100.0, 10.0, 10.0); + Line2D line = new Line2D.Double(50, 50, 50, 200); + Point2D p = lineTransformation.getIntersectionWithPathIterator(line, s.getPathIterator(new AffineTransform())); + Point2D intPoint = new Point2D.Double(50, 100); + double dist = p.distance(intPoint); + assertEquals("Invalid intersection point: " + p, 0, dist, EPSILON); + } + + @Test + public void testIntersectionWithBezierCurve() { + double x = 0; + double y = 0; + double w = 100; + double h = 200; + GeneralPath path = new GeneralPath(); + + path.moveTo((float) (x + 0.25 * w), (float) (y + 0.25 * h)); + path.quadTo((float) (x + 0.05 * w), (float) (y + 0.25 * h), x, (float) (y + 0.5 * h)); + path.curveTo(x, (float) (y + 0.66 * h), (float) (x + 0.18 * w), (float) (y + 0.9 * h), (float) (x + 0.31 * w), + (float) (y + 0.8 * h)); + path.curveTo((float) (x + 0.4 * w), (y + h), (float) (x + 0.7 * w), (y + h), (float) (x + 0.8 * w), + (float) (y + 0.8 * h)); + path.curveTo((x + w), (float) (y + 0.8 * h), (x + w), (float) (y + 0.6 * h), (float) (x + 0.875 * w), + (float) (y + 0.5 * h)); + path.curveTo((x + w), (float) (y + 0.3 * h), (float) (x + 0.8 * w), (float) (y + 0.1 * h), (float) (x + 0.625 * w), + (float) (y + 0.2 * h)); + path.curveTo((float) (x + 0.5 * w), (float) (y + 0.05 * h), (float) (x + 0.3 * w), (float) (y + 0.05 * h), + (float) (x + 0.25 * w), (float) (y + 0.25 * h)); + path.closePath(); + + Line2D line = new Line2D.Double(50, 50, 50, 300); + Point2D p = lineTransformation.getIntersectionWithPathIterator(line, path.getPathIterator(new AffineTransform())); + assertNotNull(p); + } + + @Test + public void testIntersectionToInvalidPath() { + Line2D line = new Line2D.Double(50, 50, 50, 200); + Point2D p = lineTransformation.getIntersectionWithPathIterator(line, null); + assertNull(p); + + Shape s = new Rectangle2D.Double(0.0, 0.0, 10.0, 0.0); + p = lineTransformation.getIntersectionWithPathIterator(line, s.getPathIterator(new AffineTransform())); + assertNull(p); + + p = lineTransformation.getIntersectionWithPathIterator(line, new PathIterator() { + + @Override + public void next() { + // TODO Auto-generated method stub + + } + + @Override + public boolean isDone() { + return true; + } + + @Override + public int getWindingRule() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int currentSegment(double[] coords) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int currentSegment(float[] coords) { + // TODO Auto-generated method stub + return 0; + } + }); + assertNull(p); + } + + @Test + public void testDistance() { + assertNotNull(lineTransformation); + Point2D p1 = new Point2D.Double(2, 2); + Point2D p2 = new Point2D.Double(3, 3); + Point2D p3 = new Point2D.Double(4, 4); + + double dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); + double d = Math.sqrt(2); + assertEquals(dist, d, EPSILON); + + p3 = new Point2D.Double(3, 4); + dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); + assertEquals(dist, 1, EPSILON); + + p3 = new Point2D.Double(3, 3); + dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); + assertEquals(dist, 0, EPSILON); + + p3 = new Point2D.Double(2.5, 2.5); + dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); + assertEquals(dist, 0, EPSILON); + + p3 = new Point2D.Double(2, 2); + dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); + assertEquals(dist, 0, EPSILON); + + p3 = new Point2D.Double(2, 0); + dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); + assertEquals(dist, 2, EPSILON); + + p3 = new Point2D.Double(3, 2); + dist = lineTransformation.distBetweenPointAndLineSegment(p1, p2, p3); + d = Math.sqrt(0.5); + assertEquals(dist, d, EPSILON); + + } + + @Test + public void testDistanceToEmptyLine() { + Point2D p1 = new Point2D.Double(2, 2); + Point2D p2 = new Point2D.Double(3, 3); + double dist = lineTransformation.distBetweenPointAndLineSegment(p1, p1, p2); + assertEquals(p1.distance(p2), dist, EPSILON); + + // the same with line + Line2D line = new Line2D.Double(p1, p1); + dist = lineTransformation.distBetweenPointAndLineSegment(line, p2); + assertEquals(p1.distance(p2), dist, EPSILON); + } + + @Test + public void testClosestPointOnSegmentLineToPointOnEmptyLine() { + Point2D p1 = new Point2D.Double(2, 2); + Point2D p2 = new Point2D.Double(3, 3); + Point2D p3 = lineTransformation.closestPointOnSegmentLineToPoint(p1, p1, p2); + assertEquals(p3, p1); + + // the same with line + Line2D line = new Line2D.Double(p1, p1); + p3 = lineTransformation.closestPointOnSegmentLineToPoint(line, p2); + assertEquals(p3, p1); + + } + + @Test + public void testClosestPoint() { + assertNotNull(lineTransformation); + Point2D p1 = new Point2D.Double(2, 2); + Point2D p2 = new Point2D.Double(3, 3); + Point2D p3 = new Point2D.Double(4, 4); + + Point2D res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); + double d = Math.sqrt(2); + assertEquals(res.distance(p3), d, EPSILON); + + p3 = new Point2D.Double(3, 4); + res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); + assertEquals(res.distance(p3), 1, EPSILON); + + p3 = new Point2D.Double(3, 3); + res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); + assertEquals(res.distance(p3), 0, EPSILON); + + p3 = new Point2D.Double(2.5, 2.5); + res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); + assertEquals(res.distance(p3), 0, EPSILON); + + p3 = new Point2D.Double(2, 2); + res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); + assertEquals(res.distance(p3), 0, EPSILON); + + p3 = new Point2D.Double(2, 0); + res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); + assertEquals(res.distance(p3), 2, EPSILON); + + p3 = new Point2D.Double(3, 2); + res = lineTransformation.closestPointOnSegmentLineToPoint(p1, p2, p3); + d = Math.sqrt(0.5); + assertEquals(res.distance(p3), d, EPSILON); + + } } diff --git a/commons/src/test/resources/log4j.properties b/commons/src/test/resources/log4j.properties deleted file mode 100644 index e270d0a2fa03b224428ebcbda10a6a5c5d107e46..0000000000000000000000000000000000000000 --- a/commons/src/test/resources/log4j.properties +++ /dev/null @@ -1,20 +0,0 @@ -#Set root logger 's level and its appender to an appender called CONSOLE which is defined below. -log4j.rootLogger=info, CONSOLE - -#Set the behavior of the CONSOLE appender -log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender -log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout -log4j.appender.CONSOLE.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n -#log4j.appender.CONSOLE.layout.ConversionPattern=%m%n - - -#Set the behavior of the FILE appender -log4j.appender.R=org.apache.log4j.FileAppender -log4j.appender.R.File=${catalina.home}/logs/MapViewer.log -log4j.appender.R.layout=org.apache.log4j.PatternLayout -log4j.appender.R.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n - - -log4j.logger.org.springframework=warn -log4j.logger.org.hibernate=warn -log4j.logger.lcsb=trace diff --git a/commons/src/test/resources/log4j2.properties b/commons/src/test/resources/log4j2.properties new file mode 100644 index 0000000000000000000000000000000000000000..d5d5de895501a0e3bed0fd4816fc302ed65fc346 --- /dev/null +++ b/commons/src/test/resources/log4j2.properties @@ -0,0 +1,18 @@ +rootLogger.level = INFO +appenders = console + +appender.console.type = Console +appender.console.name = STDOUT +appender.console.layout.type = PatternLayout +appender.console.layout.pattern = %d %5p [%t] (%F:%L) - %m%n + +rootLogger.appenderRefs = console + +rootLogger.appenderRef.console.ref = STDOUT + +logger.minerva.name = lcsb +logger.minerva.level = debug + +#log4j.logger.org.springframework=warn +#log4j.logger.org.hibernate=warn +#log4j.logger.lcsb=trace diff --git a/pom.xml b/pom.xml index 2414cc96294e7f5dedb9e93357d58919c901e1e4..a0952e6f10554272c463864cdc3bb3cc841f35c8 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,7 @@ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <log4j.version>1.2.17</log4j.version> + <log4j2.version>2.11.2</log4j2.version> <xercesImp.version>2.12.0</xercesImp.version>