ModelDescriptor extends BeanModelType<T> { @SuppressWarnings("unchecked") public static <T extends TemplateModel> ModelDescriptor<T> get( Class<T> modelType) { assert modelType != null; return (ModelDescriptor<T>) classToDescriptor.get(modelType); } private  ModelDescriptor(Class<T> beanType); @SuppressWarnings("unchecked") static ModelDescriptor<T> get(
            Class<T> modelType);  }
@Test public void primitiveProperties() { ModelDescriptor<?> descriptor = ModelDescriptor .get(BasicTypeModel.class); Map<String, ModelType> expectedTypes = new HashMap<>(); expectedTypes.put("boolean", BasicModelType.get(Boolean.class).get()); expectedTypes.put("booleanPrimitive", BasicModelType.get(boolean.class).get()); expectedTypes.put("string", BasicModelType.get(String.class).get()); expectedTypes.put("double", BasicModelType.get(Double.class).get()); expectedTypes.put("doublePrimitive", BasicModelType.get(double.class).get()); expectedTypes.put("int", BasicModelType.get(int.class).get()); expectedTypes.put("integer", BasicModelType.get(Integer.class).get()); Set<String> propertyNames = descriptor.getPropertyNames() .collect(Collectors.toSet()); Assert.assertEquals(expectedTypes.keySet(), propertyNames); expectedTypes.forEach((propertyName, expectedType) -> Assert.assertSame( expectedType, descriptor.getPropertyType(propertyName))); }
@Test public void missingProperty() { ModelDescriptor<?> descriptor = ModelDescriptor .get(BasicTypeModel.class); Assert.assertFalse(descriptor.hasProperty("foo")); }
@Test(expected = InvalidTemplateModelException.class) public void unsupported() { ModelDescriptor.get(NotSupportedModel.class); }
@Test public void beanProperty() { ModelDescriptor<?> descriptor = ModelDescriptor.get(BeanModel.class); Assert.assertEquals(1, descriptor.getPropertyNames().count()); BeanModelType<?> beanPropertyType = (BeanModelType<?>) descriptor .getPropertyType("bean"); Assert.assertSame(Bean.class, beanPropertyType.getProxyType()); }
@Test public void listInsideList() { ModelDescriptor<?> descriptor = ModelDescriptor .get(ListInsideListBeanModel.class); Assert.assertEquals(1, descriptor.getPropertyNames().count()); ListModelType<?> listPropertyType = (ListModelType<?>) descriptor .getPropertyType("beans"); Type javaType = listPropertyType.getJavaType(); Assert.assertTrue("Expected instanceof ParameterizedType for List", javaType instanceof ParameterizedType); javaType = ((ParameterizedType) javaType).getActualTypeArguments()[0]; Assert.assertTrue( "Expected instanceof ParameterizedType for List in List", javaType instanceof ParameterizedType); Assert.assertEquals(Bean.class, ((ParameterizedType) javaType).getActualTypeArguments()[0]); Assert.assertTrue( listPropertyType.getItemType() instanceof ListModelType<?>); ListModelType<?> type = (ListModelType<?>) listPropertyType .getItemType(); Assert.assertTrue(type.getItemType() instanceof BeanModelType<?>); BeanModelType<?> modelType = (BeanModelType<?>) type.getItemType(); Assert.assertSame(Bean.class, modelType.getProxyType()); }
@Test public void listInsideListInsideList() { ModelDescriptor<?> descriptor = ModelDescriptor .get(ListInsideListInsideList.class); Assert.assertEquals(1, descriptor.getPropertyNames().count()); ListModelType<?> listPropertyType = (ListModelType<?>) descriptor .getPropertyType("beans"); Type javaType = listPropertyType.getJavaType(); Assert.assertTrue("Expected instanceof ParameterizedType for List", javaType instanceof ParameterizedType); javaType = ((ParameterizedType) javaType).getActualTypeArguments()[0]; Assert.assertTrue( "Expected instanceof ParameterizedType for List in List", javaType instanceof ParameterizedType); javaType = ((ParameterizedType) javaType).getActualTypeArguments()[0]; Assert.assertTrue( "Expected instanceof ParameterizedType for List in List in List", javaType instanceof ParameterizedType); Assert.assertEquals(Bean.class, ((ParameterizedType) javaType).getActualTypeArguments()[0]); Assert.assertTrue( listPropertyType.getItemType() instanceof ListModelType<?>); ListModelType<?> type = (ListModelType<?>) listPropertyType .getItemType(); Assert.assertTrue(type.getItemType() instanceof ListModelType<?>); type = (ListModelType<?>) type.getItemType(); Assert.assertTrue(type.getItemType() instanceof BeanModelType<?>); BeanModelType<?> modelType = (BeanModelType<?>) type.getItemType(); Assert.assertSame(Bean.class, modelType.getProxyType()); }
@Test public void listProperty() { ModelDescriptor<?> descriptor = ModelDescriptor .get(ListBeanModel.class); Assert.assertEquals(1, descriptor.getPropertyNames().count()); ListModelType<?> listPropertyType = (ListModelType<?>) descriptor .getPropertyType("beans"); Assert.assertTrue( listPropertyType.getItemType() instanceof BeanModelType<?>); BeanModelType<?> modelType = (BeanModelType<?>) listPropertyType .getItemType(); Assert.assertSame(Bean.class, modelType.getProxyType()); }
@Test public void subBeans() { ModelDescriptor<?> descriptor = ModelDescriptor .get(SubBeansModel.class); BeanModelType<?> beanType = (BeanModelType<?>) descriptor .getPropertyType("bean"); Assert.assertEquals( Stream.of("beanClass", "value", "bean") .collect(Collectors.toSet()), beanType.getPropertyNames().collect(Collectors.toSet())); BeanModelType<?> subBeanType = (BeanModelType<?>) descriptor .getPropertyType("beanClass"); Assert.assertTrue(subBeanType.hasProperty("visible")); Assert.assertTrue(subBeanType.hasProperty("subBean")); }
@Test public void exclude() { ModelDescriptor<?> descriptor = ModelDescriptor .get(ModelWithExclude.class); BeanModelType<?> beanType = (BeanModelType<?>) descriptor .getPropertyType("bean"); Set<String> expectedProperties = new HashSet<>(beanProperties); expectedProperties.remove("doubleValue"); expectedProperties.remove("booleanObject"); Assert.assertEquals(expectedProperties, beanType.getPropertyNames().collect(Collectors.toSet())); }
@Test public void include() { ModelDescriptor<?> descriptor = ModelDescriptor .get(ModelWithInclude.class); BeanModelType<?> beanType = (BeanModelType<?>) descriptor .getPropertyType("bean"); Set<String> expectedProperties = new HashSet<>( Arrays.asList("doubleValue", "booleanObject")); Assert.assertEquals(expectedProperties, beanType.getPropertyNames().collect(Collectors.toSet())); }
@Test public void excludeAndInclude() { ModelDescriptor<?> descriptor = ModelDescriptor .get(ModelWithExcludeAndInclude.class); BeanModelType<?> beanType = (BeanModelType<?>) descriptor .getPropertyType("bean"); Set<String> expectedProperties = Collections.singleton("booleanObject"); Assert.assertEquals(expectedProperties, beanType.getPropertyNames().collect(Collectors.toSet())); }
@Test public void excludeAndIncludeSubclass() { ModelDescriptor<?> descriptor = ModelDescriptor .get(ModelWithExcludeAndIncludeSubclass.class); BeanModelType<?> beanType = (BeanModelType<?>) descriptor .getPropertyType("bean"); Set<String> expectedProperties = Collections.singleton("doubleValue"); Assert.assertEquals(expectedProperties, beanType.getPropertyNames().collect(Collectors.toSet())); }
@Test public void excludeForSubBean() { ModelDescriptor<?> descriptor = ModelDescriptor .get(ModelWithExcludeForSubBean.class); BeanModelType<?> containerType = (BeanModelType<?>) descriptor .getPropertyType("beanContainingBeans"); BeanModelType<?> beanType = (BeanModelType<?>) containerType .getPropertyType("bean1"); Assert.assertFalse(containerType.hasProperty("bean2")); Set<String> expectedProperties = new HashSet<>(beanProperties); expectedProperties.remove("booleanObject"); Assert.assertEquals(expectedProperties, beanType.getPropertyNames().collect(Collectors.toSet())); }
@Test public void includeForSubBean() { ModelDescriptor<?> descriptor = ModelDescriptor .get(ModelWithIncludeForSubBean.class); BeanModelType<?> containerType = (BeanModelType<?>) descriptor .getPropertyType("beanContainingBeans"); BeanModelType<?> beanType = (BeanModelType<?>) containerType .getPropertyType("bean1"); Assert.assertFalse(containerType.hasProperty("bean2")); Set<String> expectedProperties = Collections.singleton("booleanObject"); Assert.assertEquals(expectedProperties, beanType.getPropertyNames().collect(Collectors.toSet())); }
@Test public void excludeOnList() { ModelDescriptor<?> descriptor = ModelDescriptor .get(ModelWithExcludeOnList.class); ListModelType<?> beansType = (ListModelType<?>) descriptor .getPropertyType("beans"); Assert.assertTrue(beansType.getItemType() instanceof BeanModelType<?>); BeanModelType<?> beanType = (BeanModelType<?>) beansType.getItemType(); Set<String> expectedProperties = new HashSet<>(beanProperties); expectedProperties.remove("intValue"); Assert.assertEquals(expectedProperties, beanType.getPropertyNames().collect(Collectors.toSet())); }
@Test public void includeOnList() { ModelDescriptor<?> descriptor = ModelDescriptor .get(ModelWithIncludeOnList.class); ListModelType<?> beansType = (ListModelType<?>) descriptor .getPropertyType("beans"); Assert.assertTrue(beansType.getItemType() instanceof BeanModelType<?>); BeanModelType<?> beanType = (BeanModelType<?>) beansType.getItemType(); Set<String> expectedProperties = Collections.singleton("intValue"); Assert.assertEquals(expectedProperties, beanType.getPropertyNames().collect(Collectors.toSet())); }
@Test public void basicComplexModelType_intAndInteger_numberFromClient() { ComplexModelType<?> type = BasicComplexModelType.get(Integer.class) .get(); StateNode stateNode = type.applicationToModel(2.0d, null); Object applicationValue = type.modelToApplication(stateNode); Assert.assertEquals(Integer.valueOf(2), applicationValue); }