001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Igor A. Pyankov
019: * @version $Revision: 1.3 $
020: */package javax.print;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.InputStreamReader;
026: import java.io.Reader;
027: import java.net.MalformedURLException;
028: import java.net.URL;
029: import java.util.Locale;
030:
031: import javax.print.attribute.DocAttributeSet;
032: import javax.print.attribute.HashDocAttributeSet;
033: import javax.print.attribute.HashPrintRequestAttributeSet;
034: import javax.print.attribute.PrintRequestAttributeSet;
035: import javax.print.attribute.standard.Copies;
036: import javax.print.attribute.standard.DocumentName;
037: import javax.print.attribute.standard.JobName;
038: import javax.print.attribute.standard.MediaName;
039: import javax.print.attribute.standard.RequestingUserName;
040:
041: import junit.framework.TestCase;
042:
043: public class PrintTest extends TestCase {
044: public static void main(String[] args) {
045: junit.textui.TestRunner.run(PrintTest.class);
046: }
047:
048: public void testPrintSomething() {
049: /*Authenticator.setDefault(new PrintTestAuth());*/
050:
051: boolean testrun = true;
052:
053: System.out
054: .println("============= START PrintTest ================");
055: //if ((System.getProperty("os.name")).toLowerCase().indexOf("linux") >= 0) {
056: if (!testrun) {
057: System.out.println("The test is always pass temporary.");
058: System.out
059: .println("============= END PrintTest ================");
060: return;
061: }
062:
063: String file_txt = "/Resources/readme.txt";
064: String file_gif = "/Resources/printservice.gif";
065: String http_gif = "";
066: String http_ps = "";
067:
068: PrintService[] services = PrintServiceLookup
069: .lookupPrintServices(null, null);
070: TestUtil.checkServices(services);
071:
072: PrintService service = PrintServiceLookup
073: .lookupDefaultPrintService();
074:
075: System.out.println("Default: "
076: + (service == null ? "null" : service.getName()));
077:
078: System.out.println("Print services:");
079: for (int i = 0; i < services.length; i++) {
080: System.out.println("\t" + services[i].getName());
081: }
082:
083: DocAttributeSet dset = new HashDocAttributeSet();
084: dset.add(new DocumentName("print doc name", Locale.US));
085:
086: PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
087: aset.add(new Copies(3));
088: aset.add(MediaName.ISO_A4_WHITE);
089: aset.add(new RequestingUserName("ichebyki", Locale.US));
090:
091: try {
092: PrintService serv = services[0];
093: InputStream stream;
094: DocFlavor flavor;
095: DocPrintJob job;
096: Doc doc;
097: URL url;
098: Reader rdr;
099:
100: try {
101: flavor = DocFlavor.URL.PDF;//TEXT_HTML_HOST;
102: if (serv.isDocFlavorSupported(flavor)) {
103: aset.add(new JobName(flavor.toString(), Locale.US));
104: dset.add(new DocumentName(http_ps, Locale.US));
105: job = serv.createPrintJob();
106: url = new URL(http_ps);
107: doc = new SimpleDoc(url, flavor, dset);
108: System.out.println("\nPrinting on "
109: + job.getPrintService().getName() + "...");
110: job.print(doc, aset);
111: System.out.println("File '" + http_ps
112: + "' was printed as "
113: + flavor.getRepresentationClassName());
114: }
115:
116: flavor = DocFlavor.URL.GIF;
117: if (serv.isDocFlavorSupported(flavor)) {
118: aset.add(new JobName(flavor.toString(), Locale.US));
119: dset.add(new DocumentName(http_gif, Locale.US));
120: job = serv.createPrintJob();
121: url = new URL(http_gif);
122: doc = new SimpleDoc(url, flavor, dset);
123: System.out.println("\nPrinting on "
124: + job.getPrintService().getName() + "...");
125: job.print(doc, aset);
126: System.out.println("File '" + http_gif
127: + "' was printed as "
128: + flavor.getRepresentationClassName());
129: }
130: } catch (PrintException e1) {
131: e1.printStackTrace();
132: }
133:
134: flavor = DocFlavor.READER.TEXT_PLAIN;
135: if (serv.isDocFlavorSupported(flavor)) {
136: aset.add(new JobName(flavor.toString(), Locale.US));
137: dset.add(new DocumentName(file_txt, Locale.US));
138: job = serv.createPrintJob();
139: rdr = new InputStreamReader(getClass()
140: .getResourceAsStream(file_txt));
141: doc = new SimpleDoc(rdr, flavor, dset);
142: System.out.println("Printing on "
143: + job.getPrintService().getName() + "...");
144: job.print(doc, aset);
145: System.out.println("File '" + file_txt
146: + "' was printed as "
147: + flavor.getRepresentationClassName());
148: }
149:
150: flavor = DocFlavor.INPUT_STREAM.GIF;
151: if (serv.isDocFlavorSupported(flavor)) {
152: aset.add(new JobName(flavor.toString(), Locale.US));
153: dset.add(new DocumentName(file_gif, Locale.US));
154: job = serv.createPrintJob();
155: stream = getClass().getResourceAsStream(file_gif);
156: doc = new SimpleDoc(stream, flavor, dset);
157: System.out.println("\nPrinting on "
158: + job.getPrintService().getName() + "...");
159: job.print(doc, aset);
160: System.out.println("File '" + file_gif
161: + "' was printed as "
162: + flavor.getRepresentationClassName());
163: }
164:
165: flavor = DocFlavor.BYTE_ARRAY.JPEG;
166: if (serv.isDocFlavorSupported(flavor)) {
167: aset.add(new JobName(flavor.toString(), Locale.US));
168: dset.add(new DocumentName(file_gif, Locale.US));
169: job = serv.createPrintJob();
170: stream = getClass().getResourceAsStream(file_gif);
171: byte[] gif_buf;
172: byte[] buf = new byte[1024];
173: ByteArrayOutputStream baos = new ByteArrayOutputStream();
174: int count;
175: while ((count = stream.read(buf, 0, 1024)) > 0) {
176: baos.write(buf, 0, count);
177: }
178: stream.close();
179: gif_buf = baos.toByteArray();
180: baos.close();
181: doc = new SimpleDoc(gif_buf, flavor, dset);
182: System.out.println("\nPrinting on "
183: + job.getPrintService().getName() + "...");
184: job.print(doc, aset);
185: System.out.println("File '" + file_gif
186: + "' was printed as "
187: + flavor.getRepresentationClassName());
188: }
189: } catch (MalformedURLException e) {
190: e.printStackTrace();
191: fail("Unexpected exception occured!\n"
192: + e.getLocalizedMessage());
193: } catch (PrintException e) {
194: e.printStackTrace();
195: fail("PrintException occured!\n" + e.getLocalizedMessage());
196: } catch (IOException e) {
197: e.printStackTrace();
198: fail("Unexpected exception occured!\n"
199: + e.getLocalizedMessage());
200: }
201:
202: System.out
203: .println("============= END PrintTest ================");
204: }
205:
206: /*
207: * For authentication
208: * uncomment when swing will support JTextField and etc.
209: */
210: /*
211: class PrintTestAuth extends Authenticator {
212: protected PasswordAuthentication getPasswordAuthentication() {
213: JTextField username = new JTextField();
214: JTextField password = new JPasswordField();
215: JPanel panel = new JPanel(new GridLayout(2, 2));
216: panel.add(new JLabel("Login"));
217: panel.add(username);
218: panel.add(new JLabel("Password"));
219: panel.add(password);
220: int option = JOptionPane.CANCEL_OPTION;
221:
222: option = JOptionPane.showConfirmDialog(null,
223: new Object[] { "Site: " + getRequestingHost(),
224: "Realm: " + getRequestingPrompt(),
225: panel }, "Enter Network Password",
226: JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
227: if (option == JOptionPane.OK_OPTION) {
228: String user = username.getText();
229: char pass[] = password.getText().toCharArray();
230: return new PasswordAuthentication(user, pass);
231: }
232:
233: return null;
234: }
235: }
236: */
237:
238: }
|