PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/enhanced/archive/classlib/java6/modules/print/src/test/api/java/common/javax/print/ValueTests.java

https://bitbucket.org/varialus/harmony
Java | 587 lines | 475 code | 52 blank | 60 comment | 68 complexity | b10f7870041f768ceb8551753ba4bf65 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @author Irina A. Arkhipets
  19. * @version $Revision: 1.3 $
  20. */
  21. /*
  22. * ValueTests.java
  23. *
  24. * JUnit not-interactive Value tests for "Hello, World!" version (javax.print
  25. * package only).
  26. *
  27. */
  28. package javax.print;
  29. import java.io.ByteArrayOutputStream;
  30. import java.io.FileReader;
  31. import java.io.InputStream;
  32. import java.util.Locale;
  33. import java.util.Vector;
  34. import javax.print.attribute.Attribute;
  35. import javax.print.attribute.DocAttribute;
  36. import javax.print.attribute.DocAttributeSet;
  37. import javax.print.attribute.HashDocAttributeSet;
  38. import javax.print.attribute.HashPrintJobAttributeSet;
  39. import javax.print.attribute.HashPrintRequestAttributeSet;
  40. import javax.print.attribute.HashPrintServiceAttributeSet;
  41. import javax.print.attribute.PrintJobAttribute;
  42. import javax.print.attribute.PrintJobAttributeSet;
  43. import javax.print.attribute.PrintRequestAttribute;
  44. import javax.print.attribute.PrintRequestAttributeSet;
  45. import javax.print.attribute.PrintServiceAttribute;
  46. import javax.print.attribute.PrintServiceAttributeSet;
  47. import javax.print.attribute.standard.ColorSupported;
  48. import javax.print.attribute.standard.Copies;
  49. import javax.print.attribute.standard.MediaName;
  50. import javax.print.attribute.standard.OrientationRequested;
  51. import javax.print.attribute.standard.PrintQuality;
  52. import javax.print.attribute.standard.PrinterLocation;
  53. import junit.framework.TestCase;
  54. public class ValueTests extends TestCase {
  55. private static String OS = null;
  56. static {
  57. try {
  58. OS = System.getProperty("os.name");
  59. if (OS.startsWith("Windows")) {
  60. System.loadLibrary("ValueTestsLibrary");
  61. }
  62. } catch (Exception e) {
  63. if (OS == null) {
  64. System.out.println("WARNING: Can not get Operation System name!");
  65. } else {
  66. if (OS.startsWith("Windows")) {
  67. System.out.println(
  68. "WARNING! Can not load ValueTestsLibrary library!");
  69. }
  70. }
  71. System.out.println("Some testcases probably will not be started!");
  72. }
  73. }
  74. public void testDocFlavor() {
  75. startTest("DocFlavor class testing...");
  76. DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
  77. assertEquals(flavor, new DocFlavor.INPUT_STREAM("image/gif"));
  78. assertEquals(flavor.getMediaSubtype(), "gif");
  79. assertEquals(flavor.getMediaType(), "image");
  80. assertEquals(flavor.getMimeType(), "image/gif");
  81. assertEquals(flavor.getRepresentationClassName(), "java.io.InputStream");
  82. assertEquals(flavor.toString(),
  83. "image/gif; class=\"java.io.InputStream\"");
  84. }
  85. public void testSimpleDoc() {
  86. startTest("SimpleDoc class testing...");
  87. DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
  88. InputStream reader =
  89. getClass().getResourceAsStream("/Resources/picture.gif");
  90. if (reader == null) {
  91. fail("/Resources/picture.gif resource is not found!");
  92. }
  93. DocAttributeSet aSet = new HashDocAttributeSet();
  94. Doc doc = new SimpleDoc(reader, flavor, aSet);
  95. assertEquals(doc.getAttributes(), aSet);
  96. aSet.add(OrientationRequested.LANDSCAPE);
  97. aSet.add(MediaName.NA_LETTER_WHITE);
  98. assertEquals(doc.getAttributes(), aSet);
  99. assertEquals(doc.getDocFlavor(), DocFlavor.INPUT_STREAM.GIF);
  100. try {
  101. assertTrue(doc.getPrintData() instanceof java.io.InputStream);
  102. assertNull(doc.getReaderForText());
  103. assertEquals(doc.getStreamForBytes(), reader);
  104. } catch(Exception e) {
  105. e.printStackTrace();
  106. fail("Exception found: "+e);
  107. }
  108. }
  109. public void testDefaultPrintService() {
  110. boolean flg = false;
  111. startTest("PrintService class testing...");
  112. DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
  113. PrintRequestAttributeSet printRequestSet =
  114. new HashPrintRequestAttributeSet();
  115. PrintService service = getPrintService(flavor, printRequestSet);
  116. DocFlavor [] flavors;
  117. String myName = getDefaultPrintService();
  118. System.out.println("OS is " + OS);
  119. if (service == null) {
  120. System.out.println(
  121. "WARNING: Can not get print service which supports INPUT_STREAM.GIF!");
  122. } else {
  123. assertTrue(service.isDocFlavorSupported(flavor));
  124. flavors = service.getSupportedDocFlavors();
  125. for (int i = 0; i < flavors.length; i++) {
  126. if (flavors[i].equals(flavor)) {
  127. flg = true;
  128. break;
  129. }
  130. }
  131. assertTrue(flg);
  132. /* if (myName != null) {
  133. assertEquals(service.getName(), myName);
  134. }
  135. */
  136. }
  137. }
  138. public void testPrintJob() {
  139. startTest("PrintJob class testing...");
  140. DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
  141. PrintRequestAttributeSet printRequestSet =
  142. new HashPrintRequestAttributeSet();
  143. PrintService service = getPrintService(flavor, printRequestSet);
  144. if (service != null) {
  145. DocPrintJob job = service.createPrintJob();
  146. assertEquals(job.getPrintService(), service);
  147. } else {
  148. System.out.println(
  149. "WARNING: Can not get print service which supports INPUT_STREAM.GIF!");
  150. }
  151. }
  152. public void testStreamServiceFactory() {
  153. startTest("StreamPrintServiceFactory class testing...");
  154. boolean flg = false;
  155. DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
  156. StreamPrintServiceFactory [] aFactory = StreamPrintServiceFactory
  157. .lookupStreamPrintServiceFactories(flavor, "application/postscript");
  158. StreamPrintServiceFactory streamFactory=null;
  159. StreamPrintService streamService;
  160. DocFlavor [] flavors;
  161. if ((aFactory == null) || (aFactory.length == 0)) {
  162. fail("Can not find stream print service factory!");
  163. } else {
  164. streamFactory = aFactory[0];
  165. }
  166. streamService = streamFactory.getPrintService(new ByteArrayOutputStream());
  167. assertEquals(streamFactory.getOutputFormat(), "application/postscript");
  168. assertEquals(streamFactory.getPrintService(new ByteArrayOutputStream()),
  169. streamService);
  170. flavors = streamFactory.getSupportedDocFlavors();
  171. for (int i = 0; i < flavors.length; i++) {
  172. if (flavors[i].equals(flavor)) {
  173. flg = true;
  174. break;
  175. }
  176. }
  177. assertTrue(flg);
  178. }
  179. public void testStreamPrintService() {
  180. startTest("StreamPrintService class testing...");
  181. DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
  182. StreamPrintServiceFactory [] aFactory = StreamPrintServiceFactory
  183. .lookupStreamPrintServiceFactories(flavor, "application/postscript");
  184. StreamPrintServiceFactory streamFactory = null;
  185. StreamPrintService streamService;
  186. DocFlavor [] flavors;
  187. boolean flg = false;
  188. if ((aFactory == null) || (aFactory.length == 0)) {
  189. fail("Can not find stream print service factory!");
  190. } else {
  191. streamFactory = aFactory[0];
  192. }
  193. streamService = streamFactory.getPrintService(new ByteArrayOutputStream());
  194. assertEquals(streamService.getOutputFormat(), "application/postscript");
  195. assertTrue(streamService.isDocFlavorSupported(flavor));
  196. flavors = streamService.getSupportedDocFlavors();
  197. for (int i = 0; i < flavors.length; i++) {
  198. if (flavors[i].equals(flavor)) {
  199. flg = true;
  200. break;
  201. }
  202. }
  203. assertTrue(flg);
  204. streamService.dispose();
  205. assertTrue(streamService.isDisposed());
  206. }
  207. public void testStreamServicePrinting() {
  208. startTest("StreamPrintServiceFactory class testing...");
  209. byte [] forChecking = {'%', '!', 'P', 'S', '-', 'A', 'd', 'o', 'b', 'e'};
  210. DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
  211. StreamPrintServiceFactory [] aFactory = StreamPrintServiceFactory
  212. .lookupStreamPrintServiceFactories(flavor, "application/postscript");
  213. StreamPrintServiceFactory streamFactory = null;
  214. StreamPrintService streamService;
  215. DocPrintJob aJob;
  216. InputStream aStream =
  217. getClass().getResourceAsStream("/Resources/picture.gif");
  218. Doc doc;
  219. byte [] arr;
  220. boolean flg = true;
  221. if ((aFactory == null) || (aFactory.length == 0)) {
  222. fail("Can not find stream print service factory!");
  223. } else {
  224. streamFactory = aFactory[0];
  225. }
  226. if (aStream == null) {
  227. fail("/Resources/picture.gif resource is not found!");
  228. }
  229. streamService = streamFactory.getPrintService(new ByteArrayOutputStream());
  230. aJob = streamService.createPrintJob();
  231. doc = new SimpleDoc(aStream, flavor, null);
  232. try {
  233. aJob.print(doc, null);
  234. } catch (Exception e) {
  235. e.printStackTrace();
  236. fail("Can not print PrintJob!");
  237. }
  238. arr = ((ByteArrayOutputStream) (streamService.getOutputStream()))
  239. .toByteArray();
  240. for (int i = 0; i < 10; i++) {
  241. if(arr[i] != forChecking[i]) {
  242. flg = false;
  243. break;
  244. }
  245. }
  246. assertTrue(flg);
  247. }
  248. public void testHashDocAttributeSet() {
  249. startTest("HashDocAttributeSet class testing...");
  250. DocAttributeSet set1 = new HashDocAttributeSet();
  251. DocAttributeSet set2 =
  252. new HashDocAttributeSet(OrientationRequested.LANDSCAPE);
  253. DocAttributeSet set3 = new HashDocAttributeSet(set2);
  254. DocAttribute [] arr = {OrientationRequested.LANDSCAPE,
  255. MediaName.NA_LETTER_WHITE};
  256. DocAttributeSet set4 = new HashDocAttributeSet(arr);
  257. Attribute [] attrArr;
  258. assertTrue(set1.isEmpty());
  259. assertFalse(set2.isEmpty());
  260. assertTrue(set3.equals(set2));
  261. assertFalse(set3.equals(set1));
  262. set3.clear();
  263. assertEquals(set3, set1);
  264. set3.add(OrientationRequested.LANDSCAPE);
  265. set3.add(MediaName.NA_LETTER_WHITE);
  266. assertTrue(set3.containsKey(OrientationRequested.LANDSCAPE.getClass()));
  267. assertFalse(set2.containsKey(MediaName.NA_LETTER_WHITE.getClass()));
  268. assertTrue(set3.containsValue(OrientationRequested.LANDSCAPE));
  269. assertFalse(set3.containsValue(OrientationRequested.PORTRAIT));
  270. assertFalse(set3.containsValue(PrintQuality.DRAFT));
  271. assertEquals(set1.size(), 0);
  272. assertEquals(set2.size(), 1);
  273. assertEquals(set3.size(), 2);
  274. assertTrue(set4.equals(set3));
  275. assertEquals(set3.get(OrientationRequested.PORTRAIT.getClass()),
  276. OrientationRequested.LANDSCAPE);
  277. assertFalse((set3.get(OrientationRequested.PORTRAIT.getClass()))
  278. .equals(OrientationRequested.PORTRAIT));
  279. set1.addAll(set3);
  280. assertEquals(set3, set1);
  281. set1.remove(OrientationRequested.PORTRAIT.getClass());
  282. assertEquals(set1.size(), 1);
  283. attrArr = set1.toArray();
  284. assertEquals(attrArr.length, 1);
  285. assertEquals(attrArr[0], MediaName.NA_LETTER_WHITE);
  286. }
  287. public void testHashPrintJobAttributeSet() {
  288. startTest("HashPrintJobAttributeSet class testing...");
  289. PrintJobAttributeSet set1 = new HashPrintJobAttributeSet();
  290. PrintJobAttributeSet set2 =
  291. new HashPrintJobAttributeSet(OrientationRequested.LANDSCAPE);
  292. PrintJobAttributeSet set3 = new HashPrintJobAttributeSet(set2);
  293. PrintJobAttribute [] arr = {OrientationRequested.LANDSCAPE,
  294. MediaName.NA_LETTER_WHITE};
  295. PrintJobAttributeSet set4 = new HashPrintJobAttributeSet(arr);
  296. Attribute [] attrArr;
  297. assertTrue(set1.isEmpty());
  298. assertFalse(set2.isEmpty());
  299. assertTrue(set3.equals(set2));
  300. assertFalse(set3.equals(set1));
  301. set3.clear();
  302. assertEquals(set3, set1);
  303. set3.add(OrientationRequested.LANDSCAPE);
  304. set3.add(MediaName.NA_LETTER_WHITE);
  305. assertTrue(set3.containsKey(OrientationRequested.LANDSCAPE.getClass()));
  306. assertFalse(set2.containsKey(MediaName.NA_LETTER_WHITE.getClass()));
  307. assertTrue(set3.containsValue(OrientationRequested.LANDSCAPE));
  308. assertFalse(set3.containsValue(OrientationRequested.PORTRAIT));
  309. assertFalse(set3.containsValue(PrintQuality.DRAFT));
  310. assertEquals(set1.size(), 0);
  311. assertEquals(set2.size(), 1);
  312. assertEquals(set3.size(), 2);
  313. assertTrue(set4.equals(set3));
  314. assertEquals(set3.get(OrientationRequested.PORTRAIT.getClass()),
  315. OrientationRequested.LANDSCAPE);
  316. assertFalse((set3.get(OrientationRequested.PORTRAIT.getClass()))
  317. .equals(OrientationRequested.PORTRAIT));
  318. set1.addAll(set3);
  319. assertEquals(set3, set1);
  320. set1.remove(OrientationRequested.PORTRAIT.getClass());
  321. assertEquals(set1.size(), 1);
  322. attrArr = set1.toArray();
  323. assertEquals(attrArr.length, 1);
  324. assertEquals(attrArr[0], MediaName.NA_LETTER_WHITE);
  325. }
  326. public void testHashPrintRequestAttributeSet() {
  327. startTest("HashPrintRequestAttributeSet class testing...");
  328. Copies copies = new Copies(2);
  329. PrintRequestAttributeSet set1 = new HashPrintRequestAttributeSet();
  330. PrintRequestAttributeSet set2 = new HashPrintRequestAttributeSet(copies);
  331. PrintRequestAttributeSet set3 = new HashPrintRequestAttributeSet(set2);
  332. PrintRequestAttribute [] arr = {copies,
  333. MediaName.NA_LETTER_WHITE};
  334. PrintRequestAttributeSet set4 = new HashPrintRequestAttributeSet(arr);
  335. Attribute [] attrArr = set1.toArray();
  336. assertTrue(set1.isEmpty());
  337. assertFalse(set2.isEmpty());
  338. assertTrue(set3.equals(set2));
  339. assertFalse(set3.equals(set1));
  340. set3.clear();
  341. assertEquals(set3, set1);
  342. set3.add(copies);
  343. set3.add(MediaName.NA_LETTER_WHITE);
  344. assertTrue(set3.containsKey(copies.getClass()));
  345. assertFalse(set2.containsKey(MediaName.NA_LETTER_WHITE.getClass()));
  346. assertTrue(set3.containsValue(copies));
  347. assertFalse(set3.containsValue(OrientationRequested.PORTRAIT));
  348. assertFalse(set3.containsValue(PrintQuality.DRAFT));
  349. assertEquals(set1.size(), 0);
  350. assertEquals(set2.size(), 1);
  351. assertEquals(set3.size(), 2);
  352. assertTrue(set4.equals(set3));
  353. assertEquals(set3.get(copies.getClass()), copies);
  354. set1.addAll(set3);
  355. assertEquals(set3, set1);
  356. set1.remove(copies.getClass());
  357. assertEquals(set1.size(), 1);
  358. attrArr = set1.toArray();
  359. assertEquals(attrArr.length, 1);
  360. assertEquals(attrArr[0], MediaName.NA_LETTER_WHITE);
  361. }
  362. public void testHashPrintServiceAttributeSet() {
  363. startTest("HashPrintJobAttributeSet class testing...");
  364. PrintServiceAttributeSet set1 = new HashPrintServiceAttributeSet();
  365. PrintServiceAttributeSet set2 = new HashPrintServiceAttributeSet(
  366. ColorSupported.SUPPORTED);
  367. PrintServiceAttributeSet set3 = new HashPrintServiceAttributeSet(set2);
  368. PrinterLocation location = new PrinterLocation("room 222", Locale.ENGLISH);
  369. PrintServiceAttribute [] arr = { location,
  370. ColorSupported.SUPPORTED };
  371. PrintServiceAttributeSet set4 = new HashPrintServiceAttributeSet(arr);
  372. assertTrue(set1.isEmpty());
  373. assertFalse(set2.isEmpty());
  374. assertTrue(set3.equals(set2));
  375. assertFalse(set3.equals(set1));
  376. set3.clear();
  377. assertEquals(set3, set1);
  378. set3.add(ColorSupported.SUPPORTED);
  379. set3.add(location);
  380. assertTrue(set3.containsKey(location.getClass()));
  381. assertFalse(set2.containsKey(MediaName.NA_LETTER_WHITE.getClass()));
  382. assertTrue(set4.equals(set3));
  383. assertEquals(set3.get(location.getClass()), location);
  384. set1.addAll(set3);
  385. assertEquals(set3, set1);
  386. set1.remove(location.getClass());
  387. assertEquals(set1.size(), 1);
  388. }
  389. // ---------------------------------------------------------------------------------
  390. /*
  391. * This function search a PrintService which supports given
  392. * DocFlavor and PrintRequestAttributeSet.
  393. * If default printer supports them, this function returns default printer.
  394. * Overwise it returna first printer from all print services list.
  395. */
  396. private PrintService getPrintService(DocFlavor aFlavor,
  397. PrintRequestAttributeSet aSet)
  398. {
  399. PrintService [] services =
  400. PrintServiceLookup.lookupPrintServices(aFlavor, aSet);
  401. PrintService defaultService =
  402. PrintServiceLookup.lookupDefaultPrintService();
  403. if (services.length <= 0) {
  404. System.out.println("Can not find default print service!");
  405. return null;
  406. }
  407. for (int i = 0; i < services.length; i++) {
  408. if (services[i].equals(defaultService)) {
  409. return services[i];
  410. }
  411. }
  412. System.out.println(
  413. "System Default PrintService does not support given attributes!");
  414. return services[0];
  415. }
  416. /*
  417. * This functions returns a list of the available printers
  418. */
  419. private String[] getPrintServices() {
  420. return (OS.startsWith("Windows")
  421. ? getWindowsPrintServices()
  422. : getLinuxPrintServices());
  423. }
  424. /*
  425. * This native function returms Windows printers list
  426. */
  427. private native String [] getWindowsPrintServices();
  428. /*
  429. * This function returns Linus printers list.
  430. * On Linux all CUPS printers are listed on /etc/printcap file
  431. */
  432. private String [] getLinuxPrintServices() {
  433. Vector services = new Vector();
  434. String str = "";
  435. short state = 0;
  436. char c;
  437. String [] res;
  438. try {
  439. boolean flg = true;
  440. FileReader reader = new FileReader("/etc/printcap");
  441. while (flg) {
  442. c = (char) reader.read();
  443. if (( c <= 0) || (c == 65535)) {
  444. if (state == 2) {
  445. services.add(str);
  446. }
  447. flg = false;
  448. }
  449. switch(state) {
  450. case 0:
  451. if (c == '#') {
  452. state = 1;
  453. } else if (Character.isLetter(c)) {
  454. str = str + c;
  455. state = 2;
  456. }
  457. break;
  458. case 1:
  459. if (c == '\n') {
  460. state = 0;
  461. str = "";
  462. }
  463. break;
  464. case 2:
  465. if ((c == '|') || (c == '\n') || (c == ' ') || (c == 9)) {
  466. services.add(str);
  467. str = "";
  468. state = (c == '\n') ? (short) 0 : (short) 1;
  469. } else {
  470. str = str + c;
  471. }
  472. break;
  473. }
  474. }
  475. } catch (Exception e) {
  476. e.printStackTrace();
  477. return null;
  478. }
  479. res = new String [services.size()];
  480. for (int i = 0; i < services.size(); i++) {
  481. res[i] = (String)services.get(i);
  482. }
  483. return res;
  484. }
  485. /*
  486. * This function returns system default printer
  487. */
  488. private String getDefaultPrintService() {
  489. return OS.startsWith("Windows")
  490. ? getDefaultWindowsPrintService()
  491. : getDefaultLinuxPrintService();
  492. }
  493. /*
  494. * This function returns Windows default printer name
  495. */
  496. private native String getDefaultWindowsPrintService();
  497. /*
  498. * This function returns Linux default printer name
  499. * On Linux lpstat -d returns string like the following:
  500. * "system default destination: test"
  501. */
  502. private String getDefaultLinuxPrintService() {
  503. try {
  504. Process process = Runtime.getRuntime().exec(
  505. "lpstat -d | grep \"system default destination\"");
  506. InputStream iStream;
  507. byte [] res;
  508. String resStr;
  509. process.waitFor();
  510. if (process.exitValue() != 0) {
  511. System.out.println("Can not exec \"lpstat -d\"");
  512. return null;
  513. }
  514. iStream = process.getInputStream();
  515. res = new byte [iStream.available()];
  516. iStream.read(res);
  517. resStr = new String(res);
  518. if (!resStr.startsWith("system default destination: ")) {
  519. System.out.println(
  520. "WARNING: Can not recognize \"lpstat -d\" output:");
  521. System.out.println(resStr + "\n");
  522. return null;
  523. }
  524. // Last symbol in resStr is "\n"!
  525. return resStr.substring(28, resStr.length()-1);
  526. } catch (Exception e) {
  527. e.printStackTrace();
  528. return null;
  529. }
  530. }
  531. private void startTest(String s) {
  532. System.out.println("----------------------------------------");
  533. System.out.println(s);
  534. }
  535. }