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

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