Browse Source

Added unit test for J3MLoader to cover the new texture parameters available in #295. Also fixed a couple of issues in the code to reduce logging that was not needed and removed redundant code. This update also updates junit to 4.12 and adds Mockito and Fest Assertions as test dependencies.

Daniel Johansson 10 years ago
parent
commit
d319a7c5d3

+ 3 - 1
common.gradle

@@ -21,7 +21,9 @@ repositories {
 
 dependencies {
     // Adding dependencies here will add the dependencies to each subproject.
-    testCompile group: 'junit', name: 'junit', version: '4.10'
+    testCompile group: 'junit', name: 'junit', version: '4.12'
+    testCompile group: 'org.mockito', name: 'mockito-core', version: '2.0.28-beta'
+    testCompile group: 'org.easytesting', name: 'fest-assert-core', version: '2.0M10'
 }
 
 javadoc {

+ 5 - 0
jme3-core/build.gradle

@@ -10,6 +10,11 @@ sourceSets {
             srcDir 'src/tools/java'
         }
     }
+    test {
+        java {
+            srcDir 'src/test/java'
+        }
+    }
 }
 
 buildscript {

+ 5 - 12
jme3-core/src/plugins/java/com/jme3/material/plugins/J3MLoader.java

@@ -158,7 +158,7 @@ public class J3MLoader implements AssetLoader {
             final String value = values.get(i);
             final TextureOption textureOption = TextureOption.getTextureOption(value);
 
-            if (textureOption == null && !value.contains("\\") && !value.contains("/")) {
+            if (textureOption == null && !value.contains("\\") && !value.contains("/") && !values.get(0).equals("Flip") && !values.get(0).equals("Repeat")) {
                 logger.log(Level.WARNING, "Unknown texture option \"{0}\" encountered for \"{1}\" in material \"{2}\"", new Object[]{value, key, material.getKey().getName()});
             } else if (textureOption != null){
                 final String option = textureOption.getOptionValue(value);
@@ -179,27 +179,25 @@ public class J3MLoader implements AssetLoader {
         final List<TextureOptionValue> textureOptionValues = parseTextureOptions(textureValues);
 
         TextureKey textureKey = null;
-        boolean repeat = false;
 
         // If there is only one token on the value, it must be the path to the texture.
         if (textureValues.size() == 1) {
-            textureKey = new TextureKey(textureValues.get(0));
+            textureKey = new TextureKey(textureValues.get(0), false);
         } else {
             String texturePath = value.trim();
-            boolean flipY = false;
 
             // If there are no valid "new" texture options specified but the path is split into several parts, lets parse the old way.
             if (isTexturePathDeclaredTheTraditionalWay(textureValues.size(), textureOptionValues.size(), texturePath)) {
+                boolean flipY = false;
+
                 if (texturePath.startsWith("Flip Repeat ") || texturePath.startsWith("Repeat Flip ")) {
                     texturePath = texturePath.substring(12).trim();
                     flipY = true;
-                    repeat = true;
                 } else if (texturePath.startsWith("Flip ")) {
                     texturePath = texturePath.substring(5).trim();
                     flipY = true;
                 } else if (texturePath.startsWith("Repeat ")) {
                     texturePath = texturePath.substring(7).trim();
-                    repeat = true;
                 }
 
                 // Support path starting with quotes (double and single)
@@ -216,7 +214,7 @@ public class J3MLoader implements AssetLoader {
             }
 
             if (textureKey == null) {
-                textureKey = new TextureKey(textureValues.get(textureValues.size() - 1));
+                textureKey = new TextureKey(textureValues.get(textureValues.size() - 1), false);
             }
 
             // Apply texture options to the texture key
@@ -256,11 +254,6 @@ public class J3MLoader implements AssetLoader {
             texture.setName(textureKey.getName());
         }
 
-        // This is here for backwards compatibility, we need to do this after the texture has been instantiated.
-        if (repeat) {
-            texture.setWrap(Texture.WrapMode.Repeat);
-        }
-
         // Apply texture options to the texture
         if (!textureOptionValues.isEmpty()) {
             for (final TextureOptionValue textureOptionValue : textureOptionValues) {

+ 117 - 0
jme3-core/src/test/java/com/jme3/material/plugins/J3MLoaderTest.java

@@ -0,0 +1,117 @@
+package com.jme3.material.plugins;
+
+import com.jme3.asset.AssetInfo;
+import com.jme3.asset.AssetKey;
+import com.jme3.asset.AssetManager;
+import com.jme3.asset.TextureKey;
+import com.jme3.material.MatParamTexture;
+import com.jme3.material.Material;
+import com.jme3.material.MaterialDef;
+import com.jme3.shader.VarType;
+import com.jme3.texture.Texture;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * @author Daniel Johansson
+ * @since 2015-07-20
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class J3MLoaderTest {
+
+    private J3MLoader j3MLoader;
+
+    @Mock
+    private AssetInfo assetInfo;
+
+    @Mock
+    private AssetManager assetManager;
+
+    @Mock
+    private AssetKey<Material> assetKey;
+
+    @Mock
+    private MaterialDef materialDef;
+
+    @Before
+    public void setUp() throws Exception {
+        when(assetKey.getExtension()).thenReturn(".j3m");
+        when(assetInfo.getManager()).thenReturn(assetManager);
+        when(assetInfo.getKey()).thenReturn(assetKey);
+        when(assetManager.loadAsset(any(AssetKey.class))).thenReturn(materialDef);
+
+        j3MLoader = new J3MLoader();
+    }
+
+    @Test
+    public void oldStyleTextureParameters_shouldBeSupported() throws Exception {
+        when(assetInfo.openStream()).thenReturn(J3MLoader.class.getResourceAsStream("/texture-parameters-oldstyle.j3m"));
+
+        final Texture textureOldStyle = Mockito.mock(Texture.class);
+        final Texture textureOldStyleUsingQuotes = Mockito.mock(Texture.class);
+
+        final TextureKey textureKeyUsingQuotes = setupMockForTexture("OldStyleUsingQuotes", "old style using quotes/texture.png", true, textureOldStyleUsingQuotes);
+        final TextureKey textureKeyOldStyle = setupMockForTexture("OldStyle", "old style/texture.png", true, textureOldStyle);
+
+        j3MLoader.load(assetInfo);
+
+        verify(assetManager).loadTexture(textureKeyUsingQuotes);
+        verify(assetManager).loadTexture(textureKeyOldStyle);
+        verify(textureOldStyle).setWrap(Texture.WrapMode.Repeat);
+        verify(textureOldStyleUsingQuotes).setWrap(Texture.WrapMode.Repeat);
+    }
+
+    @Test
+    public void newStyleTextureParameters_shouldBeSupported() throws Exception {
+        when(assetInfo.openStream()).thenReturn(J3MLoader.class.getResourceAsStream("/texture-parameters-newstyle.j3m"));
+
+        final Texture textureNoParameters = Mockito.mock(Texture.class);
+        final Texture textureFlip = Mockito.mock(Texture.class);
+        final Texture textureRepeat = Mockito.mock(Texture.class);
+        final Texture textureRepeatAxis = Mockito.mock(Texture.class);
+        final Texture textureMin = Mockito.mock(Texture.class);
+        final Texture textureMag = Mockito.mock(Texture.class);
+        final Texture textureCombined = Mockito.mock(Texture.class);
+
+        final TextureKey textureKeyNoParameters = setupMockForTexture("Empty", "empty.png", false, textureNoParameters);
+        final TextureKey textureKeyFlip = setupMockForTexture("Flip", "flip.png", true, textureFlip);
+        setupMockForTexture("Repeat", "repeat.png", false, textureRepeat);
+        setupMockForTexture("RepeatAxis", "repeat-axis.png", false, textureRepeatAxis);
+        setupMockForTexture("Min", "min.png", false, textureMin);
+        setupMockForTexture("Mag", "mag.png", false, textureMag);
+        setupMockForTexture("Combined", "combined.png", true, textureCombined);
+
+        j3MLoader.load(assetInfo);
+
+        verify(assetManager).loadTexture(textureKeyNoParameters);
+        verify(assetManager).loadTexture(textureKeyFlip);
+
+        verify(textureRepeat).setWrap(Texture.WrapMode.Repeat);
+        verify(textureRepeatAxis).setWrap(Texture.WrapAxis.T, Texture.WrapMode.Repeat);
+        verify(textureMin).setMinFilter(Texture.MinFilter.Trilinear);
+        verify(textureMag).setMagFilter(Texture.MagFilter.Bilinear);
+
+        verify(textureCombined).setMagFilter(Texture.MagFilter.Nearest);
+        verify(textureCombined).setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
+        verify(textureCombined).setWrap(Texture.WrapMode.Repeat);
+    }
+
+    private TextureKey setupMockForTexture(final String paramName, final String path, final boolean flipY, final Texture texture) {
+        when(materialDef.getMaterialParam(paramName)).thenReturn(new MatParamTexture(VarType.Texture2D, paramName, texture, 0, null));
+
+        final TextureKey textureKey = new TextureKey(path, flipY);
+        textureKey.setGenerateMips(true);
+
+        when(assetManager.loadTexture(textureKey)).thenReturn(texture);
+
+        return textureKey;
+    }
+}

+ 11 - 0
jme3-core/src/test/resources/texture-parameters-newstyle.j3m

@@ -0,0 +1,11 @@
+Material Test : matdef.j3md {
+     MaterialParameters {
+         Empty: "empty.png"
+         Flip: Flip "flip.png"
+         Repeat: WrapRepeat "repeat.png"
+         Min: MinTrilinear "min.png"
+         Mag: MagBilinear "mag.png"
+         RepeatAxis: WrapRepeat_T "repeat-axis.png"
+         Combined: MagNearest MinBilinearNoMipMaps Flip WrapRepeat "combined.png"
+     }
+}

+ 6 - 0
jme3-core/src/test/resources/texture-parameters-oldstyle.j3m

@@ -0,0 +1,6 @@
+Material Test : matdef.j3md {
+     MaterialParameters {
+         OldStyle: Flip Repeat old style/texture.png
+         OldStyleUsingQuotes: Repeat Flip "old style using quotes/texture.png"
+     }
+}