PageRenderTime 21ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/netbeans-7.3/editor.lib2/test/unit/src/org/netbeans/modules/editor/lib2/WeakReferenceStableListTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 195 lines | 126 code | 20 blank | 49 comment | 11 complexity | 54e7a4deb276d0fa2b5888c9379ea6cf MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2012 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.editor.lib2;
  43. import java.lang.ref.Reference;
  44. import java.lang.ref.WeakReference;
  45. import java.util.ArrayList;
  46. import java.util.List;
  47. import java.util.logging.Level;
  48. import org.netbeans.junit.Filter;
  49. import org.netbeans.junit.NbTestCase;
  50. /**
  51. *
  52. * @author Miloslav Metelka
  53. */
  54. public class WeakReferenceStableListTest extends NbTestCase {
  55. private List<Object> expected;
  56. private WeakReferenceStableList<Object> tested;
  57. public WeakReferenceStableListTest(String testName) {
  58. super(testName);
  59. List<String> includes = new ArrayList<String>();
  60. // includes.add("testSimple1");
  61. // filterTests(includes);
  62. }
  63. private void filterTests(List<String> includeTestNames) {
  64. List<Filter.IncludeExclude> includeTests = new ArrayList<Filter.IncludeExclude>();
  65. for (String testName : includeTestNames) {
  66. includeTests.add(new Filter.IncludeExclude(testName, ""));
  67. }
  68. Filter filter = new Filter();
  69. filter.setIncludes(includeTests.toArray(new Filter.IncludeExclude[includeTests.size()]));
  70. setFilter(filter);
  71. }
  72. @Override
  73. protected void setUp() throws Exception {
  74. super.setUp(); //To change body of generated methods, choose Tools | Templates.
  75. expected = new ArrayList<Object>();
  76. tested= new WeakReferenceStableList<Object>();
  77. }
  78. @Override
  79. protected Level logLevel() {
  80. return Level.INFO; // null;
  81. // return Level.FINEST;
  82. }
  83. public void testAddNull() throws Exception {
  84. try {
  85. tested.add(null);
  86. fail("Not expected to accept nulls");
  87. } catch (AssertionError ex) {
  88. // Expected
  89. }
  90. }
  91. public void testAddRemove() throws Exception {
  92. Integer i1 = new Integer(1001);
  93. Integer i2 = new Integer(1002);
  94. Integer i3 = new Integer(1003);
  95. Integer i4 = new Integer(1004);
  96. Integer i5 = new Integer(1005);
  97. Integer i6 = new Integer(1006);
  98. add(i1);
  99. check();
  100. add(i2);
  101. check();
  102. add(i3);
  103. check();
  104. add(i4);
  105. check();
  106. assertTrue(expected.remove(i4));
  107. Reference<Integer> refI4 = new WeakReference<Integer>(i4);
  108. i4 = null;
  109. assertGC("i4 not GCable", refI4);
  110. gc();
  111. check();
  112. add(i5);
  113. check();
  114. add(i6);
  115. check();
  116. assertTrue(expected.remove(i3));
  117. i3 = null;
  118. assertTrue(expected.remove(i1));
  119. i1 = null;
  120. gc(); // should remove i3 from tested
  121. check();
  122. assertTrue(expected.remove(i5));
  123. i5 = null;
  124. assertTrue(expected.remove(i6));
  125. i6 = null;
  126. gc(); // should remove i3 from tested
  127. check();
  128. }
  129. public void testMultiGC() throws Exception {
  130. int COUNT = 50;
  131. Integer[] array = new Integer[COUNT];
  132. for (int i = 0; i < COUNT; i++) {
  133. array[i] = new Integer(1000 + i);
  134. tested.add(array[i]);
  135. }
  136. for (int i = 0; i < COUNT; i++) {
  137. if (i % 2 == 0) {
  138. array[i] = null;
  139. }
  140. }
  141. gc();
  142. for (Object o : tested.getList()) {
  143. tested.add(new Integer(2000 + ((Integer)o).intValue())); // Modify during scan
  144. }
  145. }
  146. private void add(Object o) {
  147. expected.add(o);
  148. tested.add(o);
  149. }
  150. private void check() {
  151. List<Object> testedList = tested.getList();
  152. int j = 0;
  153. for (int i = 0; i < expected.size(); i++) {
  154. Object testedValue;
  155. while ((testedValue = testedList.get(j++)) == null) { }
  156. assertSame("Index=" + i, expected.get(i), testedValue);
  157. }
  158. while (j < testedList.size()) {
  159. Object testedValue = testedList.get(j++);
  160. assertNull("Expected null", testedValue);
  161. }
  162. int i = 0;
  163. for (Object testedValue : testedList) { // Iterator skips null values
  164. assertSame("Index=" + i, expected.get(i), testedValue);
  165. i++;
  166. }
  167. assertEquals("Wrong size", expected.size(), i);
  168. }
  169. private static void gc() {
  170. System.gc();
  171. Runtime.getRuntime().runFinalization();
  172. System.gc();
  173. System.gc();
  174. }
  175. }