Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 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 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.api.queries; |
43 |
|
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.net.URI; |
47 |
import java.net.URISyntaxException; |
48 |
import org.netbeans.api.queries.SharabilityQuery.Sharability; |
49 |
import org.netbeans.junit.MockServices; |
50 |
import org.netbeans.junit.NbTestCase; |
51 |
import org.netbeans.spi.queries.SharabilityQueryImplementation; |
52 |
import org.netbeans.spi.queries.SharabilityQueryImplementation2; |
53 |
import org.openide.filesystems.FileObject; |
54 |
import org.openide.filesystems.FileUtil; |
55 |
|
56 |
/** |
57 |
* |
58 |
* @author Alexander Simon |
59 |
*/ |
60 |
public class SharabilityQueryTest extends NbTestCase { |
61 |
|
62 |
public SharabilityQueryTest(String testMethod) { |
63 |
super (testMethod); |
64 |
} |
65 |
|
66 |
@Override |
67 |
public void setUp() throws IOException { |
68 |
clearWorkDir(); |
69 |
MockServices.setServices(SharabilityQueryImplementationImpl.class, SharabilityQueryImplementation2Impl.class); |
70 |
} |
71 |
|
72 |
public void testSharableBridge2Old() throws IOException { |
73 |
File file = new File("/home/user/aFile.sharable"); |
74 |
int sharability = SharabilityQuery.getSharability(file); |
75 |
assertEquals(SharabilityQuery.SHARABLE, sharability); |
76 |
URI uri = file.toURI(); |
77 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
78 |
assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability2); |
79 |
FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "sharable"); |
80 |
assertEquals(sharability2, SharabilityQuery.getSharability(fo)); |
81 |
} |
82 |
|
83 |
public void testSharableBridge2New() throws IOException { |
84 |
File file = new File("/home/user/aFile.sharable2"); |
85 |
int sharability = SharabilityQuery.getSharability(file); |
86 |
assertEquals(SharabilityQuery.SHARABLE, sharability); |
87 |
URI uri = file.toURI(); |
88 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
89 |
assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability2); |
90 |
FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "sharable2"); |
91 |
assertEquals(sharability2, SharabilityQuery.getSharability(fo)); |
92 |
} |
93 |
|
94 |
public void testNotSharableBridge2Old() throws IOException { |
95 |
File file = new File("/home/user/aFile.not_sharable"); |
96 |
int sharability = SharabilityQuery.getSharability(file); |
97 |
assertEquals(SharabilityQuery.NOT_SHARABLE, sharability); |
98 |
URI uri = file.toURI(); |
99 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
100 |
assertEquals(SharabilityQuery.Sharability.NOT_SHARABLE, sharability2); |
101 |
FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "not_sharable"); |
102 |
assertEquals(sharability2, SharabilityQuery.getSharability(fo)); |
103 |
} |
104 |
|
105 |
public void testNotSharableBridge2New() throws IOException { |
106 |
File file = new File("/home/user/aFile.not_sharable2"); |
107 |
int sharability = SharabilityQuery.getSharability(file); |
108 |
assertEquals(SharabilityQuery.NOT_SHARABLE, sharability); |
109 |
URI uri = file.toURI(); |
110 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
111 |
assertEquals(SharabilityQuery.Sharability.NOT_SHARABLE, sharability2); |
112 |
FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "not_sharable2"); |
113 |
assertEquals(sharability2, SharabilityQuery.getSharability(fo)); |
114 |
} |
115 |
|
116 |
public void testMixedBridge2Old() throws IOException { |
117 |
File file = new File("/home/user/aFile.mixed"); |
118 |
int sharability = SharabilityQuery.getSharability(file); |
119 |
assertEquals(SharabilityQuery.MIXED, sharability); |
120 |
URI uri = file.toURI(); |
121 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
122 |
assertEquals(SharabilityQuery.Sharability.MIXED, sharability2); |
123 |
FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "mixed"); |
124 |
assertEquals(sharability2, SharabilityQuery.getSharability(fo)); |
125 |
} |
126 |
|
127 |
public void testMixedBridge2New() throws IOException { |
128 |
File file = new File("/home/user/aFile.mixed2"); |
129 |
int sharability = SharabilityQuery.getSharability(file); |
130 |
assertEquals(SharabilityQuery.MIXED, sharability); |
131 |
URI uri = file.toURI(); |
132 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
133 |
assertEquals(SharabilityQuery.Sharability.MIXED, sharability2); |
134 |
FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "mixed2"); |
135 |
assertEquals(sharability2, SharabilityQuery.getSharability(fo)); |
136 |
} |
137 |
|
138 |
public void testUnknown() throws IOException { |
139 |
File file = new File("/home/user/aFile.txt"); |
140 |
int sharability = SharabilityQuery.getSharability(file); |
141 |
assertEquals(SharabilityQuery.UNKNOWN, sharability); |
142 |
URI uri = file.toURI(); |
143 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
144 |
assertEquals(SharabilityQuery.Sharability.UNKNOWN, sharability2); |
145 |
FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "txt"); |
146 |
assertEquals(sharability2, SharabilityQuery.getSharability(fo)); |
147 |
} |
148 |
|
149 |
public void testNormalized() throws IOException { |
150 |
File file = new File("/home/user/../aFile.txt"); |
151 |
Exception exception = null; |
152 |
try { |
153 |
SharabilityQuery.getSharability(file); |
154 |
} catch (IllegalArgumentException e) { |
155 |
exception = e; |
156 |
} |
157 |
assertNotNull(exception); |
158 |
URI uri = file.toURI(); |
159 |
exception = null; |
160 |
try { |
161 |
SharabilityQuery.getSharability(uri); |
162 |
} catch (IllegalArgumentException e) { |
163 |
exception = e; |
164 |
} |
165 |
assertNotNull(exception); |
166 |
} |
167 |
|
168 |
public void testRfs() throws IOException, URISyntaxException { |
169 |
URI uri = new URI("rfs", "tester", "localhost", 22, "/home/tester/aFile.sharable", null, null); |
170 |
Sharability sharability = SharabilityQuery.getSharability(uri); |
171 |
assertEquals(SharabilityQuery.Sharability.UNKNOWN, sharability); |
172 |
uri = new URI("rfs", "tester", "localhost", 22, "/home/tester/aFile.sharable2", null, null); |
173 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
174 |
assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability2); |
175 |
} |
176 |
|
177 |
public static class SharabilityQueryImplementationImpl implements SharabilityQueryImplementation { |
178 |
|
179 |
@Override |
180 |
public int getSharability(File file) { |
181 |
String path = file.getAbsolutePath(); |
182 |
if (path.endsWith(".sharable")) { |
183 |
return SharabilityQuery.SHARABLE; |
184 |
} else if (path.endsWith(".not_sharable")) { |
185 |
return SharabilityQuery.NOT_SHARABLE; |
186 |
} else if (path.endsWith(".mixed")) { |
187 |
return SharabilityQuery.MIXED; |
188 |
} |
189 |
return SharabilityQuery.UNKNOWN; |
190 |
} |
191 |
} |
192 |
|
193 |
public static class SharabilityQueryImplementation2Impl implements SharabilityQueryImplementation2 { |
194 |
|
195 |
@Override |
196 |
public Sharability getSharability(URI uri) { |
197 |
String path = uri.getPath(); |
198 |
if (path.endsWith(".sharable2")) { |
199 |
return SharabilityQuery.Sharability.SHARABLE; |
200 |
} else if (path.endsWith(".not_sharable2")) { |
201 |
return SharabilityQuery.Sharability.NOT_SHARABLE; |
202 |
} else if (path.endsWith(".mixed2")) { |
203 |
return SharabilityQuery.Sharability.MIXED; |
204 |
} |
205 |
return SharabilityQuery.Sharability.UNKNOWN; |
206 |
} |
207 |
} |
208 |
} |