Don't use FileUtils.getBriarDirectory() in tests.

This commit is contained in:
akwizgran
2011-07-14 20:42:41 +01:00
parent 065b6e496f
commit 62d69b6fb5
7 changed files with 35 additions and 26 deletions

View File

@@ -1,12 +1,16 @@
package net.sf.briar.api.i18n;
import java.awt.Font;
import java.io.File;
import java.util.Locale;
public interface FontManager {
/** Initializes the FontManager for the given locale. */
void initialize(Locale locale);
/**
* Initializes the FontManager for the given locale. Fonts are loaded from
* the given directory if they cannot be loaded from the running jar.
*/
void initialize(Locale locale, File dir);
/** Returns the appropriate font for the given language. */
Font getFontForLanguage(String language);

View File

@@ -18,7 +18,6 @@ import java.util.logging.Logger;
import javax.swing.UIManager;
import net.sf.briar.api.i18n.FontManager;
import net.sf.briar.util.FileUtils;
// Needs to be public for installer
public class FontManagerImpl implements FontManager {
@@ -43,18 +42,15 @@ public class FontManagerImpl implements FontManager {
private volatile Font defaultFont = null, uiFont = null;
public void initialize(Locale locale) {
public void initialize(Locale locale, File dir) {
// Look for bundled fonts in the jar and the filesystem. If any fonts
// are missing or fail to load, fall back to the default font.
ClassLoader loader = getClass().getClassLoader();
for(BundledFont bf : BUNDLED_FONTS) {
try {
InputStream in = loader.getResourceAsStream(bf.filename);
if(in == null) {
File root = FileUtils.getBriarDirectory();
File file = new File(root, "Data/" + bf.filename);
in = new FileInputStream(file);
}
if(in == null)
in = new FileInputStream(new File(dir, bf.filename));
Font font = Font.createFont(Font.TRUETYPE_FONT, in);
font = font.deriveFont(bf.size);
for(String language : bf.languages) fonts.put(language, font);

View File

@@ -1,5 +1,6 @@
package net.sf.briar.ui.setup;
import java.io.File;
import java.util.Locale;
import javax.swing.UIManager;
@@ -11,6 +12,7 @@ import net.sf.briar.api.setup.SetupWorkerFactory;
import net.sf.briar.i18n.FontManagerImpl;
import net.sf.briar.i18n.I18nImpl;
import net.sf.briar.setup.SetupWorkerFactoryImpl;
import net.sf.briar.util.FileUtils;
import net.sf.briar.util.OsUtils;
public class SetupMain {
@@ -30,7 +32,8 @@ public class SetupMain {
SetupParameters parameters = new SetupParametersImpl(locationPanel);
new SetupWorkerPanel(wizard, workerFactory, parameters, i18n);
fontManager.initialize(Locale.getDefault());
File dir = new File(FileUtils.getBriarDirectory(), "Data");
fontManager.initialize(Locale.getDefault(), dir);
wizard.display();
}
}

View File

@@ -47,6 +47,14 @@ public class TestUtils {
throw new RuntimeException("Could not find build directory");
}
public static File getFontDirectory() {
File f = new File("i18n");
if(f.exists() && f.isDirectory()) return f;
f = new File("../i18n");
if(f.exists() && f.isDirectory()) return f;
throw new RuntimeException("Could not find font directory");
}
public static byte[] getRandomId() {
byte[] b = new byte[UniqueId.LENGTH];
random.nextBytes(b);

View File

@@ -1,18 +1,22 @@
package net.sf.briar.i18n;
import java.awt.Font;
import java.io.File;
import java.util.Locale;
import junit.framework.TestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.i18n.FontManager;
import org.junit.Test;
public class FontManagerTest extends TestCase {
private final File fontDir = TestUtils.getFontDirectory();
@Test
public void testBundledFontsAreLoaded() {
FontManager fontManager = new FontManagerImpl();
fontManager.initialize(Locale.UK);
fontManager.initialize(Locale.UK, fontDir);
Font font = fontManager.getFontForLanguage("en"); // English
assertEquals(12, font.getSize());
@@ -29,7 +33,7 @@ public class FontManagerTest extends TestCase {
@Test
public void testInternationalCharactersCanBeDisplayed() {
FontManager fontManager = new FontManagerImpl();
fontManager.initialize(Locale.UK);
fontManager.initialize(Locale.UK, fontDir);
Font font = fontManager.getFontForLanguage("en"); // English
assertTrue(font.canDisplay('a'));
@@ -53,7 +57,7 @@ public class FontManagerTest extends TestCase {
@Test
public void testSetAndGetUiFont() {
FontManager fontManager = new FontManagerImpl();
fontManager.initialize(Locale.UK);
fontManager.initialize(Locale.UK, fontDir);
Font font = fontManager.getUiFont();
assertEquals(12, font.getSize());

View File

@@ -18,25 +18,21 @@ import org.junit.Test;
public class I18nTest extends TestCase {
final File base =
private final File base =
new File(TestUtils.getBuildDirectory(), "i18n.properties");
final File french =
private final File french =
new File(TestUtils.getBuildDirectory(), "i18n_fr.properties");
final File testDir = TestUtils.getTestDirectory();
private final File testDir = TestUtils.getTestDirectory();
FontManager fontManager = null;
I18n i18n = null;
@Before
public void setUp() throws IOException {
TestUtils.createFile(base,
"FOO=foo\r\n" +
"BAR=bar\r\n");
TestUtils.createFile(french,
"FOO=le foo\r\n" +
"BAR=la bar\r\n");
TestUtils.createFile(base, "FOO=foo\r\nBAR=bar\r\n");
TestUtils.createFile(french, "FOO=le foo\r\nBAR=la bar\r\n");
fontManager = new FontManagerImpl();
fontManager.initialize(Locale.UK);
fontManager.initialize(Locale.UK, TestUtils.getFontDirectory());
i18n = new I18nImpl(fontManager);
}

View File

@@ -20,10 +20,8 @@ public class FileUtils {
// Running from a jar - return the jar's grandparent
f = f.getParentFile().getParentFile();
} else {
// Running from Eclipse or ant
// Running from Eclipse
f = new File(f.getParentFile(), "Briar");
if(!f.exists())
f = new File(f.getParentFile().getParentFile(), "Briar"); // Ant
}
assert f.exists();
assert f.isDirectory();