Skip to content

Commit 87928ef

Browse files
committed
FORGE-2202: Added JavaResource.setContents(InputStream,Properties)
1 parent c6a1c73 commit 87928ef

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

parser-java/api/src/main/java/org/jboss/forge/addon/parser/java/resources/JavaResource.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
package org.jboss.forge.addon.parser.java.resources;
99

1010
import java.io.FileNotFoundException;
11+
import java.io.InputStream;
12+
import java.util.Properties;
1113

1214
import org.jboss.forge.addon.resource.FileResource;
1315
import org.jboss.forge.addon.resource.Resource;
@@ -40,4 +42,9 @@ public interface JavaResource extends FileResource<JavaResource>
4042
*/
4143
<T extends JavaType<?>> T getJavaType() throws FileNotFoundException;
4244

45+
/**
46+
* Sets the contents using the given formatter properties
47+
*/
48+
JavaResource setContents(InputStream data, Properties formatterProperties);
49+
4350
}

parser-java/impl/src/main/java/org/jboss/forge/addon/parser/java/resources/JavaResourceImpl.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
/**
4545
* @author Mike Brock
4646
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
47+
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
4748
*/
4849
public class JavaResourceImpl extends AbstractFileResource<JavaResource> implements JavaResource
4950
{
@@ -212,23 +213,33 @@ public boolean supports(ResourceFacet type)
212213
@Override
213214
public JavaResource setContents(final InputStream data)
214215
{
215-
InputStream contents = data;
216216
if (formatterProfilePath != null)
217217
{
218218
try (FileInputStream fis = new FileInputStream(formatterProfilePath))
219219
{
220220
FormatterProfileReader reader = FormatterProfileReader.fromEclipseXml(fis);
221-
Properties formatterProfile = reader.getProfileNames().contains(formatterProfileName) ? reader
221+
Properties formatterProfileProperties = reader.getProfileNames().contains(formatterProfileName) ? reader
222222
.getPropertiesFor(formatterProfileName) : reader.getDefaultProperties();
223-
String formattedSource = Roaster.format(formatterProfile, Streams.toString(data));
224-
contents = new ByteArrayInputStream(formattedSource.getBytes());
223+
return setContents(data, formatterProfileProperties);
225224
}
226225
catch (IOException e)
227226
{
228227
Logger.getLogger(getClass().getName()).log(Level.FINE, "Error while reading formatter path", e);
229228
}
230229
}
231-
return super.setContents(contents);
230+
return super.setContents(data);
232231
}
233232

233+
@Override
234+
public JavaResource setContents(InputStream data, Properties formatterProperties)
235+
{
236+
InputStream contents = data;
237+
if (formatterProperties != null)
238+
{
239+
String source = Streams.toString(data);
240+
String formattedSource = Roaster.format(formatterProperties, source);
241+
contents = new ByteArrayInputStream(formattedSource.getBytes());
242+
}
243+
return super.setContents(contents);
244+
}
234245
}

parser-java/tests/src/test/java/org/jboss/forge/addon/parser/java/resources/JavaResourceTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
import static org.hamcrest.CoreMatchers.instanceOf;
1111

12+
import java.io.ByteArrayInputStream;
1213
import java.io.File;
1314
import java.io.InputStream;
1415
import java.nio.file.Files;
1516
import java.nio.file.StandardCopyOption;
17+
import java.util.Properties;
1618

1719
import javax.inject.Inject;
1820

@@ -30,6 +32,7 @@
3032
import org.jboss.forge.roaster.model.JavaType;
3133
import org.jboss.forge.roaster.model.source.JavaClassSource;
3234
import org.jboss.forge.roaster.model.source.JavaSource;
35+
import org.jboss.forge.roaster.model.util.FormatterProfileReader;
3336
import org.jboss.shrinkwrap.api.ShrinkWrap;
3437
import org.jboss.shrinkwrap.api.asset.FileAsset;
3538
import org.junit.Assert;
@@ -105,8 +108,9 @@ public void testParserClass() throws Exception
105108
}
106109

107110
@Test
108-
public void testCustomClassFormatting() throws Exception
111+
public void testDefaultClassFormatting() throws Exception
109112
{
113+
configuration.clearProperty(JavaResource.FORMATTER_PROFILE_PATH_KEY);
110114
String forgeFormatterContents = Streams.toString(getClass().getResourceAsStream("formatter_forge.jv"));
111115
String eclipseFormatterContents = Streams.toString(getClass().getResourceAsStream("formatter_eclipse.jv"));
112116
File tmpFile = File.createTempFile("MyClass", ".java");
@@ -124,4 +128,32 @@ public void testCustomClassFormatting() throws Exception
124128
resource.setContents(forgeFormatterContents);
125129
Assert.assertEquals(eclipseFormatterContents, resource.getContents());
126130
}
131+
132+
@Test
133+
public void testCustomClassFormattingProperties() throws Exception
134+
{
135+
configuration.clearProperty(JavaResource.FORMATTER_PROFILE_PATH_KEY);
136+
String forgeFormatterContents = Streams.toString(getClass().getResourceAsStream("formatter_forge.jv"));
137+
File tmpFile = File.createTempFile("MyClass", ".java");
138+
tmpFile.deleteOnExit();
139+
JavaResource resource = resourceFactory.create(JavaResource.class, tmpFile);
140+
resource.setContents(forgeFormatterContents);
141+
Assert.assertEquals(forgeFormatterContents, resource.getContents());
142+
File profileFile = File.createTempFile("profile", ".xml");
143+
try (InputStream is = getClass().getResourceAsStream("eclipse_profile.xml"))
144+
{
145+
Files.copy(is, profileFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
146+
}
147+
Properties properties = null;
148+
try (InputStream is = getClass().getResourceAsStream("forge_profile.xml"))
149+
{
150+
FormatterProfileReader reader = FormatterProfileReader.fromEclipseXml(is);
151+
properties = reader.getPropertiesFor("Forge");
152+
}
153+
configuration.setProperty(JavaResource.FORMATTER_PROFILE_PATH_KEY, profileFile.getAbsolutePath());
154+
resource = resourceFactory.create(JavaResource.class, tmpFile);
155+
resource.setContents(new ByteArrayInputStream(forgeFormatterContents.getBytes()), properties);
156+
Assert.assertEquals(forgeFormatterContents, resource.getContents());
157+
}
158+
127159
}

0 commit comments

Comments
 (0)