public void export(Session session, File targetFolder) throws IOException { List<EntityType> entityTypes = new ArrayList<EntityType>(); // collect entity types for (OWLClass owlClass : session.findInstances(OWLClass.class)) { if (owlClass.getId().isURI()) { EntityType entityType; if (oneOfAsEnum && !owlClass.getOneOf().isEmpty()) { entityType = createEnumType(owlClass); } else { entityType = createBeanType(owlClass); } entityTypes.add(entityType); } } // serialize BeanSerializer beanSerializer = new BeanSerializer(); beanSerializer.setPrintSupertype(true); for (EntityType entityType : entityTypes) { File folder = new File(targetFolder, entityType.getPackageName().replace('.', '/')); folder.mkdirs(); File javaFile = new File(folder, entityType.getSimpleName() + ".java"); javaFile.createNewFile(); Writer writer = new OutputStreamWriter(new FileOutputStream(javaFile), "UTF-8"); try { beanSerializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); } finally { writer.close(); } } }
@Test public void Interfaces2() throws IOException { BeanSerializer serializer = new BeanSerializer(); serializer.addInterface(Serializable.class); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public class DomainClass implements Serializable {")); }
@Test public void Annotations() throws IOException{ type.addAnnotation(new QueryEntityImpl()); BeanSerializer serializer = new BeanSerializer(); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); String str = writer.toString(); assertTrue(str.contains("import com.mysema.query.annotations.QueryEntity;")); assertTrue(str.contains("@QueryEntity")); }
@Test public void Interfaces() throws IOException { BeanSerializer serializer = new BeanSerializer(); serializer.addInterface(new ClassType(Serializable.class)); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public class DomainClass implements Serializable {")); }
@Test public void Capitalization() throws IOException{ // property type.addProperty(new Property(type, "cId", type)); BeanSerializer serializer = new BeanSerializer(); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public DomainClass getcId() {")); }
@Test public void Annotated_Property() throws IOException{ Property property = new Property(type, "entityField", type); property.addAnnotation(new QueryEntityImpl()); type.addProperty(property); BeanSerializer serializer = new BeanSerializer(); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); String str = writer.toString(); assertTrue(str.contains("import com.mysema.query.annotations.QueryEntity;")); assertTrue(str.contains("@QueryEntity")); }
@Test public void Annotated_Property_Not_Serialized() throws IOException{ Property property = new Property(type, "entityField", type); property.addAnnotation(new QueryEntityImpl()); type.addProperty(property); BeanSerializer serializer = new BeanSerializer(false); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); String str = writer.toString(); assertFalse(str.contains("import com.mysema.query.annotations.QueryEntity;")); assertFalse(str.contains("@QueryEntity")); }
serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); String str = writer.toString();
@Test public void ToString() throws IOException{ // property type.addProperty(new Property(type, "entityField", type)); type.addProperty(new Property(type, "collection", new SimpleType(Types.COLLECTION, typeModel))); type.addProperty(new Property(type, "listField", new SimpleType(Types.LIST, typeModel))); type.addProperty(new Property(type, "setField", new SimpleType(Types.SET, typeModel))); type.addProperty(new Property(type, "arrayField", new ClassType(TypeCategory.ARRAY, String[].class))); type.addProperty(new Property(type, "mapField", new SimpleType(Types.MAP, typeModel, typeModel))); BeanSerializer serializer = new BeanSerializer(); serializer.setAddToString(true); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(String.valueOf(writer).contains( " @Override\n" + " public String toString()")); }
@Test public void FullConstructor() throws IOException{ // property type.addProperty(new Property(type, "entityField", type)); type.addProperty(new Property(type, "collection", new SimpleType(Types.COLLECTION, typeModel))); type.addProperty(new Property(type, "listField", new SimpleType(Types.LIST, typeModel))); type.addProperty(new Property(type, "setField", new SimpleType(Types.SET, typeModel))); type.addProperty(new Property(type, "arrayField", new ClassType(TypeCategory.ARRAY, String[].class))); type.addProperty(new Property(type, "mapField", new SimpleType(Types.MAP, typeModel, typeModel))); BeanSerializer serializer = new BeanSerializer(); serializer.setAddFullConstructor(true); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); //System.out.println(writer.toString()); }