Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c6a657fdcc830b0f49a8d289093071332863c2c2 (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
/*******************************************************************************
 *  Copyright (c) 2011  Oracle. 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: 
 *  	Oracle - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.jaxb.core.tests.internal.resource;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jpt.common.core.tests.internal.utility.jdt.AnnotationTestCase;
import org.eclipse.jpt.common.utility.internal.iterable.ArrayIterable;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.jaxb.core.internal.resource.jaxbindex.JaxbIndexResourceModelProvider;
import org.eclipse.jpt.jaxb.core.resource.jaxbindex.JaxbIndexResource;


public class JaxbIndexResourceTests
		extends AnnotationTestCase {
	
	private static String JAXB_INDEX = "jaxb.index";
	
	
	public JaxbIndexResourceTests(String name) {
		super(name);
	}
	
	
	private IFile createJaxbIndex(IPath projectRelativePath, String... classNames) throws Exception {
		IFolder folder = getJavaProject().getProject().getFolder(projectRelativePath);
		if (! folder.exists()) {
			folder.create(true, false, null);
		}
		IFile jaxbIndex = getJavaProject().getProject().getFile(projectRelativePath.append(new Path(JAXB_INDEX)));
		InputStream stream = inputStream(classNames);
		jaxbIndex.create(stream, true, null);
		return jaxbIndex;
	}
	
	private void setClassNames(IFile jaxbIndex, String... classNames) throws Exception {
		jaxbIndex.setContents(inputStream(classNames), true, false, null);
	}
	
	private InputStream inputStream(String... classNames) {
		StringBuffer sb = new StringBuffer();
		for (String className : classNames) {
			sb.append(className + CR);
		}
		return new ByteArrayInputStream(sb.toString().getBytes());
	}
	
	public void testUpdateClasses() throws Exception {
		IFile jaxbIndex = createJaxbIndex(new Path("src/test"), "foo", "bar");
		JaxbIndexResource resource = JaxbIndexResourceModelProvider.instance().buildResourceModel(jaxbIndex);
		
		assertTrue(IterableTools.elementsAreEqual(
				resource.getFullyQualifiedClassNames(), 
				new ArrayIterable<String>(new String[] {"test.foo", "test.bar"})));
		
		setClassNames(jaxbIndex, "foo", "bar", "baz");
		
		assertTrue(IterableTools.elementsAreEqual(
				resource.getFullyQualifiedClassNames(), 
				new ArrayIterable<String>(new String[] {"test.foo", "test.bar", "test.baz"})));
		
		setClassNames(jaxbIndex);
		
		assertTrue(IterableTools.isEmpty(resource.getFullyQualifiedClassNames()));
		
		jaxbIndex = createJaxbIndex(new Path("src"), "foo", "bar");
		resource = JaxbIndexResourceModelProvider.instance().buildResourceModel(jaxbIndex);
		
		assertTrue(IterableTools.elementsAreEqual(
				resource.getFullyQualifiedClassNames(), 
				new ArrayIterable<String>(new String[] {"foo", "bar"})));
		
		setClassNames(jaxbIndex, "foo", "bar", "baz");
		
		assertTrue(IterableTools.elementsAreEqual(
				resource.getFullyQualifiedClassNames(), 
				new ArrayIterable<String>(new String[] {"foo", "bar", "baz"})));
		
		setClassNames(jaxbIndex);
		
		assertTrue(IterableTools.isEmpty(resource.getFullyQualifiedClassNames()));
	}
}

Back to the top