Moved FileTools.addBeforeExtension in XidynUtils. Added tests.

This commit is contained in:
Christian P. MOMON 2023-11-26 02:46:33 +01:00
parent 1637db4144
commit 3e4bf56c2a
8 changed files with 207 additions and 26 deletions

View file

@ -18,7 +18,6 @@
<classpathentry kind="lib" path="lib/UnitTesting/junit-4.12.jar" sourcepath="lib/UnitTesting/junit-4.12-sources.jar"/> <classpathentry kind="lib" path="lib/UnitTesting/junit-4.12.jar" sourcepath="lib/UnitTesting/junit-4.12-sources.jar"/>
<classpathentry kind="lib" path="lib/Logs/slf4j-api-1.7.25.jar" sourcepath="lib/Logs/slf4j-api-1.7.25-sources.jar"/> <classpathentry kind="lib" path="lib/Logs/slf4j-api-1.7.25.jar" sourcepath="lib/Logs/slf4j-api-1.7.25-sources.jar"/>
<classpathentry kind="lib" path="lib/Logs/slf4j-log4j12-1.7.25.jar" sourcepath="lib/Logs/slf4j-log4j12-1.7.25-sources.jar"/> <classpathentry kind="lib" path="lib/Logs/slf4j-log4j12-1.7.25.jar" sourcepath="lib/Logs/slf4j-log4j12-1.7.25-sources.jar"/>
<classpathentry kind="lib" path="lib/devinsy-utils-0.8.0.jar" sourcepath="lib/devinsy-utils-0.8.0-sources.zip"/>
<classpathentry kind="lib" path="lib/devinsy-strings-0.11.0.jar" sourcepath="lib/devinsy-strings-0.11.0-sources.zip"/> <classpathentry kind="lib" path="lib/devinsy-strings-0.11.0.jar" sourcepath="lib/devinsy-strings-0.11.0-sources.zip"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.11.jar" sourcepath="lib/commons-lang3-3.11-sources.jar"/> <classpathentry kind="lib" path="lib/commons-lang3-3.11.jar" sourcepath="lib/commons-lang3-3.11-sources.jar"/>
<classpathentry kind="lib" path="lib/commons-text-1.9.jar" sourcepath="lib/commons-text-1.9-sources.jar"/> <classpathentry kind="lib" path="lib/commons-text-1.9.jar" sourcepath="lib/commons-text-1.9-sources.jar"/>

View file

@ -1,7 +1,6 @@
This project is using the following libraries: This project is using the following libraries:
# Main requirements: # Main requirements:
devinsy-utils GNU LGPL useful tools (StringList…)
commons-lang3 Apache 2 commons-lang3 Apache 2
# Unit testing requirements: # Unit testing requirements:

Binary file not shown.

Binary file not shown.

View file

@ -27,7 +27,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import fr.devinsy.util.FileTools; import fr.devinsy.xidyn.utils.XidynUtils;
import fr.devinsy.xidyn.utils.cache.Cache; import fr.devinsy.xidyn.utils.cache.Cache;
/** /**
@ -35,8 +35,6 @@ import fr.devinsy.xidyn.utils.cache.Cache;
*/ */
public class PresenterFactory public class PresenterFactory
{ {
private static Logger logger = LoggerFactory.getLogger(PresenterFactory.class);
/** /**
* http://thecodersbreakfast.net/index.php?post/2008/02/25/26-de-la-bonne- * http://thecodersbreakfast.net/index.php?post/2008/02/25/26-de-la-bonne-
* implementation-du-singleton-en-java * implementation-du-singleton-en-java
@ -46,6 +44,8 @@ public class PresenterFactory
private final static PresenterFactory INSTANCE = new PresenterFactory(); private final static PresenterFactory INSTANCE = new PresenterFactory();
} }
private static Logger logger = LoggerFactory.getLogger(PresenterFactory.class);
private Cache<Presenter> cache; private Cache<Presenter> cache;
/** /**
@ -110,7 +110,7 @@ public class PresenterFactory
} }
else else
{ {
String localizedSource = FileTools.addBeforeExtension(source.toString(), "-" + locale.getLanguage()); String localizedSource = XidynUtils.insertBeforeExtension(source.toString(), "-" + locale.getLanguage());
result = this.cache.get(localizedSource); result = this.cache.get(localizedSource);
@ -200,7 +200,7 @@ public class PresenterFactory
} }
else else
{ {
File localizedSource = new File(FileTools.addBeforeExtension(source.getAbsolutePath(), "-" + locale.getLanguage())); File localizedSource = new File(XidynUtils.insertBeforeExtension(source.getAbsolutePath(), "-" + locale.getLanguage()));
result = this.cache.get(localizedSource); result = this.cache.get(localizedSource);
@ -271,7 +271,7 @@ public class PresenterFactory
{ {
URL localizedSource; URL localizedSource;
localizedSource = new URL(FileTools.addBeforeExtension(source.getPath(), "-" + locale.getLanguage())); localizedSource = new URL(XidynUtils.insertBeforeExtension(source.getPath(), "-" + locale.getLanguage()));
result = this.cache.get(localizedSource); result = this.cache.get(localizedSource);
@ -383,7 +383,7 @@ public class PresenterFactory
} }
else else
{ {
String localizedSource = FileTools.addBeforeExtension(source.toString(), "-" + locale.getLanguage()); String localizedSource = XidynUtils.insertBeforeExtension(source.toString(), "-" + locale.getLanguage());
result = create(localizedSource); result = create(localizedSource);
@ -468,7 +468,7 @@ public class PresenterFactory
} }
else else
{ {
File localizedSource = new File(FileTools.addBeforeExtension(source.getAbsolutePath(), "-" + locale.getLanguage())); File localizedSource = new File(XidynUtils.insertBeforeExtension(source.getAbsolutePath(), "-" + locale.getLanguage()));
result = create(localizedSource); result = create(localizedSource);
@ -531,7 +531,7 @@ public class PresenterFactory
} }
else else
{ {
URL localizedSource = new URL(FileTools.addBeforeExtension(source.getPath(), "-" + locale.getLanguage())); URL localizedSource = new URL(XidynUtils.insertBeforeExtension(source.getPath(), "-" + locale.getLanguage()));
result = create(localizedSource); result = create(localizedSource);

View file

@ -22,9 +22,9 @@ import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import fr.devinsy.util.FileTools;
import fr.devinsy.xidyn.XidynException; import fr.devinsy.xidyn.XidynException;
import fr.devinsy.xidyn.data.TagDataManager; import fr.devinsy.xidyn.data.TagDataManager;
import fr.devinsy.xidyn.utils.XidynUtils;
/** /**
* The Class TranslatorPresenter. * The Class TranslatorPresenter.
@ -270,7 +270,7 @@ public class TranslatorPresenter implements Presenter
} }
else else
{ {
String adaptedSource = FileTools.addBeforeExtension(this.defaultSource, "-" + language); String adaptedSource = XidynUtils.insertBeforeExtension(this.defaultSource, "-" + language);
result = PresenterFactory.create(adaptedSource); result = PresenterFactory.create(adaptedSource);
if (result.isAvailable()) if (result.isAvailable())
@ -279,7 +279,7 @@ public class TranslatorPresenter implements Presenter
} }
else else
{ {
adaptedSource = FileTools.addBeforeExtension(this.defaultSource, "_" + language); adaptedSource = XidynUtils.insertBeforeExtension(this.defaultSource, "_" + language);
if (result.isAvailable()) if (result.isAvailable())
{ {

View file

@ -597,6 +597,45 @@ public class XidynUtils
return result; return result;
} }
/**
* Insert before extension.
*
* @param fileName
* the file name
* @param addition
* the addition
* @return the string
*/
public static String insertBeforeExtension(final String fileName, final String addition)
{
String result;
if (fileName == null)
{
result = null;
}
else if (addition == null)
{
result = fileName;
}
else
{
int separatorIndex = fileName.lastIndexOf('.');
if (separatorIndex > 0)
{
result = fileName.substring(0, separatorIndex) + addition + fileName.substring(separatorIndex);
}
else
{
result = fileName + addition;
}
}
//
return result;
}
/** /**
* Load. * Load.
* *

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2006-2017 Christian Pierre MOMON * Copyright (C) 2006-2023 Christian Pierre MOMON <christian@momon.org>
* *
* This file is part of Xidyn. * This file is part of Xidyn.
* *
@ -21,7 +21,7 @@ package fr.devinsy.xidyn.utils;
import org.apache.log4j.BasicConfigurator; import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level; import org.apache.log4j.Level;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.fest.assertions.Assertions; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -49,7 +49,7 @@ public class XidynUtilsTest
String source = "aaaaa<boDY>hello</Body>zzzzz"; String source = "aaaaa<boDY>hello</Body>zzzzz";
String target = XidynUtils.extractBodyContent(source); String target = XidynUtils.extractBodyContent(source);
Assertions.assertThat(target).isEqualTo("hello"); Assert.assertEquals(target, "hello");
} }
/** /**
@ -61,7 +61,7 @@ public class XidynUtilsTest
String source = "aaaaaaa<boDY>hello</Bod>zzzzz"; String source = "aaaaaaa<boDY>hello</Bod>zzzzz";
String target = XidynUtils.extractBodyContent(source); String target = XidynUtils.extractBodyContent(source);
Assertions.assertThat(target).isEqualTo(""); Assert.assertEquals(target, "");
} }
/** /**
@ -73,7 +73,7 @@ public class XidynUtilsTest
String source = "aaaaa<body></BodY>zzzzz"; String source = "aaaaa<body></BodY>zzzzz";
String target = XidynUtils.extractBodyContent(source); String target = XidynUtils.extractBodyContent(source);
Assertions.assertThat(target).isEqualTo(""); Assert.assertEquals(target, "");
} }
/** /**
@ -85,7 +85,7 @@ public class XidynUtilsTest
String source = "aaaaa<boDY> hello </body>zzzzz"; String source = "aaaaa<boDY> hello </body>zzzzz";
String target = XidynUtils.extractBodyContent(source); String target = XidynUtils.extractBodyContent(source);
Assertions.assertThat(target).isEqualTo("hello"); Assert.assertEquals(target, "hello");
} }
/** /**
@ -114,7 +114,7 @@ public class XidynUtilsTest
// System.out.println("[" + source + "]"); // System.out.println("[" + source + "]");
String target = XidynUtils.extractBodyContent(source); String target = XidynUtils.extractBodyContent(source);
Assertions.assertThat(target).isEqualTo("WELCOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOME"); Assert.assertEquals(target, "WELCOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOME");
} }
/** /**
@ -126,7 +126,7 @@ public class XidynUtilsTest
String source = "aaaaa<boDY>hello</Body>zzzzz"; String source = "aaaaa<boDY>hello</Body>zzzzz";
String target = XidynUtils.extractDoctype(source); String target = XidynUtils.extractDoctype(source);
Assertions.assertThat(target).isNull(); Assert.assertNull(target);
} }
/** /**
@ -138,7 +138,7 @@ public class XidynUtilsTest
String source = "<html xmlns='http://www.w3.org/1999/xhtml'>aaaaa<boDY>hello</Body>zzzzz"; String source = "<html xmlns='http://www.w3.org/1999/xhtml'>aaaaa<boDY>hello</Body>zzzzz";
String target = XidynUtils.extractDoctype(source); String target = XidynUtils.extractDoctype(source);
Assertions.assertThat(target).isEqualTo(""); Assert.assertEquals(target, "");
} }
/** /**
@ -150,7 +150,7 @@ public class XidynUtilsTest
String source = "<!DOCTYPE html><html xmlns='http://www.w3.org/1999/xhtml'>aaaaa<boDY>hello</Body>zzzzz"; String source = "<!DOCTYPE html><html xmlns='http://www.w3.org/1999/xhtml'>aaaaa<boDY>hello</Body>zzzzz";
String target = XidynUtils.extractDoctype(source); String target = XidynUtils.extractDoctype(source);
Assertions.assertThat(target).isEqualTo("<!DOCTYPE html>"); Assert.assertEquals(target, "<!DOCTYPE html>");
} }
/** /**
@ -162,7 +162,7 @@ public class XidynUtilsTest
String source = "<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE html><html xmlns='http://www.w3.org/1999/xhtml'>aaaaa<boDY>hello</Body>zzzzz"; String source = "<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE html><html xmlns='http://www.w3.org/1999/xhtml'>aaaaa<boDY>hello</Body>zzzzz";
String target = XidynUtils.extractDoctype(source); String target = XidynUtils.extractDoctype(source);
Assertions.assertThat(target).isEqualTo("<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE html>"); Assert.assertEquals(target, "<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE html>");
} }
/** /**
@ -174,6 +174,150 @@ public class XidynUtilsTest
String source = "<?xml version='1.0' encoding='UTF-8' ?><!-- TOTOT --><!DOCTYPE html><html xmlns='http://www.w3.org/1999/xhtml'>aaaaa<boDY>hello</Body>zzzzz"; String source = "<?xml version='1.0' encoding='UTF-8' ?><!-- TOTOT --><!DOCTYPE html><html xmlns='http://www.w3.org/1999/xhtml'>aaaaa<boDY>hello</Body>zzzzz";
String target = XidynUtils.extractDoctype(source); String target = XidynUtils.extractDoctype(source);
Assertions.assertThat(target).isEqualTo("<?xml version='1.0' encoding='UTF-8' ?><!-- TOTOT --><!DOCTYPE html>"); Assert.assertEquals(target, "<?xml version='1.0' encoding='UTF-8' ?><!-- TOTOT --><!DOCTYPE html>");
}
/**
* Test insert before extension 01.
*/
@Test
public void testInsertBeforeExtension01()
{
String source = null;
String target = XidynUtils.insertBeforeExtension(source, null);
Assert.assertNull(target);
}
/**
* Test insert before extension 02.
*/
@Test
public void testInsertBeforeExtension02()
{
String source = null;
String target = XidynUtils.insertBeforeExtension(source, "");
Assert.assertNull(target);
}
/**
* Test insert before extension 03.
*/
@Test
public void testInsertBeforeExtension03()
{
String source = null;
String target = XidynUtils.insertBeforeExtension(source, "-fr");
Assert.assertNull(target);
}
/**
* Test insert before extension 05.
*/
@Test
public void testInsertBeforeExtension05()
{
String source = "";
String target = XidynUtils.insertBeforeExtension(source, null);
Assert.assertEquals(target, "");
}
/**
* Test insert before extension 06.
*/
@Test
public void testInsertBeforeExtension06()
{
String source = "";
String target = XidynUtils.insertBeforeExtension(source, "");
Assert.assertEquals(target, "");
}
/**
* Test insert before extension 07.
*/
@Test
public void testInsertBeforeExtension07()
{
String source = "";
String target = XidynUtils.insertBeforeExtension(source, "-fr");
Assert.assertEquals(target, "-fr");
}
/**
* Test insert before extension 08.
*/
@Test
public void testInsertBeforeExtension08()
{
String source = "foo";
String target = XidynUtils.insertBeforeExtension(source, null);
Assert.assertEquals(target, "foo");
}
/**
* Test insert before extension 09.
*/
@Test
public void testInsertBeforeExtension09()
{
String source = "foo";
String target = XidynUtils.insertBeforeExtension(source, "");
Assert.assertEquals(target, "foo");
}
/**
* Test insert before extension 10.
*/
@Test
public void testInsertBeforeExtension10()
{
String source = "foo";
String target = XidynUtils.insertBeforeExtension(source, "-fr");
Assert.assertEquals(target, "foo-fr");
}
/**
* Test insert before extension 11.
*/
@Test
public void testInsertBeforeExtension11()
{
String source = "foo.txt";
String target = XidynUtils.insertBeforeExtension(source, null);
Assert.assertEquals(target, "foo.txt");
}
/**
* Test insert before extension 12.
*/
@Test
public void testInsertBeforeExtension12()
{
String source = "foo.txt";
String target = XidynUtils.insertBeforeExtension(source, "");
Assert.assertEquals(target, "foo.txt");
}
/**
* Test insert before extension 13.
*/
@Test
public void testInsertBeforeExtension13()
{
String source = "foo.txt";
String target = XidynUtils.insertBeforeExtension(source, "-fr");
Assert.assertEquals(target, "foo-fr.txt");
} }
} }