Hallo zusammen,
ich würde gerne für das erstellen eines 2D Arrays einen JUnit-Test schreiben. Ich habe eine Klasse, welche Barcodes in verschiedenen Formen erstellen kann. In der Methode createMatrixBarcode() werden die Arrays dann erstellt. Nun würde ich gerne für diese Methode eine JUnit Test haben. Ich weiß nur leider nicht genau wie ich das am besten machen kann.
Vielen Dank im Voraus.
public class BarcodeFactory {
/**
* efüllt das Array mit "X" und "O", so das eine Diagonale entsteht.
* @param b ist das übergeben Array
* @Return
*/
private char[][] printDiagonal(char[][]b) {
for(int x = 0; x < b.length; x++){
for(int y = 0; y < b[x].length; y++) {
if(y==x) {
b [x][y] = 'X';
} else {
b [x][y] = 'O';
}
}
}
return b;
}
/**
* efüllt das Array mit "X" und "O", so das ein X entsteht.
* @param b ist das übergeben Array
* @Return
*/
private char[][] printX(char[][]b) {
for(int x = 0; x < b.length; x++){
for(int y = 0; y < b[x].length; y++) {
if(y==x || x + y == b.length -1) {
b [x][y] = 'X';
} else {
b [x][y] = 'O';
}
}
}
return b;
}
/**
* Befüllt das Array mit "X" und "O", so das ein Kreuz entsteht.
* @param b ist das übergeben Array
* @Return
*/
private char[][] printCross(char[][]b) {
for(int x = 0; x < b.length; x++){
for(int y = 0; y < b[x].length; y++) {
if(y==b[x].length/2 || x==b.length/2) {
b [x][y] = 'X';
} else {
b [x][y] = 'O';
}
}
}
return b;
}
/**
* Befüllt das Array mit "O", so das ein Kreuz entsteht.
* @param b ist das übergeben Array
* @Return
*/
private char[][] printLeer(char[][]b) {
for(int x = 0; x < b.length; x++){
for(int y = 0; y < b[x].length; y++) {
b [x][y] = 'O';
}
}
return b;
}
/**
* Methode erstellt ein Barcode, unter Berücksichtigung der angebenen Anzahl von Zeilen und Spalten und dem übergebenen Barcode Typ.
* @param row gibt die Anzahl der Zeilen an.
* @param spalte gibt die Anzahl an Spalten an.
* @param barcodeTyp gibt den Typ des Barcodes an.
* @Return
* @throws BarcodeException
*/
public char[][] createMatrixBarcode(int row, int spalte, BarcodeTyp barcodeTyp) throws BarcodeException {
char[][] b = new char[row][spalte];
if(barcodeTyp == null) {
throw new MissingBarcodeTypeException();
}
//überprüft ob Barcode Typ Diagonale übergeben wurde
if(barcodeTyp == BarcodeTyp.Diagonale) {
if(row != spalte) {
throw new InvalidDimensionException(row, spalte, barcodeTyp);
} else {
//Helper.printBarcode(printDiagonal(b));
return printDiagonal(b);
}
}
if(barcodeTyp == BarcodeTyp.X) {
if(row != spalte) {
throw new InvalidDimensionException(row, spalte, barcodeTyp);
} else {
//Helper.printBarcode(printX(b));
return printX(b);
}
}
if(barcodeTyp == BarcodeTyp.Kreuz) {
if(row == spalte) {
throw new InvalidDimensionException(row, spalte, barcodeTyp);
} else {
//Helper.printBarcode(printCross(b));
return printCross(b);
}
}
if(barcodeTyp == BarcodeTyp.Leer) {
//Helper.printBarcode(printLeer(b));
return printLeer(b);
}
return b;
}
}
ich würde gerne für das erstellen eines 2D Arrays einen JUnit-Test schreiben. Ich habe eine Klasse, welche Barcodes in verschiedenen Formen erstellen kann. In der Methode createMatrixBarcode() werden die Arrays dann erstellt. Nun würde ich gerne für diese Methode eine JUnit Test haben. Ich weiß nur leider nicht genau wie ich das am besten machen kann.
Vielen Dank im Voraus.
public class BarcodeFactory {
/**
* efüllt das Array mit "X" und "O", so das eine Diagonale entsteht.
* @param b ist das übergeben Array
* @Return
*/
private char[][] printDiagonal(char[][]b) {
for(int x = 0; x < b.length; x++){
for(int y = 0; y < b[x].length; y++) {
if(y==x) {
b [x][y] = 'X';
} else {
b [x][y] = 'O';
}
}
}
return b;
}
/**
* efüllt das Array mit "X" und "O", so das ein X entsteht.
* @param b ist das übergeben Array
* @Return
*/
private char[][] printX(char[][]b) {
for(int x = 0; x < b.length; x++){
for(int y = 0; y < b[x].length; y++) {
if(y==x || x + y == b.length -1) {
b [x][y] = 'X';
} else {
b [x][y] = 'O';
}
}
}
return b;
}
/**
* Befüllt das Array mit "X" und "O", so das ein Kreuz entsteht.
* @param b ist das übergeben Array
* @Return
*/
private char[][] printCross(char[][]b) {
for(int x = 0; x < b.length; x++){
for(int y = 0; y < b[x].length; y++) {
if(y==b[x].length/2 || x==b.length/2) {
b [x][y] = 'X';
} else {
b [x][y] = 'O';
}
}
}
return b;
}
/**
* Befüllt das Array mit "O", so das ein Kreuz entsteht.
* @param b ist das übergeben Array
* @Return
*/
private char[][] printLeer(char[][]b) {
for(int x = 0; x < b.length; x++){
for(int y = 0; y < b[x].length; y++) {
b [x][y] = 'O';
}
}
return b;
}
/**
* Methode erstellt ein Barcode, unter Berücksichtigung der angebenen Anzahl von Zeilen und Spalten und dem übergebenen Barcode Typ.
* @param row gibt die Anzahl der Zeilen an.
* @param spalte gibt die Anzahl an Spalten an.
* @param barcodeTyp gibt den Typ des Barcodes an.
* @Return
* @throws BarcodeException
*/
public char[][] createMatrixBarcode(int row, int spalte, BarcodeTyp barcodeTyp) throws BarcodeException {
char[][] b = new char[row][spalte];
if(barcodeTyp == null) {
throw new MissingBarcodeTypeException();
}
//überprüft ob Barcode Typ Diagonale übergeben wurde
if(barcodeTyp == BarcodeTyp.Diagonale) {
if(row != spalte) {
throw new InvalidDimensionException(row, spalte, barcodeTyp);
} else {
//Helper.printBarcode(printDiagonal(b));
return printDiagonal(b);
}
}
if(barcodeTyp == BarcodeTyp.X) {
if(row != spalte) {
throw new InvalidDimensionException(row, spalte, barcodeTyp);
} else {
//Helper.printBarcode(printX(b));
return printX(b);
}
}
if(barcodeTyp == BarcodeTyp.Kreuz) {
if(row == spalte) {
throw new InvalidDimensionException(row, spalte, barcodeTyp);
} else {
//Helper.printBarcode(printCross(b));
return printCross(b);
}
}
if(barcodeTyp == BarcodeTyp.Leer) {
//Helper.printBarcode(printLeer(b));
return printLeer(b);
}
return b;
}
}