From eea1cacb69d40cec5ced287846feb3d5a994da70 Mon Sep 17 00:00:00 2001 From: Nick Battle Date: Sat, 20 Jan 2024 11:18:17 +0000 Subject: [PATCH] Add vdmj.mappings resource and ClassMapperTest --- .../com/fujitsu/vdmj/mapper/ClassMapper.java | 37 ++++++++---- .../fujitsu/vdmj/mapper/ClassMapperTest.java | 58 +++++++++++++++++++ .../fujitsu/vdmj/mapper/TestDestination.java | 53 +++++++++++++++++ .../com/fujitsu/vdmj/mapper/TestSource.java | 43 ++++++++++++++ .../vdmj/mapper/extra/TestDestExtra.java | 40 +++++++++++++ .../fujitsu/vdmj/mapper/extra/TestExtra.java | 39 +++++++++++++ vdmj/src/test/resources/mapper/test.mappings | 6 ++ vdmj/src/test/resources/test.mappings | 8 +++ vdmj/src/test/resources/vdmj.mappings | 1 + 9 files changed, 275 insertions(+), 10 deletions(-) create mode 100644 vdmj/src/test/java/com/fujitsu/vdmj/mapper/ClassMapperTest.java create mode 100644 vdmj/src/test/java/com/fujitsu/vdmj/mapper/TestDestination.java create mode 100644 vdmj/src/test/java/com/fujitsu/vdmj/mapper/TestSource.java create mode 100644 vdmj/src/test/java/com/fujitsu/vdmj/mapper/extra/TestDestExtra.java create mode 100644 vdmj/src/test/java/com/fujitsu/vdmj/mapper/extra/TestExtra.java create mode 100644 vdmj/src/test/resources/mapper/test.mappings create mode 100644 vdmj/src/test/resources/test.mappings create mode 100644 vdmj/src/test/resources/vdmj.mappings diff --git a/vdmj/src/main/java/com/fujitsu/vdmj/mapper/ClassMapper.java b/vdmj/src/main/java/com/fujitsu/vdmj/mapper/ClassMapper.java index 6f091004e..07a6de779 100644 --- a/vdmj/src/main/java/com/fujitsu/vdmj/mapper/ClassMapper.java +++ b/vdmj/src/main/java/com/fujitsu/vdmj/mapper/ClassMapper.java @@ -43,6 +43,7 @@ import java.util.Vector; import com.fujitsu.vdmj.config.Properties; +import com.fujitsu.vdmj.util.GetResource; /** * A class to map classes and extend trees of objects. @@ -51,6 +52,9 @@ public class ClassMapper { /** The mappers that have already been loaded, indexed by resource name */ private final static Map mappers = new HashMap(); + + /** Resource name for mapping search extensions */ + private static final String MAPPINGS = "vdmj.mappings"; /** * These caches hold the object references converted so far, and keep a stack of @@ -263,23 +267,36 @@ private void readMappings() throws Exception } /** - * You can add extra file locations by setting the vdmj.mappingpath property. - * This allows more than one mapping file of the same name to be included within - * one jar file. + * You can add extra file locations by setting the vdmj.mapping.search_path property, + * or by putting resource folders in "vdmj.mappings". This allows more than one mapping + * file of the same name to be included within one jar file. */ + List alternativePaths = new Vector(); + String mappingPath = Properties.mapping_search_path; if (mappingPath != null) { for (String classpath: mappingPath.split(File.pathSeparator)) { - String filename = classpath + "/" + configFile; // NB. Use slash here! - InputStream is = getClass().getResourceAsStream(filename); - - if (is != null) - { - readMapping(filename, is); - } + alternativePaths.add(classpath + "/" + configFile); // NB. Use slash here! + } + } + else + { + for (String classpath: GetResource.readResource(MAPPINGS)) + { + alternativePaths.add(classpath + "/" + configFile); // NB. Use slash here! + } + } + + for (String resourceName: alternativePaths) + { + InputStream is = getClass().getResourceAsStream(resourceName); + + if (is != null) + { + readMapping(resourceName, is); } } } diff --git a/vdmj/src/test/java/com/fujitsu/vdmj/mapper/ClassMapperTest.java b/vdmj/src/test/java/com/fujitsu/vdmj/mapper/ClassMapperTest.java new file mode 100644 index 000000000..76badc350 --- /dev/null +++ b/vdmj/src/test/java/com/fujitsu/vdmj/mapper/ClassMapperTest.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * + * Copyright (c) 2024 Nick Battle. + * + * Author: Nick Battle + * + * This file is part of VDMJ. + * + * VDMJ is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * VDMJ 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with VDMJ. If not, see . + * SPDX-License-Identifier: GPL-3.0-or-later + * + ******************************************************************************/ + +package com.fujitsu.vdmj.mapper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.Test; + +import com.fujitsu.vdmj.mapper.extra.TestExtra; + +/** + * Basic tests of the ClassMapper. + */ +public class ClassMapperTest +{ + @Test + public void test() + { + // Properties.mapping_search_path = "/mapper"; + // Also uses the vdmj.mappings resource to add extra search paths + ClassMapper mapper = ClassMapper.getInstance("test.mappings"); + assertEquals(0, mapper.getNodeCount()); + + try + { + TestSource source = new TestSource("top", new TestSource("left"), new TestExtra("right")); + TestDestination dest = mapper.convert(source); + assertEquals("top[left[null,null],Extra:right[null,null]]", dest.toString()); + } + catch (Exception e) + { + fail("Failed with " + e); + } + } +} diff --git a/vdmj/src/test/java/com/fujitsu/vdmj/mapper/TestDestination.java b/vdmj/src/test/java/com/fujitsu/vdmj/mapper/TestDestination.java new file mode 100644 index 000000000..af3ccdaa8 --- /dev/null +++ b/vdmj/src/test/java/com/fujitsu/vdmj/mapper/TestDestination.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * + * Copyright (c) 2023 Nick Battle. + * + * Author: Nick Battle + * + * This file is part of VDMJ. + * + * VDMJ is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * VDMJ 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with VDMJ. If not, see . + * SPDX-License-Identifier: GPL-3.0-or-later + * + ******************************************************************************/ +package com.fujitsu.vdmj.mapper; + +public class TestDestination implements Mappable +{ + public final String name; + public final TestDestination left; + public final TestDestination right; + + public TestDestination(String name, TestDestination left, TestDestination right) + { + this.name = name; + this.left = left; + this.right = right; + } + + @Override + public String toString() + { + StringBuilder s = new StringBuilder(); + + s.append(name); + s.append("["); + s.append(left); + s.append(","); + s.append(right); + s.append("]"); + + return s.toString(); + } +} diff --git a/vdmj/src/test/java/com/fujitsu/vdmj/mapper/TestSource.java b/vdmj/src/test/java/com/fujitsu/vdmj/mapper/TestSource.java new file mode 100644 index 000000000..f344c4cba --- /dev/null +++ b/vdmj/src/test/java/com/fujitsu/vdmj/mapper/TestSource.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * + * Copyright (c) 2023 Nick Battle. + * + * Author: Nick Battle + * + * This file is part of VDMJ. + * + * VDMJ is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * VDMJ 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with VDMJ. If not, see . + * SPDX-License-Identifier: GPL-3.0-or-later + * + ******************************************************************************/ +package com.fujitsu.vdmj.mapper; + +public class TestSource implements Mappable +{ + public final String name; + public final TestSource left; + public final TestSource right; + + public TestSource(String name, TestSource left, TestSource right) + { + this.name = name; + this.left = left; + this.right = right; + } + + public TestSource(String name) + { + this(name, null, null); + } +} diff --git a/vdmj/src/test/java/com/fujitsu/vdmj/mapper/extra/TestDestExtra.java b/vdmj/src/test/java/com/fujitsu/vdmj/mapper/extra/TestDestExtra.java new file mode 100644 index 000000000..c4907811d --- /dev/null +++ b/vdmj/src/test/java/com/fujitsu/vdmj/mapper/extra/TestDestExtra.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * + * Copyright (c) 2023 Nick Battle. + * + * Author: Nick Battle + * + * This file is part of VDMJ. + * + * VDMJ is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * VDMJ 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with VDMJ. If not, see . + * SPDX-License-Identifier: GPL-3.0-or-later + * + ******************************************************************************/ +package com.fujitsu.vdmj.mapper.extra; + +import com.fujitsu.vdmj.mapper.TestDestination; + +public class TestDestExtra extends TestDestination +{ + public TestDestExtra(String name, TestDestination left, TestDestination right) + { + super(name, left, right); + } + + @Override + public String toString() + { + return "Extra:" + super.toString(); + } +} diff --git a/vdmj/src/test/java/com/fujitsu/vdmj/mapper/extra/TestExtra.java b/vdmj/src/test/java/com/fujitsu/vdmj/mapper/extra/TestExtra.java new file mode 100644 index 000000000..d926ebb4b --- /dev/null +++ b/vdmj/src/test/java/com/fujitsu/vdmj/mapper/extra/TestExtra.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * + * Copyright (c) 2023 Nick Battle. + * + * Author: Nick Battle + * + * This file is part of VDMJ. + * + * VDMJ is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * VDMJ 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with VDMJ. If not, see . + * SPDX-License-Identifier: GPL-3.0-or-later + * + ******************************************************************************/ +package com.fujitsu.vdmj.mapper.extra; + +import com.fujitsu.vdmj.mapper.TestSource; + +public class TestExtra extends TestSource +{ + public TestExtra(String name, TestExtra left, TestExtra right) + { + super(name, right, right); + } + + public TestExtra(String name) + { + this(name, null, null); + } +} diff --git a/vdmj/src/test/resources/mapper/test.mappings b/vdmj/src/test/resources/mapper/test.mappings new file mode 100644 index 000000000..8a7fd0503 --- /dev/null +++ b/vdmj/src/test/resources/mapper/test.mappings @@ -0,0 +1,6 @@ +# +# Extra mappings for ClassMapperTest +# + +package com.fujitsu.vdmj.mapper.extra to com.fujitsu.vdmj.mapper.extra; +map TestExtra{name, left, right} to TestDestExtra(name, left, right); diff --git a/vdmj/src/test/resources/test.mappings b/vdmj/src/test/resources/test.mappings new file mode 100644 index 000000000..80ea0cfcf --- /dev/null +++ b/vdmj/src/test/resources/test.mappings @@ -0,0 +1,8 @@ +# +# Test mappings for ClassMapperTest +# + +package com.fujitsu.vdmj.mapper to com.fujitsu.vdmj.mapper; +map TestSource{name, left, right} to TestDestination(name, left, right); + +unmapped java.lang.String; diff --git a/vdmj/src/test/resources/vdmj.mappings b/vdmj/src/test/resources/vdmj.mappings new file mode 100644 index 000000000..cdf28dda9 --- /dev/null +++ b/vdmj/src/test/resources/vdmj.mappings @@ -0,0 +1 @@ +/mapper \ No newline at end of file