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 7.4KB

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