TestUtilities.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * ------------------
  28. * TestUtilities.java
  29. * ------------------
  30. * (C) Copyright 2007-2013, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes:
  36. * --------
  37. * 08-Jun-2007 : Version 1 (DG);
  38. *
  39. */
  40. package org.jfree.chart;
  41. import java.io.ByteArrayInputStream;
  42. import java.io.ByteArrayOutputStream;
  43. import java.io.IOException;
  44. import java.io.ObjectInput;
  45. import java.io.ObjectInputStream;
  46. import java.io.ObjectOutput;
  47. import java.io.ObjectOutputStream;
  48. import java.util.Collection;
  49. import java.util.Iterator;
  50. /**
  51. * Some utility methods for use by the testing code.
  52. */
  53. public class TestUtilities {
  54. /**
  55. * Returns <code>true</code> if the collections contains any object that
  56. * is an instance of the specified class, and <code>false</code> otherwise.
  57. *
  58. * @param collection the collection.
  59. * @param c the class.
  60. *
  61. * @return A boolean.
  62. */
  63. public static boolean containsInstanceOf(Collection collection, Class c) {
  64. Iterator iterator = collection.iterator();
  65. while (iterator.hasNext()) {
  66. Object obj = iterator.next();
  67. if (obj != null && obj.getClass().equals(c)) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. public static Object serialised(Object original) {
  74. Object result = null;
  75. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  76. ObjectOutput out;
  77. try {
  78. out = new ObjectOutputStream(buffer);
  79. out.writeObject(original);
  80. out.close();
  81. ObjectInput in = new ObjectInputStream(
  82. new ByteArrayInputStream(buffer.toByteArray()));
  83. result = in.readObject();
  84. in.close();
  85. } catch (IOException e) {
  86. throw new RuntimeException(e);
  87. } catch (ClassNotFoundException e) {
  88. throw new RuntimeException(e);
  89. }
  90. return result;
  91. }
  92. }