Skip to content

Commit

Permalink
add test for FourStateLogicHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
SingingBush committed Oct 12, 2024
1 parent 8287967 commit 903eeba
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2006,2008 Jeremias Maerki.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -21,17 +21,16 @@
import org.krysalis.barcode4j.output.Canvas;

/**
* Logic Handler to be used by "four-state" barcodes
* Logic Handler to be used by "four-state" barcodes such as {@link RoyalMailCBCBean} and {@link USPSIntelligentMailBean}
* for painting on a Canvas.
*
*
* @author Jeremias Maerki
* @version $Id: FourStateLogicHandler.java,v 1.2 2008-05-13 13:00:43 jmaerki Exp $
*/
public class FourStateLogicHandler
extends AbstractVariableHeightLogicHandler {
public class FourStateLogicHandler extends AbstractVariableHeightLogicHandler {

/**
* Constructor
* Constructor
* @param bcBean the barcode implementation class
* @param canvas the canvas to paint to
*/
Expand All @@ -48,28 +47,28 @@ private double getStartY() {
y += bcBean.getHumanReadableHeight();
}
return y;
}
}

/** {@inheritDoc} */
public void addBar(boolean black, int height) {
final double w = bcBean.getBarWidth(1);
final double h = bcBean.getBarHeight(height);

final double middle = bcBean.getBarHeight() / 2;
double y1;
switch (height) {
case 0:
case 2:
y1 = middle - (bcBean.getBarHeight(0) / 2);
break;
case 1:
case 3:
y1 = middle - (bcBean.getBarHeight(3) / 2);
break;
default:
throw new RuntimeException("Bug!");
case 0:
case 2:
y1 = middle - (bcBean.getBarHeight(0) / 2);
break;
case 1:
case 3:
y1 = middle - (bcBean.getBarHeight(3) / 2);
break;
default:
throw new RuntimeException("Bug!");
}

canvas.drawRectWH(x, getStartY() + y1, w, h);
x += w + bcBean.getBarWidth(-1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.krysalis.barcode4j.impl.fourstate;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.krysalis.barcode4j.impl.HeightVariableBarcodeBean;
import org.krysalis.barcode4j.output.Canvas;

import static org.mockito.Mockito.anyDouble;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* @author Samael Bate (singingbush)
* created on 12/10/2024
*/
class FourStateLogicHandlerTest {

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3 })
void addBar(final int height) {
final HeightVariableBarcodeBean bean = mock(HeightVariableBarcodeBean.class);
when(bean.getBarWidth(1)).thenReturn(100.0);
when(bean.getBarHeight(height)).thenReturn(100.0);
when(bean.getBarHeight()).thenReturn((double)height);
when(bean.getBarHeight(anyInt())).thenReturn(4.0);

final Canvas canvas = mock(Canvas.class);
final FourStateLogicHandler logicHandler = new FourStateLogicHandler(bean, canvas);

logicHandler.addBar(true, height);

verify(canvas, times(1)).drawRectWH(
anyDouble(),
anyDouble(),
eq(100.0),
eq(4.0)
);
verify(bean, times(1)).getBarWidth(eq(-1));
}
}

0 comments on commit 903eeba

Please sign in to comment.