From 2e1996c1e73ded7c78e9de9d0047b3291d5d5ee6 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Tue, 18 Sep 2018 18:38:44 +0200
Subject: [PATCH] when fitting text into complex raises exception due to two
 small font size, text is not rendered

---
 .../element/species/ComplexConverter.java     |   3 +-
 .../species/AllSpeciesConverterTests.java     |   3 +-
 .../element/species/ComplexConverterTest.java | 121 ++++++++++++++++++
 3 files changed, 124 insertions(+), 3 deletions(-)
 create mode 100644 converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/ComplexConverterTest.java

diff --git a/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/ComplexConverter.java b/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/ComplexConverter.java
index cb11dd86e9..c7b730b76e 100644
--- a/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/ComplexConverter.java
+++ b/converter-graphics/src/main/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/ComplexConverter.java
@@ -144,6 +144,7 @@ public class ComplexConverter extends SpeciesConverter<Complex> {
 	public void drawText(final Complex complex, final Graphics2D graphics, final ConverterParams params) {
 		if (complex.getElements().size() > 0) {
 			if (isTransparent(complex, params)) {
+
 				super.drawText(complex, graphics, params);
 				return;
 			}
@@ -157,8 +158,6 @@ public class ComplexConverter extends SpeciesConverter<Complex> {
 			int size = (int) FontFinder.findMaxFontSize(params.getScale() * fontSize, Font.SANS_SERIF, graphics, complex.getBorder(), text);
 			FontFinder.drawText(size, Font.SANS_SERIF, graphics, complex.getBorder(), text);
 		} catch (RectangleTooSmallException e) {
-			logger.warn("Problem with finding font size", e);
-			super.drawText(complex, graphics, params);
 		}
 	}
 
diff --git a/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/AllSpeciesConverterTests.java b/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/AllSpeciesConverterTests.java
index 12de6ac30b..eeda18cf8c 100644
--- a/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/AllSpeciesConverterTests.java
+++ b/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/AllSpeciesConverterTests.java
@@ -5,7 +5,8 @@ import org.junit.runners.Suite;
 import org.junit.runners.Suite.SuiteClasses;
 
 @RunWith(Suite.class)
-@SuiteClasses({  SpeciesConverterTest.class })
+@SuiteClasses({ ComplexConverterTest.class, //
+    SpeciesConverterTest.class })
 public class AllSpeciesConverterTests {
 
 }
diff --git a/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/ComplexConverterTest.java b/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/ComplexConverterTest.java
new file mode 100644
index 0000000000..0482bcd5ea
--- /dev/null
+++ b/converter-graphics/src/test/java/lcsb/mapviewer/converter/graphics/bioEntity/element/species/ComplexConverterTest.java
@@ -0,0 +1,121 @@
+package lcsb.mapviewer.converter.graphics.bioEntity.element.species;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.geom.Point2D;
+import java.awt.image.BufferedImage;
+
+import org.apache.log4j.Logger;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import lcsb.mapviewer.commands.ColorExtractor;
+import lcsb.mapviewer.converter.graphics.ConverterParams;
+import lcsb.mapviewer.model.map.species.Complex;
+import lcsb.mapviewer.model.map.species.field.TranscriptionSite;
+
+public class ComplexConverterTest {
+  Logger logger = Logger.getLogger(ComplexConverterTest.class);
+
+  ColorExtractor colorExtractor = new ColorExtractor(Color.RED, Color.GREEN, Color.BLUE);
+
+  @Before
+  public void setUp() throws Exception {
+  }
+
+  @After
+  public void tearDown() throws Exception {
+  }
+
+  @Test
+  public void testDrawText() throws Exception {
+    try {
+      int size = 200;
+      BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
+      Graphics2D graphics = bi.createGraphics();
+      ComplexConverter rc = new ComplexConverter(colorExtractor);
+
+      Complex alias = createComplex();
+      rc.drawText(alias, graphics, new ConverterParams());
+
+      boolean onlyWhite = true;
+      for (int x = 0; x < size; x++) {
+        for (int y = 0; y < size; y++) {
+          if (bi.getRGB(x, y) != 0) {
+            onlyWhite = false;
+          }
+        }
+      }
+      assertFalse(onlyWhite);
+
+    } catch (Exception e) {
+      e.printStackTrace();
+      throw e;
+    }
+  }
+
+  @Test
+  public void testDrawTooBigText() throws Exception {
+    try {
+      int size = 200;
+      double scale = 100;
+      BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
+      Graphics2D graphics = bi.createGraphics();
+      Color color = graphics.getColor();
+      graphics.scale(1.0 / scale, 1.0 / scale);
+      ComplexConverter rc = new ComplexConverter(colorExtractor);
+
+      Complex alias = createComplex();
+      rc.drawText(alias, graphics, new ConverterParams());
+
+      assertEquals(color, graphics.getColor());
+
+    } catch (Exception e) {
+      e.printStackTrace();
+      throw e;
+    }
+  }
+
+  @Test
+  public void testDrawTranscriptionSite() throws Exception {
+    try {
+      BufferedImage bi = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
+      Graphics2D graphics = bi.createGraphics();
+
+      TranscriptionSite transcriptionSite = new TranscriptionSite();
+      transcriptionSite.setPosition(new Point2D.Double(10, 10));
+      transcriptionSite.setWidth(100);
+      transcriptionSite.setHeight(20);
+      transcriptionSite.setActive(true);
+      transcriptionSite.setDirection("LEFT");
+
+      ProteinConverter converter = Mockito.spy(new ProteinConverter(colorExtractor));
+      converter.drawModification(transcriptionSite, graphics, false);
+      verify(converter, times(1)).drawTranscriptionSite(any(), any());
+
+    } catch (Exception e) {
+      e.printStackTrace();
+      throw e;
+    }
+  }
+
+  private Complex createComplex() {
+    Complex protein = new Complex("id");
+    protein.setName("NAME_OF_THE_ELEMENT");
+    protein.setX(10);
+    protein.setY(20);
+    protein.setWidth(100);
+    protein.setHeight(80);
+    protein.setColor(Color.WHITE);
+
+    return protein;
+  }
+}
-- 
GitLab