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.

CvalAddonsCheckerTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.vaadin.tools;
  2. import static com.vaadin.tools.CvalAddonsChecker.VAADIN_AGPL;
  3. import static com.vaadin.tools.CvalAddonsChecker.VAADIN_CVAL;
  4. import static com.vaadin.tools.CvalChecker.GRACE_DAYS_MSECS;
  5. import static com.vaadin.tools.CvalChecker.computeLicenseName;
  6. import static com.vaadin.tools.CvalChecker.deleteCache;
  7. import static com.vaadin.tools.CvalCheckerTest.VALID_KEY;
  8. import static com.vaadin.tools.CvalCheckerTest.addLicensedJarToClasspath;
  9. import static com.vaadin.tools.CvalCheckerTest.cacheExists;
  10. import static com.vaadin.tools.CvalCheckerTest.captureSystemOut;
  11. import static com.vaadin.tools.CvalCheckerTest.productNameAgpl;
  12. import static com.vaadin.tools.CvalCheckerTest.productNameApache;
  13. import static com.vaadin.tools.CvalCheckerTest.productNameCval;
  14. import static com.vaadin.tools.CvalCheckerTest.readSystemOut;
  15. import static com.vaadin.tools.CvalCheckerTest.saveCache;
  16. import static com.vaadin.tools.CvalCheckerTest.unreachableLicenseProvider;
  17. import static com.vaadin.tools.CvalCheckerTest.validLicenseProvider;
  18. import java.net.URL;
  19. import java.net.URLClassLoader;
  20. import java.util.List;
  21. import org.junit.Assert;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import com.vaadin.client.metadata.ConnectorBundleLoader.CValUiInfo;
  25. import com.vaadin.tools.CvalChecker.InvalidCvalException;
  26. /**
  27. * The CvalAddonsChecker test.
  28. */
  29. public class CvalAddonsCheckerTest {
  30. CvalAddonsChecker addonChecker;
  31. private String licenseName;
  32. @Before
  33. public void setup() {
  34. addonChecker = new CvalAddonsChecker()
  35. .setLicenseProvider(validLicenseProvider).setFilter(".*test.*");
  36. licenseName = computeLicenseName(productNameCval);
  37. deleteCache(productNameCval);
  38. System.getProperties().remove(licenseName);
  39. // Set up a new URLClassLoader for the thread
  40. Thread thread = Thread.currentThread();
  41. thread.setContextClassLoader(new URLClassLoader(new URL[0], null));
  42. }
  43. @Test
  44. public void testRunChecker() throws Exception {
  45. // Create a product .jar with a cval license non required and add to our
  46. // classpath
  47. addLicensedJarToClasspath(productNameCval, VAADIN_CVAL);
  48. // Remove other products in case other tests added them previously
  49. addLicensedJarToClasspath(productNameAgpl, null);
  50. addLicensedJarToClasspath(productNameApache, null);
  51. // No license
  52. // -> Break compilation
  53. System.getProperties().remove(licenseName);
  54. addonChecker.setLicenseProvider(validLicenseProvider);
  55. try {
  56. addonChecker.run();
  57. Assert.fail();
  58. } catch (InvalidCvalException expected) {
  59. }
  60. Assert.assertFalse(cacheExists(productNameCval));
  61. // We have a license that has never been validated from the server and
  62. // we are offline
  63. // -> Show a message on compile time (“Your license for TouchKit 4 has
  64. // not been validated.")
  65. System.setProperty(licenseName, VALID_KEY);
  66. addonChecker.setLicenseProvider(unreachableLicenseProvider);
  67. captureSystemOut();
  68. addonChecker.run();
  69. Assert.assertTrue(readSystemOut().contains("has not been validated"));
  70. Assert.assertFalse(cacheExists(productNameCval));
  71. // Valid license has previously been validated from the server and we
  72. // are offline
  73. // -> Use the cached server response
  74. System.setProperty(licenseName, VALID_KEY);
  75. addonChecker.setLicenseProvider(validLicenseProvider);
  76. captureSystemOut();
  77. addonChecker.run();
  78. Assert.assertTrue(cacheExists(productNameCval));
  79. addonChecker.setLicenseProvider(unreachableLicenseProvider);
  80. addonChecker.run();
  81. // Expired license and we are offline
  82. // -> If it has expired less than 14 days ago, just work with no nag
  83. // messages
  84. System.setProperty(licenseName, VALID_KEY);
  85. addonChecker.setLicenseProvider(unreachableLicenseProvider);
  86. setCacheFileTs(System.currentTimeMillis() - (GRACE_DAYS_MSECS / 2),
  87. "normal");
  88. captureSystemOut();
  89. addonChecker.run();
  90. // Expired license and we are offline
  91. // -> After 14 days, interpret it as expired license
  92. setCacheFileTs(System.currentTimeMillis() - (GRACE_DAYS_MSECS * 2),
  93. "normal");
  94. try {
  95. addonChecker.run();
  96. Assert.fail();
  97. } catch (InvalidCvalException expected) {
  98. }
  99. // Invalid evaluation license
  100. // -> Fail compilation with a message
  101. // "Your evaluation license for TouchKit 4 is not valid"
  102. System.setProperty(licenseName, VALID_KEY);
  103. addonChecker.setLicenseProvider(unreachableLicenseProvider);
  104. setCacheFileTs(System.currentTimeMillis() - (GRACE_DAYS_MSECS / 2),
  105. "evaluation");
  106. try {
  107. addonChecker.run();
  108. Assert.fail();
  109. } catch (InvalidCvalException expected) {
  110. Assert.assertTrue(expected.getMessage().contains("expired"));
  111. }
  112. // Valid evaluation license
  113. // -> The choice on whether to show the message is generated in
  114. // widgetset
  115. // compilation phase. No license checks are done in application runtime.
  116. System.setProperty(licenseName, VALID_KEY);
  117. addonChecker.setLicenseProvider(unreachableLicenseProvider);
  118. setCacheFileTs(System.currentTimeMillis() + GRACE_DAYS_MSECS,
  119. "evaluation");
  120. List<CValUiInfo> uiInfo = addonChecker.run();
  121. Assert.assertEquals(1, uiInfo.size());
  122. Assert.assertEquals("Test " + productNameCval, uiInfo.get(0).product);
  123. Assert.assertEquals("evaluation", uiInfo.get(0).type);
  124. // Valid real license
  125. // -> Work as expected
  126. // -> Show info message “Using TouchKit 4 license
  127. // 312-312321-321312-3-12-312-312
  128. // licensed to <licensee> (1 developer license)"
  129. System.setProperty(licenseName, VALID_KEY);
  130. addonChecker.setLicenseProvider(validLicenseProvider);
  131. captureSystemOut();
  132. addonChecker.run();
  133. Assert.assertTrue(readSystemOut().contains("valid"));
  134. }
  135. @Test
  136. public void validateMultipleLicenses() throws Exception {
  137. addLicensedJarToClasspath(productNameCval, VAADIN_CVAL);
  138. addLicensedJarToClasspath(productNameAgpl, VAADIN_AGPL);
  139. addLicensedJarToClasspath(productNameApache, "apache");
  140. // We have a valid license for all products
  141. System.setProperty(licenseName, VALID_KEY);
  142. captureSystemOut();
  143. addonChecker.run();
  144. String out = readSystemOut();
  145. Assert.assertTrue(out.contains("valid"));
  146. Assert.assertTrue(out.contains("AGPL"));
  147. Assert.assertTrue(cacheExists(productNameCval));
  148. }
  149. private void setCacheFileTs(long expireTs, String type) {
  150. saveCache(productNameCval, null, false, expireTs, type);
  151. }
  152. }