Skip to content

Commit

Permalink
Better test for material loading, also ensured that the J3MExporter w…
Browse files Browse the repository at this point in the history
…rites UTF-8 files
  • Loading branch information
Nehon committed Mar 11, 2016
1 parent 3245c9a commit e0ffff3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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.
Expand Down Expand Up @@ -49,7 +50,7 @@ public void save(Savable object, OutputStream f) throws IOException {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -69,16 +98,18 @@ public void testWriteMat() {
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));
}

}
15 changes: 0 additions & 15 deletions jme3-plugins/src/test/resources/testMat.j3m

This file was deleted.

0 comments on commit e0ffff3

Please sign in to comment.