Made a huge optimization in the property check. Added test.

This commit is contained in:
Christian P. MOMON 2024-08-02 04:55:03 +02:00
parent 93c06e728a
commit e44bda8db2
2 changed files with 187 additions and 2 deletions

View file

@ -293,12 +293,12 @@ public class PropertyChecker
check = new PropertyCheck(lineIndex, "", Status.OK);
check.setComment("OK");
}
else if ((StringUtils.isEmpty(line)) || (line.matches(COMMENT)))
else if ((StringUtils.isEmpty(line)) || (isComment(line)))
{
check = new PropertyCheck(lineIndex, line, Status.OK);
check.setComment("OK");
}
else if (!line.matches("^[^#].*[^\\s].*=.*$"))
else if (!isProperty(line))
{
check = new PropertyCheck(lineIndex, line, Status.ERROR);
check.setComment("Ligne malformée");
@ -458,4 +458,83 @@ public class PropertyChecker
//
return result;
}
/**
* Checks if is comment.
*
* @param line
* the line
* @return true, if is comment
*/
public static boolean isComment(final String line)
{
boolean result;
// Method 1
// public static final Pattern COMMENT_PATTERN =
// Pattern.compile(COMMENT);
// result = COMMENT_PATTERN.matcher(line).matches();
// Method 2
// result = StringUtils.startsWith(line, "#");
// Method 3
if ((line != null) && (line.length() > 0) && (line.charAt(0) == '#'))
{
result = true;
}
else
{
result = false;
}
//
return result;
}
/**
* Checks if is property.
*
* @param line
* the line
* @return true, if is property
*/
public static boolean isProperty(final String line)
{
boolean result;
// Method 1, take so much time, > 1000 ms for each property file.
// public static final Pattern PROPERTY_PATTERN =
// Pattern.compile("^[^#].*[^\\s].*=.*$");
// result = PROPERTY_PATTERN.matcher(line).matches();
// Method 2, well optimized, < 1 ms for each property file.
if ((line == null) || (line.length() == 0))
{
result = false;
}
else
{
int splitter = line.indexOf('=');
if (splitter == -1)
{
result = false;
}
else
{
String path = line.substring(0, splitter);
if ((path.length() == 0) || (path.charAt(0) == '#') || (StringUtils.isBlank(path)))
{
result = false;
}
else
{
result = true;
}
}
}
//
return result;
}
}

View file

@ -0,0 +1,106 @@
/*
* Copyright (C) 2024 Christian Pierre MOMON <christian@momon.org>
*
* This file is part of StatoolInfos, simple key value database.
*
* StatoolInfos is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* StatoolInfos 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with StatoolInfos. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.devinsy.statoolinfos.checker;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import fr.devinsy.statoolinfos.core.StatoolInfosException;
/**
* The Class PropertyCheckerTest.
*/
public class PropertyCheckerTest
{
private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(PropertyCheckerTest.class);
/**
*
*/
@Test
public void PropertyCheckerIsCommentTest01()
{
Assert.assertFalse(PropertyChecker.isComment(null));
Assert.assertFalse(PropertyChecker.isComment(""));
Assert.assertFalse(PropertyChecker.isComment(" "));
Assert.assertFalse(PropertyChecker.isComment("foo"));
Assert.assertFalse(PropertyChecker.isComment(" foo "));
Assert.assertTrue(PropertyChecker.isComment("#"));
Assert.assertTrue(PropertyChecker.isComment("# "));
Assert.assertTrue(PropertyChecker.isComment("# foo"));
}
/**
*
*/
@Test
public void PropertyCheckerIsPropertyTest01()
{
Assert.assertFalse(PropertyChecker.isProperty(null));
Assert.assertFalse(PropertyChecker.isProperty(""));
Assert.assertFalse(PropertyChecker.isProperty(" "));
Assert.assertFalse(PropertyChecker.isProperty(" foo "));
Assert.assertFalse(PropertyChecker.isProperty("foo"));
Assert.assertFalse(PropertyChecker.isProperty("#"));
Assert.assertFalse(PropertyChecker.isProperty("# "));
Assert.assertFalse(PropertyChecker.isProperty("# foo"));
Assert.assertFalse(PropertyChecker.isProperty("#foo=bar"));
Assert.assertTrue(PropertyChecker.isProperty("foo="));
Assert.assertTrue(PropertyChecker.isProperty("foo= "));
Assert.assertTrue(PropertyChecker.isProperty("foo=bar"));
Assert.assertTrue(PropertyChecker.isProperty("foo=bar "));
Assert.assertTrue(PropertyChecker.isProperty(" foo="));
Assert.assertTrue(PropertyChecker.isProperty(" foo= "));
Assert.assertTrue(PropertyChecker.isProperty(" foo=bar"));
Assert.assertTrue(PropertyChecker.isProperty(" foo=bar "));
Assert.assertTrue(PropertyChecker.isProperty(" foo = bar"));
Assert.assertTrue(PropertyChecker.isProperty(" foo = bar "));
}
/**
* After class.
*
* @throws StatoolInfosException
* the Juga exception
*/
@AfterClass
public static void afterClass() throws StatoolInfosException
{
}
/**
* Before class.
*
* @throws StatoolInfosException
* the Juga exception
*/
@BeforeClass
public static void beforeClass() throws StatoolInfosException
{
Configurator.initialize(new DefaultConfiguration());
Configurator.setRootLevel(Level.DEBUG);
}
}