Line 0
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.After; |
53 |
import org.junit.Before; |
54 |
import org.junit.Test; |
55 |
import org.openide.filesystems.FileObject; |
56 |
import org.openide.filesystems.FileSystem; |
57 |
import org.openide.filesystems.FileUtil; |
58 |
|
59 |
/** |
60 |
* |
61 |
* @author jhavlin |
62 |
*/ |
63 |
public class FolderComparatorCaseInsensitiveTest { |
64 |
|
65 |
private String backup; |
66 |
|
67 |
public FolderComparatorCaseInsensitiveTest() { |
68 |
} |
69 |
|
70 |
@Before |
71 |
public void _before() { |
72 |
backup = System.getProperty("org.netbeans.modules.masterfs.case"); |
73 |
System.setProperty("org.netbeans.modules.masterfs.case", "insensitive"); |
74 |
} |
75 |
|
76 |
@After |
77 |
public void _after() { |
78 |
if (null != backup) { |
79 |
System.setProperty("org.netbeans.modules.masterfs.case", backup); |
80 |
} |
81 |
} |
82 |
|
83 |
|
84 |
@Test |
85 |
public void testNaturalComparatorBasic() throws IOException { |
86 |
testNaturalComparator(new String[]{ |
87 |
"b 10.txt", |
88 |
"b 9.txt", |
89 |
"a2.txt", |
90 |
"a 4 9.txt", |
91 |
"a10.txt", |
92 |
"b0070.txt", |
93 |
"a 3.txt", |
94 |
"b08.txt" |
95 |
}, new String[]{ |
96 |
"a2.txt", |
97 |
"a 3.txt", |
98 |
"a 4 9.txt", |
99 |
"a10.txt", |
100 |
"b08.txt", |
101 |
"b 9.txt", |
102 |
"b 10.txt", |
103 |
"b0070.txt" |
104 |
}); |
105 |
} |
106 |
|
107 |
@Test |
108 |
public void testNaturalComparatorWithSuffixes() throws IOException { |
109 |
testNaturalComparator(new String[]{ |
110 |
"a01b", |
111 |
"a2x", |
112 |
"a02", |
113 |
"a1" |
114 |
}, new String[]{ |
115 |
"a1", |
116 |
"a01b", |
117 |
"a02", |
118 |
"a2x" |
119 |
}); |
120 |
} |
121 |
|
122 |
@Test |
123 |
public void testUseCustomComparator() throws IOException, |
124 |
InterruptedException, InvocationTargetException { |
125 |
|
126 |
FileSystem fs = FileUtil.createMemoryFileSystem(); |
127 |
|
128 |
fs.getRoot().createData("aaaa.txt"); |
129 |
fs.getRoot().createData("bbb.txt"); |
130 |
fs.getRoot().createData("cc.txt"); |
131 |
fs.getRoot().createData("d.txt"); |
132 |
fs.getRoot().refresh(); |
133 |
|
134 |
DataFolder.SortMode custom = new DataFolder.SortMode() { |
135 |
@Override |
136 |
public int compare(DataObject o1, DataObject o2) { |
137 |
return o1.getName().length() - o2.getName().length(); |
138 |
} |
139 |
}; |
140 |
|
141 |
DataFolder df = DataFolder.findFolder(fs.getRoot()); |
142 |
df.setSortMode(custom); |
143 |
EventQueue.invokeAndWait(new Runnable() { |
144 |
@Override |
145 |
public void run() { |
146 |
} |
147 |
}); |
148 |
DataObject[] children = df.getChildren(); |
149 |
assertEquals("d.txt", children[0].getName()); |
150 |
assertEquals("cc.txt", children[1].getName()); |
151 |
assertEquals("bbb.txt", children[2].getName()); |
152 |
assertEquals("aaaa.txt", children[3].getName()); |
153 |
} |
154 |
|
155 |
@Test |
156 |
public void testNaturalComparatorFallback() throws IOException { |
157 |
testNaturalComparator(new String[]{ |
158 |
"a01.txt", |
159 |
"a001.txt", |
160 |
"A1.txt" |
161 |
}, new String[]{ |
162 |
"a001.txt", |
163 |
"a01.txt", |
164 |
"A1.txt", |
165 |
}); |
166 |
} |
167 |
|
168 |
/** |
169 |
* #139753 - test if "org.netbeans.modules.masterfs.case=insensitive" is used. |
170 |
* |
171 |
* @throws IOException |
172 |
*/ |
173 |
@Test |
174 |
public void testCaseSensitivity() throws IOException { |
175 |
testComparator(new String[]{ |
176 |
"CustomerInvoice.txt", |
177 |
"Customerappro.txt", |
178 |
"CustomerABC.txt", |
179 |
"Customerinterview.txt" |
180 |
}, new String[]{ |
181 |
"CustomerABC.txt", |
182 |
"Customerappro.txt", |
183 |
"Customerinterview.txt", |
184 |
"CustomerInvoice.txt",}, FolderComparator.FOLDER_NAMES); |
185 |
} |
186 |
|
187 |
private void testComparator(String[] fileNames, String[] expectedOrder, int mode) throws IOException { |
188 |
FolderComparator c = new FolderComparator(mode); |
189 |
FileSystem fs = FileUtil.createMemoryFileSystem(); |
190 |
FileObject root = fs.getRoot(); |
191 |
List<DataObject> list = new ArrayList<DataObject>(); |
192 |
for (String n : fileNames) { |
193 |
FileObject fo = root.createData(n); |
194 |
assertNotNull(fo); |
195 |
list.add(DataObject.find(fo)); |
196 |
} |
197 |
|
198 |
Collections.sort(list, c); |
199 |
for (int i = 0; i < expectedOrder.length; i++) { |
200 |
assertEquals(expectedOrder[i], list.get(i).getName()); |
201 |
} |
202 |
} |
203 |
|
204 |
private void testNaturalComparator(String[] fileNames, |
205 |
String[] expectedOrder) throws IOException { |
206 |
testComparator(fileNames, expectedOrder, FolderComparator.NATURAL); |
207 |
} |
208 |
} |