import java.text.Format;
import java.text.ParsePosition;
-/**
- * A formatter for formatting byte sizes. For example, formatting 12345 byes
- * results in "12.1 K" and 1234567 results in "1.18 MB".
- *
- */
public class ByteFormat extends Format {
private static final long serialVersionUID = 1L;
public ByteFormat() {
}
-
- /**
- * Formats a long which represent a number of bytes.
- */
- public String format(long bytes) {
- return format(Long.valueOf(bytes));
+
+ public String format(long value) {
+ return format(new Long(value));
}
- /**
- * Formats a long which represent a number of kilobytes.
- */
- public String formatKB(long kilobytes) {
- return format(Long.valueOf(kilobytes * 1024));
- }
-
- /**
- * Format the given object (must be a Long).
- *
- * @param obj
- * assumed to be the number of bytes as a Long.
- * @param buf
- * the StringBuffer to append to.
- * @param pos
- * @return A formatted string representing the given bytes in more
- * human-readable form.
- */
public StringBuffer format(Object obj, StringBuffer buf, FieldPosition pos) {
- if (obj instanceof Long) {
- long numBytes = ((Long) obj).longValue();
+ if (obj instanceof Number) {
+ long numBytes = ((Number) obj).longValue();
if (numBytes < 1024) {
DecimalFormat formatter = new DecimalFormat("#,##0");
buf.append(formatter.format((double) numBytes)).append(" b");
}
return buf;
}
-
- /**
- * In this implementation, returns null always.
- *
- * @param source
- * @param pos
- * @return returns null in this implementation.
- */
+
public Object parseObject(String source, ParsePosition pos) {
return null;
}
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
package com.gitblit.utils;\r
\r
import java.io.ByteArrayOutputStream;\r
\r
public static String getRootPath(String path) {\r
if (path.indexOf('/') > -1) {\r
- return path.substring(0, path.indexOf('/'));\r
+ return path.substring(0, path.lastIndexOf('/'));\r
}\r
return "";\r
}\r
int days = daysAgo(date, true);\r
if (days < 365) {\r
if (days <= 30) {\r
- ago = days + " day" + (days > 1 ? "s" : "") + " ago";\r
+ ago = days + " days ago";\r
} else if (days <= 90) {\r
int weeks = days / 7;\r
if (weeks == 12) {\r
if (weeks >= 2) {\r
months++;\r
}\r
- ago = months + " month" + (months > 1 ? "s" : "") + " ago";\r
+ ago = months + " months ago";\r
}\r
} else if (days == 365) {\r
ago = "1 year ago";\r
--- /dev/null
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.tests;\r
+\r
+import junit.framework.TestCase;\r
+\r
+import com.gitblit.utils.ByteFormat;\r
+\r
+public class ByteFormatTest extends TestCase {\r
+\r
+ public void testByteFormat() throws Exception {\r
+ ByteFormat format = new ByteFormat();\r
+ assertTrue(format.format(10).equals("10 b"));\r
+ assertTrue(format.format(1024*10).equals("10.0 KB"));\r
+ assertTrue(format.format(1024*1000).equals("1,000.0 KB"));\r
+ assertTrue(format.format(2*1024*1000).equals("2.0 MB"));\r
+ assertTrue(format.format(1024*1024*1000).equals("1,000.0 MB"));\r
+ assertTrue(format.format(2*1024*1024*1000).equals("2.0 GB"));\r
+ }\r
+}\r
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
package com.gitblit.tests;\r
\r
import java.io.File;\r
public static Test suite() {\r
TestSuite suite = new TestSuite();\r
suite.addTestSuite(TimeUtilsTest.class);\r
+ suite.addTestSuite(StringUtilsTest.class);\r
+ suite.addTestSuite(ByteFormatTest.class);\r
suite.addTestSuite(JGitUtilsTest.class);\r
suite.addTestSuite(GitBlitTest.class);\r
return new GitBlitSuite(suite);\r
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
package com.gitblit.tests;\r
\r
import java.util.List;\r
--- /dev/null
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.tests;\r
+\r
+import java.util.Arrays;\r
+\r
+import junit.framework.TestCase;\r
+\r
+import com.gitblit.utils.StringUtils;\r
+\r
+public class StringUtilsTest extends TestCase {\r
+\r
+ public void testIsEmpty() throws Exception {\r
+ assertTrue(StringUtils.isEmpty(null));\r
+ assertTrue(StringUtils.isEmpty(""));\r
+ assertTrue(StringUtils.isEmpty(" "));\r
+ assertFalse(StringUtils.isEmpty("A"));\r
+ }\r
+\r
+ public void testBreakLinesForHtml() throws Exception {\r
+ String input = "this\nis\r\na\rtest\r\n\r\nof\n\nline\r\rbreaking";\r
+ String output = "this<br/>is<br/>a<br/>test<br/><br/>of<br/><br/>line<br/><br/>breaking";\r
+ assertTrue(StringUtils.breakLinesForHtml(input).equals(output));\r
+ }\r
+ \r
+ public void testEscapeForHtml() throws Exception {\r
+ String input = "& < > \" \t";\r
+ String output_nochange = "& < > " \t";\r
+ String output_change = "& < > " ";\r
+ assertTrue(StringUtils.escapeForHtml(input, false).equals(output_nochange));\r
+ assertTrue(StringUtils.escapeForHtml(input, true).equals(output_change));\r
+ }\r
+\r
+ public void testFlattenStrings() throws Exception {\r
+ String[] strings = { "A", "B", "C", "D" };\r
+ assertTrue(StringUtils.flattenStrings(Arrays.asList(strings)).equals("A B C D"));\r
+ }\r
+\r
+ public void testTrim() throws Exception {\r
+ String input = "123456789 123456789 123456789 123456789 123456789 123456789 123456789 ";\r
+ String output = "123456789 123456789 123456789 123456789 123456789 1234567...";\r
+ assertTrue(StringUtils.trimShortLog(input).equals(output));\r
+ assertTrue(StringUtils.trimString(input, input.length()).equals(input));\r
+ }\r
+\r
+ public void testPadding() throws Exception {\r
+ String input = "test";\r
+ assertTrue(StringUtils.leftPad(input, 6 + input.length(), ' ').equals(" test"));\r
+ assertTrue(StringUtils.rightPad(input, 6 + input.length(), ' ').equals("test "));\r
+\r
+ assertTrue(StringUtils.leftPad(input, input.length(), ' ').equals(input));\r
+ assertTrue(StringUtils.rightPad(input, input.length(), ' ').equals(input));\r
+ }\r
+ \r
+ public void testSHA1() throws Exception {\r
+ assertTrue(StringUtils.getSHA1("blob 16\000what is up, doc?").equals("bd9dbf5aae1a3862dd1526723246b20206e5fc37"));\r
+ }\r
+ \r
+ public void testRootPath() throws Exception {\r
+ String input = "/nested/path/to/repository";\r
+ String output = "/nested/path/to";\r
+ assertTrue(StringUtils.getRootPath(input).equals(output));\r
+ assertTrue(StringUtils.getRootPath("repository").equals(""));\r
+ }\r
+}\r
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
package com.gitblit.tests;\r
\r
import java.util.Date;\r
\r
public class TimeUtilsTest extends TestCase {\r
\r
+ private Date offset(long subtract) {\r
+ return new Date(System.currentTimeMillis() - subtract);\r
+ }\r
+\r
+ public void testBasicTimeFunctions() throws Exception {\r
+ assertTrue(TimeUtils.minutesAgo(offset(2 * TimeUtils.MIN), false) == 2);\r
+ assertTrue(TimeUtils.minutesAgo(offset((2 * TimeUtils.MIN) + (35 * 1000L)), true) == 3);\r
+\r
+ assertTrue(TimeUtils.hoursAgo(offset(2 * TimeUtils.ONEHOUR), false) == 2);\r
+ assertTrue(TimeUtils.hoursAgo(offset(5 * TimeUtils.HALFHOUR), true) == 3);\r
+\r
+ assertTrue(TimeUtils.daysAgo(offset(4 * TimeUtils.ONEDAY), false) == 4);\r
+ assertTrue(TimeUtils.daysAgo(offset(4 * TimeUtils.ONEDAY + 12 * TimeUtils.ONEHOUR), true) == 5);\r
+ }\r
+\r
public void testToday() throws Exception {\r
- assertTrue("Is today failed!", TimeUtils.isToday(new Date()));\r
+ assertTrue(TimeUtils.isToday(new Date()));\r
}\r
\r
public void testYesterday() throws Exception {\r
- assertTrue("Is yesterday failed!", TimeUtils.isYesterday(new Date(System.currentTimeMillis() - TimeUtils.ONEDAY)));\r
+ assertTrue(TimeUtils.isYesterday(offset(TimeUtils.ONEDAY)));\r
}\r
\r
public void testDurations() throws Exception {\r
+ assertTrue(TimeUtils.duration(1).equals("1 day"));\r
assertTrue(TimeUtils.duration(5).equals("5 days"));\r
+ assertTrue(TimeUtils.duration(75).equals("3 months"));\r
assertTrue(TimeUtils.duration(364).equals("12 months"));\r
assertTrue(TimeUtils.duration(365 + 0).equals("1 year"));\r
assertTrue(TimeUtils.duration(365 + 10).equals("1 year"));\r
assertTrue(TimeUtils.duration(365 + 44).equals("1 year, 1 month"));\r
assertTrue(TimeUtils.duration(365 + 45).equals("1 year, 2 months"));\r
assertTrue(TimeUtils.duration(365 + 60).equals("1 year, 2 months"));\r
- \r
- assertTrue(TimeUtils.duration(2*365 + 0).equals("2 years"));\r
- assertTrue(TimeUtils.duration(2*365 + 10).equals("2 years"));\r
- assertTrue(TimeUtils.duration(2*365 + 15).equals("2 years, 1 month"));\r
- assertTrue(TimeUtils.duration(2*365 + 30).equals("2 years, 1 month"));\r
- assertTrue(TimeUtils.duration(2*365 + 44).equals("2 years, 1 month"));\r
- assertTrue(TimeUtils.duration(2*365 + 45).equals("2 years, 2 months"));\r
- assertTrue(TimeUtils.duration(2*365 + 60).equals("2 years, 2 months"));\r
+\r
+ assertTrue(TimeUtils.duration(2 * 365 + 0).equals("2 years"));\r
+ assertTrue(TimeUtils.duration(2 * 365 + 10).equals("2 years"));\r
+ assertTrue(TimeUtils.duration(2 * 365 + 15).equals("2 years, 1 month"));\r
+ assertTrue(TimeUtils.duration(2 * 365 + 30).equals("2 years, 1 month"));\r
+ assertTrue(TimeUtils.duration(2 * 365 + 44).equals("2 years, 1 month"));\r
+ assertTrue(TimeUtils.duration(2 * 365 + 45).equals("2 years, 2 months"));\r
+ assertTrue(TimeUtils.duration(2 * 365 + 60).equals("2 years, 2 months"));\r
}\r
- \r
+\r
public void testTimeAgo() throws Exception {\r
- long time = System.currentTimeMillis();\r
- assertTrue(TimeUtils.timeAgo(new Date(time - 1*TimeUtils.MIN)).equals("1 min ago"));\r
- assertTrue(TimeUtils.timeAgo(new Date(time - 60*TimeUtils.MIN)).equals("60 mins ago"));\r
- assertTrue(TimeUtils.timeAgo(new Date(time - 120*TimeUtils.MIN)).equals("2 hours ago"));\r
- assertTrue(TimeUtils.timeAgo(new Date(time - 15*TimeUtils.ONEHOUR)).equals("15 hours ago"));\r
- assertTrue(TimeUtils.timeAgo(new Date(time - 24*TimeUtils.ONEHOUR)).equals("yesterday"));\r
- assertTrue(TimeUtils.timeAgo(new Date(time - 2*TimeUtils.ONEDAY)).equals("2 days ago"));\r
- assertTrue(TimeUtils.timeAgo(new Date(time - 35*TimeUtils.ONEDAY)).equals("5 weeks ago"));\r
- assertTrue(TimeUtils.timeAgo(new Date(time - 84*TimeUtils.ONEDAY)).equals("3 months ago"));\r
- assertTrue(TimeUtils.timeAgo(new Date(time - 95*TimeUtils.ONEDAY)).equals("3 months ago"));\r
- assertTrue(TimeUtils.timeAgo(new Date(time - 104*TimeUtils.ONEDAY)).equals("4 months ago"));\r
- assertTrue(TimeUtils.timeAgo(new Date(time - 365*TimeUtils.ONEDAY)).equals("1 year ago"));\r
- assertTrue(TimeUtils.timeAgo(new Date(time - 395*TimeUtils.ONEDAY)).equals("13 months ago"));\r
- assertTrue(TimeUtils.timeAgo(new Date(time - (2*365 + 30)*TimeUtils.ONEDAY)).equals("2 years ago"));\r
- \r
- assertTrue(TimeUtils.timeAgoCss(new Date(time - 1*TimeUtils.MIN)).equals("age0"));\r
- assertTrue(TimeUtils.timeAgoCss(new Date(time - 60*TimeUtils.MIN)).equals("age0"));\r
- assertTrue(TimeUtils.timeAgoCss(new Date(time - 120*TimeUtils.MIN)).equals("age1"));\r
- assertTrue(TimeUtils.timeAgoCss(new Date(time - 24*TimeUtils.ONEHOUR)).equals("age1"));\r
- assertTrue(TimeUtils.timeAgoCss(new Date(time - 2*TimeUtils.ONEDAY)).equals("age2")); }\r
+ // standard time ago tests\r
+ assertTrue(TimeUtils.timeAgo(offset(1 * TimeUtils.MIN)).equals("1 min ago"));\r
+ assertTrue(TimeUtils.timeAgo(offset(60 * TimeUtils.MIN)).equals("60 mins ago"));\r
+ assertTrue(TimeUtils.timeAgo(offset(120 * TimeUtils.MIN)).equals("2 hours ago"));\r
+ assertTrue(TimeUtils.timeAgo(offset(15 * TimeUtils.ONEHOUR)).equals("15 hours ago"));\r
+ assertTrue(TimeUtils.timeAgo(offset(24 * TimeUtils.ONEHOUR)).equals("yesterday"));\r
+ assertTrue(TimeUtils.timeAgo(offset(2 * TimeUtils.ONEDAY)).equals("2 days ago"));\r
+ assertTrue(TimeUtils.timeAgo(offset(35 * TimeUtils.ONEDAY)).equals("5 weeks ago"));\r
+ assertTrue(TimeUtils.timeAgo(offset(84 * TimeUtils.ONEDAY)).equals("3 months ago"));\r
+ assertTrue(TimeUtils.timeAgo(offset(95 * TimeUtils.ONEDAY)).equals("3 months ago"));\r
+ assertTrue(TimeUtils.timeAgo(offset(104 * TimeUtils.ONEDAY)).equals("4 months ago"));\r
+ assertTrue(TimeUtils.timeAgo(offset(365 * TimeUtils.ONEDAY)).equals("1 year ago"));\r
+ assertTrue(TimeUtils.timeAgo(offset(395 * TimeUtils.ONEDAY)).equals("13 months ago"));\r
+ assertTrue(TimeUtils.timeAgo(offset((2 * 365 + 30) * TimeUtils.ONEDAY)).equals(\r
+ "2 years ago"));\r
+\r
+ // css class tests\r
+ assertTrue(TimeUtils.timeAgoCss(offset(1 * TimeUtils.MIN)).equals("age0"));\r
+ assertTrue(TimeUtils.timeAgoCss(offset(60 * TimeUtils.MIN)).equals("age0"));\r
+ assertTrue(TimeUtils.timeAgoCss(offset(120 * TimeUtils.MIN)).equals("age1"));\r
+ assertTrue(TimeUtils.timeAgoCss(offset(24 * TimeUtils.ONEHOUR)).equals("age1"));\r
+ assertTrue(TimeUtils.timeAgoCss(offset(2 * TimeUtils.ONEDAY)).equals("age2"));\r
+ }\r
}\r