Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 21309f61062584916840c026491607c436a3a38d (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
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.generator.base;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import org.eclipse.etrice.generator.fsm.base.FolderFilter;
import org.eclipse.etrice.generator.fsm.base.GenDir;
import org.eclipse.etrice.generator.fsm.base.GenFile;
import org.eclipse.etrice.generator.fsm.base.GenFileTreeBuilder;
import org.eclipse.etrice.generator.fsm.base.GenItem;
import org.junit.Test;

/**
 * @author Henrik Rentz-Reichert
 *
 */
public class TestGenFileTreeBuilder {
	
	private static final String[] files = {
		"/path/to/my/project/sub1/foo/a.txt",
		"/path/to/my/project/sub1/foo/b.txt",
		"/path/to/my/project/sub1/foo/c.txt",
		"/path/to/my/project/sub1/bar/x.txt",
		"/path/to/my/project/sub1/bar/y.txt",
		"/path/to/my/project/sub1/bar/z.txt",
		"/path/to/my/project/sub2/dir1/f.txt",
		"/path/to/my/project/sub2/dir1/g.txt",
		"/path/to/my/project/sub2/dir2/k.txt",
		"/path/to/my/project/sub2/dir2/l.txt",
		"/path/to/my/project/sub2/m.txt",
		"/path/to/my/project/sub2/n.txt"
	};

	private void collectGenFiles(GenDir root, HashMap<String, GenFile> genFiles) {
		for (GenItem item : root.getContents()) {
			if (item instanceof GenDir)
				collectGenFiles((GenDir) item, genFiles);
			else if (item instanceof GenFile)
				genFiles.put(item.getPath(), (GenFile) item);
			else
				fail("unexpected sub type");
		}
	}
	
	@Test
	public void testWithoutFilter() {
		GenFileTreeBuilder builder = new GenFileTreeBuilder("/path/to/my/project/", Arrays.asList(files), null);
		GenDir root = builder.getGenFileTree();
		assertNotNull("root available", root);
		assertEquals("name", "root", root.getName());
		
		HashMap<String, GenFile> genFiles = new HashMap<String, GenFile>();
		collectGenFiles(root, genFiles);
		
		GenFile f = genFiles.get("sub2/dir1/f.txt");
		assertNotNull("f.txt", f);
		
		f = genFiles.get("sub2/n.txt");
		assertNotNull("n.txt", f);
	}
	
	@Test
	public void testWithFilter() {
		ArrayList<String> folders = new ArrayList<String>();
		folders.add("sub2/dir1");
		folders.add("sub1");
		FolderFilter ff = new FolderFilter(folders);
		GenFileTreeBuilder builder = new GenFileTreeBuilder("/path/to/my/project/", Arrays.asList(files), ff);
		GenDir root = builder.getGenFileTree();
		assertNotNull("root available", root);
		assertEquals("name", "root", root.getName());
		
		HashMap<String, GenFile> genFiles = new HashMap<String, GenFile>();
		collectGenFiles(root, genFiles);
		
		GenFile f = genFiles.get("sub2/dir1/f.txt");
		assertNotNull("f.txt", f);
		
		f = genFiles.get("sub2/n.txt");
		assertNull("n.txt", f);
	}
}

Back to the top