PropertiesResourceImpl extends ResourceImpl { @Override protected void doSave(OutputStream outputStream, Map<?, ?> options) throws IOException { savedProperties = 0; String type = getPropertyType(options); PropertyScanner scanner = PropertyResourceUtil.createScanner(type); String encoding = scanner.getEncoding(); PropertyConverter converter = scanner.createConverter(getURI()); try{ PropertyFile file = (PropertyFile) getContents().get(0); savedProperties = converter.write(outputStream, file,encoding); } finally{ outputStream.close(); } } PropertiesResourceImpl(URI uri); @Override void load(Map<?, ?> options); @Override void save(Map<?, ?> options); int getSavedProperties(); static final String OPTION_FILEMODE; static final String OPTION_ENCODING; }
@Test public void testWriteLicenseHeaderAndComment() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); PropertyFile file = PropertiesFactory.eINSTANCE.createPropertyFile(); file.setLicenseHeader("apache license\nv3"); Property property = PropertiesFactory.eINSTANCE.createProperty(); property.setKey("key"); property.setValue("value"); property.setComment("comment"); file.getProperties().add(property); getFixture().getContents().add(file); getFixture().doSave(out, null); String result = out.toString("UTF-8"); assertEquals("#apache license\n#v3\n\n#comment\nkey = value\n", result); }
@Test public void testWriteBOMForUnicodeMode() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); PropertyFile file = PropertiesFactory.eINSTANCE.createPropertyFile(); Property property = PropertiesFactory.eINSTANCE.createProperty(); property.setKey("key"); property.setValue("value"); property.setComment("comment"); file.getProperties().add(property); getFixture().getContents().add(file); Map<String, Object> options = new HashMap<String, Object>(); options.put(PropertiesResourceImpl.OPTION_FILEMODE, JavaPropertyScannerUTF8.TYPE); getFixture().doSave(out, options); ByteOrderMark mark = ByteOrderMark.read(new ByteArrayInputStream(out.toByteArray())); assertEquals("Must write a BOM in unicode mode", ByteOrderMark.UTF_8, mark); }
@Test public void testDoNotWriteBOMForIsoMode() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); PropertyFile file = PropertiesFactory.eINSTANCE.createPropertyFile(); Property property = PropertiesFactory.eINSTANCE.createProperty(); property.setKey("key"); property.setValue("value"); property.setComment("comment"); file.getProperties().add(property); getFixture().getContents().add(file); Map<String, Object> options = new HashMap<String, Object>(); options.put(PropertiesResourceImpl.OPTION_FILEMODE, JavaPropertyScanner.TYPE); getFixture().doSave(out, options); ByteOrderMark mark = ByteOrderMark.read(new ByteArrayInputStream(out.toByteArray())); assertNull("Must not write a BOM in iso mode", mark); }