Project

General

Profile

root / trunk / compiler / ooasCompiler_tests / src / org / momut / ooas / test / utils / OoasTest.java @ 7

1
package org.momut.ooas.test.utils;
2

    
3
import java.io.ByteArrayInputStream;
4
import java.io.File;
5
import java.io.FileOutputStream;
6
import java.io.IOException;
7
import java.nio.file.Files;
8
import java.nio.file.Paths;
9

    
10
import org.junit.Assert;
11
import org.momut.ooas.Compiler;
12
import org.momut.ooas.Compiler.ILogCallBack;
13
import org.momut.ooas.CompilerConfiguration.Backend;
14

    
15

    
16

    
17
/**
18
 * Base class for MoMuT Test classes
19
 * @author DemetzM
20
 * @author KrennW
21
 *
22
 */
23
public abstract class OoasTest {
24
        /// static stuff
25
        private static final String  s_COMPILER_TEMP_DIR = "ooasComp.temp.dir";
26
        private static final String  s_tempDir        = System.getProperty(OoasTest.s_COMPILER_TEMP_DIR);
27
        private static final boolean s_hasTempDir     = s_tempDir != null && !s_tempDir.isEmpty();
28

    
29
        protected static final String s_modelsDir = "org/momut/ooas/test/models";
30
        protected static final String s_standardDir = s_modelsDir + "/standard/";
31
        protected static final String s_complexDir = s_modelsDir + "/complex/";
32

    
33
        protected static File createTempDirectory(String dirName) throws IOException {
34
                return Files.createTempDirectory(dirName).toFile();
35
        }
36

    
37
        protected static File createTempDirectory(String prefix, String directoryName) throws IOException {
38
                final File file = new File(prefix);
39
                if (!file.exists())
40
                        file.mkdirs();
41

    
42
                return Files.createTempDirectory(Paths.get(prefix), directoryName).toFile();
43
        }
44

    
45

    
46
        /**
47
         * Creates an temporary directory either in the system's default
48
         * temp directory or in the directory specified via the "momut.temp.dir"
49
         * environment variable.
50
         * @param clazz A class. Used to name the directory: "MoMuT_" + clazz.getSimpleName()
51
         * @return file object pointing to the newly created temp. dir.
52
         * @throws IOException
53
         */
54
        protected static File createOutputDirectory(Class<?> clazz) throws IOException {
55
                final String dirName = "MoMuT_" + clazz.getSimpleName() + "_";
56
                if (s_hasTempDir)
57
                        return createTempDirectory(s_tempDir, dirName);
58
                return createTempDirectory(dirName);
59
        }
60

    
61

    
62
        /// instance stuff
63
        public final String m_testName;
64
        public final String m_model;
65

    
66

    
67
        public OoasTest(String testName, String model) {
68
                m_testName = testName;
69
                m_model = model;
70
        }
71

    
72

    
73
        /** Run the OOAS Compiler and check whether there were any errors during compilation.
74
         * @param outputDir
75
         * @throws IOException */
76
        private static void runOoasCompiler(String testName, File ooasFile, Backend backend, File outputDir) throws IOException {
77
                final byte[] ooasInput = Files.readAllBytes(ooasFile.toPath());// ooasCode.getBytes("UTF-8");
78
                final FileOutputStream compilerFileOutput = new FileOutputStream(new File(outputDir, String.format("%s.compiler", testName.replace("/", "."))));
79
                final Compiler compiler = new Compiler(
80
                                testName,
81
                                new ByteArrayInputStream(ooasInput),
82
                                compilerFileOutput,
83
                                backend);
84
                final int errors = compiler.compile( new ILogCallBack(){
85
                        @Override
86
                        public void logError(boolean toConsole, String message) {
87
                                System.out.println(message);
88
                        }} );
89
                compilerFileOutput.flush();
90
                compilerFileOutput.close();
91
                if (errors != 0) {
92
                        System.out.println("##Faulty OOAS produced for " + testName);
93
                }
94
                Assert.assertEquals(
95
                                String.format("Errors during ooas compiler run: %s", compiler.toString()),
96
                                0,
97
                                errors);
98
        }
99

    
100
        public static void runOoasCompiler(String testName, File ooasFile, File outputDir, Backend backend) throws IOException {
101
                runOoasCompiler(testName, ooasFile, backend, outputDir);
102
        }
103

    
104
}