PageRenderTime 63ms CodeModel.GetById 31ms app.highlight 25ms RepoModel.GetById 0ms app.codeStats 1ms

/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 *******************************************************************************/
 11package org.eclipse.che.api.project.server.type;
 12
 13import com.google.inject.AbstractModule;
 14import com.google.inject.Guice;
 15import com.google.inject.Injector;
 16import com.google.inject.multibindings.Multibinder;
 17import org.eclipse.che.api.core.NotFoundException;
 18import org.eclipse.che.api.project.server.*;
 19import org.junit.Assert;
 20import org.junit.Before;
 21import org.junit.Test;
 22
 23import javax.inject.Inject;
 24import javax.inject.Singleton;
 25import java.util.Arrays;
 26import java.util.HashSet;
 27import java.util.List;
 28import java.util.Set;
 29
 30import static org.junit.Assert.assertEquals;
 31import static org.junit.Assert.assertNotNull;
 32
 33/**
 34 * @author gazarenkov
 35 */
 36public class ProjectTypeTest {
 37
 38    Injector injector;
 39
 40    @Before
 41    public void setUp() throws Exception {
 42        // Bind components
 43        injector = Guice.createInjector(new AbstractModule() {
 44            @Override
 45            protected void configure() {
 46
 47                install(new ProjectApiModule());
 48
 49                Multibinder<ValueProviderFactory> valueProviderMultibinder = Multibinder.newSetBinder(binder(), ValueProviderFactory.class);
 50                valueProviderMultibinder.addBinding().to(MyVPFactory.class);
 51
 52                Multibinder<ProjectTypeDef> projectTypesMultibinder = Multibinder.newSetBinder(binder(), ProjectTypeDef.class);
 53                projectTypesMultibinder.addBinding().to(MyProjectType.class);
 54
 55                bind(ProjectTypeRegistry.class);
 56            }
 57        });
 58    }
 59
 60    @Test
 61    public void testProjectTypeService() throws Exception {
 62        ProjectTypeRegistry registry = injector.getInstance(ProjectTypeRegistry.class);
 63
 64        ProjectTypeService service = new ProjectTypeService(registry);
 65
 66        assertEquals(2, service.getProjectTypes().size());
 67    }
 68
 69    @Test
 70    public void testProjectTypeDefinition() throws Exception {
 71        ProjectTypeRegistry registry = injector.getInstance(ProjectTypeRegistry.class);
 72
 73        ProjectTypeDef type = registry.getProjectType("my");
 74
 75        assertNotNull(type);
 76        assertEquals(1, type.getParents().size());
 77        assertEquals(BaseProjectType.ID, type.getParents().get(0));
 78        assertNotNull(((Variable)type.getAttribute("var")).getValueProviderFactory());
 79        Assert.assertNull(type.getAttribute("var").getValue());
 80        assertEquals(3, type.getAttributes().size());
 81        assertNotNull(type.getAttribute("const"));
 82        assertEquals(new AttributeValue("const_value"), type.getAttribute("const").getValue());
 83        assertEquals(new AttributeValue("value"), type.getAttribute("var1").getValue());
 84        Assert.assertTrue(type.getAttribute("var1").isRequired());
 85        Assert.assertTrue(type.getAttribute("var1").isVariable());
 86        Assert.assertFalse(type.getAttribute("const").isVariable());
 87    }
 88
 89    @Test
 90    public void testInvalidPTDefinition() throws Exception {
 91        ProjectTypeDef pt = new ProjectTypeDef("my", "second", true, false) {
 92        };
 93
 94        Set<ProjectTypeDef> pts = new HashSet<>();
 95        pts.add(new MyProjectType(null));
 96        pts.add(pt);
 97        ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
 98
 99        // BASE and MY (
100        assertEquals(2, reg.getProjectTypes().size());
101
102        // Invalid names
103        pts.clear();
104        pts.add(new ProjectTypeDef(null, "null id", true, false) {
105        });
106        pts.add(new ProjectTypeDef("", "empty id", true, false) {
107        });
108        pts.add(new ProjectTypeDef("invalid id", "invalid id", true, false) {
109        });
110        pts.add(new ProjectTypeDef("id1", null, true, false) {
111        });
112        pts.add(new ProjectTypeDef("id2", "", true, false) {
113        });
114        reg = new ProjectTypeRegistry(pts);
115        // BASE only
116        assertEquals(1, reg.getProjectTypes().size());
117
118        // Invalid parent
119        final ProjectTypeDef invalidParent = new ProjectTypeDef("i-parent", "parent", true, false) {
120        };
121        pts.add(new ProjectTypeDef("notRegParent", "not reg parent", true, false) {
122            {
123                addParent("i-parent");
124            }
125        });
126        reg = new ProjectTypeRegistry(pts);
127        // BASE only
128        assertEquals(1, reg.getProjectTypes().size());
129    }
130
131    @Test
132    public void testPTInheritance() throws Exception {
133        Set<ProjectTypeDef> pts = new HashSet<>();
134        final ProjectTypeDef parent = new ProjectTypeDef("parent", "parent", true, false) {
135            {
136                addConstantDefinition("parent_const", "Constant", "const_value");
137            }
138
139        };
140        final ProjectTypeDef child = new ProjectTypeDef("child", "child", true, false) {
141            {
142                addParent("parent");
143                addConstantDefinition("child_const", "Constant", "const_value");
144            }
145        };
146
147        pts.add(child);
148        pts.add(parent);
149
150        ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
151        assertEquals(3, reg.getProjectTypes().size());
152        assertEquals(1, child.getParents().size());
153        assertEquals(2, child.getAncestors().size());
154        assertEquals(2, reg.getProjectType("child").getAttributes().size());
155        assertEquals(1, reg.getProjectType("parent").getAttributes().size());
156        Assert.assertTrue(reg.getProjectType("child").isTypeOf("parent"));
157    }
158
159    @Test
160    public void testAttributeNameConflict() throws Exception {
161        Set<ProjectTypeDef> pts = new HashSet<>();
162        final ProjectTypeDef parent = new ProjectTypeDef("parent", "parent", true, false) {
163            {
164                addConstantDefinition("parent_const", "Constant", "const_value");
165            }
166
167        };
168        final ProjectTypeDef child = new ProjectTypeDef("child", "child", true, false) {
169            {
170                addParent("parent");
171                addConstantDefinition("parent_const", "Constant", "const_value");
172            }
173        };
174
175        pts.add(child);
176        pts.add(parent);
177
178        ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
179
180        assertNotNull(reg.getProjectType("parent"));
181        //Assert.assertNull(reg.getProjectType("child"));
182
183        try {
184            reg.getProjectType("child");
185            Assert.fail("NotFoundException should be thrown");
186        } catch (NotFoundException e) {
187        }
188
189        assertEquals(2, reg.getProjectTypes().size());
190    }
191
192    @Test
193    public void testMultiInheritance() throws Exception {
194        Set<ProjectTypeDef> pts = new HashSet<>();
195        final ProjectTypeDef parent1 = new ProjectTypeDef("parent1", "parent", true, false) {
196            {
197                addConstantDefinition("parent1_const", "Constant", "const_value");
198            }
199
200        };
201        final ProjectTypeDef parent2 = new ProjectTypeDef("parent2", "parent", true, false) {
202            {
203                addConstantDefinition("parent2_const", "Constant", "const_value");
204            }
205
206        };
207        final ProjectTypeDef child = new ProjectTypeDef("child", "child", true, false) {
208            {
209                addParent("parent1");
210                addParent("parent2");
211                addConstantDefinition("child_const", "Constant", "const_value");
212            }
213        };
214
215        pts.add(child);
216        pts.add(parent1);
217        pts.add(parent2);
218
219        ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
220
221        assertEquals(2, child.getParents().size());
222        assertEquals(3, reg.getProjectType("child").getAttributes().size());
223    }
224
225    @Test
226    public void testMultiInheritanceAttributeConflict() throws Exception {
227        Set<ProjectTypeDef> pts = new HashSet<>();
228        final ProjectTypeDef parent1 = new ProjectTypeDef("parent1", "parent", true, false) {
229            {
230                addConstantDefinition("parent_const", "Constant", "const_value");
231            }
232
233        };
234        final ProjectTypeDef parent2 = new ProjectTypeDef("parent2", "parent", true, false) {
235            {
236                addConstantDefinition("parent_const", "Constant", "const_value");
237            }
238
239        };
240        final ProjectTypeDef child = new ProjectTypeDef("child", "child", true, false) {
241            {
242                addParent("parent1");
243                addParent("parent2");
244                addConstantDefinition("child_const", "Constant", "const_value");
245            }
246        };
247
248        pts.add(child);
249        pts.add(parent1);
250        pts.add(parent2);
251
252        ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
253
254        assertNotNull(reg.getProjectType("parent1"));
255        assertNotNull(reg.getProjectType("parent2"));
256
257        try {
258            reg.getProjectType("child");
259            Assert.fail("NotFoundException should be thrown");
260        } catch (NotFoundException e) {
261        }
262        //Assert.assertNull(reg.getProjectType("child"));
263    }
264
265    @Test
266    public void testTypeOf() throws Exception {
267        Set<ProjectTypeDef> pts = new HashSet<>();
268        final ProjectTypeDef parent = new ProjectTypeDef("parent", "parent", true, false) {
269        };
270
271        final ProjectTypeDef parent1 = new ProjectTypeDef("parent1", "parent", true, false) {
272        };
273
274        final ProjectTypeDef parent2 = new ProjectTypeDef("parent2", "parent", true, false) {
275        };
276
277        final ProjectTypeDef child = new ProjectTypeDef("child", "child", true, false) {
278            {
279                addParent("parent");
280                addParent("parent2");
281            }
282        };
283
284        final ProjectTypeDef child2 = new ProjectTypeDef("child2", "child2", true, false) {
285            {
286                addParent("child");
287            }
288        };
289
290        pts.add(child);
291        pts.add(parent);
292        pts.add(child2);
293        pts.add(parent1);
294        pts.add(parent2);
295
296        ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
297
298        ProjectTypeDef t1 = reg.getProjectType("child2");
299
300
301        Assert.assertTrue(t1.isTypeOf("parent"));
302        Assert.assertTrue(t1.isTypeOf("parent2"));
303        Assert.assertTrue(t1.isTypeOf("blank"));
304        Assert.assertFalse(t1.isTypeOf("parent1"));
305    }
306
307    @Test
308    public void testSortPTs() throws Exception {
309        Set<ProjectTypeDef> pts = new HashSet<>();
310        final ProjectTypeDef parent = new ProjectTypeDef("parent", "parent", true, false) {
311        };
312
313        final ProjectTypeDef child = new ProjectTypeDef("child", "child", true, false) {
314            {
315                addParent("parent");
316            }
317        };
318
319        final ProjectTypeDef child2 = new ProjectTypeDef("child2", "child2", true, false) {
320            {
321                addParent("child");
322            }
323        };
324
325        pts.add(child);
326        pts.add(parent);
327        pts.add(child2);
328
329        ProjectTypeRegistry reg = new ProjectTypeRegistry(pts);
330        List<ProjectTypeDef> list = reg.getProjectTypes(new ProjectTypeRegistry.ChildToParentComparator());
331
332        assertEquals(list.get(0).getId(), "child2");
333        assertEquals(list.get(1).getId(), "child");
334        assertEquals(list.get(2).getId(), "parent");
335        assertEquals(list.get(3).getId(), "blank");
336    }
337
338    @Singleton
339    public static class MyVPFactory implements ValueProviderFactory {
340
341        @Override
342        public ValueProvider newInstance(FolderEntry projectFolder) {
343            return new MyValueProvider();
344        }
345
346        public static class MyValueProvider implements ValueProvider {
347
348            @Override
349            public List<String> getValues(String attributeName) throws ValueStorageException {
350                return Arrays.asList("gena");
351            }
352
353            @Override
354            public void setValues(String attributeName, List<String> value) throws ValueStorageException, InvalidValueException {
355            }
356        }
357    }
358
359    @Singleton
360    public static class MyProjectType extends ProjectTypeDef {
361
362        @Inject
363        public MyProjectType(MyVPFactory myVPFactory) {
364            super("my", "my type", true, false);
365
366            addConstantDefinition("const", "Constant", "const_value");
367            addVariableDefinition("var", "Variable", false, myVPFactory);
368            addVariableDefinition("var1", "var", true, new AttributeValue("value"));
369        }
370    }
371}