Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c8745ad076816443e944ff144e3cc9652761ac9a (plain) (blame)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*******************************************************************************
 * Copyright (c) 2000, 2006 QNX Software Systems and others.
 * 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:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.core.model.tests;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.eclipse.cdt.core.model.IArchive;
import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.CTestPlugin;
import org.eclipse.cdt.core.testplugin.util.ExpectedStrings;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;

/**
 * @author Peter Graves
 *
 * This file contains a set of generic tests for the core C model's Archive
 * class. There is nothing exotic here, mostly just sanity type tests
 *
 */
public class ArchiveTests extends TestCase {
    IWorkspace workspace;
    IWorkspaceRoot root;
    ICProject testProject;
    IFile cfile, exefile, libfile, archfile, objfile;
    Path cpath, exepath, libpath, archpath, objpath;
    NullProgressMonitor monitor;
    

    /**
     * Constructor for ArchiveTests
     * @param name
     */
    public ArchiveTests(String name) {
        super(name);
        /***
     * The assume that they have a working workspace
     * and workspace root object to use to create projects/files in, 
     * so we need to get them setup first.
     */
        workspace= ResourcesPlugin.getWorkspace();
        root= workspace.getRoot();
        monitor = new NullProgressMonitor();
        if (workspace==null) 
            fail("Workspace was not setup");
        if (root==null)
            fail("Workspace root was not setup");

    }
    
    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     * 
     * Example code test the packages in the project 
     *  "com.qnx.tools.ide.cdt.core"
     */
    protected void setUp() throws Exception {
            
        /***
         * Setup the various files, paths and projects that are needed by the
         * tests
         */
        testProject=CProjectHelper.createCProject("filetest", "none");
        if (testProject==null)
            fail("Unable to create project");

        cfile = testProject.getProject().getFile("exetest.c");
        if (!cfile.exists()) {
            cfile.create(new FileInputStream(
            		CTestPlugin.getDefault().getFileInPlugin(new Path("resources/exe/main.c"))),
					false, monitor);
        }
        cpath=new Path(workspace.getRoot().getLocation()+"/filetest/main.c");

        objfile = testProject.getProject().getFile("exetest.o");
        if (!objfile.exists()) {
            objfile.create(new FileInputStream(
            		CTestPlugin.getDefault().getFileInPlugin(new Path("resources/exe/x86/o.g/main.o"))),
					false, monitor);
        }
        objpath=new Path(workspace.getRoot().getLocation()+"/filetest/main.o");
        
        exefile = testProject.getProject().getFile("test_g");
        if (!exefile.exists()) {
            exefile.create(new FileInputStream(
            		CTestPlugin.getDefault().getFileInPlugin(new Path("resources/exe/x86/o.g/exe_g"))),
					false, monitor);
        }
        exepath=new Path(workspace.getRoot().getLocation()+"/filetest/exe_g");
        
        archfile = testProject.getProject().getFile("libtestlib_g.a");
        if (!archfile.exists()) {
            archfile.create(new FileInputStream(
            		CTestPlugin.getDefault().getFileInPlugin(new Path("resources/testlib/x86/a.g/libtestlib_g.a"))),
					false, monitor);
        }
        libpath=new Path(workspace.getRoot().getLocation()+"/filetest/libtestlib_g.so");
        
        libfile = testProject.getProject().getFile("libtestlib_g.so");
        if (!libfile.exists()) {
            libfile.create(new FileInputStream(
            		CTestPlugin.getDefault().getFileInPlugin(new Path("resources/testlib/x86/so.g/libtestlib_g.so"))),
					false, monitor);
        }
        archpath=new Path(workspace.getRoot().getLocation()+"/filetest/libtestlib_g.a");
	    }
    
     /**
     * Tears down the test fixture.
     *
     * Called after every test case method.
     */
    protected void tearDown()  {
		  CProjectHelper.delete(testProject);
    }
    
    public static TestSuite suite() {
    return new TestSuite(ArchiveTests.class);
    }
    
    public static void main (String[] args){
    junit.textui.TestRunner.run(suite());
    }


        
    public void testGetBinaries() throws CoreException,FileNotFoundException {
        IArchive myArchive;
        IBinary[] bins;
        ICElement[] elements;
        ExpectedStrings expBin, expObj[];
        String[] myStrings;
        int x;

        
        /****
         * Setup the expected strings for the binaries, and the elements within 
         * the binaries
         */
        myStrings=new String[2];
        myStrings[0]="test.o";
        myStrings[1]="test2.o";
        expBin=new ExpectedStrings(myStrings);

        expObj=new ExpectedStrings[2];
        myStrings[0]="func1";
        myStrings[1]="func2";
        expObj[0]=new ExpectedStrings(myStrings);
        myStrings[0]="test2func1";
        myStrings[1]="test2func2";
        expObj[1]=new ExpectedStrings(myStrings);

        /***
         * Grab the archive we want to test, and find all the binaries and 
         * all the elements in all the binaries and make sure we get 
         * everything we expect.
         */
        myArchive=CProjectHelper.findArchive(testProject, "libtestlib_g.a");
        if (myArchive==null)
            fail("Could not find archive");
        bins=myArchive.getBinaries();
        for (x=0;x<bins.length;x++) {
            expBin.foundString(bins[x].getElementName());
            elements=bins[x].getChildren();
            for (int i=0;i<elements.length;i++) {
                expObj[x].foundString(elements[i].getElementName());
            }
        }
        
        assertTrue(expBin.getMissingString(), expBin.gotAll());
        assertTrue(expBin.getExtraString(), !expBin.gotExtra());
        for (x=0;x<expObj.length;x++) {
            assertTrue("Binary " + expBin.expStrings[x] + " "  +expObj[x].getMissingString(), expObj[x].gotAll());
            assertTrue("Binary " + expBin.expStrings[x] + " " + expObj[x].getExtraString(), !expObj[x].gotExtra());
        }
    }
    /***
     *  Simple sanity test to make sure Archive.isArchive returns true
     *  
     */
    public void testIsArchive() throws CoreException,FileNotFoundException {
        IArchive myArchive;
        myArchive=CProjectHelper.findArchive(testProject, "libtestlib_g.a");

        assertTrue("A archive", myArchive != null);
        myArchive=null;


    }

}

Back to the top