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