Skip to main content
summaryrefslogtreecommitdiffstats
blob: f4683248a5dd537529af312bc5a81823186dc92d (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
package org.eclipse.wst.xml.core.tests.model;

import java.io.File;
import java.io.IOException;
import java.util.List;

import junit.framework.TestCase;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.xml.core.internal.modelhandler.XMLModelLoader;
import org.eclipse.wst.xml.core.tests.util.FileUtil;
import org.eclipse.wst.xml.core.tests.util.ProjectUnzipUtility;

/**
 *
 * 
 */
public class TestXMLModelLoader extends TestCase {
	
	private boolean isSetup = false;
	private XMLModelLoader fLoader = null;

	private final String fProjectName ="DOCUMENT-LOADER";
	private final String fZipFileName = "xml-document-loader-tests.zip";
	
	
    public TestXMLModelLoader() {
        super("TestModelLoader");
    }
	
    protected void setUp() throws Exception {
		
    	super.setUp();
		if(!this.isSetup){
			fLoader = new XMLModelLoader();
			doSetup();
			this.isSetup = true;
		}
    }
	
	private void doSetup() throws Exception {	
	
		// root of workspace directory
		Location platformLocation = Platform.getInstanceLocation();
		
		ProjectUnzipUtility unzipUtil = new ProjectUnzipUtility();
		File zipFile = FileUtil.makeFileFor(ProjectUnzipUtility.PROJECT_ZIPS_FOLDER, fZipFileName, ProjectUnzipUtility.PROJECT_ZIPS_FOLDER);
		unzipUtil.unzipAndImport(zipFile, platformLocation.getURL().getFile());
		unzipUtil.initJavaProject(fProjectName);
	}
	
	public void testCreateEmptyModel() {
		IStructuredModel model = fLoader.createModel();
		assertNotNull("model is null", model);
	}
	
	public void testCreateModelWithDocument() {
		IStructuredModel emptyModel = fLoader.createModel();
		IStructuredDocument sDoc = emptyModel.getStructuredDocument();
		sDoc.set("<?xml version=\"1.0\"?>\n<test>\n<item attr=\"val\" /></test>\n");
		
		// null handlder is normally not valid, including only for this test.
		IStructuredModel modelFromDoc = fLoader.createModel(sDoc, "/test", null);
		assertNotNull("model from doc is null", modelFromDoc);
	}
	
	public void testCreateModelWithModel() {
		IStructuredModel emptyModel = fLoader.createModel();
		IStructuredDocument sDoc = emptyModel.getStructuredDocument();
		sDoc.set("<?xml version=\"1.0\"?>\n<test>\n<item attr=\"val\" /></test>\n");
		// TODO: need an existing full model
		//IStructuredModel modelFromModel = fLoader.createModel(emptyModel);
		//assertNotNull("model from model is null", modelFromModel);
	}
	
	public void testGetAdapterFactories() {
		List factories = fLoader.getAdapterFactories();
		assertTrue("there were no adapter factories for XML", factories.size() > 0);
	}
	
	public void testLoad() {
		// from a file	
		IFile f = getFile();
		
		try {
			IStructuredModel model = fLoader.createModel();
			fLoader.load(f, model);
			String text = model.getStructuredDocument().get();
			assertTrue("failed to load", text.length() > 0);
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		catch (CoreException e) {
			e.printStackTrace();
		}
	}
	
	public void testLoadFromStream() {
		// need to fix this test
		// unless the API is going away...
//		IFile f = getFile();
//		
//		try {
//			IStructuredModel model = fLoader.createModel();
//			fLoader.load(f.getContents(), model, EncodingRule.FORCE_DEFAULT);
//			String text = model.getStructuredDocument().get();
//			assertTrue("failed to load", text.length() > 0);
//		}
//		catch (IOException e) {
//			e.printStackTrace();
//		}
//		catch (CoreException e) {
//			e.printStackTrace();
//		}
	}
	
	
	private IFile getFile() {
		return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(fProjectName + "/files/simple.xml"));	
	}
}

Back to the top