From e0ffff3af00145185e62d226cbb265c3867ef5e2 Mon Sep 17 00:00:00 2001 From: Nehon Date: Fri, 11 Mar 2016 21:27:46 +0100 Subject: [PATCH] Better test for material loading, also ensured that the J3MExporter writes UTF-8 files --- .../plugin/export/material/J3MExporter.java | 3 +- .../material/plugin/TestMaterialWrite.java | 57 ++++++++++++++----- jme3-plugins/src/test/resources/testMat.j3m | 15 ----- 3 files changed, 46 insertions(+), 29 deletions(-) delete mode 100644 jme3-plugins/src/test/resources/testMat.j3m diff --git a/jme3-plugins/src/main/java/com/jme3/material/plugin/export/material/J3MExporter.java b/jme3-plugins/src/main/java/com/jme3/material/plugin/export/material/J3MExporter.java index 285489a05..0535c4582 100644 --- a/jme3-plugins/src/main/java/com/jme3/material/plugin/export/material/J3MExporter.java +++ b/jme3-plugins/src/main/java/com/jme3/material/plugin/export/material/J3MExporter.java @@ -16,6 +16,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.nio.charset.Charset; /** * 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"); } - OutputStreamWriter out = new OutputStreamWriter(f); + OutputStreamWriter out = new OutputStreamWriter(f, Charset.forName("UTF-8")); rootCapsule.clear(); object.write(this); diff --git a/jme3-plugins/src/test/java/com/jme3/material/plugin/TestMaterialWrite.java b/jme3-plugins/src/test/java/com/jme3/material/plugin/TestMaterialWrite.java index 764e69b18..584b16f84 100644 --- a/jme3-plugins/src/test/java/com/jme3/material/plugin/TestMaterialWrite.java +++ b/jme3-plugins/src/test/java/com/jme3/material/plugin/TestMaterialWrite.java @@ -31,36 +31,65 @@ */ package com.jme3.material.plugin; +import com.jme3.asset.AssetInfo; +import com.jme3.asset.AssetKey; import com.jme3.asset.AssetManager; import com.jme3.material.Material; +import com.jme3.material.RenderState; 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 static org.junit.Assert.*; + +import com.jme3.texture.Texture; +import com.jme3.texture.Texture2D; import org.junit.Before; import org.junit.Test; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.Scanner; public class TestMaterialWrite { private AssetManager assetManager; - private Material mat; @Before public void init() { assetManager = JmeSystem.newAssetManager( TestMaterialWrite.class.getResource("/com/jme3/asset/Desktop.cfg")); - mat = assetManager.loadMaterial("/testMat.j3m"); + } @Test - public void testWriteMat() { - assertNotNull(mat); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); + public void testWriteMat() throws Exception { + + 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(); try { @@ -69,16 +98,18 @@ public class TestMaterialWrite { e.printStackTrace(); } - String reference = convertStreamToString(TestMaterialWrite.class.getResourceAsStream("/testMat.j3m")); -// System.err.println(reference); -// System.err.println(stream.toString()); + 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) { - Scanner s = new Scanner(is).useDelimiter("\\A"); - return s.hasNext() ? s.next() : ""; + assertTrue(mat.contentEquals(mat2)); } } diff --git a/jme3-plugins/src/test/resources/testMat.j3m b/jme3-plugins/src/test/resources/testMat.j3m deleted file mode 100644 index bcb9b36d0..000000000 --- a/jme3-plugins/src/test/resources/testMat.j3m +++ /dev/null @@ -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 { - } -} \ No newline at end of file