You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CompareUtilTestCase.java 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /* $Id$ */
  18. package org.apache.fop.util;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.Vector;
  22. import java.util.concurrent.ExecutorService;
  23. import java.util.concurrent.Executors;
  24. import java.util.concurrent.Future;
  25. import java.util.concurrent.atomic.AtomicBoolean;
  26. import org.junit.Test;
  27. import static org.junit.Assert.assertFalse;
  28. import static org.junit.Assert.assertNull;
  29. public class CompareUtilTestCase {
  30. @Test
  31. public void testEqual() {
  32. int numberOfParallelThreads = Runtime.getRuntime().availableProcessors();
  33. long numberOfEqualOperations = 100;
  34. double probabilityOf12 = 0.5;
  35. ExecutorService executor = Executors.newFixedThreadPool(numberOfParallelThreads);
  36. final Vector object1 = new Vector();
  37. object1.add(new Object());
  38. object1.add(new Object());
  39. object1.add(new Object());
  40. object1.add(new Object());
  41. object1.add(new Object());
  42. final Vector object2 = new Vector();
  43. object2.add(new Object());
  44. object2.add(new Object());
  45. object2.add(new Object());
  46. object2.add(new Object());
  47. object2.add(new Object());
  48. object2.add(new Object());
  49. object2.add(new Object());
  50. object2.add(new Object());
  51. object2.add(new Object());
  52. object2.add(new Object());
  53. final boolean areEqual = object1.equals(object2);
  54. final AtomicBoolean wrongResult = new AtomicBoolean(false);
  55. Runnable equal12 = new Runnable() {
  56. public void run() {
  57. if (areEqual != CompareUtil.equal(object1, object2)) {
  58. wrongResult.set(true);
  59. }
  60. }
  61. };
  62. Runnable equal21 = new Runnable() {
  63. public void run() {
  64. if (areEqual != CompareUtil.equal(object2, object1)) {
  65. wrongResult.set(true);
  66. }
  67. }
  68. };
  69. List<Future<?>> futures = new ArrayList<Future<?>>();
  70. for (int i = 1; i <= numberOfEqualOperations; i++) {
  71. Runnable randomTask = Math.random() < probabilityOf12 ? equal12 : equal21;
  72. futures.add(executor.submit(randomTask));
  73. }
  74. Exception exception = null;
  75. try {
  76. for (Future<?> future : futures) {
  77. future.get();
  78. }
  79. } catch (Exception e) {
  80. exception = e;
  81. }
  82. assertNull(exception);
  83. assertFalse(wrongResult.get());
  84. }
  85. }