summaryrefslogtreecommitdiffstats
path: root/testing/testsrc/org/aspectj/testing/harness/bridge/CompilerRunTest.java
blob: ddfaee1f4985c0fb51dedc87a083fc10132b2e9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* *******************************************************************
 * Copyright (c) 2003 Contributors.
 * All rights reserved. 
 * This program and the accompanying materials are made available 
 * under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors: 
 *     Wes Isberg     initial implementation
 * ******************************************************************/

package org.aspectj.testing.harness.bridge;

import java.io.File;
import java.util.*;

import junit.framework.TestCase;

import org.aspectj.bridge.*;
import org.aspectj.testing.run.*;
import org.aspectj.util.*;
import org.aspectj.util.LangUtil;

/**
 * Use a stub compiler/ICommand to verify command-line passed
 * to the compiler by the harness.
 */
public class CompilerRunTest extends TestCase {

    /** String for each dummy report: {run|repeat}: [{args}] */
    private static ArrayList dummyReports = new ArrayList();

    private static void dummyRunning(String[] args) {
        dummyReports.add("run: " + Arrays.asList(args));
    }
    
//    private static void dummyRepeating(String[] args) {
//        dummyReports.add("repeat: " + Arrays.asList(args));
//    }

    private File testBaseDir;
        
    public CompilerRunTest(String name) {
        super(name);
    }
    
    public void setUp() {
        testBaseDir = new File("../testing/temp-CompilerRunTest");
        File f = new File(testBaseDir, "one");
        f.mkdirs();
        assertTrue(f.canRead());
        f = new File(testBaseDir, "two");
        f.mkdirs();
        assertTrue(f.canRead());
        f = new File(testBaseDir, "Foo.java");
        String foo = "public class Foo { public void main(String[] s) { System.out.println(\"Hello!\");}}";
        String err = FileUtil.writeAsString(f, foo);
        assertTrue(err, null == err);
        assertTrue(f.canRead());
    }
    
    public void tearDown() {
        FileUtil.deleteContents(testBaseDir);
        testBaseDir.delete();
        testBaseDir = null;
    }
    
    public void testExtDirs() {
//        String[] globals = null;
        CompilerRun.Spec spec = new CompilerRun.Spec();
        spec.setExtdirs("one,two"); 
        spec.setFiles("Foo.java");
        checkCommandLine(testBaseDir, spec, null, "-extdirs");
    }

    void checkCommandLine(        
        File testBaseDir,
        CompilerRun.Spec spec, 
        String[] globals,
        String expectedInCommand) {
        assertTrue(0 == dummyReports.size());
        assertTrue(checkCompilerRun(testBaseDir, spec, globals, null));
        assertTrue(dummyReports.toString(), 1 == dummyReports.size());
        String command = (String) dummyReports.remove(0);
        assertTrue(0 == dummyReports.size());
        if ((null == command) 
            || (-1 == command.indexOf(expectedInCommand))) {
            assertTrue("expected " 
                + expectedInCommand 
                + "got "
                + command, 
                false);
        }
    }

    /** run with dummy compiler */
    boolean checkCompilerRun(
        File testBaseDir,
        CompilerRun.Spec spec, 
        String[] globals,
        MessageHandler handler) {
        LangUtil.throwIaxIfNull(spec, "spec");
        if (null == handler) {
            handler = new MessageHandler();
        }
        spec.setPermitAnyCompiler(true);
        spec.setCompiler(DummyCompiler.class.getName());
        AbstractRunSpec.RT parentRuntime = new AbstractRunSpec.RT();

        if (!LangUtil.isEmpty(globals)) {
            parentRuntime.setOptions(globals);
        }
        boolean adopted =
            spec.adoptParentValues(parentRuntime, handler);
        if (!adopted) {
            String s = "not adopted spec="
                    + spec
                    + " globals="
                    + (LangUtil.isEmpty(globals)
                        ? "[]"
                        : Arrays.asList(globals).toString())
                    + " -- "
                    + handler;
            assertTrue(s, false);
        }
        if (0 != handler.numMessages(null, true)) {
            assertTrue("unexpected " + handler, false);
        }
        return run(testBaseDir, spec);
    }
    
    /** Run the compiler run specified by the spec */
    protected boolean run(File testBaseDir, CompilerRun.Spec spec) {

//        his is created using the Spec.</li>
//         * <li>setupAjcRun(Sandbox, Validator) is invoked,
//         *     at which point this populates the shared sandbox
//         *     with values derived from the spec and also
//         *     sets up internal state based on both the sandbox
//         *     and the spec.</li>
//         * <li>run(IRunStatus) is invoked,

        LangUtil.throwIaxIfNull(spec, "spec");
        Runner runner = new Runner();
        IMessageHolder holder = new MessageHandler();
        RunStatus status = new RunStatus(holder, runner);
        status.setIdentifier(spec);
        Validator validator = new Validator(status);
        validator.lock(this);
        Sandbox sandbox = null;
        try {
            sandbox = new Sandbox(testBaseDir, validator);
            IRunIterator test = spec.makeRunIterator(sandbox, validator);
            return runner.runIterator(test, status, null);
        } finally {
            validator.unlock(this);
            validator.deleteTempFiles(true);
        }
    }
 
    
    public static class DummyCompiler implements ICommand {
        private String[] command;
        
        public DummyCompiler() {
        }
        
        public boolean runCommand(
            String[] args,
            IMessageHandler handler) {
            command = (String[]) LangUtil.safeCopy(args, new String[0]);
            CompilerRunTest.dummyRunning(command);
            return true;
        }
        
        public boolean repeatCommand(IMessageHandler handler) {
            CompilerRunTest.dummyRunning(command);
            return true;
        }
    }
}