Skip to content

Commit

Permalink
Add vdmj.mappings resource and ClassMapperTest
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbattle committed Jan 20, 2024
1 parent d80fd9f commit eea1cac
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 10 deletions.
37 changes: 27 additions & 10 deletions vdmj/src/main/java/com/fujitsu/vdmj/mapper/ClassMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -51,6 +52,9 @@ public class ClassMapper
{
/** The mappers that have already been loaded, indexed by resource name */
private final static Map<String, ClassMapper> mappers = new HashMap<String, ClassMapper>();

/** 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
Expand Down Expand Up @@ -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 <String> alternativePaths = new Vector<String>();

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);
}
}
}
Expand Down
58 changes: 58 additions & 0 deletions vdmj/src/test/java/com/fujitsu/vdmj/mapper/ClassMapperTest.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
* 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);
}
}
}
53 changes: 53 additions & 0 deletions vdmj/src/test/java/com/fujitsu/vdmj/mapper/TestDestination.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
* 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();
}
}
43 changes: 43 additions & 0 deletions vdmj/src/test/java/com/fujitsu/vdmj/mapper/TestSource.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
* 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();
}
}
39 changes: 39 additions & 0 deletions vdmj/src/test/java/com/fujitsu/vdmj/mapper/extra/TestExtra.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
* 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);
}
}
6 changes: 6 additions & 0 deletions vdmj/src/test/resources/mapper/test.mappings
Original file line number Diff line number Diff line change
@@ -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);
8 changes: 8 additions & 0 deletions vdmj/src/test/resources/test.mappings
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions vdmj/src/test/resources/vdmj.mappings
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/mapper

0 comments on commit eea1cac

Please sign in to comment.