Better test for material loading, also ensured that the J3MExporter writes UTF-8 files

cleanup_build_scripts
Nehon 9 years ago
parent 3245c9ac89
commit e0ffff3af0
  1. 3
      jme3-plugins/src/main/java/com/jme3/material/plugin/export/material/J3MExporter.java
  2. 57
      jme3-plugins/src/test/java/com/jme3/material/plugin/TestMaterialWrite.java
  3. 15
      jme3-plugins/src/test/resources/testMat.j3m

@ -16,6 +16,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
/** /**
* Saves a Material to a j3m file with proper formatting. * Saves a Material to a j3m file with proper formatting.
@ -49,7 +50,7 @@ public class J3MExporter implements JmeExporter {
throw new IllegalArgumentException("J3MExporter can only save com.jme3.material.Material class"); throw new IllegalArgumentException("J3MExporter can only save com.jme3.material.Material class");
} }
OutputStreamWriter out = new OutputStreamWriter(f); OutputStreamWriter out = new OutputStreamWriter(f, Charset.forName("UTF-8"));
rootCapsule.clear(); rootCapsule.clear();
object.write(this); object.write(this);

@ -31,36 +31,65 @@
*/ */
package com.jme3.material.plugin; package com.jme3.material.plugin;
import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetKey;
import com.jme3.asset.AssetManager; import com.jme3.asset.AssetManager;
import com.jme3.material.Material; import com.jme3.material.Material;
import com.jme3.material.RenderState;
import com.jme3.material.plugin.export.material.J3MExporter; import com.jme3.material.plugin.export.material.J3MExporter;
import com.jme3.material.plugins.J3MLoader;
import com.jme3.math.ColorRGBA;
import com.jme3.system.JmeSystem; import com.jme3.system.JmeSystem;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture2D;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner; import java.util.Scanner;
public class TestMaterialWrite { public class TestMaterialWrite {
private AssetManager assetManager; private AssetManager assetManager;
private Material mat;
@Before @Before
public void init() { public void init() {
assetManager = JmeSystem.newAssetManager( assetManager = JmeSystem.newAssetManager(
TestMaterialWrite.class.getResource("/com/jme3/asset/Desktop.cfg")); TestMaterialWrite.class.getResource("/com/jme3/asset/Desktop.cfg"));
mat = assetManager.loadMaterial("/testMat.j3m");
} }
@Test @Test
public void testWriteMat() { public void testWriteMat() throws Exception {
assertNotNull(mat);
ByteArrayOutputStream stream = new ByteArrayOutputStream(); Material mat = new Material(assetManager,"Common/MatDefs/Light/Lighting.j3md");
mat.setBoolean("UseMaterialColors", true);
mat.setColor("Diffuse", ColorRGBA.White);
mat.setColor("Ambient", ColorRGBA.DarkGray);
mat.setFloat("AlphaDiscardThreshold", 0.5f);
mat.setFloat("Shininess", 2.5f);
Texture tex = assetManager.loadTexture("Common/Textures/MissingTexture.png");
tex.setMagFilter(Texture.MagFilter.Nearest);
tex.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
tex.setWrap(Texture.WrapAxis.S, Texture.WrapMode.Repeat);
tex.setWrap(Texture.WrapAxis.T, Texture.WrapMode.MirroredRepeat);
mat.setTexture("DiffuseMap", tex);
mat.getAdditionalRenderState().setDepthWrite(false);
mat.getAdditionalRenderState().setDepthTest(false);
mat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
J3MExporter exporter = new J3MExporter(); J3MExporter exporter = new J3MExporter();
try { try {
@ -69,16 +98,18 @@ public class TestMaterialWrite {
e.printStackTrace(); e.printStackTrace();
} }
String reference = convertStreamToString(TestMaterialWrite.class.getResourceAsStream("/testMat.j3m")); System.err.println(stream.toString());
// System.err.println(reference);
// System.err.println(stream.toString());
// assertEquals(reference.replaceAll("[\\s\\r\\n]",""), stream.toString().replaceAll("[\\s\\r\\n]","")); J3MLoader loader = new J3MLoader();
} AssetInfo info = new AssetInfo(assetManager, new AssetKey("test")) {
@Override
public InputStream openStream() {
return new ByteArrayInputStream(stream.toByteArray());
}
};
Material mat2 = (Material)loader.load(info);
private String convertStreamToString(java.io.InputStream is) { assertTrue(mat.contentEquals(mat2));
Scanner s = new Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
} }
} }

@ -1,15 +0,0 @@
Material test : Common/MatDefs/Light/Lighting.j3md {
MaterialParameters {
Diffuse : 1.0 1.0 1.0 1.0
UseMaterialColors : true
ParallaxHeight : 0.05
Ambient : 1.0 1.0 1.0 1.0
DiffuseMap : Flip WrapRepeat_S WrapRepeat_T MinNearestNoMipMaps MagNearest "Textures/ColoredTex/Monkey.png"
Specular : 0.01375 0.01375 0.01375 1.0
Shininess : 50.0
}
AdditionalRenderState {
}
}
Loading…
Cancel
Save