|
| 1 | +/* |
| 2 | + * @cond LICENSE |
| 3 | + * ###################################################################################### |
| 4 | + * # LGPL License # |
| 5 | + * # # |
| 6 | + * # This file is part of the LightJason # |
| 7 | + * # Copyright (c) 2015-19, LightJason (info@lightjason.org) # |
| 8 | + * # This program is free software: you can redistribute it and/or modify # |
| 9 | + * # it under the terms of the GNU Lesser General Public License as # |
| 10 | + * # published by the Free Software Foundation, either version 3 of the # |
| 11 | + * # License, or (at your option) any later version. # |
| 12 | + * # # |
| 13 | + * # This program is distributed in the hope that it will be useful, # |
| 14 | + * # but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 15 | + * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 16 | + * # GNU Lesser General Public License for more details. # |
| 17 | + * # # |
| 18 | + * # You should have received a copy of the GNU Lesser General Public License # |
| 19 | + * # along with this program. If not, see http://www.gnu.org/licenses/ # |
| 20 | + * ###################################################################################### |
| 21 | + * @endcond |
| 22 | + */ |
| 23 | + |
| 24 | +package org.lightjason.agentspeak.testing; |
| 25 | + |
| 26 | +import com.tngtech.java.junit.dataprovider.DataProvider; |
| 27 | +import com.tngtech.java.junit.dataprovider.DataProviderRunner; |
| 28 | +import com.tngtech.java.junit.dataprovider.UseDataProvider; |
| 29 | +import org.apache.commons.io.IOUtils; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Ignore; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.lightjason.agentspeak.action.IBaseAction; |
| 36 | +import org.lightjason.agentspeak.agent.IAgent; |
| 37 | +import org.lightjason.agentspeak.common.CCommon; |
| 38 | +import org.lightjason.agentspeak.common.CPath; |
| 39 | +import org.lightjason.agentspeak.common.IPath; |
| 40 | +import org.lightjason.agentspeak.generator.CActionStaticGenerator; |
| 41 | +import org.lightjason.agentspeak.generator.CLambdaStreamingStaticGenerator; |
| 42 | +import org.lightjason.agentspeak.language.ITerm; |
| 43 | +import org.lightjason.agentspeak.language.execution.IContext; |
| 44 | +import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue; |
| 45 | +import org.lightjason.agentspeak.language.variable.CConstant; |
| 46 | + |
| 47 | +import javax.annotation.Nonnegative; |
| 48 | +import javax.annotation.Nonnull; |
| 49 | +import java.io.FileInputStream; |
| 50 | +import java.io.IOException; |
| 51 | +import java.io.InputStream; |
| 52 | +import java.nio.charset.Charset; |
| 53 | +import java.nio.file.Files; |
| 54 | +import java.nio.file.Path; |
| 55 | +import java.nio.file.Paths; |
| 56 | +import java.text.MessageFormat; |
| 57 | +import java.util.List; |
| 58 | +import java.util.concurrent.atomic.AtomicInteger; |
| 59 | +import java.util.logging.LogManager; |
| 60 | +import java.util.regex.Matcher; |
| 61 | +import java.util.regex.Pattern; |
| 62 | +import java.util.stream.IntStream; |
| 63 | +import java.util.stream.Stream; |
| 64 | + |
| 65 | + |
| 66 | +/** |
| 67 | + * test agent structure. |
| 68 | + * If a file agentprintin.conf exists on the main directory alls print statements will be shown |
| 69 | + */ |
| 70 | +@RunWith( DataProviderRunner.class ) |
| 71 | +public final class TestAsl extends IBaseTest |
| 72 | +{ |
| 73 | + /** |
| 74 | + * tag of iteration |
| 75 | + */ |
| 76 | + private static final String ITERATIONNTAG = "@iteration"; |
| 77 | + /** |
| 78 | + * regex for iteration |
| 79 | + */ |
| 80 | + private static final Pattern ITERATION = Pattern.compile( ITERATIONNTAG + "\\s+\\d+" ); |
| 81 | + /** |
| 82 | + * tag of test results |
| 83 | + */ |
| 84 | + private static final String TESTCOUNTTAG = "@testcount"; |
| 85 | + /** |
| 86 | + * regex for iteration |
| 87 | + */ |
| 88 | + private static final Pattern TESTCOUNT = Pattern.compile( TESTCOUNTTAG + "\\s+\\d+" ); |
| 89 | + /** |
| 90 | + * iteration counter |
| 91 | + */ |
| 92 | + private AtomicInteger m_count; |
| 93 | + |
| 94 | + static |
| 95 | + { |
| 96 | + // disable logger |
| 97 | + LogManager.getLogManager().reset(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * initialize |
| 102 | + */ |
| 103 | + @Before |
| 104 | + public void initialize() |
| 105 | + { |
| 106 | + m_count = new AtomicInteger(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * data provider for defining asl files |
| 111 | + * @return triple of test-cases (asl file, number of iterations, expected log items) |
| 112 | + */ |
| 113 | + @DataProvider |
| 114 | + public static Object[] generate() |
| 115 | + { |
| 116 | + try |
| 117 | + ( |
| 118 | + final Stream<Path> l_walk = Files.walk( |
| 119 | + Paths.get( TestAsl.class.getClassLoader().getResource( "" ).getPath(), "asl" ) |
| 120 | + ) |
| 121 | + ) |
| 122 | + { |
| 123 | + return l_walk.filter( Files::isRegularFile ) |
| 124 | + .map( Path::toString ) |
| 125 | + .filter( i -> i.endsWith( ".asl" ) ) |
| 126 | + .toArray(); |
| 127 | + |
| 128 | + } |
| 129 | + catch ( final IOException l_exception ) |
| 130 | + { |
| 131 | + l_exception.printStackTrace(); |
| 132 | + Assert.fail(); |
| 133 | + return Stream.of().toArray(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + /** |
| 139 | + * test for default generators and configuration |
| 140 | + * |
| 141 | + * @param p_file tripel of asl code, cycles and expected success calls |
| 142 | + * @throws Exception on any error |
| 143 | + * @todo must restructured with asl calls and action packages |
| 144 | + */ |
| 145 | + @Test |
| 146 | + @UseDataProvider( "generate" ) |
| 147 | + public void testASLDefault( @Nonnull final String p_file ) throws Exception |
| 148 | + { |
| 149 | + final IAgent<?> l_agent; |
| 150 | + final int l_iteration; |
| 151 | + final int l_testcount; |
| 152 | + |
| 153 | + try |
| 154 | + ( |
| 155 | + final InputStream l_stream = new FileInputStream( p_file ) |
| 156 | + ) |
| 157 | + { |
| 158 | + final String l_source = IOUtils.toString( l_stream, Charset.defaultCharset() ); |
| 159 | + |
| 160 | + final Matcher l_iterationmatch = ITERATION.matcher( l_source ); |
| 161 | + l_iteration = l_iterationmatch.find() |
| 162 | + ? Integer.parseInt( l_iterationmatch.group( 0 ).replace( ITERATIONNTAG, "" ).trim() ) |
| 163 | + : 1; |
| 164 | + |
| 165 | + final Matcher l_testcountmatcher = TESTCOUNT.matcher( l_source ); |
| 166 | + l_testcount = l_testcountmatcher.find() |
| 167 | + ? Integer.parseInt( l_testcountmatcher.group( 0 ).replace( TESTCOUNTTAG, "" ).trim() ) |
| 168 | + : 0; |
| 169 | + |
| 170 | + l_agent = new CAgentGenerator( |
| 171 | + l_stream, |
| 172 | + |
| 173 | + new CActionStaticGenerator( |
| 174 | + Stream.concat( |
| 175 | + PRINTENABLE |
| 176 | + ? Stream.of( new CTestResult() ) |
| 177 | + : Stream.of( new CTestResult(), new CEmptyPrint() ), |
| 178 | + CCommon.actionsFromPackage() |
| 179 | + ) |
| 180 | + ), |
| 181 | + |
| 182 | + new CLambdaStreamingStaticGenerator( CCommon.lambdastreamingFromPackage() ), |
| 183 | + |
| 184 | + ( p_agent, p_runningcontext ) -> Stream.of( |
| 185 | + new CConstant<>( "MyConstInt", 123 ), |
| 186 | + new CConstant<>( "MyConstString", "here is a test string" ) |
| 187 | + ) |
| 188 | + ).generatesingle(); |
| 189 | + } |
| 190 | + catch ( final Exception l_exception ) |
| 191 | + { |
| 192 | + l_exception.printStackTrace(); |
| 193 | + Assert.fail( p_file ); |
| 194 | + return; |
| 195 | + } |
| 196 | + |
| 197 | + IntStream.range( 0, l_iteration ) |
| 198 | + .forEach( i -> agentcycle( l_agent ) ); |
| 199 | + |
| 200 | + Assert.assertEquals( |
| 201 | + MessageFormat.format( "{0} {1}", "number of tests", p_file ), |
| 202 | + l_testcount, |
| 203 | + m_count.get() |
| 204 | + ); |
| 205 | + |
| 206 | + } |
| 207 | + |
| 208 | + // --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 209 | + |
| 210 | + /** |
| 211 | + * empty print action |
| 212 | + */ |
| 213 | + private static final class CEmptyPrint extends IBaseAction |
| 214 | + { |
| 215 | + /** |
| 216 | + * serial id |
| 217 | + */ |
| 218 | + private static final long serialVersionUID = 8344720639088993942L; |
| 219 | + |
| 220 | + @Nonnull |
| 221 | + @Override |
| 222 | + public IPath name() |
| 223 | + { |
| 224 | + return CPath.of( "generic/print" ); |
| 225 | + } |
| 226 | + |
| 227 | + @Nonnegative |
| 228 | + @Override |
| 229 | + public int minimalArgumentNumber() |
| 230 | + { |
| 231 | + return 0; |
| 232 | + } |
| 233 | + |
| 234 | + @Nonnull |
| 235 | + @Override |
| 236 | + public Stream<IFuzzyValue<?>> execute( final boolean p_parallel, @Nonnull final IContext p_context, |
| 237 | + @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return ) |
| 238 | + { |
| 239 | + return Stream.of(); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * test action |
| 245 | + */ |
| 246 | + private final class CTestResult extends IBaseAction |
| 247 | + { |
| 248 | + /** |
| 249 | + * serial id |
| 250 | + */ |
| 251 | + private static final long serialVersionUID = 9032624165822970132L; |
| 252 | + |
| 253 | + @Nonnull |
| 254 | + @Override |
| 255 | + public IPath name() |
| 256 | + { |
| 257 | + return CPath.of( "test/result" ); |
| 258 | + } |
| 259 | + |
| 260 | + @Nonnegative |
| 261 | + @Override |
| 262 | + public int minimalArgumentNumber() |
| 263 | + { |
| 264 | + return 1; |
| 265 | + } |
| 266 | + |
| 267 | + @Nonnull |
| 268 | + @Override |
| 269 | + public Stream<IFuzzyValue<?>> execute( final boolean p_parallel, @Nonnull final IContext p_context, |
| 270 | + @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return ) |
| 271 | + { |
| 272 | + Assert.assertTrue( |
| 273 | + MessageFormat.format( |
| 274 | + "{0}{1}{2}", |
| 275 | + p_context.instance().literal(), |
| 276 | + p_argument.size() > 1 ? ": " : "", |
| 277 | + p_argument.size() > 1 ? p_argument.get( 1 ).raw() : "" |
| 278 | + ), |
| 279 | + p_argument.get( 0 ).<Boolean>raw() |
| 280 | + ); |
| 281 | + m_count.incrementAndGet(); |
| 282 | + return Stream.of(); |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | +} |
0 commit comments