Skip to content

Commit

Permalink
added DrawingUtilTest
Browse files Browse the repository at this point in the history
  • Loading branch information
SingingBush committed Oct 11, 2024
1 parent d458e4f commit 4510ce7
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
*/
package org.krysalis.barcode4j.impl;

import org.jetbrains.annotations.NotNull;
import org.krysalis.barcode4j.TextAlignment;
import org.krysalis.barcode4j.output.Canvas;
import org.krysalis.barcode4j.tools.UnitConv;

/**
* Drawing utilities.
*
*
* @author Jeremias Maerki
* @version $Id: DrawingUtil.java,v 1.2 2008-05-13 13:00:45 jmaerki Exp $
*/
Expand All @@ -35,12 +36,14 @@ public class DrawingUtil {
* @param x1 the left boundary
* @param x2 the right boundary
* @param y1 the y coordinate of the font's baseline
* @deprecated use {@link Canvas#drawCenteredChar(char, double, double, double, String, double)}
*/
public static void drawCenteredChar(Canvas canvas, AbstractBarcodeBean bean,
char ch,
@Deprecated
public static void drawCenteredChar(Canvas canvas, AbstractBarcodeBean bean,

Check warning on line 42 in barcode4j/src/main/java/org/krysalis/barcode4j/impl/DrawingUtil.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Deprecated member is still used

Deprecated member 'drawCenteredChar' is still used
char ch,
double x1, double x2, double y1) {
canvas.drawCenteredChar(ch, x1, x2,
y1 - UnitConv.pt2mm(bean.getFontSize()) * 0.2,
canvas.drawCenteredChar(ch, x1, x2,
y1 - UnitConv.pt2mm(bean.getFontSize()) * 0.2,
bean.getFontName(), bean.getFontSize());
}

Expand All @@ -54,8 +57,9 @@ public static void drawCenteredChar(Canvas canvas, AbstractBarcodeBean bean,
* @param y1 the y coordinate of the font's baseline
* @deprecated Use {@link #drawText(Canvas, AbstractBarcodeBean, String, double, double, double, TextAlignment)} instead.
*/
@Deprecated
public static void drawJustifiedText(Canvas canvas, AbstractBarcodeBean bean,

Check warning on line 61 in barcode4j/src/main/java/org/krysalis/barcode4j/impl/DrawingUtil.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Deprecated member is still used

Deprecated member 'drawJustifiedText' is still used
String text,
String text,
double x1, double x2, double y1) {
drawText(canvas, bean, text, x1, x2, y1, TextAlignment.TA_JUSTIFY);
}
Expand All @@ -70,8 +74,9 @@ public static void drawJustifiedText(Canvas canvas, AbstractBarcodeBean bean,
* @param y1 the y coordinate of the font's baseline
* @deprecated Use {@link #drawText(Canvas, AbstractBarcodeBean, String, double, double, double, TextAlignment)} instead.
*/
public static void drawCenteredText(Canvas canvas, AbstractBarcodeBean bean,
String text,
@Deprecated
public static void drawCenteredText(Canvas canvas, AbstractBarcodeBean bean,

Check warning on line 78 in barcode4j/src/main/java/org/krysalis/barcode4j/impl/DrawingUtil.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Deprecated member is still used

Deprecated member 'drawCenteredText' is still used
String text,
double x1, double x2, double y1) {
drawText(canvas, bean, text, x1, x2, y1, TextAlignment.TA_CENTER);
}
Expand All @@ -86,14 +91,14 @@ public static void drawCenteredText(Canvas canvas, AbstractBarcodeBean bean,
* @param y1 the y coordinate of the font's baseline
* @param textAlign the text alignment
*/
public static void drawText(Canvas canvas, AbstractBarcodeBean bean,
String text,
public static void drawText(@NotNull final Canvas canvas, @NotNull final AbstractBarcodeBean bean,
String text,
double x1, double x2, double y1,
TextAlignment textAlign) {
canvas.drawText(text, x1, x2,
y1 - UnitConv.pt2mm(bean.getFontSize()) * 0.2,
canvas.drawText(text, x1, x2,
y1 - UnitConv.pt2mm(bean.getFontSize()) * 0.2,
bean.getFontName(), bean.getFontSize(),
textAlign);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.krysalis.barcode4j.impl;

import org.junit.jupiter.api.Test;
import org.krysalis.barcode4j.TextAlignment;
import org.krysalis.barcode4j.output.Canvas;

import static org.mockito.Mockito.*;

class DrawingUtilTest {

@Test
void drawCenteredChar() {
final Canvas mockCanvas = mock(Canvas.class);
final AbstractBarcodeBean mockBean = mock(AbstractBarcodeBean.class);
when(mockBean.getFontName()).thenReturn("TestingFontName");
when(mockBean.getFontSize()).thenReturn(1.2);

DrawingUtil.drawCenteredChar(
mockCanvas,
mockBean,
'A',
1.0,
1.0,
1.0
);

verify(mockCanvas, times(1)).drawCenteredChar(
eq('A'),
eq(1.0),
eq(1.0),
anyDouble(),
eq("TestingFontName"),
eq(1.2)
);
}

@Test
void drawJustifiedText() {
final Canvas mockCanvas = mock(Canvas.class);
final AbstractBarcodeBean mockBean = mock(AbstractBarcodeBean.class);
when(mockBean.getFontName()).thenReturn("TestingFontName");
when(mockBean.getFontSize()).thenReturn(1.2);

DrawingUtil.drawJustifiedText(
mockCanvas,
mockBean,
"Hello World",
1.0,
1.0,
1.0
);

verify(mockCanvas, times(1)).drawText(
eq("Hello World"),
eq(1.0),
eq(1.0),
anyDouble(),
eq("TestingFontName"),
eq(1.2),
eq(TextAlignment.TA_JUSTIFY)
);
}

@Test
void drawCenteredText() {
final Canvas mockCanvas = mock(Canvas.class);
final AbstractBarcodeBean mockBean = mock(AbstractBarcodeBean.class);
when(mockBean.getFontName()).thenReturn("TestingFontName");
when(mockBean.getFontSize()).thenReturn(1.2);

DrawingUtil.drawCenteredText(
mockCanvas,
mockBean,
"Hello World",
1.0,
1.0,
1.0
);

verify(mockCanvas, times(1)).drawText(
eq("Hello World"),
eq(1.0),
eq(1.0),
anyDouble(),
eq("TestingFontName"),
eq(1.2),
eq(TextAlignment.TA_CENTER)
);
}

@Test
void drawText() {
final Canvas mockCanvas = mock(Canvas.class);
final AbstractBarcodeBean mockBean = mock(AbstractBarcodeBean.class);
when(mockBean.getFontName()).thenReturn("TestingFontName");
when(mockBean.getFontSize()).thenReturn(1.2);

DrawingUtil.drawText(
mockCanvas,
mockBean,
"Hello World",
1.0,
1.0,
1.0,
TextAlignment.TA_RIGHT
);

verify(mockCanvas, times(1)).drawText(
eq("Hello World"),
eq(1.0),
eq(1.0),
anyDouble(),
eq("TestingFontName"),
eq(1.2),
eq(TextAlignment.TA_RIGHT)
);
}
}

0 comments on commit 4510ce7

Please sign in to comment.