changeset 960:cfe6aca11b12

Refactored check of heap space. Now recognize g/G and is based on regex
author Jiri Vanek <jvanek@redhat.com>
date Mon, 31 Mar 2014 16:37:23 +0200
parents 8417559b6a12
children da9335e11493
files ChangeLog netx/net/sourceforge/jnlp/JREDesc.java tests/netx/unit/net/sourceforge/jnlp/JREDescTest.java
diffstat 3 files changed, 174 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Mar 27 11:16:58 2014 -0400
+++ b/ChangeLog	Mon Mar 31 16:37:23 2014 +0200
@@ -1,3 +1,13 @@
+2014-03-31  Jiri Vanek  <jvanek@redhat.com>
+
+	Refactored check of heap space. Now recognize g/G and is based on regex
+	* netx/net/sourceforge/jnlp/JREDesc.java: Added (heapPattern) constant.
+	(checkHeapSize) now returns trimmed string and its logic is matching the
+	heapPattern instead compelx structure. (init) set result of checkHeapSize
+	as initialHeapSize and maximumHeapSize.
+	* tests/netx/unit/net/sourceforge/jnlp/JREDescTest.java: tests for (checkHeapSize)
+	and (init) of JREDesc.
+	
 2014-03-27  Andrew Azores  <aazores@redhat.com>
 
 	Fix NPE when trying to open a new file, with changes made, and wanting to
--- a/netx/net/sourceforge/jnlp/JREDesc.java	Thu Mar 27 11:16:58 2014 -0400
+++ b/netx/net/sourceforge/jnlp/JREDesc.java	Mon Mar 31 16:37:23 2014 +0200
@@ -20,6 +20,8 @@
 
 import java.net.*;
 import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * The J2SE/Java element.
@@ -28,6 +30,8 @@
  * @version $Revision: 1.5 $
  */
 public class JREDesc {
+    
+    private static final Pattern heapPattern= Pattern.compile("\\d+[kmg]?");
 
     /** the platform version or the product version if location is not null */
     final private Version version;
@@ -63,10 +67,8 @@
         this.version = version;
         this.location = location;
         this.vmArgs = vmArgs;
-        checkHeapSize(initialHeapSize);
-        this.initialHeapSize = initialHeapSize;
-        checkHeapSize(maximumHeapSize);
-        this.maximumHeapSize = maximumHeapSize;
+        this.initialHeapSize = checkHeapSize(initialHeapSize);
+        this.maximumHeapSize = checkHeapSize(maximumHeapSize);
         this.resources = resources;
     }
 
@@ -126,37 +128,24 @@
 
     /**
      * Check for valid heap size string
+     * @return trimed heapSize if correct
      * @throws ParseException if heapSize is invalid
      */
-    static private void checkHeapSize(String heapSize) throws ParseException {
+    static String checkHeapSize(String heapSize) throws ParseException {
         // need to implement for completeness even though not used in netx
         if (heapSize == null) {
-            return;
-        }
-
-        boolean lastCharacterIsDigit = true;
-        // the last character must be 0-9 or k/K/m/M
-        char lastChar = Character.toLowerCase(heapSize.charAt(heapSize.length() - 1));
-        if ((lastChar < '0' || lastChar > '9')) {
-            lastCharacterIsDigit = false;
-            if (lastChar != 'k' && lastChar != 'm') {
-                throw new ParseException(R("PBadHeapSize", heapSize));
-            }
+            return null;
         }
-
-        int indexOfLastDigit = heapSize.length() - 1;
-        if (!lastCharacterIsDigit) {
-            indexOfLastDigit = indexOfLastDigit - 1;
+        heapSize = heapSize.trim();
+        // the last character must be 0-9 or k/K/m/M/g/G
+        //0 or 0k/m/g is also accepted value
+        String heapSizeLower = heapSize.toLowerCase();
+        Matcher heapMatcher = heapPattern.matcher(heapSizeLower);
+        if (!heapMatcher.matches()) {
+            throw new ParseException(R("PBadHeapSize", heapSize));
         }
-
-        String size = heapSize.substring(0, indexOfLastDigit);
-        try {
-            // check that the number is a number!
-            Integer.valueOf(size);
-        } catch (NumberFormatException numberFormat) {
-            throw new ParseException(R("PBadHeapSize", heapSize), numberFormat);
-        }
-
+        return heapSize;
+        
     }
 
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/netx/unit/net/sourceforge/jnlp/JREDescTest.java	Mon Mar 31 16:37:23 2014 +0200
@@ -0,0 +1,146 @@
+/* 
+ Copyright (C) 2014 Red Hat, Inc.
+
+ This file is part of IcedTea.
+
+ IcedTea is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ IcedTea is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with IcedTea; see the file COPYING.  If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301 USA.
+
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library.  Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library with independent modules to produce an
+ executable, regardless of the license terms of these independent
+ modules, and to copy and distribute the resulting executable under
+ terms of your choice, provided that you also meet, for each linked
+ independent module, the terms and conditions of the license of that
+ module.  An independent module is a module which is not derived from
+ or based on this library.  If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so.  If you do not wish to do so, delete this
+ exception statement from your version. */
+package net.sourceforge.jnlp;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class JREDescTest {
+
+    @Test
+    public void testNulls() throws ParseException {
+        JREDesc a = new JREDesc(null, null, null, null, null, null);
+    }
+
+    @Test
+    public void testInitialHeapSize() throws ParseException {
+        JREDesc a = new JREDesc(null, null, null, "1", null, null);
+        a = new JREDesc(null, null, null, "99999999", null, null);
+        a = new JREDesc(null, null, null, "1k", null, null);
+        a = new JREDesc(null, null, null, "1000k", null, null);
+        a = new JREDesc(null, null, null, "1K", null, null);
+        a = new JREDesc(null, null, null, "1000K", null, null);
+        a = new JREDesc(null, null, null, "1m", null, null);
+        a = new JREDesc(null, null, null, "1m", null, null);
+        a = new JREDesc(null, null, null, "1M", null, null);
+        a = new JREDesc(null, null, null, "1g", null, null);
+        a = new JREDesc(null, null, null, "1G", null, null);
+        a = new JREDesc(null, null, null, "10000G", null, null);
+    }
+
+    @Test
+    public void testMaximumHeapSize() throws ParseException {
+        JREDesc a = new JREDesc(null, null, null, null, "1", null);
+        a = new JREDesc(null, null, null, null, "99999999", null);
+        a = new JREDesc(null, null, null, null, "1k", null);
+        a = new JREDesc(null, null, null, null, "1000k", null);
+        a = new JREDesc(null, null, null, null, "1K", null);
+        a = new JREDesc(null, null, null, null, "1000K", null);
+        a = new JREDesc(null, null, null, null, "1m", null);
+        a = new JREDesc(null, null, null, null, "1m", null);
+        a = new JREDesc(null, null, null, null, "1M", null);
+        a = new JREDesc(null, null, null, null, "1g", null);
+        a = new JREDesc(null, null, null, null, "1G", null);
+        a = new JREDesc(null, null, null, null, "10000G", null);
+    }
+
+    @Test(expected = ParseException.class)
+    public void testInitialHeapSizeBad() throws ParseException {
+        JREDesc a = new JREDesc(null, null, null, "blah", null, null);
+
+    }
+
+    @Test(expected = ParseException.class)
+    public void testMaximumHeapSizeBad() throws ParseException {
+        JREDesc a = new JREDesc(null, null, null, null, "blah", null);
+
+    }
+
+    @Test
+    public void checkHeapSize() throws ParseException {
+        String s = JREDesc.checkHeapSize(null);
+        Assert.assertEquals(null, s);
+        s = JREDesc.checkHeapSize("0");
+        Assert.assertEquals("0", s);
+        s = JREDesc.checkHeapSize(" 0k");
+        Assert.assertEquals("0k", s);
+        s = JREDesc.checkHeapSize("1 ");
+        Assert.assertEquals("1", s);
+        s = JREDesc.checkHeapSize("10");
+        Assert.assertEquals("10", s);
+        s = JREDesc.checkHeapSize(" 1k");
+        Assert.assertEquals("1k", s);
+        s = JREDesc.checkHeapSize("10m ");
+        Assert.assertEquals("10m", s);
+
+        s = JREDesc.checkHeapSize("0");
+        Assert.assertEquals("0", s);
+        s = JREDesc.checkHeapSize(" 0K");
+        Assert.assertEquals("0K", s);
+        s = JREDesc.checkHeapSize("1 ");
+        Assert.assertEquals("1", s);
+        s = JREDesc.checkHeapSize("10");
+        Assert.assertEquals("10", s);
+        s = JREDesc.checkHeapSize(" 1M");
+        Assert.assertEquals("1M", s);
+        s = JREDesc.checkHeapSize("10G ");
+        Assert.assertEquals("10G", s);
+        s = JREDesc.checkHeapSize("99K");
+        Assert.assertEquals("99K", s);
+
+    }
+
+    @Test(expected = ParseException.class)
+    public void checkHeapSizeBad1() throws ParseException {
+        JREDesc.checkHeapSize("10k10m");
+    }
+
+    @Test(expected = ParseException.class)
+    public void checkHeapSizeBad2() throws ParseException {
+        JREDesc.checkHeapSize("one gigabyte");
+    }
+
+    @Test(expected = ParseException.class)
+    public void checkHeapSizeBad3() throws ParseException {
+        JREDesc.checkHeapSize("99l");
+    }
+
+    @Test(expected = ParseException.class)
+    public void checkHeapSizeBad4() throws ParseException {
+        JREDesc.checkHeapSize("99KK");
+    }
+}