Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2015 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2015 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.openide.loaders; |
43 |
|
44 |
import java.awt.EventQueue; |
45 |
import java.io.IOException; |
46 |
import java.lang.reflect.InvocationTargetException; |
47 |
import java.util.ArrayList; |
48 |
import java.util.Collections; |
49 |
import java.util.List; |
50 |
import static junit.framework.TestCase.assertEquals; |
51 |
import static junit.framework.TestCase.assertNotNull; |
52 |
import org.junit.Test; |
53 |
import org.openide.filesystems.FileObject; |
54 |
import org.openide.filesystems.FileSystem; |
55 |
import org.openide.filesystems.FileUtil; |
56 |
|
57 |
/** |
58 |
* |
59 |
* @author jhavlin |
60 |
*/ |
61 |
public class FolderComparatorTest { |
62 |
|
63 |
public FolderComparatorTest() { |
64 |
} |
65 |
|
66 |
@Test |
67 |
public void testNaturalComparatorBasic() throws IOException { |
68 |
testNaturalComparator(new String[]{ |
69 |
"b 10.txt", |
70 |
"b 9.txt", |
71 |
"a2.txt", |
72 |
"a 4 9.txt", |
73 |
"a10.txt", |
74 |
"b0070.txt", |
75 |
"a 3.txt", |
76 |
"b08.txt" |
77 |
}, new String[]{ |
78 |
"a2.txt", |
79 |
"a 3.txt", |
80 |
"a 4 9.txt", |
81 |
"a10.txt", |
82 |
"b08.txt", |
83 |
"b 9.txt", |
84 |
"b 10.txt", |
85 |
"b0070.txt" |
86 |
}); |
87 |
} |
88 |
|
89 |
@Test |
90 |
public void testNaturalComparatorWithSuffixes() throws IOException { |
91 |
testNaturalComparator(new String[]{ |
92 |
"a01b", |
93 |
"a2x", |
94 |
"a02", |
95 |
"a1" |
96 |
}, new String[]{ |
97 |
"a1", |
98 |
"a01b", |
99 |
"a02", |
100 |
"a2x" |
101 |
}); |
102 |
} |
103 |
|
104 |
@Test |
105 |
public void testUseCustomComparator() throws IOException, |
106 |
InterruptedException, InvocationTargetException { |
107 |
|
108 |
FileSystem fs = FileUtil.createMemoryFileSystem(); |
109 |
|
110 |
fs.getRoot().createData("aaaa.txt"); |
111 |
fs.getRoot().createData("bbb.txt"); |
112 |
fs.getRoot().createData("cc.txt"); |
113 |
fs.getRoot().createData("d.txt"); |
114 |
fs.getRoot().refresh(); |
115 |
|
116 |
DataFolder.SortMode custom = new DataFolder.SortMode() { |
117 |
@Override |
118 |
public int compare(DataObject o1, DataObject o2) { |
119 |
return o1.getName().length() - o2.getName().length(); |
120 |
} |
121 |
}; |
122 |
|
123 |
DataFolder df = DataFolder.findFolder(fs.getRoot()); |
124 |
df.setSortMode(custom); |
125 |
EventQueue.invokeAndWait(new Runnable() { |
126 |
@Override |
127 |
public void run() { |
128 |
} |
129 |
}); |
130 |
DataObject[] children = df.getChildren(); |
131 |
assertEquals("d.txt", children[0].getName()); |
132 |
assertEquals("cc.txt", children[1].getName()); |
133 |
assertEquals("bbb.txt", children[2].getName()); |
134 |
assertEquals("aaaa.txt", children[3].getName()); |
135 |
} |
136 |
|
137 |
@Test |
138 |
public void testNaturalComparatorFallback() throws IOException { |
139 |
testNaturalComparator(new String[]{ |
140 |
"a01.txt", |
141 |
"a001.txt", |
142 |
"A1.txt" |
143 |
}, new String[]{ |
144 |
"A1.txt", |
145 |
"a001.txt", |
146 |
"a01.txt" |
147 |
}); |
148 |
} |
149 |
|
150 |
private void testNaturalComparator(String[] fileNames, |
151 |
String[] expectedOrder) throws IOException { |
152 |
FolderComparator c = new FolderComparator(FolderComparator.NATURAL); |
153 |
FileSystem fs = FileUtil.createMemoryFileSystem(); |
154 |
FileObject root = fs.getRoot(); |
155 |
List<DataObject> list = new ArrayList<DataObject>(); |
156 |
for (String n : fileNames) { |
157 |
FileObject fo = root.createData(n); |
158 |
assertNotNull(fo); |
159 |
list.add(DataObject.find(fo)); |
160 |
} |
161 |
|
162 |
Collections.sort(list, c); |
163 |
for (int i = 0; i < expectedOrder.length; i++) { |
164 |
assertEquals(expectedOrder[i], list.get(i).getName()); |
165 |
} |
166 |
} |
167 |
} |