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 |
|
54 |
/** |
55 |
* |
56 |
* @author Alexander Simon |
57 |
*/ |
58 |
public class SharabilityQueryTest extends NbTestCase { |
59 |
|
60 |
public SharabilityQueryTest(String testMethod) { |
61 |
super (testMethod); |
62 |
} |
63 |
|
64 |
@Override |
65 |
public void setUp() throws IOException { |
66 |
MockServices.setServices(SharabilityQueryImplementationImpl.class, SharabilityQueryImplementation2Impl.class); |
67 |
} |
68 |
|
69 |
public void testSharableBridge2Old() throws IOException { |
70 |
File file = new File("/home/user/aFile.sharable"); |
71 |
int sharability = SharabilityQuery.getSharability(file); |
72 |
assertEquals(SharabilityQuery.SHARABLE, sharability); |
73 |
URI uri = file.toURI(); |
74 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
75 |
assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability2); |
76 |
} |
77 |
|
78 |
public void testSharableBridge2New() throws IOException { |
79 |
File file = new File("/home/user/aFile.sharable2"); |
80 |
int sharability = SharabilityQuery.getSharability(file); |
81 |
assertEquals(SharabilityQuery.SHARABLE, sharability); |
82 |
URI uri = file.toURI(); |
83 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
84 |
assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability2); |
85 |
} |
86 |
|
87 |
public void testNotSharableBridge2Old() throws IOException { |
88 |
File file = new File("/home/user/aFile.not_sharable"); |
89 |
int sharability = SharabilityQuery.getSharability(file); |
90 |
assertEquals(SharabilityQuery.NOT_SHARABLE, sharability); |
91 |
URI uri = file.toURI(); |
92 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
93 |
assertEquals(SharabilityQuery.Sharability.NOT_SHARABLE, sharability2); |
94 |
} |
95 |
|
96 |
public void testNotSharableBridge2New() throws IOException { |
97 |
File file = new File("/home/user/aFile.not_sharable2"); |
98 |
int sharability = SharabilityQuery.getSharability(file); |
99 |
assertEquals(SharabilityQuery.NOT_SHARABLE, sharability); |
100 |
URI uri = file.toURI(); |
101 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
102 |
assertEquals(SharabilityQuery.Sharability.NOT_SHARABLE, sharability2); |
103 |
} |
104 |
|
105 |
public void testMixedBridge2Old() throws IOException { |
106 |
File file = new File("/home/user/aFile.mixed"); |
107 |
int sharability = SharabilityQuery.getSharability(file); |
108 |
assertEquals(SharabilityQuery.MIXED, sharability); |
109 |
URI uri = file.toURI(); |
110 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
111 |
assertEquals(SharabilityQuery.Sharability.MIXED, sharability2); |
112 |
} |
113 |
|
114 |
public void testMixedBridge2New() throws IOException { |
115 |
File file = new File("/home/user/aFile.mixed2"); |
116 |
int sharability = SharabilityQuery.getSharability(file); |
117 |
assertEquals(SharabilityQuery.MIXED, sharability); |
118 |
URI uri = file.toURI(); |
119 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
120 |
assertEquals(SharabilityQuery.Sharability.MIXED, sharability2); |
121 |
} |
122 |
|
123 |
public void testUnknown() throws IOException { |
124 |
File file = new File("/home/user/aFile.txt"); |
125 |
int sharability = SharabilityQuery.getSharability(file); |
126 |
assertEquals(SharabilityQuery.UNKNOWN, sharability); |
127 |
URI uri = file.toURI(); |
128 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
129 |
assertEquals(SharabilityQuery.Sharability.UNKNOWN, sharability2); |
130 |
} |
131 |
|
132 |
public void testNormalized() throws IOException { |
133 |
File file = new File("/home/user/../aFile.txt"); |
134 |
Exception exception = null; |
135 |
try { |
136 |
SharabilityQuery.getSharability(file); |
137 |
} catch (IllegalArgumentException e) { |
138 |
exception = e; |
139 |
} |
140 |
assertNotNull(exception); |
141 |
URI uri = file.toURI(); |
142 |
exception = null; |
143 |
try { |
144 |
SharabilityQuery.getSharability(uri); |
145 |
} catch (IllegalArgumentException e) { |
146 |
exception = e; |
147 |
} |
148 |
assertNotNull(exception); |
149 |
} |
150 |
|
151 |
public void testRfs() throws IOException, URISyntaxException { |
152 |
URI uri = new URI("rfs", "tester", "localhost", 22, "/home/tester/aFile.sharable", null, null); |
153 |
Sharability sharability = SharabilityQuery.getSharability(uri); |
154 |
assertEquals(SharabilityQuery.Sharability.UNKNOWN, sharability); |
155 |
uri = new URI("rfs", "tester", "localhost", 22, "/home/tester/aFile.sharable2", null, null); |
156 |
Sharability sharability2 = SharabilityQuery.getSharability(uri); |
157 |
assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability2); |
158 |
} |
159 |
|
160 |
public static class SharabilityQueryImplementationImpl implements SharabilityQueryImplementation { |
161 |
|
162 |
@Override |
163 |
public int getSharability(File file) { |
164 |
String path = file.getAbsolutePath(); |
165 |
if (path.endsWith(".sharable")) { |
166 |
return SharabilityQuery.SHARABLE; |
167 |
} else if (path.endsWith(".not_sharable")) { |
168 |
return SharabilityQuery.NOT_SHARABLE; |
169 |
} else if (path.endsWith(".mixed")) { |
170 |
return SharabilityQuery.MIXED; |
171 |
} |
172 |
return SharabilityQuery.UNKNOWN; |
173 |
} |
174 |
} |
175 |
|
176 |
public static class SharabilityQueryImplementation2Impl implements SharabilityQueryImplementation2 { |
177 |
|
178 |
@Override |
179 |
public Sharability getSharability(URI uri) { |
180 |
String path = uri.getPath(); |
181 |
if (path.endsWith(".sharable2")) { |
182 |
return SharabilityQuery.Sharability.SHARABLE; |
183 |
} else if (path.endsWith(".not_sharable2")) { |
184 |
return SharabilityQuery.Sharability.NOT_SHARABLE; |
185 |
} else if (path.endsWith(".mixed2")) { |
186 |
return SharabilityQuery.Sharability.MIXED; |
187 |
} |
188 |
return SharabilityQuery.Sharability.UNKNOWN; |
189 |
} |
190 |
} |
191 |
} |