選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CvalAddonsCheckerTest.java 7.2KB

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