openide.util/test/unit/src/org/openide/util/EnumerationsTest.java
changeset 833 0e00857c5827
parent 829 3b2ed3e1f01b
child 876 efc7a0e42c5f
     1.1 --- a/openide.util/test/unit/src/org/openide/util/EnumerationsTest.java	Fri Sep 25 14:02:54 2009 +0200
     1.2 +++ b/openide.util/test/unit/src/org/openide/util/EnumerationsTest.java	Fri Oct 09 15:11:13 2009 -0400
     1.3 @@ -41,19 +41,17 @@
     1.4  
     1.5  package org.openide.util;
     1.6  
     1.7 -import java.lang.ref.WeakReference;
     1.8  import java.util.ArrayList;
     1.9  import java.util.Arrays;
    1.10  import java.util.Collection;
    1.11  import java.util.Collections;
    1.12  import java.util.Enumeration;
    1.13  import java.util.Iterator;
    1.14 +import java.util.List;
    1.15  import java.util.Map;
    1.16  import java.util.NoSuchElementException;
    1.17  import java.util.Set;
    1.18 -import junit.textui.TestRunner;
    1.19  import org.netbeans.junit.NbTestCase;
    1.20 -import org.netbeans.junit.NbTestSuite;
    1.21  
    1.22  /** This is the base test for new and old enumerations. It contains
    1.23   * factory methods for various kinds of enumerations and set of tests
    1.24 @@ -72,54 +70,50 @@
    1.25      // Factory methods
    1.26      //
    1.27      
    1.28 -    protected Enumeration singleton(Object obj) {
    1.29 +    protected <T> Enumeration<T> singleton(T obj) {
    1.30          return Enumerations.singleton(obj);
    1.31      }
    1.32 -    protected Enumeration concat(Enumeration en1, Enumeration en2) {
    1.33 +    protected <T> Enumeration<T> concat(Enumeration<T> en1, Enumeration<T> en2) {
    1.34          return Enumerations.concat(en1, en2);
    1.35      }
    1.36 -    protected Enumeration concat(Enumeration enumOfEnums) {
    1.37 +    protected <T> Enumeration<T> concat(Enumeration<Enumeration<T>> enumOfEnums) {
    1.38          return Enumerations.concat(enumOfEnums);
    1.39      }
    1.40 -    protected Enumeration removeDuplicates(Enumeration en) {
    1.41 +    protected <T> Enumeration<T> removeDuplicates(Enumeration<T> en) {
    1.42          return Enumerations.removeDuplicates(en);
    1.43      }
    1.44 -    protected Enumeration empty() {
    1.45 +    protected <T> Enumeration<T> empty() {
    1.46          return Enumerations.empty();
    1.47      }
    1.48 -    protected Enumeration array(Object[] arr) {
    1.49 +    protected <T> Enumeration<T> array(T[] arr) {
    1.50          return Enumerations.array(arr);
    1.51      }
    1.52 -    protected Enumeration convert(Enumeration en, final Map map) {
    1.53 -        class P implements Enumerations.Processor {
    1.54 -            public Object process(Object obj, Collection nothing) {
    1.55 +    protected <T,R> Enumeration<R> convert(Enumeration<T> en, final Map<T,R> map) {
    1.56 +        class P implements Enumerations.Processor<T,R> {
    1.57 +            public R process(T obj, Collection<T> nothing) {
    1.58                  return map.get(obj);
    1.59              }
    1.60          }
    1.61 -        
    1.62 -        
    1.63          return Enumerations.convert(en, new P());
    1.64      }
    1.65 -    protected Enumeration removeNulls(Enumeration en) {
    1.66 +    protected <T> Enumeration<T> removeNulls(Enumeration<T> en) {
    1.67          return Enumerations.removeNulls(en);
    1.68      }
    1.69 -    protected Enumeration filter(Enumeration en, final Set filter) {
    1.70 -        class P implements Enumerations.Processor {
    1.71 -            public Object process(Object obj, Collection nothing) {
    1.72 +    protected <T> Enumeration<T> filter(Enumeration<T> en, final Set<T> filter) {
    1.73 +        class P implements Enumerations.Processor<T,T> {
    1.74 +            public T process(T obj, Collection<T> nothing) {
    1.75                  return filter.contains(obj) ? obj : null;
    1.76              }
    1.77          }
    1.78 -        
    1.79          return Enumerations.filter(en, new P());
    1.80      }
    1.81      
    1.82 -    protected Enumeration filter(Enumeration en, final QueueProcess filter) {
    1.83 -        class P implements Enumerations.Processor {
    1.84 -            public Object process(Object obj, Collection nothing) {
    1.85 +    protected <T,R> Enumeration<R> filter(Enumeration<T> en, final QueueProcess<T,R> filter) {
    1.86 +        class P implements Enumerations.Processor<T,R> {
    1.87 +            public R process(T obj, Collection<T> nothing) {
    1.88                  return filter.process(obj, nothing);
    1.89              }
    1.90          }
    1.91 -        
    1.92          return Enumerations.filter(en, new P());
    1.93      }
    1.94      
    1.95 @@ -127,9 +121,9 @@
    1.96       * @param filter the set.contains (...) is called before each object is produced
    1.97       * @return Enumeration
    1.98       */
    1.99 -    protected Enumeration queue(Collection initContent, final QueueProcess process) {
   1.100 -        class C implements Enumerations.Processor {
   1.101 -            public Object process(Object object, Collection toAdd) {
   1.102 +    protected <T,R> Enumeration<R> queue(Collection<T> initContent, final QueueProcess<T,R> process) {
   1.103 +        class C implements Enumerations.Processor<T,R> {
   1.104 +            public R process(T object, Collection<T> toAdd) {
   1.105                  return process.process(object, toAdd);
   1.106              }
   1.107          }
   1.108 @@ -141,8 +135,8 @@
   1.109      
   1.110      /** Processor interface.
   1.111       */
   1.112 -    public static interface QueueProcess {
   1.113 -        public Object process(Object object, Collection toAdd);
   1.114 +    public static interface QueueProcess<T,R> {
   1.115 +        public R process(T object, Collection<T> toAdd);
   1.116      }
   1.117      
   1.118      //
   1.119 @@ -174,10 +168,10 @@
   1.120      }
   1.121      
   1.122      public void testConcatTwoAndArray() {
   1.123 -        Object[] one = { new Integer(1), new Integer(2), new Integer(3) };
   1.124 +        Object[] one = { 1, 2, 3 };
   1.125          Object[] two = { "1", "2", "3" };
   1.126          
   1.127 -        ArrayList list = new ArrayList(Arrays.asList(one));
   1.128 +        List<Object> list = new ArrayList<Object>(Arrays.asList(one));
   1.129          list.addAll(Arrays.asList(two));
   1.130          
   1.131          assertEnums(
   1.132 @@ -187,15 +181,16 @@
   1.133      }
   1.134      
   1.135      public void testConcatTwoAndArrayAndTakeOnlyStrings() {
   1.136 -        Object[] one = { new Integer(1), new Integer(2), new Integer(3) };
   1.137 +        Object[] one = { 1, 2, 3 };
   1.138          Object[] two = { "1", "2", "3" };
   1.139 -        Object[] three = { new Long(1) };
   1.140 +        Object[] three = { 1L };
   1.141          Object[] four = { "Kuk" };
   1.142          
   1.143 -        ArrayList list = new ArrayList(Arrays.asList(two));
   1.144 +        List<Object> list = new ArrayList<Object>(Arrays.asList(two));
   1.145          list.addAll(Arrays.asList(four));
   1.146          
   1.147 -        Enumeration[] alls = {
   1.148 +        @SuppressWarnings("unchecked")
   1.149 +        Enumeration<Object>[] alls = (Enumeration<Object>[]) new Enumeration<?>[] {
   1.150              array(one), array(two), array(three), array(four)
   1.151          };
   1.152          
   1.153 @@ -206,18 +201,19 @@
   1.154      }
   1.155      
   1.156      public void testRemoveDuplicates() {
   1.157 -        Object[] one = { new Integer(1), new Integer(2), new Integer(3) };
   1.158 +        Object[] one = { 1, 2, 3 };
   1.159          Object[] two = { "1", "2", "3" };
   1.160 -        Object[] three = { new Integer(1) };
   1.161 +        Object[] three = { 1 };
   1.162          Object[] four = { "2", "3", "4" };
   1.163          
   1.164 -        Enumeration[] alls = {
   1.165 +        @SuppressWarnings("unchecked")
   1.166 +        Enumeration<Object>[] alls = (Enumeration<Object>[]) new Enumeration<?>[] {
   1.167              array(one), array(two), array(three), array(four)
   1.168          };
   1.169          
   1.170          assertEnums(
   1.171                  removeDuplicates(concat(array(alls))),
   1.172 -                array(new Object[] { new Integer(1), new Integer(2), new Integer(3), "1", "2", "3", "4" })
   1.173 +                array(new Object[] { 1, 2, 3, "1", "2", "3", "4" })
   1.174                  );
   1.175          
   1.176      }
   1.177 @@ -225,7 +221,7 @@
   1.178      public void testRemoveDuplicatesAndGCWorks() {
   1.179          
   1.180          /*** Return { i1, "", "", "", i2 } */
   1.181 -        class WeakEnum implements Enumeration {
   1.182 +        class WeakEnum implements Enumeration<Object> {
   1.183              public Object i1 = new Integer(1);
   1.184              public Object i2 = new Integer(1);
   1.185              
   1.186 @@ -249,15 +245,19 @@
   1.187          
   1.188          assertTrue("Has some elements", en.hasMoreElements());
   1.189          assertEquals("And the first one is get", weak.i1, en.nextElement());
   1.190 -        
   1.191 +
   1.192 +        /*
   1.193          try {
   1.194 -            WeakReference ref = new WeakReference(weak.i1);
   1.195 +            Reference<?> ref = new WeakReference<Object>(weak.i1);
   1.196 +         */
   1.197              weak.i1 = null;
   1.198 +        /*
   1.199              assertGC("Try hard to GC the first integer", ref);
   1.200              // does not matter whether it GCs or not
   1.201          } catch (Throwable tw) {
   1.202              // not GCed, but does not matter
   1.203          }
   1.204 +         */
   1.205          assertTrue("Next object will be string", en.hasMoreElements());
   1.206          assertEquals("is empty string", "", en.nextElement());
   1.207          
   1.208 @@ -266,12 +266,11 @@
   1.209      }
   1.210      
   1.211      public void testQueueEnum() {
   1.212 -        class Pr implements QueueProcess {
   1.213 -            public Object process(Object o, Collection c) {
   1.214 -                Integer i = (Integer)o;
   1.215 -                int plus = i.intValue() + 1;
   1.216 +        class Pr implements QueueProcess<Integer,Integer> {
   1.217 +            public Integer process(Integer i, Collection<Integer> c) {
   1.218 +                int plus = i + 1;
   1.219                  if (plus < 10) {
   1.220 -                    c.add(new Integer(plus));
   1.221 +                    c.add(plus);
   1.222                  }
   1.223                  return i;
   1.224              }
   1.225 @@ -279,7 +278,7 @@
   1.226          Pr p = new Pr();
   1.227          
   1.228          Enumeration en = queue(
   1.229 -                Collections.nCopies(1, new Integer(0)), p
   1.230 +                Collections.nCopies(1, 0), p
   1.231                  );
   1.232          
   1.233          for (int i = 0; i < 10; i++) {
   1.234 @@ -291,17 +290,15 @@
   1.235      }
   1.236      
   1.237      public void testFilteringAlsoDoesConvertions() throws Exception {
   1.238 -        class Pr implements QueueProcess {
   1.239 -            public Object process(Object o, Collection ignore) {
   1.240 -                Integer i = (Integer)o;
   1.241 -                int plus = i.intValue() + 1;
   1.242 -                return new Integer(plus);
   1.243 +        class Pr implements QueueProcess<Integer,Integer> {
   1.244 +            public Integer process(Integer i, Collection<Integer> ignore) {
   1.245 +                return i + 1;
   1.246              }
   1.247          }
   1.248          Pr p = new Pr();
   1.249          
   1.250 -        Enumeration onetwo = array(new Object[] { new Integer(1), new Integer(2) });
   1.251 -        Enumeration twothree = array(new Object[] { new Integer(2), new Integer(3) });
   1.252 +        Enumeration<Integer> onetwo = array(new Integer[] { 1, 2 });
   1.253 +        Enumeration<Integer> twothree = array(new Integer[] { 2, 3 });
   1.254          
   1.255          assertEnums(
   1.256                  filter(onetwo, p), twothree
   1.257 @@ -309,11 +306,11 @@
   1.258      }
   1.259      
   1.260      
   1.261 -    private static void assertEnums(Enumeration e1, Enumeration e2) {
   1.262 +    private static <T> void assertEnums(Enumeration<T> e1, Enumeration<T> e2) {
   1.263          int indx = 0;
   1.264          while (e1.hasMoreElements() && e2.hasMoreElements()) {
   1.265 -            Object i1 = e1.nextElement();
   1.266 -            Object i2 = e2.nextElement();
   1.267 +            T i1 = e1.nextElement();
   1.268 +            T i2 = e2.nextElement();
   1.269              assertEquals(indx++ + "th: ", i1, i2);
   1.270          }
   1.271          
   1.272 @@ -340,7 +337,7 @@
   1.273      }
   1.274      
   1.275      public void testConvertIntegersToStringRemoveNulls() {
   1.276 -        Object[] garbage = { new Integer(1), "kuk", "hle", new Integer(5) };
   1.277 +        Object[] garbage = { 1, "kuk", "hle", 5 };
   1.278          
   1.279          assertEnums(
   1.280                  removeNulls(convert(array(garbage), new MapIntegers())),
   1.281 @@ -351,8 +348,8 @@
   1.282      public void testQueueEnumerationCanReturnNulls() {
   1.283          Object[] nuls = { null, "NULL" };
   1.284          
   1.285 -        class P implements QueueProcess {
   1.286 -            public Object process(Object toRet, Collection toAdd) {
   1.287 +        class P implements QueueProcess<Object,Object> {
   1.288 +            public Object process(Object toRet, Collection<Object> toAdd) {
   1.289                  if (toRet == null) return null;
   1.290                  
   1.291                  if ("NULL".equals(toRet)) {
   1.292 @@ -372,7 +369,7 @@
   1.293      
   1.294      /** Filters only strings.
   1.295       */
   1.296 -    private static final class OnlyStrings implements Set {
   1.297 +    private static final class OnlyStrings implements Set<Object> {
   1.298          public boolean add(Object o) {
   1.299              fail("Should not be every called");
   1.300              return false;
   1.301 @@ -401,7 +398,7 @@
   1.302              return false;
   1.303          }
   1.304          
   1.305 -        public Iterator iterator() {
   1.306 +        public Iterator<Object> iterator() {
   1.307              fail("Should not be every called");
   1.308              return null;
   1.309          }
   1.310 @@ -431,7 +428,7 @@
   1.311              return null;
   1.312          }
   1.313          
   1.314 -        public Object[] toArray(Object[] a) {
   1.315 +        public <T> T[] toArray(T[] a) {
   1.316              fail("Should not be every called");
   1.317              return null;
   1.318          }
   1.319 @@ -439,7 +436,7 @@
   1.320      
   1.321      /** Filters only strings.
   1.322       */
   1.323 -    private static final class MapIntegers implements Map {
   1.324 +    private static final class MapIntegers implements Map<Object,Object> {
   1.325          public boolean containsKey(Object key) {
   1.326              fail("Should not be every called");
   1.327              return false;
   1.328 @@ -450,7 +447,7 @@
   1.329              return false;
   1.330          }
   1.331          
   1.332 -        public Set entrySet() {
   1.333 +        public Set<Map.Entry<Object,Object>> entrySet() {
   1.334              fail("Should not be every called");
   1.335              return null;
   1.336          }
   1.337 @@ -462,7 +459,7 @@
   1.338              return null;
   1.339          }
   1.340          
   1.341 -        public Set keySet() {
   1.342 +        public Set<Object> keySet() {
   1.343              fail("Should not be every called");
   1.344              return null;
   1.345          }
   1.346 @@ -476,7 +473,7 @@
   1.347              fail("Should not be every called");
   1.348          }
   1.349          
   1.350 -        public Collection values() {
   1.351 +        public Collection<Object> values() {
   1.352              fail("Should not be every called");
   1.353              return null;
   1.354          }