Skip to content

Commit e0ffff3

Browse files
committed
Better test for material loading, also ensured that the J3MExporter writes UTF-8 files
1 parent 3245c9a commit e0ffff3

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

jme3-plugins/src/main/java/com/jme3/material/plugin/export/material/J3MExporter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.IOException;
1717
import java.io.OutputStream;
1818
import java.io.OutputStreamWriter;
19+
import java.nio.charset.Charset;
1920

2021
/**
2122
* Saves a Material to a j3m file with proper formatting.
@@ -49,7 +50,7 @@ public void save(Savable object, OutputStream f) throws IOException {
4950
throw new IllegalArgumentException("J3MExporter can only save com.jme3.material.Material class");
5051
}
5152

52-
OutputStreamWriter out = new OutputStreamWriter(f);
53+
OutputStreamWriter out = new OutputStreamWriter(f, Charset.forName("UTF-8"));
5354

5455
rootCapsule.clear();
5556
object.write(this);

jme3-plugins/src/test/java/com/jme3/material/plugin/TestMaterialWrite.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,65 @@
3131
*/
3232
package com.jme3.material.plugin;
3333

34+
import com.jme3.asset.AssetInfo;
35+
import com.jme3.asset.AssetKey;
3436
import com.jme3.asset.AssetManager;
3537
import com.jme3.material.Material;
38+
import com.jme3.material.RenderState;
3639
import com.jme3.material.plugin.export.material.J3MExporter;
40+
import com.jme3.material.plugins.J3MLoader;
41+
import com.jme3.math.ColorRGBA;
3742
import com.jme3.system.JmeSystem;
3843
import static org.junit.Assert.*;
44+
45+
import com.jme3.texture.Texture;
46+
import com.jme3.texture.Texture2D;
3947
import org.junit.Before;
4048
import org.junit.Test;
4149

50+
import java.io.ByteArrayInputStream;
4251
import java.io.ByteArrayOutputStream;
4352
import java.io.IOException;
53+
import java.io.InputStream;
4454
import java.util.Scanner;
4555

4656
public class TestMaterialWrite {
4757

4858
private AssetManager assetManager;
49-
private Material mat;
5059

5160
@Before
5261
public void init() {
5362
assetManager = JmeSystem.newAssetManager(
5463
TestMaterialWrite.class.getResource("/com/jme3/asset/Desktop.cfg"));
5564

56-
mat = assetManager.loadMaterial("/testMat.j3m");
65+
5766
}
5867

5968

6069
@Test
61-
public void testWriteMat() {
62-
assertNotNull(mat);
63-
ByteArrayOutputStream stream = new ByteArrayOutputStream();
70+
public void testWriteMat() throws Exception {
71+
72+
Material mat = new Material(assetManager,"Common/MatDefs/Light/Lighting.j3md");
73+
74+
mat.setBoolean("UseMaterialColors", true);
75+
mat.setColor("Diffuse", ColorRGBA.White);
76+
mat.setColor("Ambient", ColorRGBA.DarkGray);
77+
mat.setFloat("AlphaDiscardThreshold", 0.5f);
78+
79+
mat.setFloat("Shininess", 2.5f);
80+
81+
Texture tex = assetManager.loadTexture("Common/Textures/MissingTexture.png");
82+
tex.setMagFilter(Texture.MagFilter.Nearest);
83+
tex.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
84+
tex.setWrap(Texture.WrapAxis.S, Texture.WrapMode.Repeat);
85+
tex.setWrap(Texture.WrapAxis.T, Texture.WrapMode.MirroredRepeat);
86+
87+
mat.setTexture("DiffuseMap", tex);
88+
mat.getAdditionalRenderState().setDepthWrite(false);
89+
mat.getAdditionalRenderState().setDepthTest(false);
90+
mat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
91+
92+
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
6493

6594
J3MExporter exporter = new J3MExporter();
6695
try {
@@ -69,16 +98,18 @@ public void testWriteMat() {
6998
e.printStackTrace();
7099
}
71100

72-
String reference = convertStreamToString(TestMaterialWrite.class.getResourceAsStream("/testMat.j3m"));
73-
// System.err.println(reference);
74-
// System.err.println(stream.toString());
101+
System.err.println(stream.toString());
75102

76-
// assertEquals(reference.replaceAll("[\\s\\r\\n]",""), stream.toString().replaceAll("[\\s\\r\\n]",""));
77-
}
103+
J3MLoader loader = new J3MLoader();
104+
AssetInfo info = new AssetInfo(assetManager, new AssetKey("test")) {
105+
@Override
106+
public InputStream openStream() {
107+
return new ByteArrayInputStream(stream.toByteArray());
108+
}
109+
};
110+
Material mat2 = (Material)loader.load(info);
78111

79-
private String convertStreamToString(java.io.InputStream is) {
80-
Scanner s = new Scanner(is).useDelimiter("\\A");
81-
return s.hasNext() ? s.next() : "";
112+
assertTrue(mat.contentEquals(mat2));
82113
}
83114

84115
}

jme3-plugins/src/test/resources/testMat.j3m

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)