2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.xmlgraphics.io;
21
import java.util.HashSet;
23
import java.util.regex.Pattern;
25
import org.junit.Test;
27
import static org.junit.Assert.assertEquals;
28
import static org.junit.Assert.assertFalse;
29
import static org.junit.Assert.assertNotSame;
30
import static org.junit.Assert.assertTrue;
32
public class TempResourceURIGeneratorTestCase {
34
private TempResourceURIGenerator sut = new TempResourceURIGenerator("test");
37
public void testGenerate() {
38
URI first = sut.generate();
39
URI second = sut.generate();
40
Pattern regex = Pattern.compile("tmp:///test.*");
41
assertTrue(regex.matcher(first.toASCIIString()).matches());
42
assertTrue(regex.matcher(second.toASCIIString()).matches());
43
assertNotSame(first, second);
45
// Test that they are unique over a large number of calls to generate()
46
Set<URI> uniqueSet = new HashSet<URI>();
47
int numberOfTests = 1000;
48
for (int i = 0; i < numberOfTests; i++) {
49
uniqueSet.add(sut.generate());
51
assertEquals(numberOfTests, uniqueSet.size());
55
public void testIsTemURI() {
56
assertTrue(testTempURI("tmp:///test"));
57
assertTrue(testTempURI("tmp://test"));
58
assertTrue(testTempURI("tmp:/test"));
59
assertTrue(testTempURI("tmp:test"));
61
assertFalse(testTempURI("tmp/test"));
62
assertFalse(testTempURI("temp:///test"));
65
private boolean testTempURI(String uriString) {
66
return TempResourceURIGenerator.isTempURI(URI.create(uriString));