TestGuiUtils.java
//
// Lapetus Ltd Java Class. Copyright (c) Lapetus Systems Ltd, 2009, 2010.
// -----------------------------------------------------------------------
// This is the intellectual property of Lapetus Systems Ltd, Artemis, Greece.
// --------------------------------------------------------------------------
// www.lapetus-ltd.com, www.lapetus.com.gr, www.lapetus.eu
// -------------------------------------------------------
// $LastChangedRevision: 1168 $
// $LastChangedDate:: 2010-11-06 16:41:19#$
// ----------------------------------------
import com.lapetus_ltd.api.TLptsMainJut;
import com.lapetus_ltd.api.common.*;
import junit.framework.TestCase;
import javax.swing.*;
import java.awt.*;
import java.util.Locale;
// ######################################################################################################
// #### Test code for the JUT build process. This code will stop the build process on ERROR ####
// ######################################################################################################
// Class Description : This tests the GUI util.
//
// The following tests verify the static classes for charset, file manager, fonts, locales and operating system functions.
//
public class TestGuiUtils extends TestCase
{
public static void main(String[] args)
{
TestGuiUtils test = new TestGuiUtils();
test.runAllTests(false);
}
public void runAllTests(boolean isExit) // exit is false when running batch tests
{
testCharSet();
testFileOperations();
testFontBoldItalic();
testFontSize();
testSystemInformation();
testLocale();
testOperatingSystem();
if (isExit)
System.exit(0);
}
public void testSystemInformation()
{
TLptsMainJut.init();
try
{
System.out.println("Getting Path environment variable : " + TLptsSysInfoUtil.getEnvVariable("Path"));
assertNotNull("-->PATH Environment variable is null", TLptsSysInfoUtil.getEnvVariable("Path"));
String prevName = "";
for (String name : TLptsGuiUtil.getLookAndFeelNames())
{
System.out.println("Setting L&F " + name);
TLptsGuiUtil.setLookAndFeel(name);
System.out.println("The new look and feel is " + UIManager.getLookAndFeel().getName());
assertNotSame("-->Look and feel not set ",name,prevName);
}
for(int count=0; count<10* TLptsGuiUtil.getLookAndFeelNames().size();count++)
{
prevName = UIManager.getLookAndFeel().getName();
TLptsGuiUtil.loopToNextLookAndFeel();
System.out.println("The new look and feel is " + UIManager.getLookAndFeel().getName());
assertFalse("-->Look and feel not set ",UIManager.getLookAndFeel().getName().equals(prevName));
}
GraphicsDevice defaultDev = TLptsGuiUtil.getDefaultScreenDevice() ;
if (defaultDev.getType()==GraphicsDevice.TYPE_RASTER_SCREEN)
System.out.println("The default Graphics device is a Screen");
for (GraphicsDevice device : TLptsGuiUtil.getAllGraphicsDevices())
if (device.isFullScreenSupported())
System.out.println("Added a device to the full screen support list" + device.getIDstring());
} catch (Throwable e)
{
System.out.println(e.getMessage());
}
}
public void testOperatingSystem()
{
TLptsMainJut.init();
if(TLptsSysInfoUtil.getOSName().equals(""))
fail("No Operating System");
else
System.out.println("Operating System is: "+ TLptsSysInfoUtil.getOSName());
if(TLptsSysInfoUtil.getOSVersion().equals(""))
fail("No OS Version");
else
System.out.println("OS Version :" + TLptsSysInfoUtil.getOSVersion());
if(TLptsSysInfoUtil.getOSArchitecture().equals(""))
fail("No OS Architecture");
else
System.out.println("OS Architecture : " + TLptsSysInfoUtil.getOSArchitecture());
try
{
if(TLptsSysInfoUtil.isHostWindows())
{
assertNotSame("-->Host must be either Windows Or Linux", TLptsSysInfoUtil.isHostWindows(), TLptsSysInfoUtil.isHostLinux());
assertNotSame("-->Host must be either Windows Or Mac", TLptsSysInfoUtil.isHostWindows(), TLptsSysInfoUtil.isHostMac());
assertNotSame("-->Host must be either Window Or Solaris", TLptsSysInfoUtil.isHostWindows(), TLptsSysInfoUtil.isHostSolaris());
}
else if(TLptsSysInfoUtil.isHostLinux())
{
assertNotSame("-->Host must be either Linux Or Windows", TLptsSysInfoUtil.isHostLinux(), TLptsSysInfoUtil.isHostWindows());
assertNotSame("-->Host must be either Linux Or Mac", TLptsSysInfoUtil.isHostLinux(), TLptsSysInfoUtil.isHostMac());
assertNotSame("-->Host must be either Linux Or Solaris", TLptsSysInfoUtil.isHostLinux(), TLptsSysInfoUtil.isHostSolaris());
}
else if(TLptsSysInfoUtil.isHostMac())
{
assertNotSame("-->Host must be either Mac Or Windows", TLptsSysInfoUtil.isHostMac(), TLptsSysInfoUtil.isHostWindows());
assertNotSame("-->Host must be either Mac Or Linux", TLptsSysInfoUtil.isHostMac(), TLptsSysInfoUtil.isHostLinux());
assertNotSame("-->Host must be either Mac Or Solaris", TLptsSysInfoUtil.isHostMac(), TLptsSysInfoUtil.isHostSolaris());
}
else if(TLptsSysInfoUtil.isHostMac())
{
assertNotSame("-->Host must be either Solaris Or Windows", TLptsSysInfoUtil.isHostSolaris(), TLptsSysInfoUtil.isHostWindows());
assertNotSame("-->Host must be either Solaris Or Linux", TLptsSysInfoUtil.isHostSolaris(), TLptsSysInfoUtil.isHostLinux());
assertNotSame("-->Host must be either Solaris Or Mac", TLptsSysInfoUtil.isHostSolaris(), TLptsSysInfoUtil.isHostMac());
}
} catch (Throwable e)
{
System.out.println(e.getMessage());
}
System.out.println("Operating System test passed");
}
public void testLocale()
{
TLptsMainJut.init();
System.out.println("Testing locale");
try
{
for (Locale loc : Locale.getAvailableLocales())
{
assertTrue("-->Can not setting Locale", TLptsCharSetLocaleUtil.setLocale(loc.getLanguage(),loc.getCountry()));
assertEquals("-->Set Locale Language and getCurrentLocale Language must be the same", TLptsCharSetLocaleUtil.getCurrentLocale().getLanguage(),loc.getLanguage());
assertEquals("-->Set Locale Country and getCurrentLocale Country must be the same", TLptsCharSetLocaleUtil.getCurrentLocale().getCountry(),loc.getCountry());
}
} catch (Throwable e)
{
System.out.println(e.getMessage() );
}
System.out.println("Locale test passed");
}
public void testCharSet()
{
TLptsMainJut.init();
System.out.println("Testing Charset");
try
{
assertTrue("-->Charset List must have size greater than zero", TLptsCharSetLocaleUtil.getCharsetNameList().size()>0);
for(String charset : TLptsCharSetLocaleUtil.getCharsetNameList())
{
if(!charset.equals("Database Default"))
assertNotNull("-->Charset must not be null", TLptsCharSetLocaleUtil.getCharSet(charset));
}
for(String charset : TLptsCharSetLocaleUtil.getCharsetNameList())
{
if(!charset.equals("Database Default"))
{
assertFalse("-->String must not be null ", TLptsCharSetLocaleUtil.getStringWithCharset("test".getBytes(),charset).equals(""));
assertTrue("-->String must be null", TLptsCharSetLocaleUtil.getStringWithCharset(null,charset).equals(""));
assertTrue("-->String must be null", TLptsCharSetLocaleUtil.getStringWithCharset("".getBytes(),charset).equals(""));
assertTrue("-->String must be null", TLptsCharSetLocaleUtil.getStringWithCharset("test".getBytes(),"Database Default").equals(""));
}
}
} catch (Throwable e)
{
System.out.println(e.getMessage());
}
System.out.println("Testing Charset Passed");
}
public void testFontSize()
{
TLptsMainJut.init();
System.out.println("Testing Font Sizes for all fonts");
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
for(Font font : e.getAllFonts())
{
try
{
System.out.println("## Processing font : " + font.getFontName());
if (TLptsGuiUtil.getFontHeight(font)<=0)
{
System.out.println("Font " + font.getFontName() + " has 0 font height");
continue;
}
if (TLptsGuiUtil.getFontWidth(font,"Text")<=0)
{
System.out.println("Font " + font.getFontName() + " has 0 text width");
continue;
}
if (TLptsGuiUtil.getFontWidth(font,"Text",1)<0)
{
System.out.println("Font " + font.getFontName() + " has 0 text width - with limit");
continue;
}
assertTrue("-->Font width must be equal to zero", TLptsGuiUtil.getFontWidth(font,null)==0);
assertTrue("-->Font width must equal to zero", TLptsGuiUtil.getFontWidth(font,"")==0);
assertTrue("-->Font width must equal to -1 (text=null)", TLptsGuiUtil.getFontWidth(font,null,-1)==-1);
assertTrue("-->Font width must equal to -1 (text=\"\")", TLptsGuiUtil.getFontWidth(font,"",-1)==-1);
} catch (Throwable e1)
{
System.out.println(font);
System.out.println(e1.getMessage());
break;
}
}
System.out.println("Testing Font Size Passed");
}
public void testFontBoldItalic()
{
TLptsMainJut.init();
System.out.println("Testing Font Bold/Italic");
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
for(Font font : e.getAllFonts())
{
try
{
assertNotNull("-->Font must be not null", TLptsGuiUtil.fontAddBold(font));
assertNotNull("-->Font must be not null", TLptsGuiUtil.fontAddItalic(font));
} catch (Throwable e1)
{
System.out.println(font);
System.out.println(e1.getMessage());
break;
}
}
System.out.println("Testing Font Bold/Italic Passed");
}
public void testFileOperations()
{
TLptsMainJut.init();
try
{
System.out.println("Testing FileManager");
assertNotNull("-->User Home Directory must not me null", TLptsFileUtil.getUserHomeDirectory());
assertFalse("-->User Home Directory length must not have length zero", TLptsFileUtil.getUserHomeDirectory().equals(""));
assertNotNull("-->User Home Directory must not me null", TLptsFileUtil.getUserHomeLapetusDirectory());
assertFalse("-->User Home Directory length must not have length zero", TLptsFileUtil.getUserHomeLapetusDirectory().equals(""));
assertTrue("-->Path must exist", TLptsFileUtil.isPath("C:\\"));
assertFalse("-->Path must not exist", TLptsFileUtil.isPath("P:\\test\\"));
assertNotNull("-->Directory must not be null", TLptsFileUtil.getCurrentDirectory());
assertFalse("-->Directory must not have length zero", TLptsFileUtil.getCurrentDirectory().equals(""));
assertTrue("-->File List size must be greater than zero", TLptsFileUtil.getFileList(TLptsFileUtil.getCurrentDirectory(),"bat",true).size()>0);
assertNotNull("-->ParentDirectory must not be null", TLptsFileUtil.getCurrentParentDirectory());
assertFalse("-->ParentDirectory must not have length zero", TLptsFileUtil.getCurrentParentDirectory().equals(""));
System.out.println("Testing FileManager Passed");
} catch (Throwable e)
{
System.out.println(e.getMessage());
}
}
}