KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > impl > Code39Test


1 /*
2  * $Id: Code39Test.java,v 1.4 2003/10/31 08:51:12 jmaerki Exp $
3  * ============================================================================
4  * The Krysalis Patchy Software License, Version 1.1_01
5  * Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
6  *
7  * This Licence is compatible with the BSD licence as described and
8  * approved by http://www.opensource.org/, and is based on the
9  * Apache Software Licence Version 1.1.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed for project
26  * Krysalis (http://www.krysalis.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Krysalis" and "Nicola Ken Barozzi" and
31  * "Krysalis Barcode" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact nicolaken@krysalis.org.
34  *
35  * 5. Products derived from this software may not be called "Krysalis",
36  * "Krysalis Barcode", nor may "Krysalis" appear in their name,
37  * without prior written permission of Nicola Ken Barozzi.
38  *
39  * 6. This software may contain voluntary contributions made by many
40  * individuals, who decided to donate the code to this project in
41  * respect of this licence, and was originally created by
42  * Jeremias Maerki <jeremias@maerki.org>.
43  *
44  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  * DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  * ====================================================================
57  */

58 package org.krysalis.barcode.impl;
59
60 import org.krysalis.barcode.ChecksumMode;
61
62 import junit.framework.TestCase;
63
64 /**
65  * Test class for the Code39 implementation.
66  *
67  * @author Jeremias Maerki
68  */

69 public class Code39Test extends TestCase {
70
71     public Code39Test(String JavaDoc name) {
72         super(name);
73     }
74
75     public void testChecksum() throws Exception JavaDoc {
76         assertEquals('L', Code39LogicImpl.calcChecksum("12345ABCDEZ/"));
77         assertEquals('L', Code39LogicImpl.calcChecksum("12345abcdez/"));
78         assertEquals('M', Code39LogicImpl.calcChecksum("494140"));
79         assertEquals('P', Code39LogicImpl.calcChecksum("415339"));
80     }
81     
82     public void testIllegalArguments() throws Exception JavaDoc {
83         try {
84             Code39 impl = new Code39();
85             impl.generateBarcode(null, null);
86             fail("Expected an NPE");
87         } catch (NullPointerException JavaDoc npe) {
88             assertNotNull("Error message is empty", npe.getMessage());
89         }
90     }
91     
92     public void testLogic() throws Exception JavaDoc {
93         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
94         Code39LogicImpl logic;
95         String JavaDoc expected;
96         
97         try {
98             logic = new Code39LogicImpl(ChecksumMode.CP_AUTO);
99             logic.generateBarcodeLogic(new NullClassicBarcodeLogicHandler(), "1232");
100             fail("Expected an exception complaining about illegal characters");
101         } catch (IllegalArgumentException JavaDoc iae) {
102             //must fail
103
}
104         
105         logic = new Code39LogicImpl(ChecksumMode.CP_AUTO);
106         logic.generateBarcodeLogic(new MockClassicBarcodeLogicHandler(sb), "123ABC");
107         expected = "<BC>"
108             + "<SBG:start-char:*>"
109             + "<SBG:msg-char:*>B1W2B1W1B2W1B2W1B1</SBG>"
110             + "</SBG>"
111             + "W-1"
112             + "<SBG:msg-char:1>B2W1B1W2B1W1B1W1B2</SBG>"
113             + "W-1"
114             + "<SBG:msg-char:2>B1W1B2W2B1W1B1W1B2</SBG>"
115             + "W-1"
116             + "<SBG:msg-char:3>B2W1B2W2B1W1B1W1B1</SBG>"
117             + "W-1"
118             + "<SBG:msg-char:A>B2W1B1W1B1W2B1W1B2</SBG>"
119             + "W-1"
120             + "<SBG:msg-char:B>B1W1B2W1B1W2B1W1B2</SBG>"
121             + "W-1"
122             + "<SBG:msg-char:C>B2W1B2W1B1W2B1W1B1</SBG>"
123             + "W-1"
124             + "<SBG:stop-char:*>"
125             + "<SBG:msg-char:*>B1W2B1W1B2W1B2W1B1</SBG>"
126             + "</SBG>"
127             + "</BC>";
128         //System.out.println(expected);
129
//System.out.println(sb.toString());
130
assertEquals(expected, sb.toString());
131         
132         
133         sb.setLength(0);
134         logic = new Code39LogicImpl(ChecksumMode.CP_ADD);
135         logic.generateBarcodeLogic(new MockClassicBarcodeLogicHandler(sb), "123");
136         expected = "<BC>"
137             + "<SBG:start-char:*>"
138             + "<SBG:msg-char:*>B1W2B1W1B2W1B2W1B1</SBG>"
139             + "</SBG>"
140             + "W-1"
141             + "<SBG:msg-char:1>B2W1B1W2B1W1B1W1B2</SBG>"
142             + "W-1"
143             + "<SBG:msg-char:2>B1W1B2W2B1W1B1W1B2</SBG>"
144             + "W-1"
145             + "<SBG:msg-char:3>B2W1B2W2B1W1B1W1B1</SBG>"
146             + "W-1"
147             + "<SBG:msg-char:6>B1W1B2W2B2W1B1W1B1</SBG>"
148             + "W-1"
149             + "<SBG:stop-char:*>"
150             + "<SBG:msg-char:*>B1W2B1W1B2W1B2W1B1</SBG>"
151             + "</SBG>"
152             + "</BC>";
153         //System.out.println(expected);
154
//System.out.println(sb.toString());
155
assertEquals(expected, sb.toString());
156         
157         
158         sb.setLength(0);
159         logic = new Code39LogicImpl(ChecksumMode.CP_CHECK);
160         logic.generateBarcodeLogic(new MockClassicBarcodeLogicHandler(sb), "1236");
161         expected = "<BC>"
162             + "<SBG:start-char:*>"
163             + "<SBG:msg-char:*>B1W2B1W1B2W1B2W1B1</SBG>"
164             + "</SBG>"
165             + "W-1"
166             + "<SBG:msg-char:1>B2W1B1W2B1W1B1W1B2</SBG>"
167             + "W-1"
168             + "<SBG:msg-char:2>B1W1B2W2B1W1B1W1B2</SBG>"
169             + "W-1"
170             + "<SBG:msg-char:3>B2W1B2W2B1W1B1W1B1</SBG>"
171             + "W-1"
172             + "<SBG:msg-char:6>B1W1B2W2B2W1B1W1B1</SBG>"
173             + "W-1"
174             + "<SBG:stop-char:*>"
175             + "<SBG:msg-char:*>B1W2B1W1B2W1B2W1B1</SBG>"
176             + "</SBG>"
177             + "</BC>";
178         //System.out.println(expected);
179
//System.out.println(sb.toString());
180
assertEquals(expected, sb.toString());
181         
182         
183         sb.setLength(0);
184         logic = new Code39LogicImpl(ChecksumMode.CP_CHECK);
185         try {
186             logic.generateBarcodeLogic(new MockClassicBarcodeLogicHandler(sb), "123F");
187             fail("Expected logic implementation to fail because wrong checksum is supplied");
188         } catch (IllegalArgumentException JavaDoc iae) {
189             //must fail
190
}
191     }
192
193 }
Popular Tags