PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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