Skip to content

Commit

Permalink
Added unit tests for Format and isCompatible()
Browse files Browse the repository at this point in the history
  • Loading branch information
Raptor399 authored and StreamHD committed Jan 15, 2012
1 parent d2b93a2 commit 0474fcb
Show file tree
Hide file tree
Showing 3 changed files with 370 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/main/java/net/pms/configuration/RendererConfiguration.java
Expand Up @@ -169,6 +169,28 @@ public static RendererConfiguration getRendererConfigurationByUAAHH(String heade
}
return null;
}

/**
* Tries to find a matching renderer configuration based on the name of
* the renderer. Returns true if the provided name is equal to or a
* substring of the renderer name defined in a configuration, where case
* does not matter.
*
* @param name The renderer name to match.
* @return The matching renderer configuration or <code>null</code>
*
* @since 1.50.1
*/
public static RendererConfiguration getRendererConfigurationByName(String name) {
for (RendererConfiguration conf : renderersConfs) {
if (conf.getRendererName().toLowerCase().contains(name.toLowerCase())) {
return conf;
}
}

return null;
}

private final PropertiesConfiguration configuration;
private FormatConfiguration formatConfiguration;

Expand Down Expand Up @@ -851,8 +873,14 @@ public boolean isCompatible(DLNAMediaInfo mediainfo, Format format) {
}

if (format != null) {
String noTranscode = "";

if (PMS.getConfiguration() != null) {
noTranscode = PMS.getConfiguration().getNoTranscode();
}

// Is the format among the ones to be streamed?
return format.skip(PMS.getConfiguration().getNoTranscode(), getStreamedExtensions());
return format.skip(noTranscode, getStreamedExtensions());
} else {
// Not natively supported.
return false;
Expand Down
231 changes: 231 additions & 0 deletions src/test/java/net/pms/test/formats/FormatRecognitionTest.java
@@ -0,0 +1,231 @@
/*
* PS3 Media Server, for streaming any medias to your PS3.
* Copyright (C) 2008 A.Brochard
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package net.pms.test.formats;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;

import net.pms.configuration.RendererConfiguration;
import net.pms.dlna.DLNAMediaAudio;
import net.pms.dlna.DLNAMediaInfo;
import net.pms.formats.DVRMS;
import net.pms.formats.FLAC;
import net.pms.formats.Format;
import net.pms.formats.GIF;
import net.pms.formats.ISO;
import net.pms.formats.JPG;
import net.pms.formats.M4A;
import net.pms.formats.MKV;
import net.pms.formats.MP3;
import net.pms.formats.MPG;
import net.pms.formats.OGG;
import net.pms.formats.PNG;
import net.pms.formats.RAW;
import net.pms.formats.TIF;
import net.pms.formats.WAV;
import net.pms.formats.WEB;
import net.pms.network.HTTPResource;

import org.junit.Before;
import org.junit.Test;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;


/**
* Test the recognition of formats.
*/
public class FormatRecognitionTest {
@Before
public void setUp() {
// Silence all log messages from the PMS code that is being tested
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
context.reset();

// Initialize the RendererConfiguration
RendererConfiguration.loadRendererConfigurations();
}

/**
* Test some basic functionality of {@link RendererConfiguration#isCompatible(DLNAMediaInfo, Format)}
*/
@Test
public void testRendererConfigurationBasics() {
RendererConfiguration conf = RendererConfiguration.getRendererConfigurationByName("Playstation 3");
assertNotNull("No renderer named \"Playstation 3\" found.", conf);
assertEquals("With nothing provided isCompatible() should return false", false,
conf.isCompatible(null, null));
}

/**
* Test the compatibility of the Playstation 3 with the GIF format.
*/
@Test
public void testPlaystationImageGifCompatibility() {
RendererConfiguration conf = RendererConfiguration.getRendererConfigurationByName("Playstation 3");
assertNotNull("No renderer named \"Playstation 3\" found.", conf);

// Construct GIF information
DLNAMediaInfo info = new DLNAMediaInfo();
info.setContainer("gif");
Format format = new GIF();
format.match("test.gif");
assertEquals("PS3 is reported to be incompatible with GIF", true,
conf.isCompatible(info, format));
}

/**
* Test the compatibility of the Playstation 3 with the JPG format.
*/
@Test
public void testPlaystationImageJpgCompatibility() {
RendererConfiguration conf = RendererConfiguration.getRendererConfigurationByName("Playstation 3");
assertNotNull("No renderer named \"Playstation 3\" found.", conf);

// Construct JPG information
DLNAMediaInfo info = new DLNAMediaInfo();
info.setContainer("jpg");
Format format = new JPG();
format.match("test.jpeg");
assertEquals("PS3 is reported to be incompatible with JPG", true,
conf.isCompatible(info, format));
}


/**
* Test the compatibility of the Playstation 3 with the PNG format.
*/
@Test
public void testPlaystationImagePngCompatibility() {
RendererConfiguration conf = RendererConfiguration.getRendererConfigurationByName("Playstation 3");
assertNotNull("No renderer named \"Playstation 3\" found.", conf);

// Construct JPG information
DLNAMediaInfo info = new DLNAMediaInfo();
info.setContainer("png");
Format format = new PNG();
format.match("test.png");
assertEquals("PS3 is reported to be incompatible with PNG", true,
conf.isCompatible(info, format));
}


/**
* Test the compatibility of the Playstation 3 with the TIFF format.
*/
@Test
public void testPlaystationImageTiffCompatibility() {
RendererConfiguration conf = RendererConfiguration.getRendererConfigurationByName("Playstation 3");
assertNotNull("No renderer named \"Playstation 3\" found.", conf);

// Construct JPG information
DLNAMediaInfo info = new DLNAMediaInfo();
info.setContainer("tiff");
Format format = new TIF();
format.match("test.tiff");
assertEquals("PS3 is reported to be incompatible with TIFF", true,
conf.isCompatible(info, format));
}

/**
* Test the compatibility of the Playstation 3 with the MP3 format.
*/
@Test
public void testPlaystationAudioMp3Compatibility() {
RendererConfiguration conf = RendererConfiguration.getRendererConfigurationByName("Playstation 3");
assertNotNull("No renderer named \"Playstation 3\" found.", conf);

// Construct regular two channel MP3 information
DLNAMediaInfo info = new DLNAMediaInfo();
info.setContainer("mp3");
info.setMimeType(HTTPResource.AUDIO_MP3_TYPEMIME);
DLNAMediaAudio audio = new DLNAMediaAudio();
audio.setNrAudioChannels(2);
ArrayList<DLNAMediaAudio> audioCodes = new ArrayList<DLNAMediaAudio>();
audioCodes.add(audio);
info.setAudioCodes(audioCodes);
Format format = new MP3();
format.match("test.mp3");
assertEquals("PS3 is reported to be incompatible with MP3", true,
conf.isCompatible(info, format));

// Construct five channel MP3 that the PS3 does not support natively
audio.setNrAudioChannels(5);
assertEquals("PS3 is reported to be incompatible with MP3", false,
conf.isCompatible(info, format));
}

/**
* Test the compatibility of the Playstation 3 with the MPG format.
*/
@Test
public void testPlaystationVideoMpgCompatibility() {
RendererConfiguration conf = RendererConfiguration.getRendererConfigurationByName("Playstation 3");
assertNotNull("No renderer named \"Playstation 3\" found.", conf);

// Construct regular two channel MPG information
DLNAMediaInfo info = new DLNAMediaInfo();
info.setContainer("avi");
DLNAMediaAudio audio = new DLNAMediaAudio();
audio.setCodecA("ac3");
audio.setNrAudioChannels(5);
ArrayList<DLNAMediaAudio> audioCodes = new ArrayList<DLNAMediaAudio>();
audioCodes.add(audio);
info.setAudioCodes(audioCodes);
info.setCodecV("mp4");
Format format = new MPG();
format.match("test.avi");
assertEquals("PS3 is reported to be incompatible with MPG", true,
conf.isCompatible(info, format));

// Construct MPG with wmv codec that the PS3 does not support natively
info.setCodecV("wmv");
assertEquals("PS3 is reported to be compatible with MPG with wmv codec", false,
conf.isCompatible(info, format));
}

/**
* Test the compatibility of the Playstation 3 with the MPG format.
*/
@Test
public void testPlaystationVideoMkvCompatibility() {
RendererConfiguration conf = RendererConfiguration.getRendererConfigurationByName("Playstation 3");
assertNotNull("No renderer named \"Playstation 3\" found.", conf);

// Construct MKV information
DLNAMediaInfo info = new DLNAMediaInfo();
info.setContainer("mkv");
DLNAMediaAudio audio = new DLNAMediaAudio();
audio.setCodecA("ac3");
audio.setNrAudioChannels(5);
ArrayList<DLNAMediaAudio> audioCodes = new ArrayList<DLNAMediaAudio>();
audioCodes.add(audio);
info.setAudioCodes(audioCodes);
info.setCodecV("mp4");
Format format = new MPG();
format.match("test.mkv");
assertEquals("PS3 is reported to be incompatible with MKV", false,
conf.isCompatible(info, format));
}

}
110 changes: 110 additions & 0 deletions src/test/java/net/pms/test/formats/FormatTest.java
@@ -0,0 +1,110 @@
/*
* PS3 Media Server, for streaming any medias to your PS3.
* Copyright (C) 2008 A.Brochard
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package net.pms.test.formats;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;

import net.pms.configuration.RendererConfiguration;
import net.pms.dlna.DLNAMediaAudio;
import net.pms.dlna.DLNAMediaInfo;
import net.pms.formats.DVRMS;
import net.pms.formats.FLAC;
import net.pms.formats.Format;
import net.pms.formats.GIF;
import net.pms.formats.ISO;
import net.pms.formats.JPG;
import net.pms.formats.M4A;
import net.pms.formats.MKV;
import net.pms.formats.MP3;
import net.pms.formats.MPG;
import net.pms.formats.OGG;
import net.pms.formats.PNG;
import net.pms.formats.RAW;
import net.pms.formats.TIF;
import net.pms.formats.WAV;
import net.pms.formats.WEB;
import net.pms.network.HTTPResource;

import org.junit.Before;
import org.junit.Test;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;


/**
* Test basic functionality of {@link Format}.
*/
public class FormatTest {
@Before
public void setUp() {
// Silence all log messages from the PMS code that is being tested
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
context.reset();
}

/**
* Test edge cases for {@link Format#match(String)}.
*/
@Test
public void testFormatEdgeCases() {
// Empty string
assertEquals("MP3 matches \"\"", false, new MP3().match(""));

// Null string
assertEquals("MP3 matches null", false, new MP3().match(null));

// Mixed case
assertEquals("TIFF does not match \"tEsT.TiFf\"", true, new TIF().match("tEsT.TiFf"));

// Starting with identifier instead of ending
assertEquals("TIFF matches \"tiff.test\"", false, new TIF().match("tiff.test"));

// Substring
assertEquals("TIFF matches \"not.tiff.but.mp3\"", false, new TIF().match("not.tiff.but.mp3"));
}

/**
* Test if {@link Format#match(String)} manages to match the identifiers
* specified in each format with getId().
*/
@Test
public void testFormatIdentifiers() {
// Identifier tests based on the identifiers defined in getId() of each class
assertEquals("DVRMS does not match \"test.dvr\"", true, new DVRMS().match("test.dvr"));
assertEquals("FLAC does not match \"test.flac\"", true, new FLAC().match("test.flac"));
assertEquals("GIF does not match \"test.gif\"", true, new GIF().match("test.gif"));
assertEquals("ISO does not match \"test.iso\"", true, new ISO().match("test.iso"));
assertEquals("JPG does not match \"test.jpg\"", true, new JPG().match("test.jpg"));
assertEquals("M4A does not match \"test.wma\"", true, new M4A().match("test.wma"));
assertEquals("MKV does not match \"test.mkv\"", true, new MKV().match("test.mkv"));
assertEquals("MP3 does not match \"test.mp3\"", true, new MP3().match("test.mp3"));
assertEquals("MPG does not match \"test.mpg\"", true, new MPG().match("test.mpg"));
assertEquals("OGG does not match \"test.ogg\"", true, new OGG().match("test.ogg"));
assertEquals("PNG does not match \"test.png\"", true, new PNG().match("test.png"));
assertEquals("RAW does not match \"test.arw\"", true, new RAW().match("test.arw"));
assertEquals("TIF does not match \"test.tiff\"", true, new TIF().match("test.tiff"));
assertEquals("WAV does not match \"test.wav\"", true, new WAV().match("test.wav"));
assertEquals("WEB does not match \"http\"", true, new WEB().match("http://test.org/"));
}
}

0 comments on commit 0474fcb

Please sign in to comment.