PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/platform-api/che-core-api-project/src/test/java/org/eclipse/che/api/project/server/type/ProjectTypeTest.java

https://gitlab.com/gaal/che-core
Java | 371 lines | 281 code | 69 blank | 21 comment | 0 complexity | aa7a4f5703b9968d081f63a13b6481be MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2012-2015 Codenvy, S.A.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Codenvy, S.A. - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.che.api.project.server.type;
  12. import com.google.inject.AbstractModule;
  13. import com.google.inject.Guice;
  14. import com.google.inject.Injector;
  15. import com.google.inject.multibindings.Multibinder;
  16. import org.eclipse.che.api.core.NotFoundException;
  17. import org.eclipse.che.api.project.server.*;
  18. import org.junit.Assert;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. import javax.inject.Inject;
  22. import javax.inject.Singleton;
  23. import java.util.Arrays;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Set;
  27. import static org.junit.Assert.assertEquals;
  28. import static org.junit.Assert.assertNotNull;
  29. /**
  30. * @author gazarenkov
  31. */
  32. public class ProjectTypeTest {
  33. Injector injector;
  34. @Before
  35. public void setUp() throws Exception {
  36. // Bind components
  37. injector = Guice.createInjector(new AbstractModule() {
  38. @Override
  39. protected void configure() {
  40. install(new ProjectApiModule());
  41. Multibinder<ValueProviderFactory> valueProviderMultibinder = Multibinder.newSetBinder(binder(), ValueProviderFactory.class);
  42. valueProviderMultibinder.addBinding().to(MyVPFactory.class);
  43. Multibinder<ProjectTypeDef> projectTypesMultibinder = Multibinder.newSetBinder(binder(), ProjectTypeDef.class);
  44. projectTypesMultibinder.addBinding().to(MyProjectType.class);
  45. bind(ProjectTypeRegistry.class);
  46. }
  47. });
  48. }
  49. @Test
  50. public void testProjectTypeService() throws Exception {
  51. ProjectTypeRegistry registry = injector.getInstance(ProjectTypeRegistry.class);
  52. ProjectTypeService service = new ProjectTypeService(registry);
  53. assertEquals(2, service.getProjectTypes().size());
  54. }
  55. @Test
  56. public void testProjectTypeDefinition() throws Exception {
  57. ProjectTypeRegistry registry = injector.getInstance(ProjectTypeRegistry.class);
  58. ProjectTypeDef type = registry.getProjectType("my");
  59. assertNotNull(type);
  60. assertEquals(1, type.getParents().size());
  61. assertEquals(BaseProjectType.ID, type.getParents().get(0));
  62. assertNotNull(((Variable)type.getAttribute("var")).getValueProviderFactory());
  63. Assert.assertNull(type.getAttribute("var").getValue());
  64. assertEquals(3, type.getAttributes().size());
  65. assertNotNull(type.getAttribute("const"));
  66. assertEquals(new AttributeValue("const_value"), type.getAttribute("const").getValue());
  67. assertEquals(new AttributeValue("value"), type.getAttribute("var1").getValue());
  68. Assert.assertTrue(type.getAttribute("var1").isRequired());
  69. Assert.assertTrue(type.getAttribute("var1").isVariable());
  70. Assert.assertFalse(type.getAttribute("const").isVariable());
  71. }
  72. @Test
  73. public void testInvalidPTDefinition() throws Exception {
  74. ProjectTypeDef pt = new ProjectTypeDef("my", "second", true, false) {
  75. };
  76. Set<ProjectTypeDef> pts = new HashSet<>();
  77. pts.add(new MyProjectType(null));
  78. pts.add(pt);
  79. ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
  80. // BASE and MY (
  81. assertEquals(2, reg.getProjectTypes().size());
  82. // Invalid names
  83. pts.clear();
  84. pts.add(new ProjectTypeDef(null, "null id", true, false) {
  85. });
  86. pts.add(new ProjectTypeDef("", "empty id", true, false) {
  87. });
  88. pts.add(new ProjectTypeDef("invalid id", "invalid id", true, false) {
  89. });
  90. pts.add(new ProjectTypeDef("id1", null, true, false) {
  91. });
  92. pts.add(new ProjectTypeDef("id2", "", true, false) {
  93. });
  94. reg = new ProjectTypeRegistry(pts);
  95. // BASE only
  96. assertEquals(1, reg.getProjectTypes().size());
  97. // Invalid parent
  98. final ProjectTypeDef invalidParent = new ProjectTypeDef("i-parent", "parent", true, false) {
  99. };
  100. pts.add(new ProjectTypeDef("notRegParent", "not reg parent", true, false) {
  101. {
  102. addParent("i-parent");
  103. }
  104. });
  105. reg = new ProjectTypeRegistry(pts);
  106. // BASE only
  107. assertEquals(1, reg.getProjectTypes().size());
  108. }
  109. @Test
  110. public void testPTInheritance() throws Exception {
  111. Set<ProjectTypeDef> pts = new HashSet<>();
  112. final ProjectTypeDef parent = new ProjectTypeDef("parent", "parent", true, false) {
  113. {
  114. addConstantDefinition("parent_const", "Constant", "const_value");
  115. }
  116. };
  117. final ProjectTypeDef child = new ProjectTypeDef("child", "child", true, false) {
  118. {
  119. addParent("parent");
  120. addConstantDefinition("child_const", "Constant", "const_value");
  121. }
  122. };
  123. pts.add(child);
  124. pts.add(parent);
  125. ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
  126. assertEquals(3, reg.getProjectTypes().size());
  127. assertEquals(1, child.getParents().size());
  128. assertEquals(2, child.getAncestors().size());
  129. assertEquals(2, reg.getProjectType("child").getAttributes().size());
  130. assertEquals(1, reg.getProjectType("parent").getAttributes().size());
  131. Assert.assertTrue(reg.getProjectType("child").isTypeOf("parent"));
  132. }
  133. @Test
  134. public void testAttributeNameConflict() throws Exception {
  135. Set<ProjectTypeDef> pts = new HashSet<>();
  136. final ProjectTypeDef parent = new ProjectTypeDef("parent", "parent", true, false) {
  137. {
  138. addConstantDefinition("parent_const", "Constant", "const_value");
  139. }
  140. };
  141. final ProjectTypeDef child = new ProjectTypeDef("child", "child", true, false) {
  142. {
  143. addParent("parent");
  144. addConstantDefinition("parent_const", "Constant", "const_value");
  145. }
  146. };
  147. pts.add(child);
  148. pts.add(parent);
  149. ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
  150. assertNotNull(reg.getProjectType("parent"));
  151. //Assert.assertNull(reg.getProjectType("child"));
  152. try {
  153. reg.getProjectType("child");
  154. Assert.fail("NotFoundException should be thrown");
  155. } catch (NotFoundException e) {
  156. }
  157. assertEquals(2, reg.getProjectTypes().size());
  158. }
  159. @Test
  160. public void testMultiInheritance() throws Exception {
  161. Set<ProjectTypeDef> pts = new HashSet<>();
  162. final ProjectTypeDef parent1 = new ProjectTypeDef("parent1", "parent", true, false) {
  163. {
  164. addConstantDefinition("parent1_const", "Constant", "const_value");
  165. }
  166. };
  167. final ProjectTypeDef parent2 = new ProjectTypeDef("parent2", "parent", true, false) {
  168. {
  169. addConstantDefinition("parent2_const", "Constant", "const_value");
  170. }
  171. };
  172. final ProjectTypeDef child = new ProjectTypeDef("child", "child", true, false) {
  173. {
  174. addParent("parent1");
  175. addParent("parent2");
  176. addConstantDefinition("child_const", "Constant", "const_value");
  177. }
  178. };
  179. pts.add(child);
  180. pts.add(parent1);
  181. pts.add(parent2);
  182. ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
  183. assertEquals(2, child.getParents().size());
  184. assertEquals(3, reg.getProjectType("child").getAttributes().size());
  185. }
  186. @Test
  187. public void testMultiInheritanceAttributeConflict() throws Exception {
  188. Set<ProjectTypeDef> pts = new HashSet<>();
  189. final ProjectTypeDef parent1 = new ProjectTypeDef("parent1", "parent", true, false) {
  190. {
  191. addConstantDefinition("parent_const", "Constant", "const_value");
  192. }
  193. };
  194. final ProjectTypeDef parent2 = new ProjectTypeDef("parent2", "parent", true, false) {
  195. {
  196. addConstantDefinition("parent_const", "Constant", "const_value");
  197. }
  198. };
  199. final ProjectTypeDef child = new ProjectTypeDef("child", "child", true, false) {
  200. {
  201. addParent("parent1");
  202. addParent("parent2");
  203. addConstantDefinition("child_const", "Constant", "const_value");
  204. }
  205. };
  206. pts.add(child);
  207. pts.add(parent1);
  208. pts.add(parent2);
  209. ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
  210. assertNotNull(reg.getProjectType("parent1"));
  211. assertNotNull(reg.getProjectType("parent2"));
  212. try {
  213. reg.getProjectType("child");
  214. Assert.fail("NotFoundException should be thrown");
  215. } catch (NotFoundException e) {
  216. }
  217. //Assert.assertNull(reg.getProjectType("child"));
  218. }
  219. @Test
  220. public void testTypeOf() throws Exception {
  221. Set<ProjectTypeDef> pts = new HashSet<>();
  222. final ProjectTypeDef parent = new ProjectTypeDef("parent", "parent", true, false) {
  223. };
  224. final ProjectTypeDef parent1 = new ProjectTypeDef("parent1", "parent", true, false) {
  225. };
  226. final ProjectTypeDef parent2 = new ProjectTypeDef("parent2", "parent", true, false) {
  227. };
  228. final ProjectTypeDef child = new ProjectTypeDef("child", "child", true, false) {
  229. {
  230. addParent("parent");
  231. addParent("parent2");
  232. }
  233. };
  234. final ProjectTypeDef child2 = new ProjectTypeDef("child2", "child2", true, false) {
  235. {
  236. addParent("child");
  237. }
  238. };
  239. pts.add(child);
  240. pts.add(parent);
  241. pts.add(child2);
  242. pts.add(parent1);
  243. pts.add(parent2);
  244. ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
  245. ProjectTypeDef t1 = reg.getProjectType("child2");
  246. Assert.assertTrue(t1.isTypeOf("parent"));
  247. Assert.assertTrue(t1.isTypeOf("parent2"));
  248. Assert.assertTrue(t1.isTypeOf("blank"));
  249. Assert.assertFalse(t1.isTypeOf("parent1"));
  250. }
  251. @Test
  252. public void testSortPTs() throws Exception {
  253. Set<ProjectTypeDef> pts = new HashSet<>();
  254. final ProjectTypeDef parent = new ProjectTypeDef("parent", "parent", true, false) {
  255. };
  256. final ProjectTypeDef child = new ProjectTypeDef("child", "child", true, false) {
  257. {
  258. addParent("parent");
  259. }
  260. };
  261. final ProjectTypeDef child2 = new ProjectTypeDef("child2", "child2", true, false) {
  262. {
  263. addParent("child");
  264. }
  265. };
  266. pts.add(child);
  267. pts.add(parent);
  268. pts.add(child2);
  269. ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
  270. List<ProjectTypeDef> list = reg.getProjectTypes(new ProjectTypeRegistry.ChildToParentComparator());
  271. assertEquals(list.get(0).getId(), "child2");
  272. assertEquals(list.get(1).getId(), "child");
  273. assertEquals(list.get(2).getId(), "parent");
  274. assertEquals(list.get(3).getId(), "blank");
  275. }
  276. @Singleton
  277. public static class MyVPFactory implements ValueProviderFactory {
  278. @Override
  279. public ValueProvider newInstance(FolderEntry projectFolder) {
  280. return new MyValueProvider();
  281. }
  282. public static class MyValueProvider implements ValueProvider {
  283. @Override
  284. public List<String> getValues(String attributeName) throws ValueStorageException {
  285. return Arrays.asList("gena");
  286. }
  287. @Override
  288. public void setValues(String attributeName, List<String> value) throws ValueStorageException, InvalidValueException {
  289. }
  290. }
  291. }
  292. @Singleton
  293. public static class MyProjectType extends ProjectTypeDef {
  294. @Inject
  295. public MyProjectType(MyVPFactory myVPFactory) {
  296. super("my", "my type", true, false);
  297. addConstantDefinition("const", "Constant", "const_value");
  298. addVariableDefinition("var", "Variable", false, myVPFactory);
  299. addVariableDefinition("var1", "var", true, new AttributeValue("value"));
  300. }
  301. }
  302. }