From 272711e6f7eed94d933380599cb434a5cfe38f00 Mon Sep 17 00:00:00 2001 From: Jonatan Kronqvist Date: Mon, 15 Dec 2014 15:07:15 +0200 Subject: [PATCH] Fix the license checker after elemental.json #15383 The elemental.json update changed how null values and string representations of numbers were parsed, which caused a lot of tests for CvalChecker to fail. Unfortunately the tests were never run in an automated fashion, which means that they were never discovered until we stumbled upon it due to the issue reported in #15383 Change-Id: If2cb9fa96effea7ce55a4ffe6d1666ca7521e1fb --- client-compiler/build.xml | 15 ++++++++--- client-compiler/ivy.xml | 7 ++++- .../src/com/vaadin/tools/CvalChecker.java | 13 ++++++--- .../src/com/vaadin/tools/CvalCheckerTest.java | 27 ++++++++++++++++--- 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/client-compiler/build.xml b/client-compiler/build.xml index 97189fc437..be8dec18bc 100644 --- a/client-compiler/build.xml +++ b/client-compiler/build.xml @@ -16,7 +16,16 @@ - + + + + + + + + @@ -62,8 +71,8 @@ - - WHAT? No tests for ${module.name}! + + diff --git a/client-compiler/ivy.xml b/client-compiler/ivy.xml index af60b76e22..d30dea5136 100644 --- a/client-compiler/ivy.xml +++ b/client-compiler/ivy.xml @@ -11,6 +11,7 @@ + @@ -24,7 +25,7 @@ + rev="${vaadin.version}" conf="build,test" /> @@ -96,6 +97,10 @@ + + + diff --git a/client-compiler/src/com/vaadin/tools/CvalChecker.java b/client-compiler/src/com/vaadin/tools/CvalChecker.java index b3345c7658..645667be94 100644 --- a/client-compiler/src/com/vaadin/tools/CvalChecker.java +++ b/client-compiler/src/com/vaadin/tools/CvalChecker.java @@ -35,16 +35,17 @@ import java.util.prefs.Preferences; import org.apache.commons.io.IOUtils; import elemental.json.JsonException; +import elemental.json.JsonNull; import elemental.json.JsonObject; import elemental.json.impl.JsonUtil; /** * This class is able to validate the vaadin CVAL license. - * + * * It reads the developer license file and asks the server to validate the * licenseKey. If the license is invalid it throws an exception with the * information about the problem and the server response. - * + * * @since 7.3 */ public final class CvalChecker { @@ -80,6 +81,10 @@ public final class CvalChecker { private static T get(JsonObject o, String k, Class clz) { Object ret = null; try { + if (o == null || o.get(k) == null + || o.get(k) instanceof JsonNull) { + return null; + } if (clz == String.class) { ret = o.getString(k); } else if (clz == JsonObject.class) { @@ -299,7 +304,7 @@ public final class CvalChecker { /** * Given a product name returns the name of the file with the license key. - * + * * Traditionally we have delivered license keys with a name like * 'vaadin.touchkit.developer.license' but our database product name is * 'vaadin-touchkit' so we have to replace '-' by '.' to maintain @@ -340,7 +345,7 @@ public final class CvalChecker { /** * Validate whether there is a valid license key for a product. - * + * * @param productName * for example vaadin-touchkit * @param productVersion diff --git a/client-compiler/tests/src/com/vaadin/tools/CvalCheckerTest.java b/client-compiler/tests/src/com/vaadin/tools/CvalCheckerTest.java index 2985f61631..64a38fae2e 100644 --- a/client-compiler/tests/src/com/vaadin/tools/CvalCheckerTest.java +++ b/client-compiler/tests/src/com/vaadin/tools/CvalCheckerTest.java @@ -66,9 +66,14 @@ public class CvalCheckerTest { static final String responseJson = "{'licenseKey':'" + VALID_KEY + "'," + "'licensee':'Test User','type':'normal'," - + "'expiredEpoch':'1893511225000'," + "'product':{'name':'" + + "'expiredEpoch':1893511225000," + "'product':{'name':'" + productNameCval + "', 'version': 2}}"; + static final String responseJsonWithNullVersion = "{'licenseKey':'" + VALID_KEY + "'," + + "'licensee':'Test User','type':'normal'," + + "'expiredEpoch':1893511225000," + "'product':{'name':'" + + productNameCval + "', 'version': null}}"; + private static ByteArrayOutputStream outContent; // A provider returning a valid license if productKey is valid or null if @@ -114,7 +119,7 @@ public class CvalCheckerTest { static final CvalServer unlimitedLicenseProvider = new CvalServer() { @Override String askServer(String productName, String productKey, int timeout) { - return responseJson.replaceFirst("1893511225000", ""); + return responseJson.replaceFirst("1893511225000", "null"); } }; // An unreachable provider @@ -128,6 +133,14 @@ public class CvalCheckerTest { return super.askServer(productName, productKey, 1000); } }; + // A provider with 'null' in the version field + static final CvalServer nullVersionLicenseProvider = new CvalServer() { + @Override + String askServer(String productName, String productKey, int timeout) + throws IOException { + return responseJsonWithNullVersion; + } + }; private CvalChecker licenseChecker; private String licenseName; @@ -245,6 +258,7 @@ public class CvalCheckerTest { Assert.assertTrue(cacheExists(productNameCval)); // Check an unlimited license + deleteCache(productNameCval); licenseChecker.setLicenseProvider(unlimitedLicenseProvider); licenseChecker .validateProduct(productNameCval, "2.1", productTitleCval); @@ -262,6 +276,12 @@ public class CvalCheckerTest { assertEquals(productNameCval, expected.name); } Assert.assertTrue(cacheExists(productNameCval)); + + deleteCache(productNameCval); + licenseChecker.setLicenseProvider(nullVersionLicenseProvider); + licenseChecker + .validateProduct(productNameCval, "2.1", productTitleCval); + Assert.assertTrue(cacheExists(productNameCval)); } /* @@ -282,8 +302,7 @@ public class CvalCheckerTest { testManifest.getMainAttributes().putValue(VAADIN_ADDON_VERSION, "2"); // Create a temporary Jar - String tmpDir = System.getProperty("java.io.tmpdir"); - File testJarFile = new File(tmpDir + "vaadin." + productName + ".jar"); + File testJarFile = File.createTempFile("vaadin." + productName, "jar"); testJarFile.deleteOnExit(); JarOutputStream target = new JarOutputStream(new FileOutputStream( testJarFile), testManifest); -- 2.39.5