3ea135bb |
1 | /**\r |
2 | * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>\r |
3 | *\r |
4 | * This library is free software; you can redistribute it and/or\r |
5 | * modify it under the terms of the GNU Lesser General Public\r |
6 | * License as published by the Free Software Foundation; either\r |
7 | * version 2.1 of the License, or (at your option) any later version.\r |
8 | * \r |
9 | * This library is distributed in the hope that it will be useful,\r |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of\r |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r |
12 | * Lesser General Public License for more details.\r |
13 | * \r |
14 | * You should have received a copy of the GNU Lesser General Public\r |
15 | * License along with this library; if not, write to the Free Software\r |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r |
17 | * \r |
18 | */\r |
19 | package de.lohndirekt.print.attribute;\r |
20 | \r |
21 | import java.io.ByteArrayOutputStream;\r |
22 | import java.io.IOException;\r |
23 | import java.net.URI;\r |
24 | import java.net.URISyntaxException;\r |
25 | import java.util.Calendar;\r |
26 | import java.util.Date;\r |
27 | import java.util.Locale;\r |
28 | import java.util.TimeZone;\r |
29 | \r |
30 | import javax.print.attribute.standard.CopiesSupported;\r |
31 | import javax.print.attribute.standard.JobHoldUntil;\r |
32 | import javax.print.attribute.standard.JobName;\r |
33 | import javax.print.attribute.standard.JobPriority;\r |
34 | import javax.print.attribute.standard.JobState;\r |
35 | import javax.print.attribute.standard.PrinterResolution;\r |
36 | import javax.print.attribute.standard.PrinterURI;\r |
37 | \r |
38 | import junit.framework.TestCase;\r |
39 | import de.lohndirekt.print.attribute.cups.JobPageLimit;\r |
40 | import de.lohndirekt.print.attribute.ipp.Charset;\r |
41 | import de.lohndirekt.print.attribute.ipp.jobdesc.JobUri;\r |
42 | import de.lohndirekt.print.attribute.ipp.printerdesc.PrinterCurrentTime;\r |
43 | import de.lohndirekt.print.attribute.ipp.printerdesc.supported.PageRangesSupported;\r |
44 | \r |
45 | /**\r |
46 | * @author ld-development\r |
47 | * \r |
48 | * \r |
49 | */\r |
50 | public class AttributeWriterTest extends TestCase {\r |
51 | \r |
52 | ByteArrayOutputStream out;\r |
53 | \r |
54 | protected void setUp() throws Exception {\r |
55 | super.setUp();\r |
56 | out = new ByteArrayOutputStream();\r |
57 | }\r |
58 | \r |
59 | \r |
60 | private Date date(String timeZoneId){\r |
61 | TimeZone zone = TimeZone.getTimeZone(timeZoneId);\r |
62 | Calendar cal = Calendar.getInstance(zone);\r |
63 | \r |
64 | cal.set(Calendar.YEAR, 2003);\r |
65 | cal.set(Calendar.MONTH, Calendar.JUNE);\r |
66 | cal.set(Calendar.DAY_OF_MONTH, 1);\r |
67 | cal.set(Calendar.MINUTE, 48);\r |
68 | cal.set(Calendar.SECOND, 9);\r |
69 | cal.set(Calendar.MILLISECOND, 47);\r |
70 | cal.set(Calendar.HOUR_OF_DAY, 13);\r |
71 | Date date = cal.getTime();\r |
72 | \r |
73 | return date;\r |
74 | }\r |
75 | /**\r |
76 | * \r |
77 | */\r |
78 | public void testAttributeWriterForDateTimeSyntax() throws IOException {\r |
79 | \r |
80 | //testing for TimeZone Asia/Omsk\r |
81 | //1:48:09 P.M. in Greenwich obviously means 1:48:09 P.M. in Greenwich\r |
82 | Date d = date("UTC");\r |
83 | \r |
84 | // testing lohndirekt attribute\r |
85 | AttributeWriter.attributeBytes(new PrinterCurrentTime(d), out);\r |
86 | assertEquals(new byte[] { 49, 0, 20, 112, 114, 105, 110, 116, 101, 114, 45, 99, 117, 114, 114, 101, 110, 116, 45, 116, 105, 109, 101, 0, 11, 7, -45, 6, 1, 13, 48, 9, 0, 43, 0, 0 }, out.toByteArray());\r |
87 | \r |
88 | //testing javax.print attribute\r |
89 | out.reset();\r |
90 | AttributeWriter.attributeBytes(new JobHoldUntil(d), out);\r |
91 | assertEquals(new byte[] { 49, 0, 14, 106, 111, 98, 45, 104, 111, 108, 100, 45, 117, 110, 116, 105, 108, 0, 11, 7, -45, 6, 1, 13, 48, 9, 0, 43, 0, 0 }, out.toByteArray());\r |
92 | \r |
93 | //testing for TimeZone Asia/Omsk\r |
94 | //1:48:09 P.M. in Omsk means 6:48:09 A.M. in Greenwich\r |
95 | d = date("Asia/Omsk");\r |
96 | \r |
97 | // testing lohndirekt attribute\r |
98 | out.reset();\r |
99 | AttributeWriter.attributeBytes(new PrinterCurrentTime(d), out);\r |
100 | assertEquals(new byte[] { 49, 0, 20, 112, 114, 105, 110, 116, 101, 114, 45, 99, 117, 114, 114, 101, 110, 116, 45, 116, 105, 109, 101, 0, 11, 7, -45, 6, 1, 6, 48, 9, 0, 43, 0, 0 }, out.toByteArray());\r |
101 | \r |
102 | //testing javax.print attribute\r |
103 | out.reset();\r |
104 | AttributeWriter.attributeBytes(new JobHoldUntil(d), out);\r |
105 | assertEquals(new byte[] { 49, 0, 14, 106, 111, 98, 45, 104, 111, 108, 100, 45, 117, 110, 116, 105, 108, 0, 11, 7, -45, 6, 1, 6, 48, 9, 0, 43, 0, 0 }, out.toByteArray());\r |
106 | }\r |
107 | \r |
108 | /**\r |
109 | * \r |
110 | */\r |
111 | public void testAttributeWriterForEnumSyntax() throws IOException {\r |
112 | // testing lohndirekt attribute\r |
113 | AttributeWriter.attributeBytes(PageRangesSupported.SUPPORTED, out);\r |
114 | assertEquals(\r |
115 | new byte[] { 65, 0, 21, 112, 97, 103, 101, 45, 114, 97, 110, 103, 101, 115, 45, 115, 117, 112, 112, 111, 114, 116, 101, 100, 0, 13, 110, 111, 116, 32, 115, 117, 112, 112, 111, 114, 116, 101, 100 },\r |
116 | out.toByteArray());\r |
117 | \r |
118 | // testing javax.print attribute\r |
119 | out.reset();\r |
120 | AttributeWriter.attributeBytes(JobState.CANCELED, out);\r |
121 | assertEquals(new byte[] { 65, 0, 9, 106, 111, 98, 45, 115, 116, 97, 116, 101, 0, 8, 99, 97, 110, 99, 101, 108, 101, 100 }, out.toByteArray());\r |
122 | \r |
123 | }\r |
124 | /**\r |
125 | * \r |
126 | */\r |
127 | public void testAttributeWriterForIntegerSyntax() throws IOException {\r |
128 | // testing lohndirekt attribute\r |
129 | AttributeWriter.attributeBytes(new JobPageLimit(3), out);\r |
130 | assertEquals(new byte[] { 33, 0, 14, 106, 111, 98, 45, 112, 97, 103, 101, 45, 108, 105, 109, 105, 116, 0, 4, 0, 0, 0, 3 }, out.toByteArray());\r |
131 | \r |
132 | // testing javax.print attribute\r |
133 | out.reset();\r |
134 | AttributeWriter.attributeBytes(new JobPriority(3), out);\r |
135 | assertEquals(new byte[] { 33, 0, 12, 106, 111, 98, 45, 112, 114, 105, 111, 114, 105, 116, 121, 0, 4, 0, 0, 0, 3 }, out.toByteArray());\r |
136 | }\r |
137 | /**\r |
138 | * \r |
139 | */\r |
140 | public void testAttributeWriterForResolutionSyntax() throws IOException {\r |
141 | // testing javax.print attribute\r |
142 | AttributeWriter.attributeBytes(new PrinterResolution(800, 800, PrinterResolution.DPI), out);\r |
143 | assertEquals(\r |
144 | new byte[] { 33, 0, 18, 112, 114, 105, 110, 116, 101, 114, 45, 114, 101, 115, 111, 108, 117, 116, 105, 111, 110, 0, 9, 0, 0, 3, 32, 0, 0, 3, 32, 100},\r |
145 | out.toByteArray());\r |
146 | }\r |
147 | /**\r |
148 | * \r |
149 | */\r |
150 | public void testAttributeWriterForSetOfIntegerSyntax() throws IOException {\r |
151 | // testing javax.print attribute\r |
152 | AttributeWriter.attributeBytes(new CopiesSupported(1,5), out);\r |
153 | assertEquals(\r |
154 | new byte[] { 51, 0, 16, 99, 111, 112, 105, 101, 115, 45, 115, 117, 112, 112, 111, 114, 116, 101, 100, 0, 8, 0, 0, 0, 1, 0, 0, 0, 5 },\r |
155 | out.toByteArray());\r |
156 | }\r |
157 | /**\r |
158 | * \r |
159 | */\r |
160 | public void testAttributeWriterForTextSyntax() throws IOException {\r |
161 | // testing lohndirekt attribute\r |
162 | AttributeWriter.attributeBytes(Charset.ISO_8859_15, out);\r |
163 | assertEquals(\r |
164 | new byte[] { 71, 0, 18, 97, 116, 116, 114, 105, 98, 117, 116, 101, 115, 45, 99, 104, 97, 114, 115, 101, 116, 0, 11, 105, 115, 111, 45, 56, 56, 53, 57, 45, 49, 53 },\r |
165 | out.toByteArray());\r |
166 | \r |
167 | // testing javax.print attribute\r |
168 | out.reset();\r |
169 | AttributeWriter.attributeBytes(new JobName("testJob",Locale.GERMANY), out);\r |
170 | assertEquals(\r |
171 | new byte[] { 66, 0, 8, 106, 111, 98, 45, 110, 97, 109, 101, 0, 7, 116, 101, 115, 116, 74, 111, 98 },\r |
172 | out.toByteArray());\r |
173 | }\r |
174 | /**\r |
175 | * \r |
176 | */\r |
177 | public void testAttributeWriterForURISyntax() throws URISyntaxException, IOException {\r |
178 | URI uri = new URI("http://www.google.de/");\r |
179 | // testing lohndirekt attribute\r |
180 | AttributeWriter.attributeBytes(new JobUri(uri), out);\r |
181 | assertEquals(\r |
182 | new byte[] { 69, 0, 7, 106, 111, 98, 45, 117, 114, 105, 0, 21, 104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 103, 111, 111, 103, 108, 101, 46, 100, 101, 47 },\r |
183 | out.toByteArray());\r |
184 | \r |
185 | // testing javax.print attribute\r |
186 | out.reset();\r |
187 | AttributeWriter.attributeBytes(new PrinterURI(uri), out);\r |
188 | assertEquals(\r |
189 | new byte[] { 69, 0, 11, 112, 114, 105, 110, 116, 101, 114, 45, 117, 114, 105, 0, 21, 104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 103, 111, 111, 103, 108, 101, 46, 100, 101, 47 },\r |
190 | out.toByteArray());\r |
191 | }\r |
192 | \r |
193 | /**\r |
194 | * @param expected\r |
195 | * @param actual\r |
196 | */\r |
197 | private void assertEquals(byte[] expected, byte[] actual) {\r |
198 | assertEquals(new String(expected), new String(actual));\r |
199 | }\r |
200 | \r |
201 | \r |
202 | \r |
203 | }\r |