Przeglądaj źródła

Extend book store test cases.

Timur Sağlam 4 lat temu
rodzic
commit
59a468b7d9

+ 39 - 0
jplag.frontend-testutils/src/test/java/de/jplag/testutils/TokenUtils.java

@@ -0,0 +1,39 @@
+package de.jplag.testutils;
+
+import static java.util.stream.Collectors.toList;
+
+import java.util.List;
+import java.util.stream.StreamSupport;
+
+import de.jplag.Token;
+import de.jplag.TokenList;
+
+public final class TokenUtils {
+
+    private TokenUtils() {
+        // private constructor to prevent instantiation
+    }
+
+    /**
+     * Returns the type of all tokens in a {@link TokenList} that belong to a file.
+     * @param tokens is the {@link TokenList}.
+     * @param name is the name of the target file.
+     * @return the immutable list of token types.
+     */
+    public static List<Integer> tokenTypesByFile(TokenList tokenList, String name) {
+        var tokens = tokensByFile(tokenList, name);
+        return tokens.stream().map(Token::getType).collect(toList());
+    }
+
+    /**
+     * Returns the tokens in a {@link TokenList} that belong to a file.
+     * @param tokens is the {@link TokenList}.
+     * @param name is the name of the target file.
+     * @return the immutable list of tokens.
+     */
+    public static List<Token> tokensByFile(TokenList tokenList, String name) {
+        var tokens = StreamSupport.stream(tokenList.allTokens().spliterator(), false);
+        return tokens.filter(it -> it.getFile().startsWith(name)).collect(toList());
+    }
+
+}

+ 9 - 2
jplag.frontend.emf-metamodel-dynamic/src/test/java/de/jplag/emf/dynamic/MinimalEmfFrontendTest.java → jplag.frontend.emf-metamodel-dynamic/src/test/java/de/jplag/emf/dynamic/MinimalDynamicMetamodelTest.java

@@ -2,6 +2,7 @@ package de.jplag.emf.dynamic;
 
 import static java.util.stream.Collectors.toList;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertIterableEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.File;
@@ -18,8 +19,9 @@ import org.slf4j.LoggerFactory;
 import de.jplag.TokenList;
 import de.jplag.TokenPrinter;
 import de.jplag.testutils.TestErrorConsumer;
+import de.jplag.testutils.TokenUtils;
 
-class MinimalEmfFrontendTest {
+class MinimalDynamicMetamodelTest {
     private final Logger logger = LoggerFactory.getLogger("JPlag-Test");
 
     private static final Path BASE_PATH = Path.of("src", "test", "resources", "de", "jplag", "models");
@@ -46,6 +48,12 @@ class MinimalEmfFrontendTest {
         logger.info("parsed tokens: " + result.allTokens().toString());
         assertEquals(7, DynamicMetamodelTokenConstants.getTokenStrings().size());
         assertEquals(64, result.size());
+
+        var bookstoreTokens = TokenUtils.tokenTypesByFile(result, TEST_SUBJECT[0]);
+        var bookstoreRenamedTokens = TokenUtils.tokenTypesByFile(result, TEST_SUBJECT[2]);
+        var bookstoreExtendedTokens = TokenUtils.tokenTypesByFile(result, TEST_SUBJECT[1]);
+        assertTrue(bookstoreTokens.size() < bookstoreExtendedTokens.size());
+        assertIterableEquals(bookstoreTokens, bookstoreRenamedTokens);
     }
 
     @AfterEach
@@ -53,5 +61,4 @@ class MinimalEmfFrontendTest {
         File baseFile = new File(BASE_PATH.toString());
         Arrays.stream(baseFile.listFiles()).filter(it -> it.getName().endsWith(Language.VIEW_FILE_SUFFIX)).forEach(File::delete);
     }
-
 }

+ 10 - 14
jplag.frontend.emf-metamodel-dynamic/src/test/resources/de/jplag/models/bookStore.ecore

@@ -1,20 +1,16 @@
 <?xml version="1.0" encoding="ASCII"?>
-<ecore:EPackage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="BookStorePackage" nsURI="http:///com.ibm.dynamic.example.bookstore.ecore" nsPrefix="bookStore">
+<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="BookStorePackage" nsURI="http:///com.ibm.dynamic.example.bookstore.ecore"
+    nsPrefix="bookStore">
   <eClassifiers xsi:type="ecore:EClass" name="BookStore">
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="owner">
-      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
-    </eStructuralFeatures>
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="location">
-      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
-    </eStructuralFeatures>
-    <eStructuralFeatures xsi:type="ecore:EReference" name="books" upperBound="-1" eType="//Book" containment="true"/>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="owner" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="location" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
+    <eStructuralFeatures xsi:type="ecore:EReference" name="books" upperBound="-1"
+        eType="#//Book" containment="true"/>
   </eClassifiers>
   <eClassifiers xsi:type="ecore:EClass" name="Book">
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name">
-      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
-    </eStructuralFeatures>
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="isbn">
-      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
-    </eStructuralFeatures>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="isbn" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"
+        iD="true"/>
   </eClassifiers>
 </ecore:EPackage>

+ 9 - 1
jplag.frontend.emf-metamodel/src/test/java/de/jplag/emf/MinimalEmfFrontendTest.java → jplag.frontend.emf-metamodel/src/test/java/de/jplag/emf/MinimalMetamodelTest.java

@@ -2,6 +2,7 @@ package de.jplag.emf;
 
 import static java.util.stream.Collectors.toList;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertIterableEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.File;
@@ -19,8 +20,9 @@ import org.slf4j.LoggerFactory;
 import de.jplag.TokenList;
 import de.jplag.TokenPrinter;
 import de.jplag.testutils.TestErrorConsumer;
+import de.jplag.testutils.TokenUtils;
 
-class MinimalEmfFrontendTest {
+class MinimalMetamodelTest {
     private final Logger logger = LoggerFactory.getLogger("JPlag-Test");
 
     private static final Path BASE_PATH = Path.of("src", "test", "resources", "de", "jplag", "models");
@@ -49,6 +51,12 @@ class MinimalEmfFrontendTest {
         logger.info("Parsed tokens: " + result.allTokens().toString());
         assertEquals(21, constants.size());
         assertEquals(43, result.size());
+
+        var bookstoreTokens = TokenUtils.tokenTypesByFile(result, TEST_SUBJECT[0]);
+        var bookstoreRenamedTokens = TokenUtils.tokenTypesByFile(result, TEST_SUBJECT[2]);
+        var bookstoreExtendedTokens = TokenUtils.tokenTypesByFile(result, TEST_SUBJECT[1]);
+        assertTrue(bookstoreTokens.size() < bookstoreExtendedTokens.size());
+        assertIterableEquals(bookstoreTokens, bookstoreRenamedTokens);
     }
 
     @AfterEach

+ 10 - 14
jplag.frontend.emf-metamodel/src/test/resources/de/jplag/models/bookStore.ecore

@@ -1,20 +1,16 @@
 <?xml version="1.0" encoding="ASCII"?>
-<ecore:EPackage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="BookStorePackage" nsURI="http:///com.ibm.dynamic.example.bookstore.ecore" nsPrefix="bookStore">
+<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="BookStorePackage" nsURI="http:///com.ibm.dynamic.example.bookstore.ecore"
+    nsPrefix="bookStore">
   <eClassifiers xsi:type="ecore:EClass" name="BookStore">
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="owner">
-      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
-    </eStructuralFeatures>
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="location">
-      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
-    </eStructuralFeatures>
-    <eStructuralFeatures xsi:type="ecore:EReference" name="books" upperBound="-1" eType="//Book" containment="true"/>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="owner" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="location" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
+    <eStructuralFeatures xsi:type="ecore:EReference" name="books" upperBound="-1"
+        eType="#//Book" containment="true"/>
   </eClassifiers>
   <eClassifiers xsi:type="ecore:EClass" name="Book">
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name">
-      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
-    </eStructuralFeatures>
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="isbn">
-      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
-    </eStructuralFeatures>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="isbn" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"
+        iD="true"/>
   </eClassifiers>
 </ecore:EPackage>

+ 1 - 0
jplag.frontend.emf-model/src/test/java/de/jplag/emf/model/BookStoreFactory.java

@@ -69,6 +69,7 @@ public class BookStoreFactory {
         bookName.setEType(theCorePackage.getEString());
         EAttribute bookISBN = theCoreFactory.createEAttribute();
         bookISBN.setName("isbn");
+        bookISBN.setID(true);
         bookISBN.setEType(theCorePackage.getEInt());
 
         bookStoreEClass.getEStructuralFeatures().add(bookStoreOwner);

+ 1 - 1
jplag.frontend.emf-model/src/test/java/de/jplag/emf/model/ModelInstanceTest.java → jplag.frontend.emf-model/src/test/java/de/jplag/emf/model/MinimalModelInstanceTest.java

@@ -22,7 +22,7 @@ import de.jplag.TokenPrinter;
 import de.jplag.emf.dynamic.DynamicMetamodelTokenConstants;
 import de.jplag.testutils.TestErrorConsumer;
 
-class ModelInstanceTest {
+class MinimalModelInstanceTest {
     private final Logger logger = LoggerFactory.getLogger("JPlag-Test");
 
     private static final Path BASE_PATH = Path.of("src", "test", "resources", "de", "jplag", "books");

+ 10 - 14
jplag.frontend.emf-model/src/test/resources/de/jplag/books/bookStore.ecore

@@ -1,20 +1,16 @@
 <?xml version="1.0" encoding="ASCII"?>
-<ecore:EPackage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="BookStorePackage" nsURI="http:///com.ibm.dynamic.example.bookstore.ecore" nsPrefix="bookStore">
+<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="BookStorePackage" nsURI="http:///com.ibm.dynamic.example.bookstore.ecore"
+    nsPrefix="bookStore">
   <eClassifiers xsi:type="ecore:EClass" name="BookStore">
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="owner">
-      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
-    </eStructuralFeatures>
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="location">
-      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
-    </eStructuralFeatures>
-    <eStructuralFeatures xsi:type="ecore:EReference" name="books" upperBound="-1" eType="//Book" containment="true"/>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="owner" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="location" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
+    <eStructuralFeatures xsi:type="ecore:EReference" name="books" upperBound="-1"
+        eType="#//Book" containment="true"/>
   </eClassifiers>
   <eClassifiers xsi:type="ecore:EClass" name="Book">
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name">
-      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
-    </eStructuralFeatures>
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="isbn">
-      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
-    </eStructuralFeatures>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="isbn" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"
+        iD="true"/>
   </eClassifiers>
 </ecore:EPackage>