PageRenderTime 714ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/de/indisopht/guice/groovy/utest/spi/SPIIntegrationTest.java

http://groovy-guice.googlecode.com/
Java | 439 lines | 380 code | 40 blank | 19 comment | 4 complexity | ded3e8c0902118fcd207bd4833941df8 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright (C) 2008 Stefan Maassen
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package de.indisopht.guice.groovy.utest.spi;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertNotNull;
  19. import groovy.lang.GroovyClassLoader;
  20. import groovy.lang.GroovyCodeSource;
  21. import groovy.lang.Script;
  22. import java.io.File;
  23. import java.io.FileWriter;
  24. import java.io.IOException;
  25. import java.net.URLClassLoader;
  26. import java.security.AccessControlException;
  27. import java.util.concurrent.TimeUnit;
  28. import org.apache.log4j.Logger;
  29. import org.junit.After;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32. import com.google.inject.AbstractModule;
  33. import com.google.inject.Binder;
  34. import com.google.inject.Guice;
  35. import com.google.inject.Injector;
  36. import com.google.inject.Key;
  37. import com.google.inject.Module;
  38. import com.google.inject.name.Names;
  39. import de.indisopht.guice.groovy.GroovyGuice;
  40. import de.indisopht.guice.groovy.GroovyProvider;
  41. import de.indisopht.guice.groovy.utest.TestInterface;
  42. public class SPIIntegrationTest {
  43. @SuppressWarnings("unused")
  44. private static final Logger logger = Logger.getLogger(SPIIntegrationTest.class);
  45. private static final String dynamicSource = "import de.indisopht.guice.groovy.utest.TestInterface; " +
  46. "class TestClass implements TestInterface {" +
  47. "String getValue() {" +
  48. "'fromString'" +
  49. "}" +
  50. "}";
  51. @Before
  52. public void setUp() throws Exception {
  53. }
  54. @After
  55. public void tearDown() throws Exception {
  56. }
  57. /**
  58. * TestClass.groovy is in the default package of the script root directory
  59. * ".../src/test/resources"
  60. */
  61. @Test
  62. public void loadFromFile() {
  63. final String classpath = new File(URLClassLoader.getSystemResource("TestClass.groovy").getPath()).getParent();
  64. Module groovyModule = new Module() {
  65. @Override
  66. public void configure(Binder binder) {
  67. binder.bind(TestInterface.class).toProvider(new GroovyProvider<TestInterface>("TestClass"){});
  68. }
  69. };
  70. Module groovyGuiceModule = GroovyGuice
  71. .createModule()
  72. .addClasspath(classpath)
  73. .enableRecompilation()
  74. .build();
  75. Injector injector = Guice.createInjector(groovyModule, groovyGuiceModule);
  76. assertEquals("fromFile", injector.getInstance(TestInterface.class).getValue());
  77. }
  78. @Test
  79. public void loadFromString() {
  80. Module groovyModule = new Module() {
  81. @Override
  82. public void configure(Binder binder) {
  83. binder.bind(TestInterface.class).toProvider(new GroovyProvider<TestInterface>(dynamicSource){});
  84. }
  85. };
  86. Module groovyGuiceModule= GroovyGuice
  87. .createModule()
  88. .build();
  89. assertEquals("fromString", Guice.createInjector(groovyModule, groovyGuiceModule).getInstance(TestInterface.class).getValue());
  90. }
  91. @Test
  92. public void loadScriptFromStringWithoutSecurity() {
  93. Module groovyGuiceModule= GroovyGuice
  94. .createModule()
  95. .bindScript("println('groovy hello'); 'groovy'")
  96. .build();
  97. assertEquals("groovy", Guice.createInjector(groovyGuiceModule).getInstance(Script.class).run());
  98. }
  99. @Test
  100. public void loadScriptFromStringWithExplicitBinding() {
  101. Module groovyModule = new Module() {
  102. @Override
  103. public void configure(Binder binder) {
  104. binder.bind(Script.class).toProvider(new GroovyProvider<Script>("println('groovy hello'); 'groovy'"){});
  105. }
  106. };
  107. Module groovyGuiceModule= GroovyGuice
  108. .createModule()
  109. .build();
  110. assertEquals("groovy", Guice.createInjector(groovyModule, groovyGuiceModule).getInstance(Script.class).run());
  111. }
  112. @Test
  113. public void loadScriptFromFile() {
  114. Module groovyGuiceModule= GroovyGuice
  115. .createModule()
  116. .addClasspath(URLClassLoader.getSystemResource("TestScript.groovy").getPath())
  117. .bindScript("TestScript")
  118. .build();
  119. assertEquals("fromFile", Guice.createInjector(groovyGuiceModule).getInstance(Script.class).run());
  120. }
  121. @Test
  122. public void loadScriptFromFileWithExplicitBinding() {
  123. Module groovyModule = new Module() {
  124. @Override
  125. public void configure(Binder binder) {
  126. binder.bind(Script.class).toProvider(new GroovyProvider<Script>("TestScript"){});
  127. }
  128. };
  129. Module groovyGuiceModule= GroovyGuice
  130. .createModule()
  131. .addClasspath(URLClassLoader.getSystemResource("TestScript.groovy").getPath())
  132. .build();
  133. assertEquals("fromFile", Guice.createInjector(groovyModule, groovyGuiceModule).getInstance(Script.class).run());
  134. }
  135. @Test
  136. public void loadFromFileWithExternalDependency() {
  137. Module groovyModule = new Module() {
  138. @Override
  139. public void configure(Binder binder) {
  140. binder.bind(TestInterface.class).toProvider(new GroovyProvider<TestInterface>("TestClassWithGroovyDependency"){});
  141. }
  142. };
  143. Module groovyGuiceModule= GroovyGuice
  144. .createModule()
  145. .addClasspath(new File(URLClassLoader.getSystemResource("TestClassWithGroovyDependency.groovy").getPath()).getParent().toString())
  146. .enableRecompilation()
  147. .build();
  148. assertEquals("called successfully", Guice.createInjector(groovyModule, groovyGuiceModule).getInstance(TestInterface.class).getValue());
  149. }
  150. @Test
  151. public void testManualClassRecompilation() throws IOException {
  152. File f=null;
  153. FileWriter writer=null;
  154. Module groovyModule = new Module() {
  155. @Override
  156. public void configure(Binder binder) {
  157. binder.bind(TestInterface.class).toProvider(new GroovyProvider<TestInterface>("TestClass"){});
  158. }
  159. };
  160. Module groovyGuiceModule= GroovyGuice
  161. .createModule()
  162. .addClasspath(new File(URLClassLoader.getSystemResource("TestClass.groovy").getPath()).getParent().toString())
  163. .enableRecompilation()
  164. .build();
  165. Injector injector=Guice.createInjector(groovyModule, groovyGuiceModule);
  166. assertEquals("fromFile", injector.getInstance(TestInterface.class).getValue());
  167. try {
  168. f=new File(URLClassLoader.getSystemResource("TestClass.groovy").getPath());
  169. f.delete();
  170. f.createNewFile();
  171. writer=new FileWriter(f);
  172. writer.write("import de.indisopht.guice.groovy.utest.TestInterface\n"+
  173. " class TestClass implements TestInterface {\n"+
  174. "String getValue() {\n"+
  175. "'again fromFile'\n"+
  176. "}\n"+
  177. "}");
  178. writer.close();
  179. assertEquals("again fromFile", injector.getInstance(TestInterface.class).getValue());
  180. } finally {
  181. if (f!=null) {
  182. f.delete();
  183. f.createNewFile();
  184. writer=new FileWriter(f);
  185. writer.write("import de.indisopht.guice.groovy.utest.TestInterface\n"+
  186. " class TestClass implements TestInterface {\n"+
  187. "String getValue() {\n"+
  188. "'fromFile'\n"+
  189. "}\n"+
  190. "}");
  191. writer.close();
  192. }
  193. }
  194. }
  195. @Test
  196. public void testDynamicClassRecompilation() throws IOException {
  197. File f=null;
  198. FileWriter writer=null;
  199. Module groovyModule = new AbstractModule() {
  200. @Override
  201. public void configure() {
  202. bind(TestInterface.class).toProvider(new GroovyProvider<TestInterface>("TestClass", true){});
  203. }
  204. };
  205. Module groovyGuiceModule = GroovyGuice
  206. .createModule()
  207. .addClasspath(new File(URLClassLoader.getSystemResource("TestClass.groovy").getPath()).getParent().toString())
  208. .enableRecompilation()
  209. .build();
  210. TestInterface ti=Guice.createInjector(groovyModule, groovyGuiceModule).getInstance(TestInterface.class);
  211. assertEquals("fromFile", ti.getValue());
  212. try {
  213. f=new File(URLClassLoader.getSystemResource("TestClass.groovy").getPath());
  214. f.delete();
  215. f.createNewFile();
  216. writer=new FileWriter(f);
  217. writer.write("import de.indisopht.guice.groovy.utest.TestInterface\n"+
  218. " class TestClass implements TestInterface {\n"+
  219. "String getValue() {\n"+
  220. "'again fromFile'\n"+
  221. "}\n"+
  222. "}");
  223. writer.close();
  224. try {
  225. TimeUnit.MILLISECONDS.sleep(500l);
  226. } catch (InterruptedException e) {
  227. }
  228. assertEquals("fromFile", ti.getValue());
  229. try {
  230. TimeUnit.SECONDS.sleep(5l);
  231. } catch (InterruptedException e) {
  232. }
  233. assertEquals("again fromFile", ti.getValue());
  234. } finally {
  235. if (f!=null) {
  236. f.delete();
  237. f.createNewFile();
  238. writer=new FileWriter(f);
  239. writer.write("import de.indisopht.guice.groovy.utest.TestInterface\n"+
  240. " class TestClass implements TestInterface {\n"+
  241. "String getValue() {\n"+
  242. "'fromFile'\n"+
  243. "}\n"+
  244. "}");
  245. writer.close();
  246. }
  247. }
  248. }
  249. @Test
  250. public void testDynamicClassRecompilationDisabled() throws IOException {
  251. File f=null;
  252. FileWriter writer=null;
  253. Module groovyModule = new Module() {
  254. @Override
  255. public void configure(Binder binder) {
  256. binder.bind(TestInterface.class).toProvider(new GroovyProvider<TestInterface>("TestClass"){});
  257. }
  258. };
  259. Module groovyGuiceModule= GroovyGuice
  260. .createModule()
  261. .addClasspath(new File(URLClassLoader.getSystemResource("TestClass.groovy").getPath()).getParent().toString())
  262. .disableRecompilation()
  263. .build();
  264. Injector injector=Guice.createInjector(groovyModule, groovyGuiceModule);
  265. assertEquals("fromFile", injector.getInstance(TestInterface.class).getValue());
  266. try {
  267. f=new File(URLClassLoader.getSystemResource("TestClass.groovy").getPath());
  268. f.delete();
  269. f.createNewFile();
  270. writer=new FileWriter(f);
  271. writer.write("import de.indisopht.guice.groovy.utest.TestInterface\n"+
  272. " class TestClass implements TestInterface {\n"+
  273. "String getValue() {\n"+
  274. "'again fromFile'\n"+
  275. "}\n"+
  276. "}");
  277. writer.close();
  278. assertEquals("fromFile", injector.getInstance(TestInterface.class).getValue());
  279. } finally {
  280. if (f!=null) {
  281. f.delete();
  282. f.createNewFile();
  283. writer=new FileWriter(f);
  284. writer.write("import de.indisopht.guice.groovy.utest.TestInterface\n"+
  285. " class TestClass implements TestInterface {\n"+
  286. "String getValue() {\n"+
  287. "'fromFile'\n"+
  288. "}\n"+
  289. "}");
  290. writer.close();
  291. }
  292. }
  293. }
  294. @Test
  295. public void testDynamicClassRecompilationWithoutChanges() throws IOException {
  296. Module groovyModule = new Module() {
  297. @Override
  298. public void configure(Binder binder) {
  299. binder.bind(TestInterface.class).toProvider(new GroovyProvider<TestInterface>("TestClass", true){});
  300. }
  301. };
  302. Module groovyGuiceModule= GroovyGuice.createModule()
  303. .addClasspath(new File(URLClassLoader.getSystemResource("TestClass.groovy").getPath()).getParent().toString())
  304. .disableRecompilation()
  305. .build();
  306. TestInterface ti = Guice.createInjector(groovyModule, groovyGuiceModule).getInstance(TestInterface.class);
  307. assertEquals("fromFile", ti.getValue());
  308. try {
  309. TimeUnit.MILLISECONDS.sleep(500l);
  310. } catch (InterruptedException e) {
  311. }
  312. assertEquals("fromFile", ti.getValue());
  313. try {
  314. TimeUnit.SECONDS.sleep(5l);
  315. } catch (InterruptedException e) {
  316. }
  317. assertEquals("fromFile", ti.getValue());
  318. }
  319. @Test
  320. public void testBasicLoadingModuleFromScript() {
  321. Module groovyGuiceModule= GroovyGuice.createWithGroovyModules("TestBasicSourceModule")
  322. .addClasspath(new File(URLClassLoader.getSystemResource("TestBasicSourceModule.groovy").getPath()).getParent().toString())
  323. .enableRecompilation()
  324. .build();
  325. assertEquals("Yes, it works", Guice.createInjector(groovyGuiceModule).getInstance(Key.<String>get(String.class, Names.named("testScript"))));
  326. }
  327. @Test
  328. public void testLoadingModuleFromScriptAndClasses() {
  329. Module groovyGuiceModule= GroovyGuice.createWithGroovyModules("TestModule")
  330. .addClasspath(new File(URLClassLoader.getSystemResource("TestModule.groovy").getPath()).getParent().toString())
  331. .enableRecompilation()
  332. .build();
  333. Injector injector=Guice.createInjector(groovyGuiceModule);
  334. assertNotNull(injector.getInstance(TestInterface.class));
  335. assertEquals("fromFile", injector.getInstance(TestInterface.class).getValue());
  336. }
  337. @Test
  338. public void testReloadingModule() throws IOException {
  339. Module groovyGuiceModule= GroovyGuice.createWithGroovyModules("TestModule")
  340. .addClasspath(new File(URLClassLoader.getSystemResource("TestModule.groovy").getPath()).getParent().toString())
  341. .enableRecompilation()
  342. .build();
  343. Injector injector=Guice.createInjector(groovyGuiceModule);
  344. assertNotNull(injector.getInstance(TestInterface.class));
  345. assertEquals("fromFile", injector.getInstance(TestInterface.class).getValue());
  346. File f=null;
  347. FileWriter writer;
  348. try {
  349. f=new File(URLClassLoader.getSystemResource("TestModule.groovy").getPath());
  350. f.delete();
  351. f.createNewFile();
  352. writer=new FileWriter(f);
  353. writer.write("");
  354. writer.close();
  355. try {
  356. TimeUnit.SECONDS.sleep(5l);
  357. TimeUnit.MILLISECONDS.sleep(100l);
  358. } catch (InterruptedException e) {
  359. }
  360. assertNotNull(injector.getInstance(TestInterface.class));
  361. assertEquals("fromFile", injector.getInstance(TestInterface.class).getValue());
  362. } finally {
  363. if (f!=null) {
  364. f.delete();
  365. f.createNewFile();
  366. writer=new FileWriter(f);
  367. writer.write("import com.google.inject.AbstractModule;\n"+
  368. "import com.google.inject.name.Names;\n"+
  369. "import de.indisopht.guice.groovy.utest.TestInterface;\n"+
  370. "\n"+
  371. "public class TestModule extends AbstractModule {\n"+
  372. " protected void configure() {\n"+
  373. " bind(TestInterface).to(TestClass);\n"+
  374. " }\n"+
  375. "}");
  376. writer.close();
  377. }
  378. }
  379. }
  380. @Test(expected = AccessControlException.class)
  381. public void loadScriptFromStringWithSecurity() {
  382. System.setProperty("java.security.policy", URLClassLoader.getSystemResource("security" + File.separator + "groovy_guiceV2.policy").toExternalForm());
  383. System.setSecurityManager(new SecurityManager());
  384. Module groovyGuiceModule= GroovyGuice.createModule()
  385. .useCodeBase("/serverCodeBase/restrictedClient")
  386. .bindScript("System.setProperty('file.encoding', 'UTF-8')")
  387. .build();
  388. Guice.createInjector(groovyGuiceModule).getInstance(Script.class).run();
  389. }
  390. @SuppressWarnings("unchecked")
  391. @Test(expected = AccessControlException.class)
  392. public void loadScriptFromStringWithSecurity_pureGroovy() throws InstantiationException, IllegalAccessException {
  393. System.setProperty("java.security.policy", URLClassLoader.getSystemResource("security" + File.separator + "groovy_guiceV2.policy").toExternalForm());
  394. System.setSecurityManager(new SecurityManager());
  395. GroovyClassLoader groovyLoader=new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
  396. Class<Script> script=(Class<Script>)groovyLoader.parseClass(new GroovyCodeSource("System.setProperty('file.encoding', 'UTF-8')", "SecurityTestScript", "/serverCodeBase/restrictedClient"), true);
  397. script.newInstance().run();
  398. }
  399. }